39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
/** Lot D: the history's byte bound and its steps. */
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
HISTORY_LIMIT_BYTES, emptyHistory, entryOf, recordCommit, stepBack, stepForward,
|
|
} from '@/editor/state/undo'
|
|
|
|
const chunk = (bytes: number, tag: string) => tag + 'x'.repeat(bytes - tag.length)
|
|
|
|
describe('history', () => {
|
|
it('measures the UTF-8 size of each entry once', () => {
|
|
expect(entryOf('é').bytes).toBe(2)
|
|
})
|
|
|
|
it('drops the oldest undo entries while both stacks exceed the bound', () => {
|
|
const third = Math.floor(HISTORY_LIMIT_BYTES / 3) + 1
|
|
let history = emptyHistory()
|
|
for (const tag of ['a', 'b', 'c', 'd']) history = recordCommit(history, chunk(third, tag))
|
|
expect(history.undo.map((e) => e.text[0])).toEqual(['c', 'd'])
|
|
const total = history.undo.reduce((sum, e) => sum + e.bytes, 0)
|
|
expect(total).toBeLessThanOrEqual(HISTORY_LIMIT_BYTES)
|
|
})
|
|
|
|
it('counts the redo stack in the bound and clears it on a commit', () => {
|
|
const half = Math.floor(HISTORY_LIMIT_BYTES / 2)
|
|
let history = recordCommit(recordCommit(emptyHistory(), chunk(half, 'a')), chunk(half, 'b'))
|
|
const back = stepBack(history, chunk(half, 'c'))
|
|
expect(back?.text[0]).toBe('b')
|
|
history = back!.history
|
|
expect(history.redo).toHaveLength(1)
|
|
expect(stepForward(history, 'now')?.text[0]).toBe('c')
|
|
expect(recordCommit(history, 'z').redo).toEqual([])
|
|
})
|
|
|
|
it('answers null with nothing to step to', () => {
|
|
expect(stepBack(emptyHistory(), 'x')).toBeNull()
|
|
expect(stepForward(emptyHistory(), 'x')).toBeNull()
|
|
})
|
|
})
|