/** U2 Task 2: the annotation work store and its stale stamp (R-13). */ import { afterEach, describe, expect, it } from 'vitest' import { dropStale, resetWork, useAnnotationWork, type Preview, } from '@/editor/annotations/workStore' import { useEditorStore } from '@/editor/state/editorStore' const preview = (base: string): Preview => ({ annotationId: 'a', geometry: { type: 'circle', cx: 1, cy: 2, r: 3 }, base, source: 'move', }) afterEach(() => { resetWork() useEditorStore.setState({ documentText: '', session: 0 }) }) describe('dropStale (R-13)', () => { it('drops what was computed on another text, keeps what was computed on this one', () => { useAnnotationWork.setState({ preview: preview('one'), chosen: { annotationId: 'a', vertex: 1, base: 'one' }, }) dropStale('one') expect(useAnnotationWork.getState().preview).toEqual(preview('one')) expect(useAnnotationWork.getState().chosen).not.toBeNull() dropStale('two') expect(useAnnotationWork.getState().preview).toBeNull() expect(useAnnotationWork.getState().chosen).toBeNull() }) it('runs on every document change of the editor store', () => { useAnnotationWork.setState({ preview: preview('one') }) useEditorStore.setState({ documentText: 'one' }) expect(useAnnotationWork.getState().preview).not.toBeNull() useEditorStore.setState({ documentText: 'two' }) expect(useAnnotationWork.getState().preview).toBeNull() }) it('a new session (another sheet, the editor closed) resets the work', () => { useAnnotationWork.setState({ mode: { kind: 'move', annotationId: 'a', started: true }, }) useEditorStore.setState({ session: useEditorStore.getState().session + 1 }) expect(useAnnotationWork.getState().mode).toBeNull() }) })