Files
faunosteo/tests/unit/state.reportStore.test.ts
T
Bastien ChanotandClaude Opus 4.7 e8c29c1fdf 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>
2026-05-18 23:32:01 +02:00

121 lines
3.9 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { useReportStore } from '@/state/reportStore'
const draft = {
date: '2026-05-18',
practitioner: { name: 'Dr. Dupont' },
animal: { speciesId: 'equine', name: 'Mistral' },
owner: { name: 'Jean Martin' },
observations: [],
conclusion: '',
recommendations: '',
}
beforeEach(() => {
useReportStore.getState().reset()
})
describe('reportStore.initReport', () => {
it('sets a report with version=1, id, and createdAt', () => {
useReportStore.getState().initReport(draft)
const r = useReportStore.getState().report!
expect(r.version).toBe(1)
expect(r.id.length).toBeGreaterThan(0)
expect(r.createdAt).toMatch(/^\d{4}-\d{2}-\d{2}T/)
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)
})
})
describe('reportStore.reset', () => {
it('clears the report', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().reset()
expect(useReportStore.getState().report).toBeNull()
})
})
describe('reportStore.addObservation', () => {
it('appends an empty observation and sets it active', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().addObservation()
const s = useReportStore.getState()
expect(s.report!.observations.length).toBe(2)
expect(s.activeObservationIndex).toBe(1)
})
})
describe('reportStore.updateObservation', () => {
it('replaces the observation at the given index', () => {
useReportStore.getState().initReport(draft)
useReportStore.getState().addObservation()
const next = {
...useReportStore.getState().report!.observations[0],
comment: 'patate',
}
useReportStore.getState().updateObservation(0, next)
expect(useReportStore.getState().report!.observations[0].comment).toBe('patate')
})
})
describe('reportStore.removeObservation', () => {
it('drops the observation at the given index', () => {
const store = useReportStore.getState()
store.initReport(draft) // seeds 1
store.addObservation()
store.addObservation()
store.removeObservation(0)
expect(useReportStore.getState().report!.observations.length).toBe(2)
})
it('clamps activeObservationIndex within bounds', () => {
const store = useReportStore.getState()
store.initReport(draft) // seeds 1
store.addObservation()
store.addObservation() // total 3
store.setActiveObservation(2)
store.removeObservation(2)
expect(useReportStore.getState().activeObservationIndex).toBe(1)
})
})
describe('reportStore.move up/down', () => {
it('moveObservationUp swaps with previous', () => {
const s = useReportStore.getState()
s.initReport(draft)
s.addObservation()
s.addObservation()
const idA = useReportStore.getState().report!.observations[0].id
const idB = useReportStore.getState().report!.observations[1].id
s.moveObservationUp(1)
const after = useReportStore.getState().report!.observations
expect(after[0].id).toBe(idB)
expect(after[1].id).toBe(idA)
})
it('moveObservationDown swaps with next', () => {
const s = useReportStore.getState()
s.initReport(draft)
s.addObservation()
s.addObservation()
const idA = useReportStore.getState().report!.observations[0].id
const idB = useReportStore.getState().report!.observations[1].id
s.moveObservationDown(0)
const after = useReportStore.getState().report!.observations
expect(after[0].id).toBe(idB)
expect(after[1].id).toBe(idA)
})
})