Files
faunosteo/apps/app/tests/unit/char.schemaPersistence.test.ts
T
bchanot 8673475293 chore(workspace): move the app to apps/app under a pnpm workspace (step 2)
144 files moved with git mv (pure renames). Root package.json delegates
dev/typecheck/test:run/lint/build to the packages; Node 24 LTS and
pnpm 10.13.1 pinned (.nvmrc, .node-version, engines, packageManager,
root devDependency, engine-strict). Dockerfile builds @faunosteo/app on
node:24-alpine and copies apps/app/dist; nginx and runtime config
unchanged. CI workflow for Gitea Actions. packages/ reserved (README only).
Docs synced: WORKSPACE.md, CLAUDE.md, README, DEPLOY, CHANGELOG, plan.
271/271 tests, typecheck, lint, build green from the root.
2026-10-08 21:51:13 +02:00

116 lines
4.8 KiB
TypeScript

/**
* Characterisation (Step 1): what the Report v2 schema accepts or rejects,
* what the browser persistence keeps, and the storage keys the SaaS
* migration must read (plan Step 14 one-time draft import).
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { ReportSchema } from '@/domain/schema/report'
import { ObservationSchema, VertebraSchema, SeveritySchema } from '@/domain/schema/observation'
import { readPersistedDraft, useReportStore } from '@/state/reportStore'
import { readCachedPractitioner, writeCachedPractitioner } from '@/state/practitionerCache'
import { useLocaleStore } from '@/state/localeStore'
import full from '../fixtures/report-v2.full.json'
import minimal from '../fixtures/report-v2.minimal.json'
import type { Report } from '@/domain/schema/report'
describe('Report v2 schema on the golden fixtures', () => {
it('accepts both fixtures unchanged', () => {
expect(ReportSchema.parse(full)).toEqual(full)
expect(ReportSchema.parse(minimal)).toEqual(minimal)
})
it('strips unknown keys silently (forward-compat fields are lost on reimport)', () => {
const parsed = ReportSchema.parse({ ...minimal, futureField: 1, animal: { ...minimal.animal, chip: 'x' } })
expect(parsed).toEqual(minimal)
})
it('rejects version 1 and 3, accepts only the literal 2', () => {
expect(ReportSchema.safeParse({ ...minimal, version: 1 }).success).toBe(false)
expect(ReportSchema.safeParse({ ...minimal, version: 3 }).success).toBe(false)
})
it('does not bound vertebra indexes by the species formula (C99 accepted)', () => {
expect(VertebraSchema.safeParse({ region: 'C', index: 99 }).success).toBe(true)
expect(VertebraSchema.safeParse({ region: 'X', index: 1 }).success).toBe(false)
expect(VertebraSchema.safeParse({ region: 'C', index: 0 }).success).toBe(false)
})
it('stores severities as French enum values', () => {
expect(SeveritySchema.options).toEqual(['légère', 'modérée', 'sévère'])
})
it('accepts findings whose zoneId matches no zone of the observation (orphans)', () => {
const obs = { ...full.observations[0], zones: [] }
expect(ObservationSchema.safeParse(obs).success).toBe(true)
})
it('accepts a speciesId that no manifest registers (checked only at render time)', () => {
expect(ReportSchema.safeParse({ ...minimal, animal: { speciesId: 'dragon', name: 'X' } }).success).toBe(true)
})
it('accepts two zones with the same boneId and different sides, and the same boneId from main and detail', () => {
const obs = {
...minimal,
observations: [
{
id: 'o',
zones: [
{ boneId: 'a', side: 'left' },
{ boneId: 'a', side: 'right' },
{ boneId: 'a', detailSourceId: 'head' },
],
findings: [],
freeTextFindings: [],
comment: '',
},
],
}
expect(ReportSchema.safeParse(obs).success).toBe(true)
})
})
describe('browser persistence', () => {
beforeEach(() => {
sessionStorage.clear()
localStorage.clear()
useReportStore.setState({ report: null, activeObservationIndex: 0 })
})
it('mirrors the full fixture to sessionStorage["osteo:draft"] and restores it unchanged', () => {
useReportStore.getState().hydrateReport(full as Report)
const raw = sessionStorage.getItem('osteo:draft')
expect(raw).not.toBeNull()
expect(JSON.parse(raw!)).toEqual(full)
expect(readPersistedDraft()).toEqual(full)
})
it('hydrateReport does not revalidate (an invalid report can enter the store, the draft is then unreadable)', () => {
const broken = { ...(minimal as Report), version: 7 } as unknown as Report
useReportStore.getState().hydrateReport(broken)
expect(useReportStore.getState().report?.version).toBe(7)
expect(readPersistedDraft()).toBeNull()
})
it('initReport creates one empty observation, a uuid id and version 2', () => {
const { observations, ...draft } = minimal as Report
void observations
useReportStore.getState().initReport({ ...draft, observations: [] } as never)
const r = useReportStore.getState().report!
expect(r.version).toBe(2)
expect(r.id).toMatch(/^[0-9a-f-]{36}$/)
expect(r.observations).toHaveLength(1)
expect(r.observations[0]).toMatchObject({ zones: [], findings: [], freeTextFindings: [], comment: '' })
})
it('caches the practitioner block under localStorage["osteo:lastPractitioner"]', () => {
writeCachedPractitioner(full.practitioner)
expect(JSON.parse(localStorage.getItem('osteo:lastPractitioner')!)).toEqual(full.practitioner)
expect(readCachedPractitioner()).toEqual(full.practitioner)
})
it('persists the locale under localStorage["osteo:locale"] (shared with pathology-editor.html)', () => {
useLocaleStore.getState().setLocale('en')
expect(localStorage.getItem('osteo:locale')).toBe('en')
})
})