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,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