From 9cb3cb75d4ce30525e336260f21c92fe3009aa16 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 19 Oct 2026 01:28:03 +0200 Subject: [PATCH] =?UTF-8?q?test(tests):=20R4.6=20session=20suite=20imports?= =?UTF-8?q?=20every=20R4=20export=20back=20through=20the=20module's=20read?= =?UTF-8?q?er=20(export=20Rust=20=E2=86=92=20import=20Rust,=20no=20loss)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/pdf-export.session.test.ts | 69 ++++++++++++++++--- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/packages/core-wasm/tests/pdf-export.session.test.ts b/packages/core-wasm/tests/pdf-export.session.test.ts index f7f64355..f60877c0 100644 --- a/packages/core-wasm/tests/pdf-export.session.test.ts +++ b/packages/core-wasm/tests/pdf-export.session.test.ts @@ -1,12 +1,14 @@ /** * R4.6: the export session through the shipped module and the runtime wrapper * (`CoreExportSession`): driven request by request on the O2 runs it writes the native - * bytes (`goldens/r4/pdf.*.sha256`); 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 (`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 file's size (OD-2, RUST.md §10.8). + * 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 + * (`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 + * file's size (OD-2, RUST.md §10.8). */ import { createHash } from 'node:crypto' import { readFileSync } from 'node:fs' @@ -26,6 +28,31 @@ import { readAttachedDocument } from './n-1/scanner/attachment/read' const ROOT = fileURLToPath(new URL('../../../', import.meta.url)) const FIXTURES = join(ROOT, 'apps/app/tests/fixtures') +const SEED = join(ROOT, 'supabase/seed/anatomical-sheets/equine/draft') +const read = (path: string) => readFileSync(path, 'utf8') + +/** The bundled draft exactly as `apps/app/vite/sheet-bundle.ts` embeds it. */ +function draftBundle(): string { + const manifest = JSON.parse(read(join(SEED, 'assets.json'))) as Array<{ + file: string + sha256: string + svg_asset_id: string + }> + const assets: Record = {} + const assetHashes: Record = {} + for (const asset of manifest) { + assets[asset.svg_asset_id] = read(join(SEED, 'assets', asset.file)) + assetHashes[asset.svg_asset_id] = asset.sha256 + } + return JSON.stringify({ + status: 'draft', + ref: JSON.parse(read(join(SEED, 'sheet-ref.json'))), + document: JSON.parse(read(join(SEED, 'document.json'))), + migrationMap: JSON.parse(read(join(SEED, 'report-migration-map.json'))), + assets, + assetHashes, + }) +} const sha256 = (data: Uint8Array | string) => createHash('sha256').update(data).digest('hex') const ONE_PX_PNG = Buffer.from( @@ -51,6 +78,15 @@ interface Envelope { exportedAt: string } +interface ImportEnvelope { + ok: boolean + kind?: string + value?: { + read: { origin: string; snapshotRejected?: string } + rebase: { report: unknown; rebased: boolean } + } +} + interface Run { name: string r3: string @@ -145,8 +181,12 @@ function drive(session: CoreExportSession, raster: Run['raster']): Driven { } describe('R4.6 export session through the shipped module', () => { - it('writes the native bytes on every O2 run, asking for the SVGs the engine composes', async () => { + it('writes the native bytes on every O2 run, asking for the SVGs the engine composes; the module imports them back', async () => { const runtime = await nodeCoreRuntime('pkg') + const sheet = unwrap( + runtime.invoke((glue) => ({ ok: true as const, value: new glue.SheetHandle(draftBundle()) })), + 'sheet', + ) for (const run of RUNS) { const envelope = await envelopeOf(run.r3) const session = unwrap( @@ -165,10 +205,23 @@ describe('R4.6 export session through the shipped module', () => { ).trim() expect(sha256(driven.pdf), run.name).toBe(expected) expect(driven.warnings, run.name).toEqual([]) - // Every view request is the TypeScript composition of that view for that observation. const report = run.report ? (JSON.parse(readFileSync(join(FIXTURES, run.report), 'utf8')) as Envelope['report']) : envelope.report + // Export Rust → import Rust: the module's reader gives the report back, the archive + // kept, on the sheet the fixtures were written on (same reference: not rebased). + const outcome = unwrap( + runtime.invoke(() => ({ + ok: true as const, + value: JSON.parse(sheet.import_pdf(driven.pdf)) as ImportEnvelope, + })), + run.name, + ) + expect(outcome.ok, `${run.name}: ${JSON.stringify(outcome).slice(0, 200)}`).toBe(true) + 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. let views = 0 for (const request of driven.requests) { if (!request.key.startsWith('view:')) continue