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:
1 parent
bdf5e6475b
commit
e8c29c1fdf
7 files changed
+449
-113
No files matched your search
@@ -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)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user