feat(pdf): attach + extract report JSON via pdf-lib (with fallback traversal)

This commit is contained in:
Bastien Chanot committed 2026-05-18 21:53:26 +02:00
1 parent 1c0b507458
commit fb9a8fc3bd
2 files changed
+120 -19

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
import { describe, it, expect } from 'vitest'
import { PDFDocument } from 'pdf-lib'
import { attachReportJson, extractReportJson } from '@/pdf/attachment'
import type { Report } from '@/domain/schema/report'
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: 'tout va bien',
recommendations: 'continuer',
}
describe('PDF attachment roundtrip', () => {
it('attaches and extracts the report JSON', async () => {
const doc = await PDFDocument.create()
doc.addPage()
await attachReportJson(doc, report)
const bytes = await doc.save()
const reopened = await PDFDocument.load(bytes)
const out = await extractReportJson(reopened)
expect(out.conclusion).toBe('tout va bien')
expect(out.animal.name).toBe('Mistral')
expect(out.version).toBe(1)
})
it('throws on PDFs without the attachment', async () => {
const doc = await PDFDocument.create()
doc.addPage()
const bytes = await doc.save()
const reopened = await PDFDocument.load(bytes)
await expect(extractReportJson(reopened)).rejects.toThrow()
})
})