Files
faunosteo/tests/unit/pdf.roundtrip.test.ts
T
Bastien ChanotandClaude Opus 4.7 d6027266bc feat(i18n): migrate site + PDF to locale-aware rendering
- formatDate(iso, locale) replaces formatDateFr in src/lib/dates.ts
- exportReportToPdf(report, { locale }) takes explicit locale option
- LanguageSwitcher segmented FR | EN toggle added; integrated in
  ReportHeader, HomeRoute, NewReportRoute
- All routes (Home, NewReport, Editor, NotFound) + observation
  components (FindingsTreePicker, ObservationCard, FreeTextFindingInput,
  SeveritySelector, VertebraPicker, ObservationList) + report forms
  (PractitionerForm, AnimalForm, ConclusionPanel, NextAppointmentPicker)
  route hardcoded FR via useT()
- PDF render functions (export.ts, header.ts, footer.ts,
  observationPage.ts, conclusionPage.ts) take a locale arg and read
  static labels from the dict snapshot at call time
- Catalog finding leaf labels rendered via labelFr/EnSnapshot per locale
- description_fr / scope_disclaimer stay FR-only (out of scope —
  vet-expert authoring needed for EN copy)
- LanguageSwitcher unit test (2) + lib.dates locale tests (7) cover
  the new behavior

151/151 tests pass, lint 0, typecheck clean, vite build OK
(902KB main + 15KB lazy pathologies chunk).

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

60 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { AFRelationship, PDFDocument } from 'pdf-lib'
import { exportReportToPdf } from '@/pdf/export'
import { importReportFromPdf, ImportError } from '@/pdf/import'
import type { Report } from '@/domain/schema/report'
vi.mock('@/pdf/render/svgToPng', () => ({ rasterizeSvg: async () => new Uint8Array([137, 80, 78, 71]) }))
const report: Report = {
version: 2,
id: 'r1',
createdAt: '2026-05-18T10:00:00Z',
date: '2026-05-18',
practitioner: { name: 'Dr. Dupont' },
animal: { speciesId: 'equine', name: 'Mistral' },
owner: { name: 'Jean Martin' },
observations: [],
conclusion: 'ok',
recommendations: '',
}
describe('PDF roundtrip', () => {
it('rejects unrelated PDF bytes with ImportError', async () => {
const fake = new Uint8Array([37, 80, 68, 70, 45, 49, 46, 52, 10, 37, 226, 227, 207, 211, 10])
await expect(importReportFromPdf(fake)).rejects.toThrow(ImportError)
})
it('roundtrips a minimal report (no observations)', async () => {
const bytes = await exportReportToPdf(report, { locale: 'fr' })
const imported = await importReportFromPdf(bytes)
expect(imported.animal.name).toBe('Mistral')
expect(imported.version).toBe(2)
})
it('rejects a legacy v1 PDF with a version-specific message', async () => {
const doc = await PDFDocument.create()
doc.addPage()
const legacyPayload = {
version: 1,
id: 'legacy',
createdAt: '2025-12-01T00:00:00Z',
date: '2025-12-01',
practitioner: { name: 'Dr. Dupont' },
animal: { speciesId: 'equine', name: 'Mistral' },
owner: { name: 'Jean Martin' },
observations: [],
conclusion: '',
recommendations: '',
}
const bytes = new TextEncoder().encode(JSON.stringify(legacyPayload))
await doc.attach(bytes, 'osteo-report.json', {
mimeType: 'application/json',
description: 'Osteo Report state v1',
afRelationship: AFRelationship.Data,
})
const pdfBytes = await doc.save()
await expect(importReportFromPdf(pdfBytes)).rejects.toThrow(/v1/)
})
})