Files
faunosteo/apps/app/tests/unit/pdf.import.test.ts
T
bchanot da8a0370cb fix(app): set lone-surrogate texts aside or refuse them with a message (L1)
A draft the core refuses as lone_surrogate is unreadable content: it
goes to osteo:quarantine:draft verbatim with the existing notice. An
import is refused as ImportError('lone_surrogate') with the owner's
wording (home.import.error_lone_surrogate, FR/EN), the file untouched.
The publish gate reads the kind as invalid_shape. Tests on the real
core: quarantine keeps the exact text, an editor action leaves the
report as it was, normal text with pairs and literal backslashes reads
unchanged.
2026-10-14 21:49:39 +02:00

75 lines
2.6 KiB
TypeScript

/**
* Replaces pdf.attachment.test.ts and pdf.attachment.inflate.test.ts on the
* engine read API: the typed ImportError kinds the home route maps. The
* attachment roundtrip moved to pdf.roundtrip.test.ts ("attaches the
* envelope and roundtrips") and the engine's tests/read.test.ts (T4); the
* inflate bomb cap lives in the engine's tests/security.test.ts.
*/
import { describe, it, expect, beforeAll } from 'vitest'
import { PDFDocument } from 'pdf-lib'
import {
importAttachment,
importReportFromPdf,
ImportError,
MAX_PDF_BYTES,
} from '@/pdf/import'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
let sheet: SheetIndex
beforeAll(async () => {
sheet = await loadTestSheet()
})
/** Saved like the export (no object streams, BLK-009) so the keyword stamp is in clear. */
async function pdfWithoutAttachment(keywords?: string[]): Promise<Uint8Array> {
const doc = await PDFDocument.create()
doc.addPage()
if (keywords) doc.setKeywords(keywords)
return doc.save({ useObjectStreams: false })
}
async function rejectsWith(bytes: Uint8Array, kind: string): Promise<void> {
const promise = importReportFromPdf(bytes, sheet)
await expect(promise).rejects.toBeInstanceOf(ImportError)
await expect(promise).rejects.toMatchObject({ kind })
}
describe('importReportFromPdf error kinds', () => {
it('no_attachment for a PDF without the attachment', async () => {
await rejectsWith(await pdfWithoutAttachment(), 'no_attachment')
})
it('attachment_stripped for a stamped archive whose attachment was removed', async () => {
await rejectsWith(
await pdfWithoutAttachment(['faunosteo-archive/1']),
'attachment_stripped',
)
})
it('invalid_pdf for an oversized file, before parsing it', async () => {
await rejectsWith(new Uint8Array(MAX_PDF_BYTES + 1), 'invalid_pdf')
})
})
describe('importAttachment on the core (L1)', () => {
it('refuses an attachment with a lone surrogate as lone_surrogate, never rewritten', () => {
for (const text of ['a\ud800b', '\udfff']) {
const attachment = minimalReport(sheet, { conclusion: text })
expect(() => importAttachment(attachment, sheet)).toThrow(
expect.objectContaining({ kind: 'lone_surrogate' }),
)
expect(attachment.conclusion).toBe(text)
}
})
it('imports normal text with pairs and literal backslashes unchanged', () => {
const text = 'é 😀 \\ud800 \ufffd'
const imported = importAttachment(
minimalReport(sheet, { conclusion: text }),
sheet,
)
expect(imported.rebase.report.conclusion).toBe(text)
})
})