51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { describe, it, expect, beforeAll } from 'vitest'
|
|
import { PDFDocument } from 'pdf-lib'
|
|
import { attachReportJson, extractReportJson } from '@/pdf/attachment'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
|
|
|
let sheet: SheetIndex
|
|
beforeAll(async () => {
|
|
sheet = await loadTestSheet()
|
|
})
|
|
|
|
describe('PDF attachment roundtrip', () => {
|
|
it('attaches and extracts the v3 report JSON', async () => {
|
|
const doc = await PDFDocument.create()
|
|
doc.addPage()
|
|
await attachReportJson(
|
|
doc,
|
|
minimalReport(sheet, { conclusion: 'tout va bien' }),
|
|
)
|
|
const bytes = await doc.save()
|
|
|
|
const reopened = await PDFDocument.load(bytes)
|
|
const out = await extractReportJson(reopened, sheet.sheet.migrationMap)
|
|
expect(out.report.conclusion).toBe('tout va bien')
|
|
expect(out.report.animal.name).toBe('Mistral')
|
|
expect(out.report.version).toBe(3)
|
|
expect(out.migrated).toBe(false)
|
|
})
|
|
|
|
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, sheet.sheet.migrationMap),
|
|
).rejects.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('size caps', () => {
|
|
it('refuses an oversized PDF before parsing it', async () => {
|
|
const { importReportFromPdf, ImportError, MAX_PDF_BYTES } =
|
|
await import('@/pdf/import')
|
|
const huge = new Uint8Array(MAX_PDF_BYTES + 1)
|
|
await expect(
|
|
importReportFromPdf(huge, sheet.sheet.migrationMap),
|
|
).rejects.toBeInstanceOf(ImportError)
|
|
})
|
|
})
|