diff --git a/src/pdf/export.ts b/src/pdf/export.ts index 3253468d..6a52d10b 100644 --- a/src/pdf/export.ts +++ b/src/pdf/export.ts @@ -13,11 +13,16 @@ async function buildZoneLabels(report: Report): Promise> { const labels = new Map() const species = getSpecies(report.animal.speciesId) if (!species) return labels - 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)) + 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). } return labels } diff --git a/src/pdf/import.ts b/src/pdf/import.ts index 94b91802..ccd78389 100644 --- a/src/pdf/import.ts +++ b/src/pdf/import.ts @@ -1,25 +1,24 @@ +import { PDFDocument } from 'pdf-lib' +import { extractReportJson } from './attachment' import type { Report } from '@/domain/schema/report' -/** - * Raised when the PDF does not contain a valid osteo-report JSON attachment. - * The message is the French user-facing error string. - */ export class ImportError extends Error { constructor() { - super( - "Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).", - ) + super("Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).") this.name = 'ImportError' } } -/** - * Loads a PDF from bytes, extracts the attached report JSON, validates it - * with Zod, and returns a hydrated Report. - * Throws ImportError on any failure. - */ -export async function importReportFromPdf( - _pdfBytes: Uint8Array, -): Promise { - throw new Error('not implemented') +export async function importReportFromPdf(pdfBytes: Uint8Array): Promise { + let pdfDoc: PDFDocument + try { + pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true }) + } catch { + throw new ImportError() + } + try { + return await extractReportJson(pdfDoc) + } catch { + throw new ImportError() + } } diff --git a/tests/unit/pdf.roundtrip.test.ts b/tests/unit/pdf.roundtrip.test.ts new file mode 100644 index 00000000..2f1cb658 --- /dev/null +++ b/tests/unit/pdf.roundtrip.test.ts @@ -0,0 +1,32 @@ +import { describe, it, expect, vi } from 'vitest' +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: 1, + 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) + const imported = await importReportFromPdf(bytes) + expect(imported.animal.name).toBe('Mistral') + }) +})