test(tests): R3 freeze O1 canvas trees and the visual comparison script

This commit is contained in:
bchanot committed 2026-10-14 23:26:20 +02:00
1 parent fbc9526e57
commit 47c5f49014
7 files changed
+40542

No files matched your search

File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+63
View File
@@ -0,0 +1,63 @@
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { expect } from 'vitest'
import { FIXTURES_DIR } from './goldens'
/**
* R3 freeze (plan 2026-10-14 §3 O1): a DOM tree as structure and semantic
* properties, never bytes. Elements by namespace + local name, attributes as
* a map keyed by qualified name (their order is not part of the contract),
* children in document order with text nodes kept (the canvas imports the
* drawing's whitespace), comments and processing instructions named.
*/
export type TreeNode =
| {
ns: string | null
name: string
attrs: Record<string, string>
children: TreeNode[]
}
| { text: string }
| { comment: string }
| { pi: string }
const ELEMENT = 1
const TEXT = 3
const CDATA = 4
const PI = 7
const COMMENT = 8
export function treeOf(node: Node): TreeNode {
if (node.nodeType === TEXT || node.nodeType === CDATA) {
return { text: node.nodeValue ?? '' }
}
if (node.nodeType === COMMENT) return { comment: node.nodeValue ?? '' }
if (node.nodeType === PI) return { pi: node.nodeName }
if (node.nodeType !== ELEMENT) throw new Error(`node type ${node.nodeType}`)
const el = node as Element
const attrs: Record<string, string> = {}
for (const name of Array.from(el.attributes, (a) => a.name).sort()) {
attrs[name] = el.getAttribute(name) ?? ''
}
return {
ns: el.namespaceURI,
name: el.localName,
attrs,
children: Array.from(el.childNodes, treeOf),
}
}
export const R3_GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/r3')
/** `MAKE_R3_GOLDEN=1` writes the R3 freeze; never set it without the owner's decision once frozen. */
export const MAKE_R3_GOLDEN = process.env.MAKE_R3_GOLDEN === '1'
export function checkR3Golden(name: string, actual: unknown): void {
const file = resolve(R3_GOLDEN_DIR, name)
if (MAKE_R3_GOLDEN) {
mkdirSync(R3_GOLDEN_DIR, { recursive: true })
writeFileSync(file, `${JSON.stringify(actual, null, 1)}\n`)
return
}
expect(actual, name).toEqual(JSON.parse(readFileSync(file, 'utf8')))
}
+71
View File
@@ -0,0 +1,71 @@
/**
* R3 freeze O1 (plan 2026-10-14 §3): the editor canvas as the TypeScript
* built it before the anatomy core moved to Rust. For each view, the whole
* tree with no state (root attributes, the drawing's nodes, the layer), and
* for every state set the editor can produce from the three reference
* reports (each observation active, zones of every view, as EditorRoute
* builds them) plus "every annotation", the annotation layer. After the
* switch the same assertions hold on the Rust composition: trees compared by
* structure and semantic properties, never by bytes or attribute order.
*/
import { beforeAll, describe, expect, it } from 'vitest'
import { buildViewSvg, type ViewStates } from '@/anatomy/drawing'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
import {
buildGoldenReports,
GOLDEN_REPORTS,
type GoldenReports,
} from '../helpers/goldenReports'
import { checkR3Golden, treeOf, type TreeNode } from '../helpers/svgTree'
let sheet: SheetIndex
let golden: GoldenReports
beforeAll(async () => {
sheet = await loadTestSheet()
golden = buildGoldenReports(sheet)
})
const VIEWS = ['main', 'head'] as const
/** The state sets of the editor: no selection, every observation of the three reports, all. */
function stateCases(): Array<[string, ViewStates]> {
const cases: Array<[string, ViewStates]> = [['none', new Map()]]
for (const name of GOLDEN_REPORTS) {
for (const obs of golden.reports[name].observations) {
const states = new Map<string, 'current'>()
obs.zones.forEach((z) => states.set(z.annotationId, 'current'))
cases.push([`${name}:${obs.id}`, states])
}
}
const all = new Map<string, 'current'>()
sheet.sheet.document.annotations.forEach((a) => all.set(a.id, 'current'))
cases.push(['all', all])
return cases
}
/** The layer is the last element child of the root; everything before it is the drawing. */
function layerOf(tree: TreeNode): TreeNode {
if (!('children' in tree)) throw new Error('not an element')
const last = tree.children.at(-1)
if (!last || !('attrs' in last) || last.attrs['data-layer'] !== 'annotations') {
throw new Error('the layer is not the last child')
}
return last
}
describe.each(VIEWS)('the %s canvas', (viewId) => {
it('builds the frozen tree with no state', () => {
const tree = treeOf(buildViewSvg(sheet, viewId, new Map()))
checkR3Golden(`canvas.${viewId}.json`, tree)
})
it('builds the frozen layer for every state set of the reference reports', () => {
const layers: Record<string, TreeNode> = {}
for (const [name, states] of stateCases()) {
layers[name] = layerOf(treeOf(buildViewSvg(sheet, viewId, states)))
}
expect(Object.keys(layers).length).toBeGreaterThan(8)
checkR3Golden(`canvas.${viewId}.layers.json`, layers)
})
})