Files
faunosteo/packages/anatomy/tests/r5.legacy-synthetic-oracles.test.ts
T

287 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* R5.2 freeze (owner brief 2026-10-22 §9): the TypeScript legacy importer on synthetic
* inputs, so the Rust port is held to the generic contract and not only to the equine
* files. Each case is a full importer input (two SVG texts, the catalog text, the
* vertebrae text); the oracle records the four rendered files and the result's parts, or
* the refusal. `MAKE_R5_ORACLES=1` writes crates/core/tests/oracles/r5/legacy-synthetic.json.
*/
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { importEquineLegacy, renderArtifacts } from '../src'
const HERE = dirname(fileURLToPath(import.meta.url))
const OUT = resolve(HERE, '../../../crates/core/tests/oracles/r5')
const MAKE = process.env.MAKE_R5_ORACLES === '1'
interface Case {
name: string
main: string
head: string
catalog: string
vertebrae: string
}
const SVG_HEAD = '<?xml version="1.0" encoding="UTF-8"?>\n'
function svg(viewBox: string | null, body: string): string {
const box = viewBox === null ? '' : ` viewBox="${viewBox}"`
return `${SVG_HEAD}<svg xmlns="http://www.w3.org/2000/svg"${box}>\n${body}\n</svg>\n`
}
function anno(
tag: string,
attrs: Record<string, string>,
geometry: string,
): string {
const data = Object.entries(attrs)
.map(([k, v]) => ` data-anno-${k}="${v}"`)
.join('')
return ` <${tag}${geometry}${data}/>`
}
const VERTEBRAE = `// varies 18 - 22 caudal
export const FORMULA = { C: 7, T: 18, L: 6, S: 5, Cd: 18, Cd: 20 }
`
function node(id: string, extra: Record<string, unknown> = {}) {
return { id, name_fr: `${id} fr`, name_en: `${id} en`, ...extra }
}
function zone(id: string, fr: string, en: string, categories: unknown[]) {
return { id, name_fr: fr, name_en: en, categories }
}
const CATALOG = {
schema_version: 2.5,
scope_disclaimer: 'Périmètre « test »',
zones: {
hip: zone('hip', 'Hanche', 'Hip', [
node('hip-dysfunctions', {
children: [
node('Épaule Gauche', { description_fr: 'seulement FR' }),
node('ok', { requires_vet_referral: true, children: [] }),
node('deep', {
children: [node('leaf', { description_en: 'EN only' })],
}),
],
}),
node('anamnesis', { children: [node('age'), node('use')] }),
node('spine-observations', { children: [node('ok')] }),
]),
'10': zone('10', 'Dix', 'Ten', [
node('anamnesis', { children: [node('age')] }),
]),
'2': zone('2', 'Deux', 'Two', [
node('weird'),
node('anamnesis', { children: [node('age'), node('use')] }),
]),
spine: zone('spine', 'Colonne', 'Spine', [
node('spine-dysfunctions', { children: [node('ok'), node('x y')] }),
]),
},
}
const MAIN_BODY = [
anno(
'circle',
{
id: 'a1',
'name-fr': 'Hanche',
'name-en': 'Hip',
category: 'articulations',
'category-name': 'articulations',
lateral: 'bilateral',
},
' cx="10.5" cy=" 20 " r=" 2 "',
),
anno(
'circle',
{ id: 'a1', 'name-fr': 'Hanche', 'name-en': 'Hip' },
' cx="1" cy="1" r="1"',
),
anno(
'polygon',
{
id: 'p1',
'name-fr': 'Colonne',
'name-en': 'Spine',
category: 'charni-res-vert-brales',
special: 'vertebral-precision',
lateral: 'left',
},
' points=" 1,2 3,4 5,6 7 "',
),
anno(
'polygon',
{
id: 'p2',
'name-fr': 'Région sacrée',
category: 'zone-colonne',
'category-name': 'Colonne vertébrale',
special: 'vertebral-precision',
},
' points="0x10,1e1 2,3 4,5"',
),
anno(
'rect',
{ id: 'r1', 'name-en': 'Rect only', category: 'os', special: 'other' },
' x="1" y="2"',
),
anno(
'circle',
{ id: 't1', 'opens-detail': 'head', category: 'cadrans-du-cr-ne' },
' cx="5" cy="5" r="2"',
),
anno(
'circle',
{
id: 'x1',
'name-fr': 'Dix',
'name-en': 'Ten',
category: 'os',
lateral: 'unpaired',
},
' cx="840.1692504882812" cy="-0" r="3"',
),
].join('\n')
const HEAD_BODY = [
anno(
'circle',
{ id: 'h1', 'name-fr': 'Deux', category: 'os' },
' cx="3" cy="4" r="5"',
),
anno(
'circle',
{ id: 'h2', 'name-fr': 'Deux', 'name-en': 'Two', category: 'os' },
' cx="3" cy="4" r="5"',
),
anno(
'circle',
{
id: 'a1',
'name-fr': 'Thoracique',
'name-en': 'Thoracic',
category: 'os',
special: 'vertebral-precision',
},
' cx="1" cy="1" r="1"',
),
].join('\n')
function cases(): Case[] {
const base: Case = {
name: 'generic',
main: svg('0 0 100 50', MAIN_BODY),
head: svg(null, HEAD_BODY),
catalog: JSON.stringify(CATALOG, null, 2),
vertebrae: VERTEBRAE,
}
const minimal: Case = {
name: 'minimal',
main:
'' +
svg(
'0 0 1 1',
anno(
'circle',
{ id: 'm', 'name-fr': 'Hanche', 'name-en': 'Hip', category: 'os' },
' cx="1" cy="1" r="1"',
),
),
head: svg('0 0 1 1', ''),
catalog: JSON.stringify({
schema_version: '2.0',
zones: {
hip: zone('hip', 'Hanche', 'Hip', [
node('hip-dysfunctions', { children: [node('a')] }),
]),
},
}),
vertebrae: 'const V = {C:7,T:18,L:6,S:5,Cd:21}',
}
const refusals: Case[] = [
{
...base,
name: 'schema',
main: base.main.replace('r="3"', 'r="Infinity"'),
},
{ ...base, name: 'no-zones', catalog: '{"schema_version":"2.0"}' },
{ ...base, name: 'catalog-bom', catalog: '' + base.catalog },
{
...base,
name: 'no-cd',
vertebrae: 'const V = { C: 7, T: 18, L: 6, S: 5 }',
},
{ ...base, name: 'no-formula', vertebrae: 'nothing here' },
{
...base,
name: 'inline-sources',
catalog: JSON.stringify({
zones: {
hip: zone('hip', 'Hanche', 'Hip', [node('c', { sources: [{}] })]),
},
}),
},
{
...base,
name: 'key-not-id',
catalog: JSON.stringify({
zones: { hip: zone('hop', 'Hanche', 'Hip', []) },
}),
},
{
...base,
name: 'truthy-referral',
catalog: JSON.stringify({
zones: {
hip: zone('hip', 'Hanche', 'Hip', [
node('c', {
children: [node('d', { requires_vet_referral: 'yes' })],
}),
]),
},
}),
},
]
return [base, minimal, ...refusals]
}
async function run(c: Case) {
const input = {
mainSvg: { fileName: 'Main.svg', bytes: new TextEncoder().encode(c.main) },
headSvg: { fileName: 'Head.svg', bytes: new TextEncoder().encode(c.head) },
catalogJson: c.catalog,
vertebraeSource: c.vertebrae,
}
try {
const result = importEquineLegacy(input)
const parts: Record<string, unknown> = { ...result }
delete parts.document
delete parts.legacyIdMap
return { ...c, ok: true, files: await renderArtifacts(result), parts }
} catch (error) {
return {
...c,
ok: false,
error: error instanceof Error ? error.message : String(error),
}
}
}
describe('R5.2 synthetic importer oracle', () => {
it('legacy-synthetic.json', async () => {
const rows = []
for (const c of cases()) rows.push(await run(c))
const text = `${JSON.stringify({ cases: rows })}\n`
const file = resolve(OUT, 'legacy-synthetic.json')
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(file, text)
}
expect(text).toBe(readFileSync(file, 'utf8'))
})
})