/** * R0 proof: the core's JSON output is byte-identical to `JSON.stringify(v, null, 2)` on the * real documents, optional fields stay absent and nullable fields stay `null`, and the * current Zod readers accept what the core writes (review I.2, D-R11). */ import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { ReportZoneSchema, SheetRefSchema } from './n-1/domain' import type { ReportZone, SheetRef } from '../types/index' import type { CoreRuntime } from '../src/index' import { nodeCoreRuntime } from '../src/node' const FIXTURES = [ 'apps/app/tests/fixtures/report-v3.demo.json', 'apps/app/tests/fixtures/report-v2.full.json', 'supabase/seed/anatomical-sheets/equine/draft/document.json', 'supabase/seed/anatomical-sheets/equine/draft/report-migration-map.json', ] const NUMBER_CASES = [ 1523, 1715.4285714285713, 0.85, 1e21, 1e-7, 0.000001, -0, 123456789012345680000, 5e-324, 1.7976931348623157e308, 100, -2.5, 0.1, 1.5e300, 12345678901234567890, ] function repoFile(relative: string): string { return readFileSync( fileURLToPath(new URL(`../../../${relative}`, import.meta.url)), 'utf8', ) } function prettyByCore(core: CoreRuntime, text: string): string { const outcome = core.call('pretty_json', text) if (!outcome.ok) throw new Error(`${outcome.kind}: ${outcome.detail ?? ''}`) return outcome.value } describe('serialisation contract (wasm)', () => { it.each(FIXTURES)( '%s: core pretty output equals JSON.stringify(…, null, 2)', async (relative) => { const core = await nodeCoreRuntime() const text = repoFile(relative) const expected = JSON.stringify(JSON.parse(text), null, 2) expect(prettyByCore(core, text)).toBe(expected) }, ) it('prints every number as JavaScript does', async () => { const core = await nodeCoreRuntime() const text = JSON.stringify(NUMBER_CASES) expect(prettyByCore(core, text)).toBe( JSON.stringify(JSON.parse(text), null, 2), ) }) it('writes optional fields absent and nullable fields null, readable by the Zod schemas', async () => { const core = await nodeCoreRuntime() const bare = core.call( 'report_zone', JSON.stringify({ annotationId: 'hip', viewId: 'main' }), ) expect(bare).toEqual({ ok: true, value: { annotationId: 'hip', viewId: 'main' }, }) if (!bare.ok) return expect(Object.keys(bare.value)).toEqual(['annotationId', 'viewId']) expect(ReportZoneSchema.parse(bare.value)).toEqual(bare.value) const full = core.call( 'report_zone', JSON.stringify({ viewId: 'main', annotationId: 'hip', side: 'left', unresolved: true, extra: 1, }), ) expect(full).toEqual({ ok: true, value: { annotationId: 'hip', viewId: 'main', side: 'left', unresolved: true, }, }) const sheet = core.call( 'sheet_ref', JSON.stringify({ sheetId: 'faunosteo-cheval', versionId: null, documentSha256: 'a'.repeat(64), }), ) expect(sheet.ok && sheet.value.versionId).toBeNull() if (sheet.ok) expect(SheetRefSchema.parse(sheet.value)).toEqual(sheet.value) const refused = core.call( 'report_zone', JSON.stringify({ annotationId: 'a', viewId: 'v', unresolved: false }), ) expect(refused).toMatchObject({ ok: false, kind: 'invalid_shape' }) }) })