/** * R2 freeze (RUST_ARCHITECTURE_REVIEW VI.1): what the Zod schemas output for the reference * documents, written once as `JSON.stringify(Schema.parse(x), null, 2)` under * crates/core/tests/oracles so the Rust contracts are checked byte for byte against the * TypeScript they replace. `MAKE_R2_ORACLES=1` writes them; otherwise they are asserted. */ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' import type { z } from 'zod' import { AnatomicalSheetDocumentSchema, ReportMigrationMapSchema, ReportSchema, ReportV2Schema, } from '../src' const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../../..') const OUT = resolve(ROOT, 'crates/core/tests/oracles') const MAKE = process.env.MAKE_R2_ORACLES === '1' const SEED = 'supabase/seed/anatomical-sheets/equine' const CASES: Array<[string, string, z.ZodTypeAny]> = [ ['sheet.draft.json', `${SEED}/draft/document.json`, AnatomicalSheetDocumentSchema], ['sheet.legacy-import.json', `${SEED}/legacy-import/document.json`, AnatomicalSheetDocumentSchema], ['migration-map.draft.json', `${SEED}/draft/report-migration-map.json`, ReportMigrationMapSchema], ['report-v3.demo.json', 'apps/app/tests/fixtures/report-v3.demo.json', ReportSchema], ['report-v2.full.json', 'apps/app/tests/fixtures/report-v2.full.json', ReportV2Schema], ['report-v2.minimal.json', 'apps/app/tests/fixtures/report-v2.minimal.json', ReportV2Schema], ] describe('Zod output oracles of the R2 switch', () => { it.each(CASES)('%s', (name, source, schema) => { const input: unknown = JSON.parse(readFileSync(resolve(ROOT, source), 'utf8')) const text = JSON.stringify(schema.parse(input), null, 2) const file = resolve(OUT, name) if (MAKE) { mkdirSync(OUT, { recursive: true }) writeFileSync(file, `${JSON.stringify({ source, output: JSON.parse(text) })}\n`) } const oracle = JSON.parse(readFileSync(file, 'utf8')) as { source: string; output: unknown } expect(oracle.source).toBe(source) expect(JSON.stringify(oracle.output, null, 2)).toBe(text) }) })