/** * R3 freeze O3 (plan 2026-10-14 §3): the engine's highlighted views as the * TypeScript composes them (parse, allowlist, `composeViewSvg` with the halo * on main views), for the Rust highlight to reproduce at tree level. Every * (report, observation, view) of the three reference reports, plus an edge * sheet: a `` already present (nested), a detail view with its own * asset and no viewBox, highlighted triggers, path geometry, an empty * selection. Trees as structure (namespace, local name, attributes by * qualified name, children with text), never bytes. When a composed root * holds the sanitised asset unchanged, the case points at that tree instead * of repeating it. `MAKE_R3_ORACLES=1` writes * `crates/core/tests/oracles/r3/highlight.json`. */ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { DOMParser, type Node as XmlNode } from '@xmldom/xmldom' import { describe, expect, it } from 'vitest' import type { Observation, SheetSnapshot } from '@faunosteo/domain' import { cleanSvgRoot, renderHighlightedView } from '../src/svg/view' import { serializeSvg } from '../src/svg/parse' import { loadGoldenReports, REPORT_NAMES } from './helpers/reports' const HERE = dirname(fileURLToPath(import.meta.url)) const R3 = resolve(HERE, '../../../crates/core/tests/oracles/r3') const OUT = resolve(R3, 'highlight.json') const MAKE = process.env.MAKE_R3_ORACLES === '1' type TreeNode = | { ns: string | null; name: string; attrs: Record; children: TreeNode[] } | { text: string } | { comment: string } | { pi: string } function treeOf(node: XmlNode): TreeNode { if (node.nodeType === 3 || node.nodeType === 4) return { text: node.nodeValue ?? '' } if (node.nodeType === 8) return { comment: node.nodeValue ?? '' } if (node.nodeType === 7) return { pi: node.nodeName } const el = node as unknown as Element const attrs: Record = {} const names = Array.from({ length: el.attributes.length }, (_, i) => el.attributes[i]!.name) for (const name of names.sort()) attrs[name] = el.getAttribute(name) ?? '' const children = Array.from({ length: node.childNodes.length }, (_, i) => node.childNodes[i]!) return { ns: el.namespaceURI, name: el.localName, attrs, children: children.map(treeOf) } } const parsedTree = (svg: string) => treeOf(new DOMParser().parseFromString(svg, 'image/svg+xml').documentElement!) const EDGE_MAIN = [ '', '', '\n', '', ].join('') const EDGE_OWN = '' const OWN_ASSET = '9d0e5f3a-1b2c-4d5e-8f90-a1b2c3d4e5f6' function edgeSnapshot(): SheetSnapshot { const layer = JSON.parse(readFileSync(resolve(R3, 'layer.json'), 'utf8')) const document = structuredClone(layer.edge) const main = document.views.find((v: { id: string }) => v.id === 'main') document.views.push({ id: 'detail-own', kind: 'detail', parent_view_id: 'main', name: { fr: 'Détail', en: 'Detail' }, svg_asset_id: OWN_ASSET, view_box: null, }) document.annotations.push({ id: 'own-point', kind: 'point', view_id: 'detail-own', category_id: 'joints', laterality: 'unpaired', geometry: { type: 'circle', cx: 5, cy: 5, r: 20 }, review_status: 'inherited', color: null, name: { fr: 'Point', en: 'Point' }, finding_zone_id: 'hip', vertebral_region_code: null, }) return { status: 'draft', ref: { sheetId: 'edge', versionId: null, documentSha256: 'e'.repeat(64) }, document, assets: [ { svgAssetId: main.svg_asset_id, sha256: 'a'.repeat(64), svg: EDGE_MAIN }, { svgAssetId: OWN_ASSET, sha256: 'b'.repeat(64), svg: EDGE_OWN }, ], } as unknown as SheetSnapshot } const zone = (annotationId: string, viewId: string) => ({ annotationId, viewId }) const observation = (id: string, zones: Array<{ annotationId: string; viewId: string }>) => ({ id, zones, findings: [], freeTextFindings: [], comment: '' }) as unknown as Observation const EDGE_OBSERVATIONS = [ observation('edge-1', [ zone('point-polygon', 'main'), zone('zone-path', 'main'), zone('trigger-named', 'main'), zone('zone-circle', 'main'), zone('trigger-circle', 'main'), ]), observation('edge-2', [zone('head-point', 'head'), zone('hip', 'main')]), observation('edge-3', [zone('own-point', 'detail-own')]), observation('edge-4', []), ] interface HighlightCase { sheet: string observation: Observation viewId: string size: { widthPx: number; heightPx: number } root: { ns: string | null; name: string; attrs: Record } /** Children before the asset's, the asset's (or `asset:` when unchanged), the layer. */ before: TreeNode[] asset: TreeNode[] | string layer: TreeNode } function assetIdOf(sheet: SheetSnapshot, viewId: string): string { const views = new Map(sheet.document.views.map((v) => [v.id, v])) let view = views.get(viewId) while (view && view.svg_asset_id === null && view.kind === 'detail') { view = views.get(view.parent_view_id) } return view!.svg_asset_id! } function highlightCase( name: string, sheet: SheetSnapshot, obs: Observation, viewId: string, sanitized: Record, ): HighlightCase { const rendered = renderHighlightedView(sheet, viewId, obs) const tree = parsedTree(rendered.svg) if (!('children' in tree)) throw new Error('no root') const assetId = assetIdOf(sheet, viewId) const assetTree = sanitized[assetId]! const assetChildren = 'children' in assetTree ? assetTree.children : [] const before = tree.children.slice(0, tree.children.length - 1 - assetChildren.length) const middle = tree.children.slice(before.length, -1) const same = JSON.stringify(middle) === JSON.stringify(assetChildren) return { sheet: name, observation: obs, viewId, size: rendered.size, root: { ns: tree.ns, name: tree.name, attrs: tree.attrs }, before: same ? before : [], asset: same ? `asset:${assetId}` : tree.children.slice(0, -1), layer: tree.children.at(-1)!, } } function sanitizedTrees(sheet: SheetSnapshot): Record { return Object.fromEntries( sheet.assets.map((a) => [a.svgAssetId, parsedTree(serializeSvg(cleanSvgRoot(a.svg)))]), ) } function build() { const golden = loadGoldenReports() const edge = edgeSnapshot() const sanitized = { ...sanitizedTrees(golden.snapshots.demo), ...sanitizedTrees(edge) } const cases: HighlightCase[] = [] for (const name of REPORT_NAMES) { for (const obs of golden.reports[name].observations) { for (const viewId of ['main', 'head']) { cases.push(highlightCase(name, golden.snapshots[name], obs, viewId, sanitized)) } } } for (const obs of EDGE_OBSERVATIONS) { for (const viewId of ['main', 'head', 'detail-own']) { cases.push(highlightCase('edge', edge, obs, viewId, sanitized)) } } return { edgeAssets: { main: EDGE_MAIN, own: EDGE_OWN, ownAssetId: OWN_ASSET }, sanitized, cases } } describe('R3 highlight oracles (TypeScript freeze)', () => { it('matches the frozen highlighted trees', () => { const actual = build() if (MAKE) { mkdirSync(R3, { recursive: true }) writeFileSync(OUT, `${JSON.stringify(actual, null, 1)}\n`) return } expect(actual).toEqual(JSON.parse(readFileSync(OUT, 'utf8'))) }) })