test(anatomy,app): every legacy catalog leaf (612) and annotation (27) through the v2 to v3 migration against the draft document, the sheet index and a PDF roundtrip

This commit is contained in:
bchanot committed 2026-10-10 15:44:20 +02:00
1 parent e7f4167ada
commit 1e0491f3bc
3 files changed
+436

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { resolve } from 'node:path'
import {
AnatomicalSheetDocumentSchema,
ReportV2Schema,
type AnatomicalSheetDocument,
type FindingCategory,
type FindingNode,
type ObservationV2,
type ReportV2,
} from '@faunosteo/domain'
/**
* The legacy catalog as the application's archive knew it: every leaf of the
* generated baseline (`legacy-import/document.json`) becomes a Report v2
* finding, grouped in one observation per zone. Mirrors the anatomy package
* helper of the same name; both read the committed seed, never a copy.
*/
const BASELINE = resolve(
fileURLToPath(import.meta.url),
'../../../../../supabase/seed/anatomical-sheets/equine/legacy-import/document.json',
)
export interface LegacyLeaf {
zoneId: string
path: string[]
name: { fr: string; en: string }
vet: boolean
}
export function loadLegacyBaseline(): AnatomicalSheetDocument {
return AnatomicalSheetDocumentSchema.parse(
JSON.parse(readFileSync(BASELINE, 'utf8')),
)
}
export function legacyLeaves(doc: AnatomicalSheetDocument): LegacyLeaf[] {
const out: LegacyLeaf[] = []
const walk = (
zoneId: string,
nodes: readonly (FindingCategory | FindingNode)[],
prefix: string[],
) => {
for (const node of nodes) {
const path = [...prefix, node.id]
if (node.children.length > 0) walk(zoneId, node.children, path)
else {
const vet =
'requires_vet_referral' in node && node.requires_vet_referral
out.push({ zoneId, path, name: node.name, vet })
}
}
}
for (const zone of doc.finding_zones) walk(zone.id, zone.categories, [])
return out
}
export function leavesAsV2Report(leaves: LegacyLeaf[]): ReportV2 {
const byZone = new Map<string, ObservationV2>()
for (const leaf of leaves) {
let obs = byZone.get(leaf.zoneId)
if (!obs) {
obs = {
id: `obs-${leaf.zoneId}`,
zones: [],
findings: [],
freeTextFindings: [],
comment: '',
}
byZone.set(leaf.zoneId, obs)
}
obs.findings.push({
zoneId: leaf.zoneId,
path: leaf.path,
labelFrSnapshot: leaf.name.fr,
labelEnSnapshot: leaf.name.en || leaf.name.fr,
...(leaf.vet ? { requiresVetReferral: true } : {}),
})
}
return ReportV2Schema.parse({
version: 2,
id: 'all-leaves',
createdAt: '2026-10-10T00:00:00Z',
date: '2026-10-10',
practitioner: { name: 'Synthetic' },
animal: { speciesId: 'equine', name: 'Synthetic' },
owner: { name: 'Synthetic' },
observations: [...byZone.values()],
conclusion: '',
recommendations: '',
})
}
@@ -0,0 +1,71 @@
/**
* The 612 legacy leaves through the application itself: migrated with the
* bundled sheet's map, resolved by the sheet index the editor and the PDF
* use, exported to a v3 PDF and read back without loss.
*/
import { describe, it, expect, vi, beforeAll } from 'vitest'
import {
readReportDocument,
type Finding,
type Report,
} from '@faunosteo/domain'
import { exportReportToPdf } from '@/pdf/export'
import { importReportFromPdf } from '@/pdf/import'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
import {
leavesAsV2Report,
legacyLeaves,
loadLegacyBaseline,
} from '../helpers/legacyLeaves'
const ONE_PX_PNG = Uint8Array.from(
Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
'base64',
),
)
vi.mock('@/pdf/render/svgToPng', () => ({
rasterizeSvg: vi.fn(async () => ONE_PX_PNG),
}))
let sheet: SheetIndex
let report: Report
let findings: Finding[]
let unresolvedFindings: number
beforeAll(async () => {
sheet = await loadTestSheet()
const leaves = legacyLeaves(loadLegacyBaseline())
const v2 = leavesAsV2Report(leaves)
const outcome = readReportDocument(v2, sheet.sheet.migrationMap)
report = outcome.report
findings = report.observations.flatMap((o) => o.findings)
unresolvedFindings = outcome.unresolvedFindings
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
})
describe('the 612 legacy leaves in the application', () => {
it('resolves every migrated leaf to a leaf node of the bundled sheet', () => {
expect(findings).toHaveLength(612)
expect(unresolvedFindings).toBe(29)
for (const f of findings) {
const node = sheet.nodeAt(f.findingZoneId, f.path)
const where = `${f.findingZoneId}/${f.path.join('/')}`
if (f.unresolved) expect(node, where).toBeUndefined()
else {
expect(node, where).toBeDefined()
expect(node!.children, where).toHaveLength(0)
}
}
})
it('keeps every finding, resolved or not, through a v3 PDF export and import', async () => {
const bytes = await exportReportToPdf(report, sheet, { locale: 'fr' })
const back = await importReportFromPdf(bytes, sheet.sheet.migrationMap)
expect(back.migrated).toBe(false)
expect(back.report).toEqual(report)
const again = back.report.observations.flatMap((o) => o.findings)
expect(again.filter((f) => f.unresolved)).toHaveLength(29)
}, 30_000)
})