packages/core-wasm: browser-safe CoreRuntime (synchronous calls once ready; a WebAssembly.RuntimeError poisons the instance, the call answers internal, a fresh instance comes from the kept compiled module and a glue record imported in advance; at most 3 recoveries per page; retry after a load failure), SheetSession (handle rebuilt per instance generation, never called on a dropped one; invalid_sheet without poisoning), Node source. crates/wasm: SheetHandle (sheet held in the module memory). Two builds: pkg/ shipped without trap exports (checked), pkg-proofs/ for the trap tests; BUILD_STAMP (hash of the Rust sources) refused when missing or stale by the vitest global setup instead of skipping. Failure envelope carries a numeric found version; compact JS-number output.
112 lines
3.4 KiB
TypeScript
112 lines
3.4 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 './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<string>('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<ReportZone>(
|
|
'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<ReportZone>(
|
|
'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<SheetRef>(
|
|
'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' })
|
|
})
|
|
})
|