feat(app): application on the canonical sheet — report v3 store with v2 draft migration, bundled SheetProvider (SHEET_BUNDLE), annotation layer rendered from the document, zone interaction service, pickers and cards on the sheet index, PDF v3 export with v2 archive import; legacy registry, matchers, editors and assets removed

This commit is contained in:
bchanot committed 2026-10-10 11:36:14 +02:00
1 parent 0a17e390cf
commit ce2ec26112
114 files changed
+3560 -14637

No files matched your search

+129 -94
View File
@@ -1,120 +1,155 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { useReportStore } from '@/state/reportStore'
/**
* 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'
const draft = {
date: '2026-05-18',
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: '',
}
beforeEach(() => {
useReportStore.getState().reset()
})
describe('reportStore.initReport', () => {
it('sets a report with version=2, id, and createdAt', () => {
useReportStore.getState().initReport(draft)
const r = useReportStore.getState().report!
expect(r.version).toBe(2)
expect(r.id.length).toBeGreaterThan(0)
expect(r.createdAt).toMatch(/^\d{4}-\d{2}-\d{2}T/)
expect(r.practitioner.name).toBe('Dr. Dupont')
})
it('seeds a single empty observation when the draft has none', () => {
useReportStore.getState().initReport(draft)
const r = useReportStore.getState().report!
expect(r.observations.length).toBe(1)
expect(r.observations[0].zones).toEqual([])
expect(r.observations[0].comment).toBe('')
})
it('resets activeObservationIndex to 0', () => {
useReportStore.getState().initReport(draft)
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('reportStore.reset', () => {
it('clears the report', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().reset()
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('reportStore.addObservation', () => {
it('appends an empty observation and sets it active', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().addObservation()
const s = useReportStore.getState()
expect(s.report!.observations.length).toBe(2)
expect(s.activeObservationIndex).toBe(1)
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()
})
})
describe('reportStore.updateObservation', () => {
it('replaces the observation at the given index', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().addObservation()
const next = {
...useReportStore.getState().report!.observations[0],
comment: 'patate',
}
useReportStore.getState().updateObservation(0, next)
expect(useReportStore.getState().report!.observations[0].comment).toBe('patate')
})
})
describe('reportStore.removeObservation', () => {
it('drops the observation at the given index', () => {
const store = useReportStore.getState()
store.initReport(draft) // seeds 1
store.addObservation()
store.addObservation()
store.removeObservation(0)
expect(useReportStore.getState().report!.observations.length).toBe(2)
})
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')
it('clamps activeObservationIndex within bounds', () => {
const store = useReportStore.getState()
store.initReport(draft) // seeds 1
store.addObservation()
store.addObservation() // total 3
store.setActiveObservation(2)
store.removeObservation(2)
expect(useReportStore.getState().activeObservationIndex).toBe(1)
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)
})
})
describe('reportStore.move up/down', () => {
it('moveObservationUp swaps with previous', () => {
const s = useReportStore.getState()
s.initReport(draft)
s.addObservation()
s.addObservation()
const idA = useReportStore.getState().report!.observations[0].id
const idB = useReportStore.getState().report!.observations[1].id
s.moveObservationUp(1)
const after = useReportStore.getState().report!.observations
expect(after[0].id).toBe(idB)
expect(after[1].id).toBe(idA)
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('moveObservationDown swaps with next', () => {
const s = useReportStore.getState()
s.initReport(draft)
s.addObservation()
s.addObservation()
const idA = useReportStore.getState().report!.observations[0].id
const idB = useReportStore.getState().report!.observations[1].id
s.moveObservationDown(0)
const after = useReportStore.getState().report!.observations
expect(after[0].id).toBe(idB)
expect(after[1].id).toBe(idA)
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 })
})
})