feat(domain): equine legacy importer (packages/anatomy) + generated official equine sheet seed as draft
This commit is contained in:
1 parent
f4682cdba5
commit
52bfc11553
43 files changed
+18466
-33
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import {
|
||||
AnatomicalSheetDocumentSchema,
|
||||
validateSheetDocument,
|
||||
} from '@faunosteo/domain'
|
||||
import { importEquineLegacy, renderArtifacts } from '../src'
|
||||
import { loadInput, runImport } from './helpers'
|
||||
|
||||
describe('equine document', () => {
|
||||
it('parses and validates with no error and no warning', async () => {
|
||||
const { document, issues } = await runImport()
|
||||
const parsed = AnatomicalSheetDocumentSchema.parse(document)
|
||||
expect(validateSheetDocument(parsed)).toEqual([])
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
|
||||
it('is deterministic: same result and same bytes on two runs', async () => {
|
||||
const input = await loadInput()
|
||||
const first = importEquineLegacy(input)
|
||||
const second = importEquineLegacy(input)
|
||||
expect(second).toEqual(first)
|
||||
expect(await renderArtifacts(second)).toEqual(await renderArtifacts(first))
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,36 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
EQUINE_SOURCE_PATHS,
|
||||
importEquineLegacy,
|
||||
readEquineSources,
|
||||
type EquineLegacyInput,
|
||||
type ImportResult,
|
||||
} from '../src'
|
||||
import type { LegacyCatalog } from '../src/legacy/catalog'
|
||||
|
||||
/** Tests run on the real legacy sources, never on copies. */
|
||||
export const REPO_ROOT = fileURLToPath(new URL('../../../', import.meta.url))
|
||||
|
||||
export function loadInput(): Promise<EquineLegacyInput> {
|
||||
return readEquineSources(REPO_ROOT)
|
||||
}
|
||||
|
||||
export async function runImport(): Promise<ImportResult> {
|
||||
return importEquineLegacy(await loadInput())
|
||||
}
|
||||
|
||||
export async function loadCatalog(): Promise<LegacyCatalog> {
|
||||
const text = await readFile(
|
||||
join(REPO_ROOT, EQUINE_SOURCE_PATHS.catalog),
|
||||
'utf8',
|
||||
)
|
||||
return JSON.parse(text) as LegacyCatalog
|
||||
}
|
||||
|
||||
export async function legacyAnnoIds(path: string): Promise<string[]> {
|
||||
const text = await readFile(join(REPO_ROOT, path), 'utf8')
|
||||
const ids = [...text.matchAll(/data-anno-id="([^"]+)"/g)].map((m) => m[1]!)
|
||||
return [...new Set(ids)]
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { EQUINE_SOURCE_PATHS, type ImportResult } from '../src'
|
||||
import { legacyAnnoIds, runImport } from './helpers'
|
||||
|
||||
let result: ImportResult
|
||||
|
||||
beforeAll(async () => {
|
||||
result = await runImport()
|
||||
})
|
||||
|
||||
describe('equine import volumes', () => {
|
||||
it('imports 27 annotations, 26 linked to a finding zone', () => {
|
||||
const annotations = result.document.annotations
|
||||
expect(annotations).toHaveLength(27)
|
||||
const linked = annotations.filter(
|
||||
(a) => a.kind !== 'detail_trigger' && a.finding_zone_id !== null,
|
||||
)
|
||||
expect(linked).toHaveLength(26)
|
||||
})
|
||||
|
||||
it('has two views and one trigger opening head from main', () => {
|
||||
expect(result.document.views.map((v) => v.id)).toEqual(['main', 'head'])
|
||||
const triggers = result.document.annotations.filter(
|
||||
(a) => a.kind === 'detail_trigger',
|
||||
)
|
||||
expect(triggers).toHaveLength(1)
|
||||
expect(triggers[0]).toMatchObject({
|
||||
view_id: 'main',
|
||||
opens_view_id: 'head',
|
||||
})
|
||||
})
|
||||
|
||||
it('codes exactly the five zone-colonne polygons with C, T, L, S, Cd', () => {
|
||||
const coded = result.document.annotations.filter(
|
||||
(a) => a.kind !== 'detail_trigger' && a.vertebral_region_code !== null,
|
||||
)
|
||||
expect(coded).toHaveLength(5)
|
||||
expect(new Set(coded.map((a) => a.kind))).toEqual(new Set(['zone']))
|
||||
expect(new Set(coded.map((a) => a.category_id))).toEqual(
|
||||
new Set(['zone-colonne']),
|
||||
)
|
||||
const codes = coded.map((a) =>
|
||||
a.kind === 'detail_trigger' ? null : a.vertebral_region_code,
|
||||
)
|
||||
expect(new Set(codes)).toEqual(new Set(['C', 'T', 'L', 'S', 'Cd']))
|
||||
})
|
||||
|
||||
it('counts 9 bilateral annotations', () => {
|
||||
const bilateral = result.document.annotations.filter(
|
||||
(a) => a.laterality === 'bilateral',
|
||||
)
|
||||
expect(bilateral).toHaveLength(9)
|
||||
})
|
||||
|
||||
it('keeps 26 zones, 792 nodes, 612 leaves, 41 vet referrals', () => {
|
||||
expect(result.document.finding_zones).toHaveLength(26)
|
||||
expect(result.stats).toMatchObject({
|
||||
findingZones: 26,
|
||||
nodes: 792,
|
||||
leaves: 612,
|
||||
vetReferrals: 41,
|
||||
})
|
||||
})
|
||||
|
||||
it('maps every data-anno-id of both SVGs', async () => {
|
||||
const ids = [
|
||||
...(await legacyAnnoIds(EQUINE_SOURCE_PATHS.mainSvg)),
|
||||
...(await legacyAnnoIds(EQUINE_SOURCE_PATHS.headSvg)),
|
||||
]
|
||||
expect(ids).toHaveLength(27)
|
||||
expect(Object.keys(result.legacyIdMap.annotations).sort()).toEqual(
|
||||
ids.sort(),
|
||||
)
|
||||
})
|
||||
|
||||
it('places the nine head annotations in view head', async () => {
|
||||
const headIds = await legacyAnnoIds(EQUINE_SOURCE_PATHS.headSvg)
|
||||
const newIds = new Set(
|
||||
headIds.map((id) => result.legacyIdMap.annotations[id]),
|
||||
)
|
||||
const inHead = result.document.annotations.filter((a) => newIds.has(a.id))
|
||||
expect(inHead).toHaveLength(9)
|
||||
expect(inHead.every((a) => a.view_id === 'head')).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,109 @@
|
||||
import type { FindingNodeInput } from '@faunosteo/domain'
|
||||
import { renderImportReport, type ImportResult } from '../src'
|
||||
import type { LegacyCatalog, LegacyNode } from '../src/legacy/catalog'
|
||||
import { loadCatalog, runImport } from './helpers'
|
||||
|
||||
let result: ImportResult
|
||||
let catalog: LegacyCatalog
|
||||
|
||||
beforeAll(async () => {
|
||||
;[result, catalog] = await Promise.all([runImport(), loadCatalog()])
|
||||
})
|
||||
|
||||
function annotation(id: string) {
|
||||
const found = result.document.annotations.find((a) => a.id === id)
|
||||
if (!found) throw new Error(`annotation ${id} missing`)
|
||||
return found
|
||||
}
|
||||
|
||||
function zone(id: string) {
|
||||
const found = result.document.finding_zones.find((z) => z.id === id)
|
||||
if (!found) throw new Error(`zone ${id} missing`)
|
||||
return found
|
||||
}
|
||||
|
||||
/** Id skeleton of a tree: same shape whatever the label language. */
|
||||
function idTree(
|
||||
nodes: ReadonlyArray<LegacyNode | FindingNodeInput>,
|
||||
): unknown[] {
|
||||
return nodes.map((n) => [n.id, idTree(n.children ?? [])])
|
||||
}
|
||||
|
||||
describe('equine import special cases', () => {
|
||||
it('turns vertebral-precision into a region code', () => {
|
||||
expect(annotation('lumbar')).toMatchObject({
|
||||
kind: 'zone',
|
||||
vertebral_region_code: 'L',
|
||||
finding_zone_id: 'lumbar',
|
||||
})
|
||||
expect(annotation('caudal')).toMatchObject({ vertebral_region_code: 'Cd' })
|
||||
})
|
||||
|
||||
it('makes data-anno-lateral explicit', () => {
|
||||
expect(annotation('hip').laterality).toBe('bilateral')
|
||||
expect(annotation('c7-t1').laterality).toBe('unpaired')
|
||||
})
|
||||
|
||||
it('imports the trigger without name and without finding zone', () => {
|
||||
const trigger = annotation('head-trigger')
|
||||
expect(trigger.kind).toBe('detail_trigger')
|
||||
expect(trigger.name).toBeNull()
|
||||
expect(trigger).not.toHaveProperty('finding_zone_id')
|
||||
})
|
||||
|
||||
it('keeps ssb-dysfunctions under sphenoid and names it in the report', () => {
|
||||
expect(zone('sphenoid').categories[0]!.id).toBe('ssb-dysfunctions')
|
||||
expect(zone('ssb').categories[0]!.id).toBe('ssb-dysfunctions')
|
||||
const report = renderImportReport(result)
|
||||
expect(report).toContain('sphenoid/ssb-dysfunctions')
|
||||
expect(report).toContain('sphenoid-dysfunctions attendu')
|
||||
})
|
||||
|
||||
it('keeps the anamnesis category in all zones with its three variants', () => {
|
||||
const imported = result.document.finding_zones.map((z) => {
|
||||
const category = z.categories.find((c) => c.id === 'anamnesis')
|
||||
expect(category?.kind).toBe('anamnesis')
|
||||
return idTree(category?.children ?? []).join('|')
|
||||
})
|
||||
const legacy = Object.values(catalog.zones).map((z) =>
|
||||
idTree(
|
||||
z.categories.find((c) => c.id === 'anamnesis')!.children ?? [],
|
||||
).join('|'),
|
||||
)
|
||||
expect(imported).toHaveLength(26)
|
||||
expect(imported).toEqual(legacy)
|
||||
expect(new Set(imported).size).toBe(3)
|
||||
})
|
||||
|
||||
it('writes no en key where description_en is absent', () => {
|
||||
const category = zone('parietal').categories.find(
|
||||
(c) => c.id === 'anamnesis',
|
||||
)!
|
||||
expect(category.description).toEqual({
|
||||
fr: catalog.zones.parietal!.categories[2]!.description_fr,
|
||||
})
|
||||
const mobility = zone('parietal').categories[0]!.children[0]!
|
||||
expect(mobility.description).toBeDefined()
|
||||
expect(mobility.description).not.toHaveProperty('en')
|
||||
})
|
||||
|
||||
it('maps category kinds from their ids', () => {
|
||||
const kinds = zone('hip').categories.map((c) => [c.id, c.kind])
|
||||
expect(kinds).toEqual([
|
||||
['hip-dysfunctions', 'dysfunction'],
|
||||
['hip-observations', 'consultation_reason'],
|
||||
['anamnesis', 'anamnesis'],
|
||||
])
|
||||
})
|
||||
|
||||
it('preserves node ids at every level of every zone', () => {
|
||||
for (const [key, legacy] of Object.entries(catalog.zones)) {
|
||||
expect(idTree(zone(key).categories)).toEqual(idTree(legacy.categories))
|
||||
}
|
||||
})
|
||||
|
||||
it('writes the lean form: no default keys on plain leaves', () => {
|
||||
const leaf = zone('parietal').categories[0]!.children[0]!.children![0]!
|
||||
expect(Object.keys(leaf).sort()).toEqual(['id', 'name'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,49 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
EQUINE_SEED_DIR,
|
||||
EQUINE_SOURCE_PATHS,
|
||||
SEED_SQL_FILE,
|
||||
renderArtifacts,
|
||||
sha256Hex,
|
||||
type AssetRecord,
|
||||
} from '../src'
|
||||
import { REPO_ROOT, runImport } from './helpers'
|
||||
|
||||
const seedDir = join(REPO_ROOT, EQUINE_SEED_DIR)
|
||||
let artifacts: Record<string, string>
|
||||
|
||||
beforeAll(async () => {
|
||||
artifacts = await renderArtifacts(await runImport())
|
||||
})
|
||||
|
||||
describe('committed equine seed (golden)', () => {
|
||||
it.each([
|
||||
'document.json',
|
||||
'legacy-id-map.json',
|
||||
'assets.json',
|
||||
SEED_SQL_FILE,
|
||||
'IMPORT-REPORT.md',
|
||||
])('%s equals a fresh run (rerun pnpm import:equine)', async (file) => {
|
||||
const committed = await readFile(join(seedDir, file), 'utf8')
|
||||
expect(committed).toBe(artifacts[file])
|
||||
})
|
||||
|
||||
it('copies the SVG assets byte for byte with the recorded sha256', async () => {
|
||||
const assets = JSON.parse(artifacts['assets.json']!) as AssetRecord[]
|
||||
const sources = [EQUINE_SOURCE_PATHS.mainSvg, EQUINE_SOURCE_PATHS.headSvg]
|
||||
for (const [index, asset] of assets.entries()) {
|
||||
const copy = await readFile(join(seedDir, 'assets', asset.file))
|
||||
const original = await readFile(join(REPO_ROOT, sources[index]!))
|
||||
expect(copy.equals(original)).toBe(true)
|
||||
expect(sha256Hex(copy)).toBe(asset.sha256)
|
||||
expect(copy.byteLength).toBe(asset.byte_size)
|
||||
}
|
||||
})
|
||||
|
||||
it('seeds a draft and never publishes', () => {
|
||||
const sql = artifacts[SEED_SQL_FILE]!
|
||||
expect(sql).toContain('private.insert_sheet_draft')
|
||||
expect(sql).not.toContain('publish_sheet_version')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user