Files
faunosteo/apps/app/tests/unit/canvas.trees.test.ts
T

72 lines
2.8 KiB
TypeScript

/**
* 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)
})
})