/** * 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() 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) })