test(pdf): R3 freeze O3 highlighted compositions as trees
This commit is contained in:
1 parent
969b872b98
commit
d1aa35e97c
2 files changed
+14588
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* 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 `<defs>` 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<string, string>; 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<string, string> = {}
|
||||
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 = [
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"',
|
||||
' viewBox="0 0 100 80" width="100" height="80">',
|
||||
'<g id="g1"><defs id="d1"><linearGradient id="lg"><stop offset="0" stop-color="#000"/>',
|
||||
'</linearGradient></defs><path d="M0 0h10" fill="url(#lg)"/></g>\n',
|
||||
'<rect x="1" y="1" width="5" height="5"/></svg>',
|
||||
].join('')
|
||||
const EDGE_OWN = '<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50"><circle cx="5" cy="5" r="2"/></svg>'
|
||||
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<string, string> }
|
||||
/** Children before the asset's, the asset's (or `asset:<id>` 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<string, TreeNode>,
|
||||
): 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<string, TreeNode> {
|
||||
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')))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user