feat(editor): the canvas tells an engine failure (retry) from a view it cannot draw (no retry)

This commit is contained in:
bchanot committed 2026-10-15 01:03:16 +02:00
1 parent 6882c6da4a
commit c0a5c8c7ce
8 files changed
+182 -36

No files matched your search

@@ -1,10 +1,12 @@
import { beforeAll, describe, expect, it, vi, afterEach } from 'vitest'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
import { buildViewSvg } from '@/anatomy/drawing'
import type { SheetIndex } from '@/services/sheetIndex'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import { en } from '@/i18n/en'
import { bundledSheet, loadTestSheet, sheetFromBundle } from '../helpers/sheet'
let sheet: SheetIndex
@@ -13,6 +15,13 @@ beforeAll(async () => {
})
afterEach(cleanup)
/** The composed view, or a test failure naming why it is missing. */
function svgOf(index: SheetIndex, viewId: string): SVGSVGElement {
const build = buildViewSvg(index, viewId)
if (!build.ok) throw new Error(`${viewId}: ${build.failure}`)
return build.svg
}
/** The bundled sheet with its drawing replaced by `svg` (a hostile asset). */
async function sheetDrawing(svg: string): Promise<SheetIndex> {
const bundle = await bundledSheet()
@@ -22,8 +31,8 @@ async function sheetDrawing(svg: string): Promise<SheetIndex> {
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')!
const head = buildViewSvg(sheet, 'head')!
const main = svgOf(sheet, 'main')
const head = svgOf(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(
@@ -38,7 +47,7 @@ describe('buildViewSvg', () => {
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()
expect(svgOf(sheet, 'main').querySelector('[data-state]')).toBeNull()
})
it('refuses a drawing the core would have to alter: nothing of it reaches the page (LRN-025)', async () => {
@@ -46,7 +55,7 @@ describe('buildViewSvg', () => {
'<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>',
)
expect(hostile.canvasSvg('main')).toMatchObject({ ok: false, kind: 'svg_rejected' })
expect(buildViewSvg(hostile, 'main')).toBeNull()
expect(buildViewSvg(hostile, 'main')).toEqual({ ok: false, failure: 'view' })
const malformed = await sheetDrawing('<not svg')
expect(malformed.canvasSvg('main')).toMatchObject({
ok: false,
@@ -99,17 +108,55 @@ describe('AnatomyCanvas', () => {
expect(marked).toEqual(['stifle', 'stifle'])
})
it('shows the degraded notice for a drawing it cannot draw, and nothing of the drawing', async () => {
it('names a view it cannot draw, offers no retry and keeps the drawing and the draft as they are', async () => {
useLocaleStore.setState({ locale: 'fr' })
const hostile = await sheetDrawing(
'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
)
const drawing = '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>'
const hostile = await sheetDrawing(drawing)
sessionStorage.setItem('osteo:draft', '{"kept":true}')
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()
await screen.findByText(fr['canvas.view_unavailable']!)
expect(screen.queryByRole('button', { name: fr['sheet.core_retry'] })).toBeNull()
expect(screen.queryByText(fr['sheet.core_unavailable']!)).toBeNull()
expect(container.querySelector('script, [data-layer], [data-anno-id]')).toBeNull()
// The refused drawing stays the bundle's own text; the session is untouched.
expect(hostile.canvasSvg('main')).toMatchObject({ ok: false, kind: 'svg_rejected' })
expect(sessionStorage.getItem('osteo:draft')).toBe('{"kept":true}')
})
it('offers a retry when the engine did not run, and draws the view once it does', async () => {
useLocaleStore.setState({ locale: 'fr' })
const user = userEvent.setup()
let engineDown = true
const flaky = Object.create(sheet) as SheetIndex
flaky.canvasSvg = (viewId: string) =>
engineDown ? { ok: false, kind: 'core_unavailable' } : sheet.canvasSvg(viewId)
const { container } = render(
<AnatomyCanvas index={flaky} viewId="main" states={new Map()} onZoneClick={() => undefined} />,
)
await screen.findByText(fr['sheet.core_unavailable']!)
expect(screen.queryByText(fr['canvas.view_unavailable']!)).toBeNull()
engineDown = false
await user.click(screen.getByRole('button', { name: fr['sheet.core_retry'] }))
await waitFor(() => expect(container.querySelector('[data-anno-id="hip"]')).not.toBeNull())
expect(screen.queryByText(fr['sheet.core_unavailable']!)).toBeNull()
})
it('words both failures in English too', async () => {
useLocaleStore.setState({ locale: 'en' })
const hostile = await sheetDrawing('<svg xmlns="http://www.w3.org/2000/svg"><script/></svg>')
const first = render(
<AnatomyCanvas index={hostile} viewId="main" states={new Map()} onZoneClick={() => undefined} />,
)
await screen.findByText(en['canvas.view_unavailable']!)
first.unmount()
const down = Object.create(sheet) as SheetIndex
down.canvasSvg = () => ({ ok: false, kind: 'internal' })
render(<AnatomyCanvas index={down} viewId="main" states={new Map()} onZoneClick={() => undefined} />)
await screen.findByText(en['sheet.core_unavailable']!)
expect(screen.getByRole('button', { name: en['sheet.core_retry'] })).toBeInTheDocument()
useLocaleStore.setState({ locale: 'fr' })
})
it('restyles in place when the states change', async () => {