Review surfaced 3 critical paths where hardcoded French leaked into
the EN UI:
- src/pdf/import.ts ImportError default message
- src/pdf/attachment.ts IncompatibleReportVersionError message
- src/app/routes/HomeRoute.tsx propagated err.message verbatim
Refactor:
- ImportError carries a typed `kind` discriminant
('invalid_pdf' | 'incompatible_version' | 'no_attachment') plus
optional `foundVersion`. Removes hardcoded localized strings.
- IncompatibleReportVersionError keeps `foundVersion` only; message
collapses to an internal sentinel.
- HomeRoute resolves the user-facing message via the active i18n
dict using `err.kind`, with `{version}` template substitution for
the incompatible-version case.
- 3 new dict keys (fr+en): home.import.error_invalid_pdf,
home.import.error_no_attachment, home.import.error_incompatible_version
- pdf.roundtrip v1 case asserts ImportError instance + kind +
foundVersion instead of grepping the message text.
154/154 tests pass, lint clean, typecheck clean, build OK.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
97 lines
3.6 KiB
TypeScript
97 lines
3.6 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('roundtrips a report exported with locale=fr', 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('roundtrips a report exported with locale=en', async () => {
|
|
const bytes = await exportReportToPdf(report, { locale: 'en' })
|
|
const imported = await importReportFromPdf(bytes)
|
|
expect(imported.animal.name).toBe('Mistral')
|
|
expect(imported.version).toBe(2)
|
|
})
|
|
|
|
it('FR and EN exports both produce non-empty byte streams', async () => {
|
|
// pdf-lib subsets fonts (LRN-016), so plain-text grep over the bytes
|
|
// is unreliable. The imported JSON is the strongest invariant — the
|
|
// locale lives outside the Report, so both imports return the same
|
|
// payload while the static label snapshots differ inside the PDF.
|
|
const fr = await exportReportToPdf(report, { locale: 'fr' })
|
|
const en = await exportReportToPdf(report, { locale: 'en' })
|
|
expect(fr.byteLength).toBeGreaterThan(0)
|
|
expect(en.byteLength).toBeGreaterThan(0)
|
|
const frImported = await importReportFromPdf(fr)
|
|
const enImported = await importReportFromPdf(en)
|
|
expect(frImported.animal.name).toBe(enImported.animal.name)
|
|
expect(frImported.version).toBe(enImported.version)
|
|
})
|
|
|
|
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()
|
|
try {
|
|
await importReportFromPdf(pdfBytes)
|
|
throw new Error('expected rejection')
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(ImportError)
|
|
const e = err as ImportError
|
|
expect(e.kind).toBe('incompatible_version')
|
|
expect(e.foundVersion).toBe(1)
|
|
}
|
|
})
|
|
})
|