fix(pdf): make buildZoneLabels resilient to per-source failures
The old buildZoneLabels wrapped the main-SVG fetch and every detail-SVG fetch in a single try/catch. As soon as one source threw — for example a 404 on a detail asset in a misconfigured deployment — the loop bailed out and the labels Map stayed populated only with whatever the loop had already set. That left every observation zone whose boneId belonged to a failed source falling back to the raw, opaque "point-mpbyo7zq" string in the rendered PDF. The refactor: - Loads every source independently in parallel. - Wraps each load+parse in its own try/catch. - Logs a console.warn on failure so the issue is observable in dev. - Skips empty bone.fr values so the lookup never returns "" (which the `??` fallback would treat as a value). Exports buildZoneLabels so it can be unit-tested directly. The new suite (4 cases) loads the real Horse + Horse_head SVG fixtures with a mocked fetch and asserts: - detail-popup boneIds resolve to their FR labels, - main-SVG boneIds resolve to their FR labels, - main labels survive a 404 on the detail asset, - unknown species returns an empty map. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
34acb0cc94
commit
93294a449b
2 files changed
+128
-12
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { buildZoneLabels } from '@/pdf/export'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
const HORSE_SVG = readFileSync(
|
||||
resolve(HERE, '../../public/species/equine/Horse.svg'),
|
||||
'utf8',
|
||||
)
|
||||
const HEAD_SVG = readFileSync(
|
||||
resolve(HERE, '../../public/species/equine/Horse_head.svg'),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
const realFetch = globalThis.fetch
|
||||
let detailFetchShouldFail = false
|
||||
|
||||
beforeAll(() => {
|
||||
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
||||
const url = String(input)
|
||||
if (url.endsWith('/Horse.svg')) {
|
||||
return new Response(HORSE_SVG, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'image/svg+xml' },
|
||||
})
|
||||
}
|
||||
if (url.endsWith('/Horse_head.svg')) {
|
||||
if (detailFetchShouldFail) {
|
||||
return new Response('not found', { status: 404 })
|
||||
}
|
||||
return new Response(HEAD_SVG, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'image/svg+xml' },
|
||||
})
|
||||
}
|
||||
return new Response('not found', { status: 404 })
|
||||
}) as typeof fetch
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
globalThis.fetch = realFetch
|
||||
})
|
||||
|
||||
const baseReport: Report = {
|
||||
version: 2,
|
||||
id: 'r1',
|
||||
createdAt: '2026-05-20T10:00:00Z',
|
||||
date: '2026-05-20',
|
||||
practitioner: { name: 'Dr. Dupont' },
|
||||
animal: { speciesId: 'equine', name: 'Mistral' },
|
||||
owner: { name: 'Jean Martin' },
|
||||
observations: [],
|
||||
conclusion: '',
|
||||
recommendations: '',
|
||||
}
|
||||
|
||||
describe('buildZoneLabels', () => {
|
||||
it('maps detail-popup boneIds to their FR labels', async () => {
|
||||
detailFetchShouldFail = false
|
||||
const labels = await buildZoneLabels(baseReport)
|
||||
// ATM hitbox in Horse_head.svg
|
||||
expect(labels.get('point-mpbyo7zq')).toBe(
|
||||
'Articulation Temporo-Mandibulaire (ATM)',
|
||||
)
|
||||
// Sphénoïde hitbox in Horse_head.svg
|
||||
expect(labels.get('point-mpbymcln')).toBe('Sphénoïde')
|
||||
})
|
||||
|
||||
it('maps main-SVG boneIds to their FR labels', async () => {
|
||||
detailFetchShouldFail = false
|
||||
const labels = await buildZoneLabels(baseReport)
|
||||
expect(labels.size).toBeGreaterThan(0)
|
||||
// The map should contain at least one Vertèbres entry from the main SVG.
|
||||
const values = Array.from(labels.values())
|
||||
expect(values.some((v) => v.includes('Vertèbres'))).toBe(true)
|
||||
})
|
||||
|
||||
it('keeps the main labels even when the detail SVG fetch fails', async () => {
|
||||
detailFetchShouldFail = true
|
||||
const labels = await buildZoneLabels(baseReport)
|
||||
// Detail entries gone — but main entries must survive the partial failure.
|
||||
expect(labels.get('point-mpbymcln')).toBeUndefined()
|
||||
const values = Array.from(labels.values())
|
||||
expect(values.some((v) => v.includes('Vertèbres'))).toBe(true)
|
||||
})
|
||||
|
||||
it('returns an empty map for an unknown species', async () => {
|
||||
const labels = await buildZoneLabels({
|
||||
...baseReport,
|
||||
animal: { speciesId: 'cat', name: 'X' },
|
||||
})
|
||||
expect(labels.size).toBe(0)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user