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

+137
View File
@@ -0,0 +1,137 @@
/**
* T3 (STEP6 §7, §17): the engine reproduces every Phase 0 golden, per
* report × locale × logo mode. A mismatch is a visible change: the golden
* is never edited here.
*/
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { PDFDocument } from 'pdf-lib'
import { defaultPdfLabels, exportReportArchive } from '../src'
import type { PdfLocale } from '../src'
import {
brandingOf,
checkGolden,
FIXED_EXPORTED_AT,
ONE_PX_PNG,
rasterizerOf,
readGolden,
readLinks,
recordDrawing,
snapshotViews,
TWO_BY_ONE_PNG,
type ImageEntry,
type LinkEntry,
type LogoMode,
type TextEntry,
type ViewSnapshot,
} from './helpers/goldens'
import {
loadGoldenReports,
REPORT_NAMES,
type GoldenReports,
type ReportName,
} from './helpers/reports'
const MODES: readonly LogoMode[] = ['nologos', 'logos']
let golden: GoldenReports
beforeAll(() => {
golden = loadGoldenReports()
})
afterAll(() => vi.restoreAllMocks())
interface Exported {
bytes: Uint8Array
texts: TextEntry[]
images: ImageEntry[]
links: LinkEntry[]
}
async function exportWith(
name: ReportName,
mode: LogoMode,
locale: PdfLocale,
): Promise<Exported> {
const recording = recordDrawing()
try {
const { pdf, warnings } = await exportReportArchive({
report: golden.reports[name],
sheet: golden.snapshots[name],
branding: brandingOf(mode),
labels: defaultPdfLabels,
locale,
rasterizer: rasterizerOf(mode),
exportedAt: FIXED_EXPORTED_AT,
})
expect(warnings).toEqual([])
const { texts, images } = recording
return { bytes: pdf, texts, images, links: await readLinks(pdf) }
} finally {
recording.stop()
}
}
const pagesOf = (entries: readonly { page: number }[]) =>
entries.map((e) => e.page)
describe('golden inputs', () => {
it('the 2×1 PNG decodes as 2×1 and the 1×1 as 1×1', async () => {
const doc = await PDFDocument.create()
const two = await doc.embedPng(TWO_BY_ONE_PNG)
expect([two.width, two.height]).toEqual([2, 1])
const one = await doc.embedPng(ONE_PX_PNG)
expect([one.width, one.height]).toEqual([1, 1])
})
it('the v2unresolved report carries the unresolved findings of the freeze', () => {
expect(golden.unresolvedOfV2Unresolved).toBeGreaterThan(0)
const findings = golden.reports.v2unresolved.observations.flatMap(
(o) => o.findings,
)
expect(findings.filter((f) => f.unresolved)).toHaveLength(
golden.unresolvedOfV2Unresolved,
)
})
})
describe.each(REPORT_NAMES)('goldens of %s', (name) => {
describe.each(MODES)('%s', (mode) => {
it('text, images and links per locale; same pagination across locales', async () => {
const fr = await exportWith(name, mode, 'fr')
const en = await exportWith(name, mode, 'en')
for (const [locale, out] of [
['fr', fr],
['en', en],
] as const) {
checkGolden(`text.${name}.${locale}.${mode}.json`, out.texts)
checkGolden(`images.${name}.${locale}.${mode}.json`, out.images)
checkGolden(`links.${name}.${locale}.${mode}.json`, out.links)
}
expect(pagesOf(en.images)).toEqual(pagesOf(fr.images))
expect(pagesOf(en.links)).toEqual(pagesOf(fr.links))
}, 30_000)
})
// Hidden markers are absent in the engine (the browser hid them with
// display="none") and the layer <g> carries no data-* any more: `titles`
// shrinks to the highlighted markers plus the asset's own, and
// `dataAttributes` drops from 2 to 0. Everything else must match.
it('highlighted views: same facts but titles and data attributes', () => {
const actual = snapshotViews(golden.snapshots[name], golden.reports[name])
const expected = readGolden<ViewSnapshot[]>(`views.${name}.json`)
const comparable = (v: ViewSnapshot) => ({
observationId: v.observationId,
viewId: v.viewId,
viewBox: v.viewBox,
halo: v.halo,
highlighted: v.highlighted,
nonHighlightedVisible: v.nonHighlightedVisible,
})
expect(actual.map(comparable)).toEqual(expected.map(comparable))
for (const view of actual) {
expect(view.dataAttributes).toBe(0)
expect(view.titles).toBeGreaterThanOrEqual(view.highlighted.length)
}
for (const view of expected) expect(view.dataAttributes).toBe(2)
})
})