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

+51 -103
View File
@@ -1,36 +1,26 @@
import { createHash } from 'node:crypto'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { expect, vi } from 'vitest'
import {
PDFArray,
PDFDict,
PDFDocument,
PDFHexString,
PDFName,
PDFNumber,
PDFPage,
PDFString,
} from 'pdf-lib'
import type {
Branding,
Observation,
Report,
SheetSnapshot,
} from '@faunosteo/core-wasm/types'
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
/** The engine's own (Zod-inferred) types of the same JSON contracts. */
type EngineView = Parameters<typeof composeHighlightedViewSvg>
import { PdfFile, nameOf, numbersOf, subDict, textOf } from './pdfFile'
import { nologosBranding } from './sheet'
/**
* Step 6 Phase 0 freeze: what the PDF pipeline drew before the engine,
* recorded as JSON goldens (docs/development/STEP6_PDF_ARCHIVE_ANALYSIS.md
* §17) that the app export path must still reproduce. The recorder spies on
* pdf-lib, the link reader parses the saved bytes, the view snapshot parses
* the engine's highlighted SVG with the browser DOM. Numbers are rounded to
* 2 decimals; nothing ordered by drawing order is sorted.
* §17) that the app export path must still reproduce. Since R4.8 the text
* and image facts are read back from the file's content streams
* (`pdfRead.ts`), the links from its annotations (`pdfFile.ts`), and the
* view snapshot parses the SVG the core's session asked the rasteriser for,
* with the browser DOM. Numbers are rounded to 2 decimals; nothing ordered
* by drawing order is sorted.
*/
/** 1×1 PNG (nologos: the legacy stub) and 2×1 PNG (logos: deterministic partner logo width). */
@@ -53,6 +43,13 @@ export const GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/step6')
/** `MAKE_GOLDEN=1` writes the goldens instead of asserting them. */
export const MAKE_GOLDEN = process.env.MAKE_GOLDEN === '1'
/** The R4 digests of the writer's own bytes (`goldens/r4/pdf.*.sha256`), frozen by the cargo suite. */
export const R4_GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/r4')
/** The export time of every R4 run (the engine suites' `FIXED_EXPORTED_AT` at the freeze). */
export const FIXED_EXPORTED_AT = new Date('2026-10-11T12:00:00.000Z')
export const sha256 = (data: Uint8Array | string): string =>
createHash('sha256').update(data).digest('hex')
export type LogoMode = 'nologos' | 'logos'
@@ -106,82 +103,19 @@ export function round2(value: unknown): number | null {
return typeof value === 'number' ? Math.round(value * 100) / 100 : null
}
/** Pages are appended in order and never moved: the index at draw time is final. */
function pageIndex(page: PDFPage): number {
return page.doc.getPages().indexOf(page)
}
export interface DrawRecording {
texts: TextEntry[]
images: ImageEntry[]
stop(): void
}
/** Records every drawText / drawImage while letting pdf-lib draw for real. */
export function recordDrawing(): DrawRecording {
const texts: TextEntry[] = []
const images: ImageEntry[] = []
const drawText = PDFPage.prototype.drawText
const drawImage = PDFPage.prototype.drawImage
const textSpy = vi
.spyOn(PDFPage.prototype, 'drawText')
.mockImplementation(function (this: PDFPage, text, options) {
texts.push({
page: pageIndex(this),
text,
x: round2(options?.x),
y: round2(options?.y),
size: round2(options?.size),
font: options?.font?.name ?? null,
})
return drawText.call(this, text, options)
})
const imageSpy = vi
.spyOn(PDFPage.prototype, 'drawImage')
.mockImplementation(function (this: PDFPage, image, options) {
images.push({
page: pageIndex(this),
x: round2(options?.x),
y: round2(options?.y),
width: round2(options?.width),
height: round2(options?.height),
})
return drawImage.call(this, image, options)
})
return {
texts,
images,
stop: () => {
textSpy.mockRestore()
imageSpy.mockRestore()
},
}
}
function linkOf(dict: PDFDict, page: number): LinkEntry | null {
if (dict.get(PDFName.of('Subtype')) !== PDFName.of('Link')) return null
const rect = dict
.lookup(PDFName.of('Rect'), PDFArray)
.asArray()
.map((n) => round2((n as PDFNumber).asNumber()) ?? 0)
const uri = dict.lookup(PDFName.of('A'), PDFDict).lookup(PDFName.of('URI'))
const text =
uri instanceof PDFString || uri instanceof PDFHexString
? uri.decodeText()
: String(uri)
return { page, rect, uri: text }
}
/** Every `/Annot /Link` of the saved PDF, page by page, in annotation order. */
export async function readLinks(bytes: Uint8Array): Promise<LinkEntry[]> {
const doc = await PDFDocument.load(bytes)
const file = PdfFile.parse(bytes)
const out: LinkEntry[] = []
doc.getPages().forEach((page, index) => {
const annots = page.node.Annots()
if (!annots) return
for (let i = 0; i < annots.size(); i += 1) {
const link = linkOf(annots.lookup(i, PDFDict), index)
if (link) out.push(link)
file.pages().forEach((page, index) => {
for (const annot of file.annots(page)) {
if (nameOf(annot.dict, 'Subtype') !== 'Link') continue
const action = subDict(annot.dict, 'A') ?? ''
out.push({
page: index,
rect: numbersOf(annot.dict, 'Rect').map((n) => round2(n) ?? 0),
uri: textOf(action, 'URI') ?? '',
})
}
})
return out
@@ -239,22 +173,18 @@ function countDataAttributes(root: Element): number {
)
}
/** The facts of one composed view, from the SVG the core's session asked the rasteriser for. */
export function snapshotView(
sheet: SheetSnapshot,
observation: Observation,
observationId: string,
viewId: string,
svg: string,
): ViewSnapshot {
const svg = composeHighlightedViewSvg(
sheet as EngineView[0],
viewId,
observation as EngineView[2],
)
const root = new DOMParser().parseFromString(
svg,
'image/svg+xml',
).documentElement
return {
observationId: observation.id,
observationId,
viewId,
viewBox: root.getAttribute('viewBox'),
halo: root.querySelector('filter#osteo-halo') !== null,
@@ -279,14 +209,32 @@ export function viewIdsOf(
.sort((a, b) => Number(b === main) - Number(a === main))
}
/** The (observation, view) pairs an export rasterises, in the session's request order. */
export function viewPairsOf(
sheet: SheetSnapshot,
report: Report,
): { observationId: string; viewId: string }[] {
return report.observations.flatMap((observation) =>
viewIdsOf(sheet, observation).map((viewId) => ({
observationId: observation.id,
viewId,
})),
)
}
/**
* The view snapshots of an export: `svgs` are the view SVGs the session asked the
* rasteriser for (every logo raster excluded), in request order, one per pair.
*/
export function snapshotViews(
sheet: SheetSnapshot,
report: Report,
svgs: readonly string[],
): ViewSnapshot[] {
return report.observations.flatMap((observation) =>
viewIdsOf(sheet, observation).map((viewId) =>
snapshotView(sheet, observation, viewId),
),
const pairs = viewPairsOf(sheet, report)
expect(svgs).toHaveLength(pairs.length)
return pairs.map((pair, i) =>
snapshotView(pair.observationId, pair.viewId, svgs[i]!),
)
}