Files
faunosteo/apps/app/tests/unit/canvas.trees.test.tsx

96 lines
3.7 KiB
TypeScript

/**
* R3 freeze O1 (plan 2026-10-14 §3), now the switch's oracle: the editor canvas as the
* TypeScript built it before the anatomy core moved to Rust (frozen at 47c5f49, never
* regenerated to make this pass). For each view, the whole tree with no state; 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 of the mounted canvas (the core composes, the canvas restyles in place).
* Trees compared by structure and semantic properties, never by bytes or attribute order.
*/
import { afterEach, beforeAll, describe, expect, it } from 'vitest'
import { cleanup, render, waitFor } from '@testing-library/react'
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
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,
withTriggerNamesPerLanguage,
type TreeNode,
} from '../helpers/svgTree'
let sheet: SheetIndex
let golden: GoldenReports
beforeAll(async () => {
sheet = await loadTestSheet()
golden = buildGoldenReports(sheet)
})
afterEach(cleanup)
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
}
/** The declared change of the owner's decision 2026-10-15, on the draft's views. */
function declared(frozen: unknown): unknown {
const views = sheet.sheet.document.views.map((v) => ({ id: v.id, ...v.name }))
return withTriggerNamesPerLanguage(frozen, views)
}
async function mountedLayer(viewId: string, states: ViewStates): Promise<TreeNode> {
const { container, unmount } = render(
<AnatomyCanvas index={sheet} viewId={viewId} states={states} onZoneClick={() => undefined} />,
)
await waitFor(() => expect(container.querySelector('svg')).not.toBeNull())
const layer = layerOf(treeOf(container.querySelector('svg')!))
unmount()
return layer
}
describe.each(VIEWS)('the %s canvas', (viewId) => {
it('builds the frozen tree with no state', () => {
const build = buildViewSvg(sheet, viewId)
if (!build.ok) throw new Error(`canvas ${viewId}: ${build.failure}`)
checkR3Golden(`canvas.${viewId}.json`, treeOf(build.svg), declared)
})
it('shows the frozen layer for every state set of the reference reports', async () => {
const layers: Record<string, TreeNode> = {}
for (const [name, states] of stateCases()) {
layers[name] = await mountedLayer(viewId, states)
}
expect(Object.keys(layers).length).toBeGreaterThan(8)
checkR3Golden(`canvas.${viewId}.layers.json`, layers, declared)
})
})