feat(editor): the canvas draws the view the Rust core composed; denylist and anatomy dependency removed

This commit is contained in:
bchanot committed 2026-10-14 23:59:46 +02:00
1 parent 43a370aed1
commit 09a6cf7def
11 files changed
+165 -158

No files matched your search

@@ -1,14 +1,15 @@
/**
* 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.
* 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 { beforeAll, describe, expect, it } from 'vitest'
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'
@@ -25,6 +26,7 @@ beforeAll(async () => {
sheet = await loadTestSheet()
golden = buildGoldenReports(sheet)
})
afterEach(cleanup)
const VIEWS = ['main', 'head'] as const
@@ -54,16 +56,27 @@ function layerOf(tree: TreeNode): TreeNode {
return last
}
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 tree = treeOf(buildViewSvg(sheet, viewId, new Map()))
checkR3Golden(`canvas.${viewId}.json`, tree)
const svg = buildViewSvg(sheet, viewId)
expect(svg).not.toBeNull()
checkR3Golden(`canvas.${viewId}.json`, treeOf(svg!))
})
it('builds the frozen layer for every state set of the reference reports', () => {
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] = layerOf(treeOf(buildViewSvg(sheet, viewId, states)))
layers[name] = await mountedLayer(viewId, states)
}
expect(Object.keys(layers).length).toBeGreaterThan(8)
checkR3Golden(`canvas.${viewId}.layers.json`, layers)
@@ -1,9 +1,11 @@
import { beforeAll, describe, expect, it, vi, afterEach } from 'vitest'
import { cleanup, render, waitFor } from '@testing-library/react'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
import { buildViewSvg, parseDrawing, sanitizeSvg } from '@/anatomy/drawing'
import { buildViewSvg } from '@/anatomy/drawing'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import { bundledSheet, loadTestSheet, sheetFromBundle } from '../helpers/sheet'
let sheet: SheetIndex
beforeAll(async () => {
@@ -11,10 +13,17 @@ beforeAll(async () => {
})
afterEach(cleanup)
/** The bundled sheet with its drawing replaced by `svg` (a hostile asset). */
async function sheetDrawing(svg: string): Promise<SheetIndex> {
const bundle = await bundledSheet()
const assets = Object.fromEntries(Object.keys(bundle.assets).map((id) => [id, svg]))
return sheetFromBundle({ ...bundle, assets })
}
describe('buildViewSvg', () => {
it('frames the main view on the asset box and the head view on its crop of the same drawing', () => {
const main = buildViewSvg(sheet, 'main', new Map())
const head = buildViewSvg(sheet, 'head', new Map())
const main = buildViewSvg(sheet, 'main')!
const head = buildViewSvg(sheet, 'head')!
expect(main.getAttribute('viewBox')).toBe('0 0 2073.4 1629.4')
expect(head.getAttribute('viewBox')).toBe('60 100 500 380')
expect(main.querySelectorAll('path').length).toBe(
@@ -27,25 +36,23 @@ describe('buildViewSvg', () => {
expect(main.hasAttribute('width')).toBe(false)
})
it('applies states as data-state on the generated markers', () => {
const svg = buildViewSvg(sheet, 'main', new Map([['stifle', 'current']]))
const marked = Array.from(
svg.querySelectorAll('[data-state="current"]'),
).map((el) => el.getAttribute('data-anno-id'))
expect(marked).toEqual(['stifle', 'stifle'])
it('composes the view once per sheet (a constant text) and carries no state', () => {
expect(sheet.canvasSvg('main')).toEqual(sheet.canvasSvg('main'))
expect(buildViewSvg(sheet, 'main')!.querySelector('[data-state]')).toBeNull()
})
it('sanitises scripts, embedded documents and handlers out of an asset', () => {
const root = parseDrawing(
it('refuses a drawing the core would have to alter: nothing of it reaches the page (LRN-025)', async () => {
const hostile = await sheetDrawing(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><script>alert(1)</script><g onclick="x()"><a href="javascript:void(0)"><path d="M0 0"/></a></g><foreignObject/></svg>',
)
sanitizeSvg(root)
expect(root.querySelector('script')).toBeNull()
expect(root.querySelector('foreignObject')).toBeNull()
expect(root.querySelector('g')!.hasAttribute('onclick')).toBe(false)
expect(root.querySelector('a')!.hasAttribute('href')).toBe(false)
expect(root.querySelector('path')).not.toBeNull()
expect(() => parseDrawing('<not svg')).toThrow()
expect(hostile.canvasSvg('main')).toMatchObject({ ok: false, kind: 'svg_rejected' })
expect(buildViewSvg(hostile, 'main')).toBeNull()
const malformed = await sheetDrawing('<not svg')
expect(malformed.canvasSvg('main')).toMatchObject({
ok: false,
kind: 'svg_rejected',
detail: 'malformed',
})
})
})
@@ -74,6 +81,37 @@ describe('AnatomyCanvas', () => {
expect(onZoneClick).toHaveBeenCalledTimes(1)
})
it('applies the states right after the build, on both elements of a point', async () => {
const { container } = render(
<AnatomyCanvas
index={sheet}
viewId="main"
states={new Map([['stifle', 'current']])}
onZoneClick={() => undefined}
/>,
)
await waitFor(() =>
expect(container.querySelector('[data-anno-id="stifle"]')).not.toBeNull(),
)
const marked = Array.from(
container.querySelectorAll('[data-state="current"]'),
).map((el) => el.getAttribute('data-anno-id'))
expect(marked).toEqual(['stifle', 'stifle'])
})
it('shows the degraded notice for a drawing it cannot draw, and nothing of the drawing', async () => {
useLocaleStore.setState({ locale: 'fr' })
const hostile = await sheetDrawing(
'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
)
const { container } = render(
<AnatomyCanvas index={hostile} viewId="main" states={new Map()} onZoneClick={() => undefined} />,
)
await screen.findByText(fr['sheet.core_unavailable']!)
expect(screen.getByRole('button', { name: fr['sheet.core_retry'] })).toBeInTheDocument()
expect(container.querySelector('script, [data-layer], [data-anno-id]')).toBeNull()
})
it('restyles in place when the states change', async () => {
const { container, rerender } = render(
<AnatomyCanvas