test(tests): R2 freeze — archive envelopes of the three golden reports and the frozen N-1 reader

Envelopes exported by the TypeScript domain before the Rust switch (FR, no
logos, fixed export time) are the semantic oracle of the switch (D-R11:
key order may change, values may not). The shipped Zod schemas are copied
verbatim under packages/core-wasm/tests/n-1 (develop 1a7ee7d) so a rollback
reader stays testable after packages/domain is deleted at R5.
This commit is contained in:
bchanot committed 2026-10-13 23:53:12 +02:00
1 parent 1a7ee7d9ac
commit 3114d67959
29 files changed
+8581 -46

No files matched your search

@@ -0,0 +1,83 @@
/**
* R2 freeze (RUST_ARCHITECTURE_REVIEW VI.1, VI.2 R2): the archive envelope
* (PDF attachment) of the three golden reports, exported by the application
* path with fixed inputs (FR, every logo slot empty, 1×1 raster, fixed
* export time), committed as the TypeScript domain wrote it before the
* switch to the Rust core. Whatever writes the attachment afterwards, it
* must parse with the frozen N-1 reader and be deep-equal to the frozen
* envelope; only the key order may differ (D-R11). `MAKE_R2_FREEZE=1`
* writes the files: never once the switch has landed without an owner
* decision.
*/
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { readAttachmentBytes } from '@faunosteo/pdf'
import {
readArchiveDocument,
ReportArchiveEnvelopeSchema,
} from '@faunosteo/core-wasm/n-1'
import { exportReportToPdf } from '@/pdf/export'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
import { FIXTURES_DIR, ONE_PX_PNG } from '../helpers/goldens'
import {
buildGoldenReports,
GOLDEN_REPORTS,
type GoldenReports,
} from '../helpers/goldenReports'
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 FREEZE_DIR = resolve(FIXTURES_DIR, 'goldens/r2')
const MAKE_FREEZE = process.env.MAKE_R2_FREEZE === '1'
const EXPORTED_AT = new Date('2026-10-13T09:00:00.000Z')
let sheet: SheetIndex
let golden: GoldenReports
beforeAll(async () => {
sheet = await loadTestSheet()
golden = buildGoldenReports(sheet)
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
vi.useFakeTimers({ toFake: ['Date'] })
vi.setSystemTime(EXPORTED_AT)
})
afterAll(() => {
vi.useRealTimers()
vi.restoreAllMocks()
})
async function exportedAttachment(
name: (typeof GOLDEN_REPORTS)[number],
): Promise<string> {
const { pdf } = await exportReportToPdf(golden.reports[name], sheet, {
locale: 'fr',
})
return new TextDecoder().decode(await readAttachmentBytes(pdf))
}
describe.each(GOLDEN_REPORTS)('archive envelope of %s', (name) => {
const file = resolve(FREEZE_DIR, `envelope.${name}.fr.nologos.json`)
it('matches the frozen envelope and the N-1 reader accepts it', async () => {
const text = await exportedAttachment(name)
if (MAKE_FREEZE) {
mkdirSync(FREEZE_DIR, { recursive: true })
writeFileSync(file, text)
}
const data: unknown = JSON.parse(text)
expect(data).toEqual(JSON.parse(readFileSync(file, 'utf8')))
expect(ReportArchiveEnvelopeSchema.safeParse(data).success).toBe(true)
const outcome = readArchiveDocument(data, sheet.sheet.migrationMap)
expect(outcome.origin).toBe('envelope_v1')
expect(outcome.archive).not.toBeNull()
expect(outcome.snapshotRejected).toBeUndefined()
}, 30_000)
})