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

-1
View File
@@ -21,7 +21,6 @@
"build": "tsc -p tsconfig.json --noEmit"
},
"devDependencies": {
"@faunosteo/pdf": "workspace:*",
"@types/node": "^20.19.41",
"eslint": "^9.17.0",
"globals": "^15.14.0",
+3
View File
@@ -50,6 +50,9 @@ export interface GlueSheetHandle {
import_pdf_bytes(bytes: GlueFileBytes): string
/** R4.3: a rescue copy streamed into the module's memory (consumed): `draft.restore`'s kinds. */
restore_copy_bytes(bytes: GlueFileBytes): string
/** wasm-bindgen's: the page's session keeps its handle for the instance's life, a worker
* dies with its own; the tests' throwaway handles free theirs. */
free(): void
}
/** A file gathered chunk by chunk in the module's memory (crates/wasm `FileBytes`). */
+13
View File
@@ -19,3 +19,16 @@ export {
type ReportArchiveEnvelope,
type ReportMigrationMap,
} from './domain'
/**
* The N-1 PDF reader (R4 plan B.2.2, O11): the byte-scan attachment reader of the R3 build
* and release 0.2.0, copied verbatim under `./scanner` (never edited). Since R4.8 it is the
* only TypeScript reader left: the application suites that compare an attachment's text with
* a frozen envelope read it here, as a rollback to that build would.
*/
export {
readAttachedDocument,
readAttachmentBytes,
ArchiveReadError,
type ArchiveReadErrorKind,
} from './scanner'
@@ -0,0 +1,4 @@
/** The frozen scanner's surface (see README.md): the copies are never edited, this index only re-exports them. */
export { readAttachedDocument } from './attachment/read'
export { readAttachmentBytes } from './attachment/scan'
export { ArchiveReadError, type ArchiveReadErrorKind } from './errors'
@@ -3,8 +3,10 @@
* (`CoreExportSession`): driven request by request on the O2 runs it writes the native
* bytes (`goldens/r4/pdf.*.sha256`), which the module's own reader imports back onto the
* bundled sheet as the report they carry (export Rust → import Rust, no loss); every view
* it asks for is the SVG the TypeScript engine composes (`composeHighlightedViewSvg`)
* and, on the real-raster run, the digests and sizes the R4.1 rasteriser recorded
* it asks for is composed on the view's own frame with the observation's markers
* highlighted (the text itself is pinned in cargo: `pdf_session` against `compose_highlight`,
* `anatomy_compose` against the frozen R3 trees of the TypeScript engine) and, on the
* real-raster run, the digests and sizes the R4.1 rasteriser recorded
* (`pdf/r3/real/manifest.json`); the refusals are closed kinds; a trap mid-session
* poisons the instance and the session answers `core_unavailable` from then on, `free()`
* included; the memory held by a fresh instance after an export is printed against the
@@ -14,7 +16,6 @@ import { createHash } from 'node:crypto'
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
import {
CoreRuntime,
@@ -221,22 +222,24 @@ describe('R4.6 export session through the shipped module', () => {
expect(outcome.value!.read.origin, run.name).toBe('envelope_v1')
expect(outcome.value!.read.snapshotRejected, run.name).toBeUndefined()
expect(outcome.value!.rebase.report, run.name).toEqual(report)
// Every view request is the TypeScript composition of that view for that observation.
// Every view request is a composition on that view's frame with the observation's
// markers highlighted and nothing of the editor's attributes (the exact text is the
// cargo suites' business).
let views = 0
const snapshotViews = (
envelope.sheetSnapshot as { document: { views: Array<{ id: string }> } }
).document.views
for (const request of driven.requests) {
if (!request.key.startsWith('view:')) continue
const [, observationId, viewId] = request.key.split(':')
const observation = report.observations.find((o) => o.id === observationId)!
expect(
request.svg,
`${run.name} ${request.key}`,
).toBe(
composeHighlightedViewSvg(
envelope.sheetSnapshot as never,
viewId!,
observation as never,
),
)
const where = `${run.name} ${request.key}`
expect(snapshotViews.some((v) => v.id === viewId), where).toBe(true)
expect(request.svg, where).toMatch(/^<svg /)
expect(request.svg, where).not.toContain('data-anno-id')
expect(request.svg, where).not.toContain('display="none"')
const highlighted = observation.zones.some((z) => z.viewId === viewId)
expect(request.svg.includes('fill="#b85a3f"'), where).toBe(highlighted)
views += 1
}
// Every (observation, view) the snapshot holds was asked for, once.
@@ -1,170 +0,0 @@
/**
* R4.5: the Rust writer's output under the TypeScript engine's own reader (plan §B.2.2: the
* R3 build reads R4 PDFs) and against that engine's export on the same inputs and
* conditions: the demo, L and CAP reports with the stub rasteriser, in Node, time, process
* RSS growth and file size (E2: ≤ R3 + 5 % on the O2 runs). The Rust side runs through the
* proofs build; the TypeScript side is `@faunosteo/pdf`'s `exportReportArchive`. Numbers
* printed for RUST.md §10.7. The engine and this test leave with R4.8.
*/
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import {
exportReportArchive,
readAttachedDocument,
readReportArchive,
type ExportInput,
} from '@faunosteo/pdf'
import { loadProofModule } from '../src/node'
import { ReportMigrationMapSchema } from './n-1/domain'
const ROOT = fileURLToPath(new URL('../../../', import.meta.url))
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
const ONE_PX_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
'base64',
)
const TWO_BY_ONE_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC',
'base64',
)
const FIXED_EXPORTED_AT = new Date('2026-10-11T12:00:00.000Z')
type Report = ExportInput['report']
interface Envelope {
report: Report
sheetSnapshot: ExportInput['sheet']
branding: ExportInput['branding']
labels: ExportInput['labels']
locale: 'fr' | 'en'
exportedAt: string
}
function rastersFor(report: Report, png: Buffer): string {
const seen = new Set<string>()
const views: Array<{ observationId: string; viewId: string; png: string }> =
[]
for (const observation of report.observations) {
for (const zone of observation.zones) {
const key = `${observation.id}\u0000${zone.viewId}`
if (seen.has(key)) continue
seen.add(key)
views.push({
observationId: observation.id,
viewId: zone.viewId,
png: png.toString('base64'),
})
}
}
const logo = png.toString('base64')
return JSON.stringify({
headerLogo: logo,
footerLogo: logo,
poweredBy: logo,
partner: logo,
views,
})
}
function migrationMap() {
return ReportMigrationMapSchema.parse(
JSON.parse(
readFileSync(
join(
ROOT,
'supabase/seed/anatomical-sheets/equine/draft/report-migration-map.json',
),
'utf8',
),
),
)
}
describe('R4 writer under the TypeScript engine (same inputs, Node)', () => {
it('the engine reads every R4 PDF of the O2 runs back as it reads the R3 one; E2 size', async () => {
const proofs = await loadProofModule()
const map = migrationMap()
for (const report of ['demo', 'v2full', 'v2unresolved']) {
for (const locale of ['fr', 'en']) {
for (const mode of ['nologos', 'logos']) {
const name = `${report}.${locale}.${mode}`
const r3 = new Uint8Array(
readFileSync(join(FIXTURES, `pdf/r3/${name}.pdf`)),
)
const envelope = (await readAttachedDocument(r3)) as Envelope
const png = mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG
const pdf = proofs.export_pdf_for_test(
JSON.stringify(envelope),
rastersFor(envelope.report, png),
)
expect(await readReportArchive(pdf, map), name).toEqual(
await readReportArchive(r3, map),
)
expect(pdf.byteLength, `${name}: size`).toBeLessThanOrEqual(
Math.ceil(r3.byteLength * 1.05),
)
}
}
}
})
it('costs less than the engine on demo, L and CAP with the stub rasters (printed)', async () => {
const proofs = await loadProofModule()
const map = migrationMap()
const demo = (await readAttachedDocument(
new Uint8Array(readFileSync(join(FIXTURES, 'pdf/r3/demo.fr.logos.pdf'))),
)) as Envelope
const rows = [
'| Report | TS ms | TS RSS MiB | TS bytes | Rust ms | Rust RSS MiB | Rust bytes |',
'|---|---|---|---|---|---|---|',
]
for (const name of ['demo', 'L', 'CAP']) {
const report: Report =
name === 'demo'
? demo.report
: JSON.parse(
readFileSync(join(FIXTURES, `r4/reports/${name}.json`), 'utf8'),
)
if (name !== 'demo') report.sheet = demo.report.sheet
let rss = process.memoryUsage().rss
let started = performance.now()
const ts = await exportReportArchive({
report,
sheet: demo.sheetSnapshot,
branding: demo.branding,
labels: demo.labels,
locale: 'fr',
rasterizer: async () => new Uint8Array(TWO_BY_ONE_PNG),
exportedAt: FIXED_EXPORTED_AT,
})
const tsMs = performance.now() - started
const tsRss = (process.memoryUsage().rss - rss) / 1024 / 1024
const input = JSON.stringify({
...demo,
report,
locale: 'fr',
exportedAt: FIXED_EXPORTED_AT.toISOString(),
})
rss = process.memoryUsage().rss
started = performance.now()
const rust = proofs.export_pdf_for_test(
input,
rastersFor(report, TWO_BY_ONE_PNG),
)
const rustMs = performance.now() - started
const rustRss = (process.memoryUsage().rss - rss) / 1024 / 1024
rows.push(
`| ${name} | ${tsMs.toFixed(0)} | ${tsRss.toFixed(1)} | ${ts.pdf.byteLength} | ${rustMs.toFixed(0)} | ${rustRss.toFixed(1)} | ${rust.byteLength} |`,
)
expect(
(await readReportArchive(rust, map)).report.observations.length,
).toBe(report.observations.length)
expect(rustMs).toBeLessThan(tsMs)
}
console.info(
`[core-wasm] R4 writer vs TS engine (Node, stub rasters)\n${rows.join('\n')}`,
)
}, 300_000)
})