Files
faunosteo/packages/core-wasm/tests/serialisation.test.ts
T

116 lines
3.5 KiB
TypeScript

/**
* 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 '@faunosteo/domain'
import type { ReportZone, SheetRef } from '../types/index'
import { callCore, loadCore, type CoreModule } from '../src/index'
import { coreBuilt } from './helpers/core'
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: CoreModule, text: string): string {
const outcome = callCore<string>(core, 'pretty_json', text)
if (!outcome.ok) throw new Error(`${outcome.kind}: ${outcome.detail ?? ''}`)
return outcome.value
}
describe.skipIf(!coreBuilt)('serialisation contract (wasm)', () => {
it.each(FIXTURES)(
'%s: core pretty output equals JSON.stringify(…, null, 2)',
async (relative) => {
const core = await loadCore()
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 loadCore()
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 loadCore()
const bare = callCore<ReportZone>(
core,
'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 = callCore<ReportZone>(
core,
'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 = callCore<SheetRef>(
core,
'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 = callCore(
core,
'report_zone',
JSON.stringify({ annotationId: 'a', viewId: 'v', unresolved: false }),
)
expect(refused).toMatchObject({ ok: false, kind: 'invalid_shape' })
})
})