Refuse local references to ids a drawing does not define, scope canvas ids per view, upload every drawing in one stored form checked by the verdict and the preview, keep strict numeric rgb() colours, and commit real Inkscape 1.4.3 exports with their generator.
129 lines
4.7 KiB
TypeScript
129 lines
4.7 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
|
|
}
|
|
|
|
/**
|
|
* Owner decision 2026-10-29 (V1-2.2): the canvas, inlined in the page, scopes the drawing's
|
|
* ids and the references to them to its view (`fv.<view>.<id>`). Applied to a frozen tree (a
|
|
* deep copy), written apart from the core's rewrite: every `id`, every `#id` href and every
|
|
* plain `url(#` (the only spelling of the product drawing). Mirrors
|
|
* `crates/core/tests/common/svg_tree.rs`.
|
|
*/
|
|
export function withIdsScopedPerView(tree: unknown, viewId: string): unknown {
|
|
const copy = structuredClone(tree)
|
|
const scope = `fv.${viewId}.`
|
|
const visit = (node: unknown): void => {
|
|
if (typeof node !== 'object' || node === null) return
|
|
const n = node as { attrs?: Record<string, string>; children?: unknown[] }
|
|
for (const [name, value] of Object.entries(n.attrs ?? {})) {
|
|
if (name === 'id') n.attrs![name] = `${scope}${value}`
|
|
else if ((name === 'href' || name === 'xlink:href') && value.startsWith('#')) {
|
|
n.attrs![name] = `#${scope}${value.slice(1)}`
|
|
} else n.attrs![name] = value.replaceAll('url(#', `url(#${scope}`)
|
|
}
|
|
n.children?.forEach(visit)
|
|
}
|
|
visit(copy)
|
|
return copy
|
|
}
|