104 lines
3.6 KiB
TypeScript
104 lines
3.6 KiB
TypeScript
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'
|
|
|
|
/**
|
|
* Compares `actual` with the frozen R3 golden `name`. `declared` applies an owner-validated
|
|
* change to the frozen value before comparing (the file itself never changes); with one,
|
|
* rewriting the golden is refused.
|
|
*/
|
|
export function checkR3Golden(
|
|
name: string,
|
|
actual: unknown,
|
|
declared?: (frozen: unknown) => unknown,
|
|
): void {
|
|
const file = resolve(R3_GOLDEN_DIR, name)
|
|
if (MAKE_R3_GOLDEN && declared) throw new Error(`${name}: frozen, a declared change applies`)
|
|
if (MAKE_R3_GOLDEN) {
|
|
mkdirSync(R3_GOLDEN_DIR, { recursive: true })
|
|
writeFileSync(file, `${JSON.stringify(actual, null, 1)}\n`)
|
|
return
|
|
}
|
|
const frozen: unknown = JSON.parse(readFileSync(file, 'utf8'))
|
|
expect(actual, name).toEqual(declared ? declared(frozen) : frozen)
|
|
}
|
|
|
|
export interface ViewName {
|
|
id: string
|
|
fr: string
|
|
en: string
|
|
}
|
|
|
|
/**
|
|
* Owner decision 2026-10-15 (R3): a trigger without a name of its own carries the opened
|
|
* view's name in each language; the TypeScript freeze carries the French name twice. Applied
|
|
* to a frozen tree (a deep copy): `data-anno-name-en` takes the English name and the title
|
|
* text reads "FR / EN". Mirrors `crates/core/tests/common/svg_tree.rs`.
|
|
*/
|
|
export function withTriggerNamesPerLanguage(tree: unknown, views: readonly ViewName[]): unknown {
|
|
const copy = structuredClone(tree)
|
|
const visit = (node: unknown): void => {
|
|
if (typeof node !== 'object' || node === null) return
|
|
const n = node as { attrs?: Record<string, string>; text?: string; children?: unknown[] }
|
|
const view = views.find((v) => v.id === n.attrs?.['data-anno-opens-view'])
|
|
if (view && n.attrs?.['data-anno-name-en'] === view.fr) n.attrs['data-anno-name-en'] = view.en
|
|
for (const v of views) {
|
|
if (n.text === `${v.fr} / ${v.fr}`) n.text = `${v.fr} / ${v.en}`
|
|
}
|
|
n.children?.forEach(visit)
|
|
if (!('attrs' in n) && !('children' in n)) Object.values(n).forEach(visit)
|
|
}
|
|
visit(copy)
|
|
return copy
|
|
}
|