import { describe, it, expect } from 'vitest'; import { createHistory } from './history'; describe('createHistory', () => { it('starts empty; undo/redo are no-ops', () => { const h = createHistory(); expect(h.canUndo()).toBe(false); expect(h.canRedo()).toBe(false); expect(h.undo(1)).toBeUndefined(); expect(h.redo(1)).toBeUndefined(); }); it('records pre-edit snapshots, then undoes and redoes through them', () => { const h = createHistory(); // document went a -> b (record a) -> c (record b); current is 'c' h.record('a'); h.record('b'); expect(h.canUndo()).toBe(true); expect(h.undo('c')).toBe('b'); // current 'c' pushed to redo expect(h.canRedo()).toBe(true); expect(h.undo('b')).toBe('a'); expect(h.canUndo()).toBe(false); expect(h.redo('a')).toBe('b'); expect(h.redo('b')).toBe('c'); expect(h.canRedo()).toBe(false); }); it('record() clears the redo stack (no dead redo after a fresh edit)', () => { const h = createHistory(); h.record('a'); h.undo('b'); // redo now holds 'b' expect(h.canRedo()).toBe(true); h.record('x'); expect(h.canRedo()).toBe(false); }); it('caps the stack depth', () => { const h = createHistory(3); for (let i = 0; i < 5; i++) h.record(i); let undos = 0; let cur = 99; while (h.canUndo()) { cur = h.undo(cur)!; undos++; } expect(undos).toBe(3); }); it('clear() empties both stacks', () => { const h = createHistory(); h.record(1); h.undo(2); h.clear(); expect(h.canUndo()).toBe(false); expect(h.canRedo()).toBe(false); }); });