Files
faunosteo/tests/unit/pdf.zoneLabels.test.ts
T
Bastien ChanotandClaude Opus 4.7 9cf8a66dad feat(i18n): navigator-detect default locale + locale-aware bone labels
- localeStore.readInitial now consults navigator.language(s) when
  localStorage is empty (first fr/en hit, FR fallback). Persisted user
  choice still wins.
- New helper src/anatomy/labels.ts (boneLabel(b, locale, fallback))
  with cross-language fallback so missing labels never render blank.
- AnatomyCanvas rewrites native SVG <title> tooltips per locale on
  mount and on locale change (upstream SVG ships titles as "FR / EN"
  concat; we replace with the active-locale label only). Detail-popup
  trigger titles ("🔍 Zoom: …") left intact.
- DetailModal + EditorRoute detail title prefix + vertebra picker
  title now route through useT()/boneLabel.
- ObservationCard.describeZone takes locale, picks bone.fr / bone.en.
- PDF: buildZoneLabels returns Map<id, { fr, en }> instead of FR-only;
  observationPage picks per locale with cross-language fallback.
- Tests: FindingsTreePicker + VertebraPicker reset locale to 'fr' in
  beforeEach (navigator-detect may land on EN in happy-dom).
- pdf.zoneLabels test updated to assert the new pair shape.

154/154 tests pass, lint clean, typecheck clean, build OK.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-20 07:56:22 +02:00

97 lines
3.0 KiB
TypeScript

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 + EN labels', async () => {
detailFetchShouldFail = false
const labels = await buildZoneLabels(baseReport)
const atm = labels.get('point-mpbyo7zq')
expect(atm?.fr).toBe('Articulation Temporo-Mandibulaire (ATM)')
expect(atm?.en).toBe('Temporomandibular Joint (TMJ)')
const sphenoid = labels.get('point-mpbymcln')
expect(sphenoid?.fr).toBe('Sphénoïde')
expect(sphenoid?.en).toBe('Sphenoid')
})
it('maps main-SVG boneIds to their FR + EN labels', async () => {
detailFetchShouldFail = false
const labels = await buildZoneLabels(baseReport)
expect(labels.size).toBeGreaterThan(0)
const pairs = Array.from(labels.values())
expect(pairs.some((p) => p.fr.includes('Vertèbres'))).toBe(true)
expect(pairs.some((p) => p.en.includes('Vertebrae'))).toBe(true)
})
it('keeps the main labels even when the detail SVG fetch fails', async () => {
detailFetchShouldFail = true
const labels = await buildZoneLabels(baseReport)
expect(labels.get('point-mpbymcln')).toBeUndefined()
const pairs = Array.from(labels.values())
expect(pairs.some((p) => p.fr.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)
})
})