fix(app): closed hash and canonical-form drawing messages, split crop and drawing views

This commit is contained in:
bchanot committed 2026-11-04 13:34:02 +01:00
1 parent 7bf1404946
commit 1bbf97e65b
16 files changed
+487 -248

No files matched your search

@@ -12,7 +12,10 @@ import { SheetFailure } from '@faunosteo/supabase'
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
import ViewsTab from '@/components/editor/tabs/ViewsTab'
import { cleanSvg } from '@/editor/core/cleaning'
import { MAX_DRAWING_BYTES } from '@/editor/drawingUpload'
import { createHash } from 'node:crypto'
import { getDict } from '@/i18n'
import { uploadErrorMessage } from '@/editor/uploadMessages'
import { MAX_DRAWING_BYTES, attachToView } from '@/editor/drawingUpload'
import { useLocaleStore } from '@/state/localeStore'
import { ASSET } from '../helpers/editorFixtures'
import { openReady, resetStore, sb, store } from '../helpers/editorHarness'
@@ -139,13 +142,11 @@ describe('drawing upload flow', () => {
expect(
within(dialog).getByTestId('cleaning-preview').querySelector('svg'),
).not.toBeNull()
await userEvent
.setup()
.click(
within(dialog).getByRole('button', {
name: 'Nettoyer et utiliser cette version',
}),
)
await userEvent.setup().click(
within(dialog).getByRole('button', {
name: 'Nettoyer et utiliser cette version',
}),
)
await waitFor(() => expect(mainAsset()).toBe(NEW_ASSET))
expect(sb.uploadSheetDrawing).toHaveBeenCalledTimes(1)
})
@@ -255,6 +256,35 @@ describe('drawing upload flow', () => {
expect(mainAsset()).toBe(ASSET)
})
it('a held drawing outside the stored form is refused at attach as svg_not_canonical', async () => {
const bytes = new TextEncoder().encode(
svg('0 0 200 100', ' <rect width="4" height="4"/>'),
)
const sha256 = createHash('sha256').update(bytes).digest('hex')
expect(
store.getState().addDrawing({ assetId: NEW_ASSET, sha256, bytes }).ok,
).toBe(true)
expect(attachToView('view-main', NEW_ASSET)).toEqual({
ok: false,
error: { code: 'svg_not_canonical' },
})
expect(mainAsset()).toBe(ASSET)
})
it('phrases svg_not_canonical and the new reader reasons', () => {
const t = (key: string) => getDict('fr')[key as never] as string
const context = { doc: null, locale: 'fr' as const }
expect(
uploadErrorMessage(t, { code: 'svg_not_canonical' }, context),
).toMatch(/pas sous la forme enregistrée/)
const reason = (value: string) =>
uploadErrorMessage(t, { code: 'svg_rejected', reason: value }, context)
expect(reason('processing_instruction')).toMatch(
/instruction de traitement/,
)
expect(reason('removed <script>')).toMatch(/éléments à retirer/)
})
it('a refused frame change leaves the drawing uploaded and held, the view unchanged', async () => {
serverGets(svg('0 0 20 20'))
await pick(svg('0 0 20 20'))
+9
View File
@@ -51,6 +51,15 @@ describe('open and sessions', () => {
expect(store.getState().previewState?.drawings[0]).toMatchObject({ state: 'missing' })
})
it('records a drawing whose bytes fail the hash check at load', async () => {
serverHas(baseText(), 3)
sb.downloadSheetDrawing.mockResolvedValue(new TextEncoder().encode('<svg/>'))
store.getState().open(VERSION, USER)
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
expect(store.getState().drawingFailures[ASSET]).toBe('drawing_hash_mismatch')
expect(store.getState().drawings).toEqual([])
})
it('drops a late load after close and frees the preview it built', async () => {
const late = deferred<unknown>()
sb.getSheetVersion.mockReturnValue(late.promise as never)
+24 -3
View File
@@ -111,7 +111,7 @@ describe('Vues et illustrations: drawing states', () => {
).toBeTruthy()
})
it('a rejected drawing shows the core detail', async () => {
it('a rejected drawing shows the closed not-canonical message, no raw detail', async () => {
const bytes = new TextEncoder().encode(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> <rect width="4" height="4"/></svg>',
)
@@ -125,8 +125,29 @@ describe('Vues et illustrations: drawing states', () => {
store.getState().open('ver-1', 'user-1', { canEdit: true })
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
render(<ViewsTab {...props} />)
expect(screen.getByText(/Illustration refusée/)).toBeTruthy()
expect(screen.getByText(/Détail : not_canonical/)).toBeTruthy()
expect(
screen.getByText(
/Illustration refusée : elle n'est pas sous la forme enregistrée/,
),
).toBeTruthy()
expect(screen.queryByText(/not_canonical/)).toBeNull()
})
it('bytes that fail the hash check at load show their own message, not « introuvable »', async () => {
store.getState().close()
sb.getSheetVersion.mockResolvedValue(
versionAnswer(baseText(), 3, true) as never,
)
sb.downloadSheetDrawing.mockResolvedValue(
new TextEncoder().encode('<svg/>'),
)
store.getState().open('ver-1', 'user-1', { canEdit: true })
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
render(<ViewsTab {...props} />)
expect(
screen.getByText(/ne correspond pas à son empreinte enregistrée/),
).toBeTruthy()
expect(screen.queryByText(/introuvable/)).toBeNull()
})
it('read-only shows states as text and no action', async () => {