Files
faunosteo/apps/app/tests/unit/canvas.trees.test.tsx
T
bchanot e823655f09 fix(anatomy): V1-2.2 owner arbitrations on SVG references and bytes
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.
2026-10-29 16:24:07 +01:00

100 lines
3.9 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,
withIdsScopedPerView,
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}`)
// and the owner's decision 2026-10-29: the drawing's ids scoped to the view
checkR3Golden(`canvas.${viewId}.json`, treeOf(build.svg), (frozen) =>
withIdsScopedPerView(declared(frozen), viewId),
)
})
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)
})
})