Files
faunosteo/apps/app/tests/unit/archive.envelopes.test.ts
T
bchanot 74fcb37564 feat(app): R2 switch — the application runs its domain rules in the Rust core
Consumers now fed by the core (crates/core through @faunosteo/core-wasm):
- the sheet: the bundled payload is parsed and indexed by the core
  (SheetHandle, SheetIndexData); SheetIndex reads precomputed tables only;
- the store: new report, add / remove / move observation, draft restore
  (v2 migration + rebase) through the core, actions answering { ok };
- the editor click chain (zone.click), laterality and vertebra pickers
  (zone.add_sides, vertebra.initial / sync / select), the findings picker
  (finding.toggle by clicked leaf position), zone removal;
- the import: engine byte scan (TypeScript until R4), then the core's
  layered reading and rebase in one call; the export: the core builds the
  sheet snapshot, the frozen TypeScript engine writes the PDF (R4).
Lazy core: Home and the new-report form read the sheet availability from
the bundle and load the module on import / submit only; the auth shell
never references it. Degraded states (core_unavailable, invalid_sheet)
show the validated notice with a retry and never redirect or touch the
draft; an unreadable draft or archive is set aside under
osteo:quarantine:* (only content kinds; a failed write keeps it in place
and the editor stays), a rebase that flags items keeps the raw pre-rebase
draft. Four strings, wording validated by the owner.
Deleted: services/zoneInteraction.ts and its suite (13 cases ported to
cargo), the TypeScript Finding builder and vertebra selection rules of the
pickers, store rules, persistence parsing, hydrateReport. ESLint forbids
@faunosteo/domain in apps/app/src; the report:compare CLI keeps the frozen
domain until R4 (scripts/compare/sheet.ts). App suite 313/313 on the real
core (24 new tests: quarantine, degraded editor, core failures, N-1
reader); step6 goldens and the frozen envelopes unchanged.
2026-10-14 00:41:34 +02:00

86 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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. Since R2 the golden reports are read and the snapshot built by
* the Rust core: the attachment differs from the frozen one by key order at
* most (the deep-equality below ignores it).
*/
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 { bundledMigrationMap, 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, await bundledMigrationMap())
expect(outcome.origin).toBe('envelope_v1')
expect(outcome.archive).not.toBeNull()
expect(outcome.snapshotRejected).toBeUndefined()
}, 30_000)
})