/** * R5.6 in the application, on the switched bundled sheet (`sha256-rfc8785-validated-v1`): * a v2 report migrated now carries the canonical value and exports one consistent envelope * that the N-1 readers accept; an old report on the historical value opens with no rebase and * re-exports with its own stamp in the snapshot (the goldens prove the bytes); no visible * change. The historical goldens run in their original context (`helpers/goldenReports.ts`). */ import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' import { readArchiveDocument, readAttachmentBytes, ReportArchiveEnvelopeSchema, } from '@faunosteo/core-wasm/n-1' import { exportReportToPdf } from '@/pdf/export' import type { SheetIndex } from '@/services/sheetIndex' import { bundledMigrationMap, loadTestSheet } from '../helpers/sheet' import { FIXTURES_DIR, ONE_PX_PNG } from '../helpers/goldens' import { HISTORICAL_FINGERPRINT } from '../helpers/goldenReports' import { importAttachment } from '../helpers/pdfImport' vi.mock('@/pdf/browserRasterizer', () => ({ browserRasterizer: vi.fn(async () => ONE_PX_PNG), })) vi.mock('@/pdf/branding', async () => { const { brandingOf } = await import('../helpers/goldens') return { loadBranding: vi.fn(() => brandingOf('nologos')) } }) const CANONICAL = '12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2' let sheet: SheetIndex beforeAll(async () => { sheet = await loadTestSheet() vi.spyOn(console, 'warn').mockImplementation(() => undefined) }) afterAll(() => vi.restoreAllMocks()) interface Stamped { report: { sheet: { documentSha256: string } } sheetSnapshot: { ref: { documentSha256: string } } } async function exported( report: Parameters[0], ): Promise { const { pdf } = await exportReportToPdf(report, sheet, { locale: 'fr' }) const bytes = new Uint8Array(await pdf.arrayBuffer()) return JSON.parse( new TextDecoder().decode(await readAttachmentBytes(bytes)), ) as Stamped } describe('R5.6 on the switched sheet', () => { it('stamps a new or newly migrated report with the canonical value', async () => { expect(sheet.sheet.ref.documentSha256).toBe(CANONICAL) const v2 = JSON.parse( readFileSync(resolve(FIXTURES_DIR, 'report-v2.full.json'), 'utf8'), ) const migrated = importAttachment(v2, sheet).read.report expect(migrated.sheet.documentSha256).toBe(CANONICAL) const envelope = await exported(migrated) expect(envelope.report.sheet.documentSha256).toBe(CANONICAL) expect(envelope.sheetSnapshot.ref.documentSha256).toBe(CANONICAL) expect(ReportArchiveEnvelopeSchema.safeParse(envelope).success).toBe(true) const outcome = readArchiveDocument(envelope, await bundledMigrationMap()) expect(outcome.snapshotRejected).toBeUndefined() }, 30_000) it('opens an old report with no rebase and re-exports it with one stamp', async () => { const demo = JSON.parse( readFileSync(resolve(FIXTURES_DIR, 'report-v3.demo.json'), 'utf8'), ) const imported = importAttachment(demo, sheet) expect(imported.rebase.rebased).toBe(false) expect(imported.rebase.report.sheet.documentSha256).toBe( HISTORICAL_FINGERPRINT, ) const envelope = await exported(imported.rebase.report) expect(envelope.report.sheet.documentSha256).toBe(HISTORICAL_FINGERPRINT) expect(envelope.sheetSnapshot.ref.documentSha256).toBe( HISTORICAL_FINGERPRINT, ) }, 30_000) })