import { beforeEach, describe, expect, it } from 'vitest'; import { emptyReport, useReportStore } from '@/state/reportStore'; beforeEach(() => { useReportStore.getState().reset(); localStorage.clear(); }); describe('useReportStore', () => { it('starts empty', () => { const s = useReportStore.getState(); expect(s.report).toBeNull(); expect(s.selection).toEqual([]); }); it('hydrate sets report', () => { const r = emptyReport({ animal: { name: 'Pégase', owner: 'X' } }); useReportStore.getState().hydrate(r); expect(useReportStore.getState().report?.animal.name).toBe('Pégase'); }); it('toggleZone adds then removes', () => { const { toggleZone } = useReportStore.getState(); toggleZone('z1'); expect(useReportStore.getState().selection).toEqual(['z1']); toggleZone('z2'); expect(useReportStore.getState().selection).toEqual(['z1', 'z2']); toggleZone('z1'); expect(useReportStore.getState().selection).toEqual(['z2']); }); it('addFinding appends and clears selection', () => { useReportStore.getState().hydrate(emptyReport()); useReportStore.getState().setSelection(['z1', 'z2']); const f = useReportStore.getState().addFinding({ zones: ['z1', 'z2'], linkedZones: [], pathologies: [], severity: 'mild', comment: '', }); expect(useReportStore.getState().report?.findings).toHaveLength(1); expect(useReportStore.getState().selection).toEqual([]); expect(f.id).toMatch(/^finding_/); }); it('updateFinding merges', () => { useReportStore.getState().hydrate(emptyReport()); const f = useReportStore.getState().addFinding({ zones: ['z1'], linkedZones: [], pathologies: [], severity: 'mild', comment: '', }); useReportStore.getState().updateFinding(f.id, { severity: 'severe', comment: 'lourd' }); const found = useReportStore.getState().report?.findings[0]; expect(found?.severity).toBe('severe'); expect(found?.comment).toBe('lourd'); }); it('removeFinding removes by id', () => { useReportStore.getState().hydrate(emptyReport()); const f = useReportStore.getState().addFinding({ zones: ['z1'], linkedZones: [], pathologies: [], severity: 'mild', comment: '', }); useReportStore.getState().removeFinding(f.id); expect(useReportStore.getState().report?.findings).toHaveLength(0); }); it('setConclusion / setRecommendations / setNextAppointment', () => { useReportStore.getState().hydrate(emptyReport()); useReportStore.getState().setConclusion('A'); useReportStore.getState().setRecommendations('B'); useReportStore.getState().setNextAppointment('2026-06-01'); const r = useReportStore.getState().report; expect(r?.conclusion).toBe('A'); expect(r?.recommendations).toBe('B'); expect(r?.nextAppointment).toBe('2026-06-01'); }); it('persists report to localStorage', () => { useReportStore.getState().hydrate(emptyReport({ animal: { name: 'X', owner: 'Y' } })); const raw = localStorage.getItem('osteo-report-draft'); expect(raw).not.toBeNull(); const parsed = JSON.parse(raw ?? '{}') as { state?: { report?: { animal?: { name?: string } } } }; expect(parsed.state?.report?.animal?.name).toBe('X'); }); });