Files
edentext/tests/unit/autosave-recovery.test.ts
T
Steffen Becker 28a39e017a feat: a document that blocks the start is not loaded twice
An autosaved document that hangs or throws while the editor comes up was
reloaded from localStorage on every refresh, leaving no way back into the app.
Loading now raises a boot flag that only a completed startup clears; finding it
still set means the last attempt never finished, so the document moves to
odf-editor-doc-broken and the app starts empty with a message.
2026-08-09 14:13:17 +00:00

39 lines
1.6 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { loadDocument, markDocumentLoaded, saveDocument } from '../../src/lib/storage/autosave';
// A document that freezes the editor would be reloaded from localStorage forever, so
// loading raises a boot flag that only a completed startup clears.
describe('autosave crash recovery', () => {
beforeEach(() => {
localStorage.clear();
vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => { void cb; return 0; });
vi.stubGlobal('alert', () => {});
});
it('loads the document again after a startup that completed', () => {
localStorage.setItem('odf-editor-doc', '{"type":"doc"}');
expect(loadDocument()).toEqual({ type: 'doc' });
markDocumentLoaded();
expect(loadDocument()).toEqual({ type: 'doc' });
});
it('parks the document and starts empty when the last startup never finished', () => {
localStorage.setItem('odf-editor-doc', '{"type":"doc"}');
loadDocument(); // raises the boot flag, then the "editor hangs"
expect(loadDocument()).toBeNull();
expect(localStorage.getItem('odf-editor-doc-broken')).toBe('{"type":"doc"}');
expect(localStorage.getItem('odf-editor-doc')).toBeNull();
});
it('keeps the next document after a recovery', () => {
localStorage.setItem('odf-editor-doc', '{"type":"doc","content":[]}');
loadDocument();
expect(loadDocument()).toBeNull(); // recovered: boot flag cleared, doc parked
vi.useFakeTimers();
saveDocument({ type: 'doc' });
vi.runAllTimers();
vi.useRealTimers();
expect(loadDocument()).toEqual({ type: 'doc' });
});
});