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:
Bastien ChanotandClaude committed 2026-05-20 04:55:36 +02:00
1 parent 34acb0cc94
commit 93294a449b
2 files changed
+128 -12

No files matched your search

+31 -12
View File
@@ -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<Map<string, string>> {
/**
* 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<Map<string, string>> {
const labels = new Map<string, string>()
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
}