feat(pdf): @faunosteo/pdf engine — explicit inputs, envelope v1 attachment, layered reader, xmldom composition, allowlist SVG sanitiser, WinAnsi guard, rasterizer adapter; goldens reproduced

This commit is contained in:
bchanot committed 2026-10-11 13:09:17 +02:00
1 parent e7419cb4cb
commit c17956031d
46 files changed
+4396 -1

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
/**
* T9 (STEP6 §15): the WinAnsi canary. One text ingress, so a glyph the
* standard fonts cannot encode never reaches pdf-lib; `formatDate` never
* throws on user data.
*/
import { beforeAll, describe, expect, it } from 'vitest'
import { PDFDocument, StandardFonts } from 'pdf-lib'
import { buildSheetSnapshot } from '@faunosteo/domain'
import { defaultPdfLabels, exportReportArchive } from '../src'
import { formatDate } from '../src/render/dates'
import { wrapLines } from '../src/render/text'
import { safeText } from '../src/text'
import {
FIXED_EXPORTED_AT,
nologosBranding,
rasterizerOf,
recordDrawing,
} from './helpers/goldens'
import { loadGoldenReports, type GoldenReports } from './helpers/reports'
let golden: GoldenReports
beforeAll(() => {
golden = loadGoldenReports()
})
describe('safeText', () => {
it('keeps the WinAnsi glyphs the pages use', () => {
const kept = 'Fixation (C2–C7) — bilan • zone · Sévérité à l’écu €'
expect(safeText(kept)).toBe(kept)
})
it('replaces unencodable glyphs by ? and strips control characters', () => {
expect(safeText('⚠ alerte')).toBe('? alerte')
expect(safeText('a → b')).toBe('a ? b')
expect(safeText('ok 😀')).toBe('ok ?')
expect(safeText('a\tb')).toBe('ab')
expect(safeText('a
b')).toBe('ab')
expect(safeText('a\x1bb')).toBe('ab')
})
it('wrapLines splits on any whitespace first, then makes each word safe', async () => {
const doc = await PDFDocument.create()
const font = await doc.embedFont(StandardFonts.Helvetica)
expect(wrapLines('un\tdeux\ntrois ⚠', font, 10, 1000)).toEqual([
'un deux trois ?',
])
})
})
describe('export with hostile text', () => {
it('a report with ⚠ in a comment exports; the glyph prints as ?', async () => {
const demo = golden.reports.demo
const report = {
...demo,
observations: [
{ ...demo.observations[0]!, comment: 'Attention ⚠ tab\tici → fin' },
],
conclusion: 'Conclusion ⚠',
}
const recording = recordDrawing()
try {
const { warnings } = await exportReportArchive({
report,
sheet: buildSheetSnapshot(golden.draft, report),
branding: nologosBranding(),
labels: defaultPdfLabels,
locale: 'fr',
rasterizer: rasterizerOf('nologos'),
exportedAt: FIXED_EXPORTED_AT,
})
expect(warnings).toEqual([])
const drawn = recording.texts.map((t) => t.text)
expect(drawn).toContain('Attention ? tab ici ? fin')
expect(drawn).toContain('Conclusion ?')
expect(drawn.every((t) => safeText(t) === t)).toBe(true)
} finally {
recording.stop()
}
})
})
describe('formatDate', () => {
it('formats an ISO date per locale and returns anything else unchanged', () => {
expect(formatDate('2026-11-20', 'fr')).toBe('20 novembre 2026')
expect(formatDate('2026-11-20', 'en')).toBe('November 20, 2026')
expect(formatDate('n/a', 'fr')).toBe('n/a')
expect(formatDate('2026-13-45', 'en')).toBe('2026-13-45')
expect(formatDate('2026-02-30', 'en')).toBe('2026-02-30')
})
})