/** * R5.6 M1 (owner 2026-10-25) through the module and the frozen N-1 readers: an old PDF stamped * with the registered historical value, re-exported on the switched sheet, gets its snapshot * from the core (`archive.snapshot` on the sheet handle: the loaded sheet verified its `v1` * value, recomputed now), carrying the report's own stamp; the N-1 scanner and Zod readers of * the R3 build and 0.2.0 keep that snapshot and its content. On an unverified v0 sheet the * core's snapshot carries the sheet's value and the export refuses the old report. The native * side is `crates/core/tests/r5_6_switch.rs`. */ import { readFileSync } from 'node:fs' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { CoreRuntime } from '../src/index' import { loadProofModule, nodeCoreSource } from '../src/node' import { readArchiveDocument, ReportArchiveEnvelopeSchema } from './n-1' import { ReportMigrationMapSchema } from './n-1/domain' import { readAttachedDocument } from './n-1/scanner/attachment/read' const ROOT = fileURLToPath(new URL('../../../', import.meta.url)) const SEED = join(ROOT, 'supabase/seed/anatomical-sheets/equine/draft') const ALIAS = '27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660' const CANONICAL = '12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2' const json = (path: string): unknown => JSON.parse(readFileSync(path, 'utf8')) interface Envelope { report: { sheet: { documentSha256: string } } sheetSnapshot: { ref: { documentSha256: string }; document: unknown; assets: unknown } branding: unknown labels: unknown locale: string } /** The bundle as `apps/app/vite/sheet-bundle.ts` builds it, with `ref` replaceable. */ function bundle(ref: unknown): string { const manifest = json(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] = readFileSync(join(SEED, 'assets', asset.file), 'utf8') assetHashes[asset.svg_asset_id] = asset.sha256 } return JSON.stringify({ status: 'draft', ref, document: json(join(SEED, 'document.json')), migrationMap: json(join(SEED, 'report-migration-map.json')), assets, assetHashes, }) } async function coreSnapshot(ref: unknown, report: unknown): Promise { const runtime = new CoreRuntime(nodeCoreSource('pkg')) expect(await runtime.ready()).toBe(true) const outcome = runtime .sheet(bundle(ref)) .call('archive.snapshot', JSON.stringify({ report })) expect(outcome.ok).toBe(true) return (outcome as { ok: true; value: Envelope['sheetSnapshot'] }).value } function exportInput(old: Envelope, snapshot: unknown): string { return JSON.stringify({ report: old.report, sheetSnapshot: snapshot, branding: old.branding, labels: old.labels, locale: old.locale, exportedAt: '2026-10-25T12:00:00.000Z', }) } async function oldEnvelope(): Promise { const pdf = new Uint8Array( readFileSync(join(ROOT, 'apps/app/tests/fixtures/pdf/r3/demo.fr.nologos.pdf')), ) return (await readAttachedDocument(pdf)) as Envelope } describe('R5.6 re-export of an old archive, read by the N-1 readers', () => { it('writes one stamp and the frozen readers keep the snapshot and its content', async () => { const proofs = await loadProofModule() const old = await oldEnvelope() expect(old.report.sheet.documentSha256).toBe(ALIAS) const ref = json(join(SEED, 'sheet-ref.json')) as { document_sha256: string } expect(ref.document_sha256).toBe(CANONICAL) const snapshot = await coreSnapshot(ref, old.report) expect(snapshot.ref.documentSha256).toBe(ALIAS) const written = (await readAttachedDocument( proofs.export_pdf_for_test(exportInput(old, snapshot), JSON.stringify({ views: [] })), )) as Envelope expect(written.report.sheet.documentSha256).toBe(ALIAS) expect(written.sheetSnapshot.ref.documentSha256).toBe(ALIAS) expect(ReportArchiveEnvelopeSchema.safeParse(written).success).toBe(true) const map = ReportMigrationMapSchema.parse(json(join(SEED, 'report-migration-map.json'))) const outcome = readArchiveDocument(written, map) expect(outcome.snapshotRejected).toBeUndefined() expect(outcome.archive).toBeDefined() expect(written.sheetSnapshot.document).toEqual(old.sheetSnapshot.document) expect(written.sheetSnapshot.assets).toEqual(old.sheetSnapshot.assets) expect(written.labels).toEqual(old.labels) expect(written.branding).toEqual(old.branding) }, 60_000) it('refuses the old report on an unverified v0 sheet carrying the canonical value', async () => { const proofs = await loadProofModule() const old = await oldEnvelope() const v0 = json(join(SEED, 'sheet-ref.json')) as Record delete v0.fingerprint const snapshot = await coreSnapshot(v0, old.report) expect(snapshot.ref.documentSha256).toBe(CANONICAL) expect(() => proofs.export_pdf_for_test(exportInput(old, snapshot), JSON.stringify({ views: [] })), ).toThrow(/sheet_mismatch/) }, 60_000) })