test(anatomy): R3 freeze O2 layer trees with attribute order

This commit is contained in:
bchanot committed 2026-10-14 23:27:46 +02:00
1 parent 47c5f49014
commit 969b872b98
2 files changed
+4731

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,162 @@
/**
* R3 freeze O2 (plan 2026-10-14 §3): the annotation layer as the TypeScript
* renderer builds it, attribute order kept, for the Rust layer to reproduce
* exactly. Cases: the curated draft (main, head; no state, a state set,
* every annotation) and an edge document covering what the draft lacks
* (shape by geometry type, twin radius floor, exponent numbers, path
* geometry, a circle trigger without a name, colour fallback, laterality).
* `MAKE_R3_ORACLES=1` writes `crates/core/tests/oracles/r3/layer.json`;
* otherwise the TypeScript still matches its own freeze (it is frozen too).
*/
import { readFileSync, mkdirSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
AnatomicalSheetDocumentSchema,
type AnatomicalSheetDocument,
type AnatomicalSheetDocumentInput,
} from '@faunosteo/domain'
import { renderAnnotationLayer, type AnnotationState } from '../src/render'
import { miniSheet } from './fixtures/mini-sheet'
const HERE = dirname(fileURLToPath(import.meta.url))
const ORACLES = resolve(HERE, '../../../crates/core/tests/oracles')
const OUT = resolve(ORACLES, 'r3/layer.json')
const MAKE = process.env.MAKE_R3_ORACLES === '1'
function draft(): AnatomicalSheetDocument {
const file = JSON.parse(readFileSync(resolve(ORACLES, 'sheet.draft.json'), 'utf8'))
return file.output as AnatomicalSheetDocument
}
const observable = {
category_id: 'joints',
laterality: 'unpaired' as const,
finding_zone_id: 'hip',
vertebral_region_code: null,
}
/** The mini sheet plus one annotation per behaviour the draft never exercises. */
export function edgeDocument(): AnatomicalSheetDocument {
const base = miniSheet()
const doc: AnatomicalSheetDocumentInput = {
...base,
categories: [
{ id: 'joints', name: { fr: 'Articulations', en: 'Joints' }, color: '#ff9500' },
{ id: 'bones', name: { fr: 'Os', en: 'Bones' }, color: null },
],
annotations: [
...base.annotations,
{
...observable,
id: 'point-polygon',
kind: 'point',
view_id: 'main',
laterality: 'left',
geometry: { type: 'polygon', points: [[0.1, 0.2], [1.5e-7, 3], [2, 1e21]] },
name: { fr: 'Polygone « point »', en: 'Point & <polygon>' },
},
{
...observable,
id: 'zone-circle',
kind: 'zone',
view_id: 'main',
laterality: 'right',
geometry: { type: 'circle', cx: 743.0451049804688, cy: 0.30000000000000004, r: 12.5 },
name: { fr: 'Zone ronde', en: 'Round zone' },
color: '#123456',
},
{
...observable,
id: 'zone-path',
kind: 'zone',
view_id: 'main',
category_id: 'bones',
geometry: { type: 'path', d: 'M0 0 L10 10 Z' },
name: { fr: 'Chemin', en: 'Path' },
finding_zone_id: null,
vertebral_region_code: 'T',
},
{
id: 'trigger-circle',
kind: 'detail_trigger',
view_id: 'main',
category_id: 'missing-category',
laterality: 'unpaired',
geometry: { type: 'circle', cx: 5, cy: 5, r: 40 },
name: null,
opens_view_id: 'head',
},
{
id: 'trigger-named',
kind: 'detail_trigger',
view_id: 'main',
category_id: 'bones',
laterality: 'unpaired',
geometry: { type: 'polygon', points: [[0, 0], [1, 0], [1, 1]] },
name: { fr: 'Ouvrir', en: 'Open' },
opens_view_id: 'head',
},
{
...observable,
id: 'head-point',
kind: 'point',
view_id: 'head',
laterality: 'bilateral',
geometry: { type: 'circle', cx: 1, cy: 1, r: 27.5 },
name: { fr: 'Tête', en: 'Head' },
},
],
}
return AnatomicalSheetDocumentSchema.parse(doc)
}
interface LayerCase {
doc: 'draft' | 'edge'
viewId: string
states: string[]
layer: unknown
}
function layerCase(
name: LayerCase['doc'],
document: AnatomicalSheetDocument,
viewId: string,
states: string[],
): LayerCase {
const map = new Map<string, AnnotationState>(states.map((id) => [id, 'current']))
return { doc: name, viewId, states, layer: renderAnnotationLayer({ document, viewId, states: map }) }
}
function cases(): { edge: AnatomicalSheetDocument; cases: LayerCase[] } {
const d = draft()
const e = edgeDocument()
const allDraft = d.annotations.map((a) => a.id)
const allEdge = e.annotations.map((a) => a.id)
return {
edge: e,
cases: [
layerCase('draft', d, 'main', []),
layerCase('draft', d, 'main', ['hip', 'cervical', 'head-trigger', 'atm']),
layerCase('draft', d, 'main', allDraft),
layerCase('draft', d, 'head', []),
layerCase('draft', d, 'head', ['atm', 'frontal', 'hip']),
layerCase('edge', e, 'main', []),
layerCase('edge', e, 'main', allEdge),
layerCase('edge', e, 'head', ['head-point']),
],
}
}
describe('R3 layer oracles (TypeScript freeze)', () => {
it('matches the frozen layer trees', () => {
const actual = cases()
if (MAKE) {
mkdirSync(dirname(OUT), { recursive: true })
writeFileSync(OUT, `${JSON.stringify(actual, null, 1)}\n`)
return
}
expect(actual).toEqual(JSON.parse(readFileSync(OUT, 'utf8')))
})
})