diff --git a/src/pdf/export.ts b/src/pdf/export.ts index 6a52d10b..f281432d 100644 --- a/src/pdf/export.ts +++ b/src/pdf/export.ts @@ -9,21 +9,40 @@ import { loadSvg } from '@/anatomy/svgLoader' import { parseSvgBones } from '@/anatomy/svgConvention' import { getSpecies } from '@/domain/species/registry' -async function buildZoneLabels(report: Report): Promise> { +/** + * Builds a `data-anno-id → French label` map by parsing every SVG the + * species manifest knows about. Each SVG is loaded independently so a + * failure on the detail asset never prevents the main labels (or vice + * versa) from making it into the returned map. + * + * Exported for unit testing — the editor calls it indirectly via + * `exportReportToPdf`. + */ +export async function buildZoneLabels(report: Report): Promise> { const labels = new Map() const species = getSpecies(report.animal.speciesId) if (!species) return labels - try { - const mainRoot = await loadSvg(species.mainSvg) - parseSvgBones(mainRoot).forEach((b, id) => labels.set(id, b.fr)) - for (const detailPath of Object.values(species.details)) { - const detailRoot = await loadSvg(detailPath) - parseSvgBones(detailRoot).forEach((b, id) => labels.set(id, b.fr)) - } - } catch { - // SVG assets unavailable (e.g., test environment without dev server); - // returns whatever labels were collected so far (possibly empty). - } + + const sources: string[] = [ + species.mainSvg, + ...Object.values(species.details), + ] + await Promise.all( + sources.map(async (src) => { + try { + const root = await loadSvg(src) + parseSvgBones(root).forEach((b, id) => { + if (b.fr) labels.set(id, b.fr) + }) + } catch (err) { + // Asset unavailable (404 / fetch failure / parse error). Log and + // continue — observers downstream fall back to the raw bone id, + // which is preferable to crashing the whole export. + console.warn(`buildZoneLabels: skipped ${src}`, err) + } + }), + ) + return labels } diff --git a/tests/unit/pdf.zoneLabels.test.ts b/tests/unit/pdf.zoneLabels.test.ts new file mode 100644 index 00000000..bb47a539 --- /dev/null +++ b/tests/unit/pdf.zoneLabels.test.ts @@ -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) + }) +})