refactor(pdf): R4.8 the TypeScript PDF engine leaves: packages/pdf, pdf-lib, the TS CLI and import path, the oracle generators deleted; test read-back on an own reader of the writer's files (pdfFile.ts), view goldens from the session's SVGs, import helper on the module's reader, N-1 scanner exported by core-wasm/n-1; app no longer depends on @faunosteo/domain; r4.census suite

This commit is contained in:
bchanot committed 2026-10-19 12:17:01 +02:00
1 parent 77e11a53e4
commit 0dc1ba27c7
96 files changed
+743 -15769

No files matched your search

+47 -22
View File
@@ -4,22 +4,24 @@
* with the bundled sheet snapshotted, the bundled branding (mocked to every
* slot empty for the `nologos` mode, the real module for `logos`) and the
* browser rasterizer (mocked to fixed PNGs, as the Phase 0 stub was). Text,
* images and links goldens are read back from the file's content streams and
* asserted as frozen; `MAKE_GOLDEN=1` rewrites them, never without a user
* decision. At the fixed export time the file's bytes are the R4 digests the
* cargo suite froze (`goldens/r4/pdf.*.sha256`): the app path writes what the
* native core writes. Never regenerate `report-v3.bare.pdf`: it is the Step 5
* bare export, an import fixture only.
* images and links goldens are read back from the file (`tests/helpers/pdfFile.ts`,
* R4.8: no pdf-lib) and asserted as frozen; `MAKE_GOLDEN=1` rewrites them,
* never without a user decision. The highlighted-view goldens are read from
* the SVGs the core's session asks the rasteriser for. At the fixed export
* time the file's bytes are the R4 digests the cargo suite froze
* (`goldens/r4/pdf.*.sha256`): the app path writes what the native core
* writes. Never regenerate `report-v3.bare.pdf`: it is the Step 5 bare
* export, an import fixture only.
*/
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
import { PDFDocument } from 'pdf-lib'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import type { Report } from '@faunosteo/core-wasm/types'
import { exportReportToPdf } from '@/pdf/export'
import { importReportFromPdf } from '@/pdf/import'
import type { SheetIndex } from '@/services/sheetIndex'
import type { Locale } from '@/i18n/types'
import { importReportFromPdf } from '../helpers/pdfImport'
import { PdfFile, numberOf } from '../helpers/pdfFile'
import { loadTestSheet, sheetSnapshot } from '../helpers/sheet'
import {
buildGoldenReports,
@@ -28,10 +30,13 @@ import {
} from '../helpers/goldenReports'
import {
checkGolden,
FIXED_EXPORTED_AT,
FIXTURES_DIR,
ONE_PX_PNG,
R4_GOLDEN_DIR,
readGolden,
readLinks,
sha256,
snapshotViews,
TWO_BY_ONE_PNG,
type ImageEntry,
@@ -41,11 +46,18 @@ import {
type ViewSnapshot,
} from '../helpers/goldens'
import { bytesOf, readDrawing } from '../helpers/pdfRead'
import { FIXED_EXPORTED_AT, R4_GOLDEN_DIR, sha256 } from '../helpers/r4-draw'
const raster = vi.hoisted(() => ({ png: new Uint8Array() }))
// The fixed PNG for every raster; the SVGs the session asks for are kept, in request
// order (the view goldens are read from them).
const raster = vi.hoisted(() => ({
png: new Uint8Array(),
svgs: [] as string[],
}))
vi.mock('@/pdf/browserRasterizer', () => ({
browserRasterizer: vi.fn(async () => raster.png),
browserRasterizer: vi.fn(async (svg: string) => {
raster.svgs.push(svg)
return raster.png
}),
}))
const logoMode = vi.hoisted(() => ({ current: 'nologos' as LogoMode }))
vi.mock('@/pdf/branding', async () => {
@@ -86,6 +98,7 @@ async function exportWith(
locale: Locale,
): Promise<Exported> {
raster.png = mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG
raster.svgs = []
logoMode.current = mode
const { pdf, warnings } = await exportReportToPdf(report, sheet, { locale })
expect(warnings).toEqual([])
@@ -115,13 +128,25 @@ function r4Digest(name: string, locale: Locale, mode: LogoMode): string {
const pagesOf = (entries: readonly { page: number }[]) =>
entries.map((e) => e.page)
/** The size of every image XObject the file's first page draws. */
function imageSizesOf(bytes: Uint8Array): [number, number][] {
const file = PdfFile.parse(bytes)
return [...file.xobjects(file.pages()[0]!).values()].map((image) => [
numberOf(image.dict, 'Width')!,
numberOf(image.dict, 'Height')!,
])
}
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])
// logos: the view and partner rasters are the 2×1 PNG, the bundled PNG logos their own size.
const two = await exportWith(reports.demo, 'logos', 'fr')
expect(imageSizesOf(two.bytes)).toContainEqual([2, 1])
// nologos: every image of the page is a view raster, the 1×1 PNG.
const one = await exportWith(reports.demo, 'nologos', 'fr')
const sizes = imageSizesOf(one.bytes)
expect(sizes.length).toBeGreaterThan(0)
expect(new Set(sizes.map(String))).toEqual(new Set(['1,1']))
})
it('the demo report covers the sheet features the goldens must freeze', () => {
@@ -170,9 +195,7 @@ describe.each(REPORTS)('goldens of %s', (name) => {
inPageOrder(`images.${id}.json`),
)
checkGolden(`links.${id}.json`, out.links)
expect(sha256(out.bytes), id).toBe(
r4Digest(name, locale, mode),
)
expect(sha256(out.bytes), id).toBe(r4Digest(name, locale, mode))
}
expect(pagesOf(en.images)).toEqual(pagesOf(fr.images))
expect(pagesOf(en.links)).toEqual(pagesOf(fr.links))
@@ -183,9 +206,11 @@ describe.each(REPORTS)('goldens of %s', (name) => {
// 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', () => {
it('highlighted views: same facts but titles and data attributes', async () => {
const snapshot = sheetSnapshot(sheet, reports[name])
const actual = snapshotViews(snapshot, reports[name])
// Every slot empty: the session asks for the view rasters only, in request order.
await exportWith(reports[name], 'nologos', 'fr')
const actual = snapshotViews(snapshot, reports[name], raster.svgs)
const expected = readGolden<ViewSnapshot[]>(`views.${name}.json`)
const comparable = (v: ViewSnapshot) => ({
observationId: v.observationId,
@@ -207,7 +232,7 @@ describe.each(REPORTS)('goldens of %s', (name) => {
describe('report-v3.bare.pdf', () => {
it('is the demo report exported bare by Step 5 and imports with no archive', async () => {
const bytes = new Uint8Array(readFileSync(BARE_PDF))
const { read: outcome } = await importReportFromPdf(bytes, sheet)
const { read: outcome } = await importReportFromPdf(bytes)
expect(outcome.origin).toBe('bare_v3')
expect(outcome.migrated).toBe(false)
expect(outcome.archive).toBeNull()