/** * Replaces state.reportStore / state.reportStore.restore / .setters / * state.draftMirror: the v3 store, its actions, the sessionStorage mirror * and the migration of a v2 draft on restore. */ import { beforeAll, beforeEach, describe, expect, it } from 'vitest' import { createReportStore, useReportStore, emptyObservation, } from '@/state/reportStore' import { DRAFT_KEY, sessionStoragePersistence } from '@/state/persistence' import type { SheetIndex } from '@/services/sheetIndex' import { loadTestSheet, minimalReport } from '../helpers/sheet' import fullV2 from '../fixtures/report-v2.full.json' let sheet: SheetIndex beforeAll(async () => { sheet = await loadTestSheet() }) beforeEach(() => { sessionStorage.clear() useReportStore.setState({ report: null, activeObservationIndex: 0 }) }) const draft = () => ({ date: '2026-10-10', practitioner: { name: 'Dr. Dupont' }, animal: { speciesId: 'equine', name: 'Mistral' }, owner: { name: 'Jean Martin' }, observations: [], conclusion: '', recommendations: '', }) describe('initReport', () => { it('stamps version 3, id, createdAt, the sheet ref and one empty observation', () => { useReportStore.getState().initReport(draft(), sheet.sheet.ref) const report = useReportStore.getState().report! expect(report.version).toBe(3) expect(report.id).toBeTruthy() expect(report.createdAt).toMatch(/^\d{4}-/) expect(report.sheet).toEqual(sheet.sheet.ref) expect(report.observations).toHaveLength(1) expect(report.localAnnotations).toEqual([]) expect(useReportStore.getState().activeObservationIndex).toBe(0) }) }) describe('observation actions', () => { it('adds, updates, moves and removes observations, keeping the active index sane', () => { const s = useReportStore.getState() s.initReport(draft(), sheet.sheet.ref) s.addObservation() expect(useReportStore.getState().report!.observations).toHaveLength(2) expect(useReportStore.getState().activeObservationIndex).toBe(1) const [first, second] = useReportStore.getState().report!.observations s.updateObservation(1, { ...second!, comment: 'note' }) s.moveObservationUp(1) expect( useReportStore.getState().report!.observations.map((o) => o.id), ).toEqual([second!.id, first!.id]) s.moveObservationDown(0) expect( useReportStore.getState().report!.observations.map((o) => o.id), ).toEqual([first!.id, second!.id]) s.moveObservationUp(0) s.moveObservationDown(1) s.removeObservation(1) expect(useReportStore.getState().report!.observations).toHaveLength(1) expect(useReportStore.getState().activeObservationIndex).toBe(0) s.setActiveObservation(-1) expect(useReportStore.getState().activeObservationIndex).toBe(-1) }) it('setters replace identity, conclusion and appointment; nextAppointment can be cleared', () => { const s = useReportStore.getState() s.initReport(draft(), sheet.sheet.ref) s.setPractitioner({ name: 'Dr. X' }) s.setAnimal({ speciesId: 'equine', name: 'Y' }) s.setOwner({ name: 'Z' }) s.setConclusion('c') s.setRecommendations('r') s.setNextAppointment({ kind: 'date', date: '2026-11-01' }) const r = useReportStore.getState().report! expect([ r.practitioner.name, r.animal.name, r.owner.name, r.conclusion, r.recommendations, ]).toEqual(['Dr. X', 'Y', 'Z', 'c', 'r']) expect(r.nextAppointment).toEqual({ kind: 'date', date: '2026-11-01' }) s.setNextAppointment(undefined) expect('nextAppointment' in useReportStore.getState().report!).toBe(false) s.reset() expect(useReportStore.getState().report).toBeNull() }) it('every action is a no-op without a report', () => { const s = useReportStore.getState() s.addObservation() s.updateObservation(0, emptyObservation()) s.setConclusion('x') expect(useReportStore.getState().report).toBeNull() }) }) describe('sessionStorage mirror and restore', () => { it('mirrors every change under osteo:draft and removes it on reset', () => { useReportStore.getState().initReport(draft(), sheet.sheet.ref) expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).version).toBe(3) useReportStore.getState().reset() expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull() }) it('restores a v3 draft, and migrates a v2 draft left by the previous version', () => { const v3 = minimalReport(sheet, { conclusion: 'kept' }) sessionStorage.setItem(DRAFT_KEY, JSON.stringify(v3)) useReportStore.getState().restoreDraft(sheet.sheet.migrationMap) expect(useReportStore.getState().report?.conclusion).toBe('kept') useReportStore.setState({ report: null }) sessionStorage.setItem(DRAFT_KEY, JSON.stringify(fullV2)) useReportStore.getState().restoreDraft(sheet.sheet.migrationMap) const restored = useReportStore.getState().report! expect(restored.version).toBe(3) expect( restored.observations.flatMap((o) => o.zones).map((z) => z.annotationId), ).toContain('hip') expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).version).toBe(3) }) it('ignores an unreadable draft and does not replace an open report', () => { sessionStorage.setItem(DRAFT_KEY, '{not json') expect(sessionStoragePersistence.load(sheet.sheet.migrationMap)).toBeNull() sessionStorage.setItem(DRAFT_KEY, JSON.stringify({ version: 1 })) expect(sessionStoragePersistence.load(sheet.sheet.migrationMap)).toBeNull() useReportStore.getState().initReport(draft(), sheet.sheet.ref) const before = useReportStore.getState().report sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet))) useReportStore.getState().restoreDraft(sheet.sheet.migrationMap) expect(useReportStore.getState().report).toBe(before) }) it('createReportStore accepts another persistence adapter', () => { const saved: unknown[] = [] const store = createReportStore({ load: () => null, save: (r) => saved.push(r), }) store.getState().initReport(draft(), sheet.sheet.ref) expect(saved.at(-1)).toMatchObject({ version: 3 }) }) }) describe('store edge cases', () => { it('hydrateReport stores the given object as is, without revalidating', () => { const odd = { ...minimalReport(sheet), extra: 1 } useReportStore.getState().hydrateReport(odd) expect(useReportStore.getState().report).toBe(odd) expect( (useReportStore.getState().report as unknown as { extra: number }).extra, ).toBe(1) }) it('identity setters are no-ops without a report', () => { const s = useReportStore.getState() s.setPractitioner({ name: 'X' }) s.setAnimal({ speciesId: 'equine', name: 'X' }) s.setOwner({ name: 'X' }) expect(useReportStore.getState().report).toBeNull() }) it('restoreDraft with no draft leaves the store empty', () => { useReportStore.getState().restoreDraft(sheet.sheet.migrationMap) expect(useReportStore.getState().report).toBeNull() }) })