fix(editor,pdf): accordion observation cards + seed 1 obs + native SVG rasterization

Three issues from manual QA:

1. UX restructure — observations are now an accordion list. Each card has
   a header that's always visible (title + chips + chevron + action
   buttons) and a body that expands inline showing zones / pathologies /
   severity / comment. Click the header (or the chevron) to toggle.
   ObservationEditor has been merged into ObservationCard so each card
   owns its own details. Only one observation is expanded at a time; the
   chevron on the active card collapses everything. EditorRoute uses
   activeObservationIndex === -1 to represent "all collapsed".

2. App used to start with 2 observations instead of 1. Root cause:
   EditorRoute had a "seed first observation" useEffect that fired twice
   under React 19 StrictMode in dev, both invocations seeing length=0 and
   queueing addObservation(). Fixed by moving the seed into the store's
   initReport (synchronous, one-shot) and removing the useEffect entirely.
   Store tests updated to assert the seed behaviour (90/90 pass).

3. PDF export still failed with "The input is not a PNG file!" — canvg
   was returning bytes that pdf-lib rejected at embedPng(). Switched
   rasterizeSvg to the browser-native pipeline: serialize the SVG into a
   Blob URL, load it with an Image element, drawImage onto a 2D canvas,
   then toBlob → PNG bytes (with a toDataURL fallback). Validates the
   PNG signature before returning. Tree-shaking dropped canvg from the
   bundle (979 kB → 874 kB JS).

Tests updated for the new initReport behaviour and the new svgToPng
pipeline (Image mock + dataURL fallback case). lint + typecheck + build
all clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude Opus 4.7 committed 2026-05-18 23:32:01 +02:00
1 parent bdf5e6475b
commit e8c29c1fdf
7 files changed
+449 -113

No files matched your search

+73 -10
View File
@@ -1,24 +1,87 @@
import { describe, it, expect, vi } from 'vitest'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { rasterizeSvg } from '@/pdf/render/svgToPng'
vi.mock('canvg', () => ({
Canvg: { from: vi.fn(async () => ({ render: vi.fn(async () => {}) })) },
}))
const PNG_BYTES = new Uint8Array([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x00,
])
class MockImage {
decoding: 'sync' | 'async' | 'auto' = 'auto'
crossOrigin: string | null = null
onload: (() => void) | null = null
onerror: ((err?: unknown) => void) | null = null
private _src = ''
get src(): string {
return this._src
}
set src(value: string) {
this._src = value
queueMicrotask(() => this.onload?.())
}
}
describe('rasterizeSvg', () => {
it('returns a Uint8Array', async () => {
beforeEach(() => {
vi.stubGlobal('Image', MockImage)
vi.stubGlobal('URL', {
createObjectURL: () => 'blob:mock',
revokeObjectURL: () => undefined,
})
})
it('returns PNG bytes for a valid SVG', async () => {
const canvas = document.createElement('canvas')
canvas.getContext = (() => ({
fillStyle: '',
fillRect: () => {},
fillRect: () => undefined,
drawImage: () => undefined,
})) as unknown as HTMLCanvasElement['getContext']
canvas.toBlob = ((cb: BlobCallback) => {
cb(new Blob([new Uint8Array([1, 2, 3])], { type: 'image/png' }))
cb(new Blob([PNG_BYTES], { type: 'image/png' }))
}) as HTMLCanvasElement['toBlob']
vi.spyOn(document, 'createElement').mockImplementationOnce(() => canvas)
vi.spyOn(document, 'createElement').mockImplementation(
((tag: string) =>
tag === 'canvas'
? canvas
: ({}) as HTMLElement) as typeof document.createElement,
)
const out = await rasterizeSvg('<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10"/>', 2)
const out = await rasterizeSvg(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"/>',
2,
)
expect(out).toBeInstanceOf(Uint8Array)
expect(out.length).toBeGreaterThan(0)
expect(out[0]).toBe(0x89)
expect(out[1]).toBe(0x50)
expect(out[2]).toBe(0x4e)
expect(out[3]).toBe(0x47)
})
it('falls back to toDataURL when toBlob returns null', async () => {
const canvas = document.createElement('canvas')
canvas.getContext = (() => ({
fillStyle: '',
fillRect: () => undefined,
drawImage: () => undefined,
})) as unknown as HTMLCanvasElement['getContext']
canvas.toBlob = ((cb: BlobCallback) => {
cb(null)
}) as HTMLCanvasElement['toBlob']
canvas.toDataURL = () =>
'data:image/png;base64,' +
Buffer.from(PNG_BYTES).toString('base64')
vi.spyOn(document, 'createElement').mockImplementation(
((tag: string) =>
tag === 'canvas'
? canvas
: ({}) as HTMLElement) as typeof document.createElement,
)
const out = await rasterizeSvg(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"/>',
2,
)
expect(out[0]).toBe(0x89)
expect(out[1]).toBe(0x50)
})
})
+17 -9
View File
@@ -25,6 +25,14 @@ describe('reportStore.initReport', () => {
expect(r.practitioner.name).toBe('Dr. Dupont')
})
it('seeds a single empty observation when the draft has none', () => {
useReportStore.getState().initReport(draft)
const r = useReportStore.getState().report!
expect(r.observations.length).toBe(1)
expect(r.observations[0].zones).toEqual([])
expect(r.observations[0].comment).toBe('')
})
it('resets activeObservationIndex to 0', () => {
useReportStore.getState().initReport(draft)
expect(useReportStore.getState().activeObservationIndex).toBe(0)
@@ -44,8 +52,8 @@ describe('reportStore.addObservation', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().addObservation()
const s = useReportStore.getState()
expect(s.report!.observations.length).toBe(1)
expect(s.activeObservationIndex).toBe(0)
expect(s.report!.observations.length).toBe(2)
expect(s.activeObservationIndex).toBe(1)
})
})
@@ -65,21 +73,21 @@ describe('reportStore.updateObservation', () => {
describe('reportStore.removeObservation', () => {
it('drops the observation at the given index', () => {
const store = useReportStore.getState()
store.initReport(draft)
store.initReport(draft) // seeds 1
store.addObservation()
store.addObservation()
store.removeObservation(0)
expect(useReportStore.getState().report!.observations.length).toBe(1)
expect(useReportStore.getState().report!.observations.length).toBe(2)
})
it('clamps activeObservationIndex within bounds', () => {
const store = useReportStore.getState()
store.initReport(draft)
store.initReport(draft) // seeds 1
store.addObservation()
store.addObservation()
store.setActiveObservation(1)
store.removeObservation(1)
expect(useReportStore.getState().activeObservationIndex).toBe(0)
store.addObservation() // total 3
store.setActiveObservation(2)
store.removeObservation(2)
expect(useReportStore.getState().activeObservationIndex).toBe(1)
})
})