Files
faunosteo/packages/core-wasm/tests/runtime.test.ts
T
bchanot 6c0533c1da feat(workspace): R2 facade operations for the editor rules, the publish check and the sheet index data
Free ops: sheet.check, report.new, report.add_observation,
report.remove_observation, report.move_observation, vertebra.select.
Sheet ops: index_data, zone.click, zone.add_sides, zone.remove_at,
vertebra.initial, vertebra.sync, finding.toggle. Input contracts exported
to TypeScript; a JavaScript index that names no position removes nothing
in the filters and is refused by the move (the TypeScript swap would have
written a hole). Shipped module 999 KB raw, 319 KB gzip (budget 600 KB).
2026-10-14 00:19:20 +02:00

148 lines
5.2 KiB
TypeScript

/**
* The core runtime (R2 plan P9 rev 2) on the proofs build, whose trap exports make the
* failure paths real: lazy load, poison on a trap, re-instantiation from the kept compiled
* module and the pre-imported glue record (no network), handles rebuilt per instance and
* never called on a dropped one, the recovery cap, load failure and retry.
*/
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { CoreRuntime, type CoreSource, type GlueModule } from '../src/index'
import { nodeCoreSource, type ProofModule } from '../src/node'
const SEED = '../../../supabase/seed/anatomical-sheets/equine/draft/'
function seedJson(name: string): unknown {
const path = fileURLToPath(new URL(`${SEED}${name}`, import.meta.url))
return JSON.parse(readFileSync(path, 'utf8'))
}
const BUNDLE = JSON.stringify({
status: 'draft',
ref: {
sheet_id: 'sheet',
version_id: null,
document_sha256: '0'.repeat(64),
schema_version: 2,
species_id: 'equine',
},
document: seedJson('document.json'),
migrationMap: seedJson('report-migration-map.json'),
assets: {},
assetHashes: {},
})
/** Counts glue imports; imports after `offlineAfter` fail like a lost network. */
function countingSource(offlineAfter = Infinity) {
const inner = nodeCoreSource('pkg-proofs')
const source = {
imports: 0,
compile: () => inner.compile(),
importGlue: async (n: number): Promise<GlueModule> => {
source.imports += 1
if (source.imports > offlineAfter) throw new TypeError('offline')
return inner.importGlue(n)
},
}
return source
}
/** Traps the live instance through the proofs build (stack exhaustion). */
function trap(runtime: CoreRuntime) {
return runtime.invoke((glue) => {
;(glue as ProofModule).recurse_for_test(2_000_000)
return { ok: true, value: null }
})
}
describe('core runtime', () => {
it('loads on first need only', async () => {
const source = countingSource()
const runtime = new CoreRuntime(source)
expect(runtime.status).toBe('idle')
expect(runtime.call('core_version', '')).toMatchObject({
ok: false,
kind: 'core_unavailable',
})
expect(source.imports).toBe(0)
expect(await runtime.ready()).toBe(true)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
})
it('poisons on a trap and recovers offline from the spare glue record', async () => {
const source = countingSource(2)
const runtime = new CoreRuntime(source)
await runtime.ready()
expect(trap(runtime)).toMatchObject({ ok: false, kind: 'internal' })
expect(runtime.status).toBe('recovering')
expect(runtime.call('core_version', '')).toMatchObject({
kind: 'core_unavailable',
})
expect(await runtime.ready()).toBe(true)
expect(runtime.generation).toBe(2)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
})
it('rebuilds a sheet handle on the new instance and never calls the dropped one', async () => {
const runtime = new CoreRuntime(countingSource())
await runtime.ready()
const sheet = runtime.sheet(BUNDLE)
expect(sheet.call('index_data', '')).toMatchObject({
ok: true,
value: { ref: { sheetId: 'sheet', versionId: null } },
})
trap(runtime)
expect(sheet.call('index_data', '')).toMatchObject({
kind: 'core_unavailable',
})
await runtime.ready()
// A call through the stack-trapped instance would fail in its allocator and poison
// again: one generation more and a ready runtime prove the handle was rebuilt.
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
expect(runtime.generation).toBe(2)
expect(runtime.status).toBe('ready')
})
it('stops recovering after the cap', async () => {
const runtime = new CoreRuntime(countingSource(), { maxRecoveries: 1 })
await runtime.ready()
trap(runtime)
expect(await runtime.ready()).toBe(true)
trap(runtime)
expect(runtime.status).toBe('unavailable')
expect(await runtime.retry()).toBe(false)
expect(runtime.call('core_version', '')).toMatchObject({
kind: 'core_unavailable',
})
})
it('reports a load failure and loads again on retry', async () => {
const inner = nodeCoreSource('pkg')
let failNext = true
const source: CoreSource = {
compile: () => {
if (failNext) {
failNext = false
return Promise.reject(new WebAssembly.CompileError('blocked'))
}
return inner.compile()
},
importGlue: (n) => inner.importGlue(n),
}
const runtime = new CoreRuntime(source)
expect(await runtime.ready()).toBe(false)
expect(runtime.status).toBe('unavailable')
expect(await runtime.retry()).toBe(true)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
})
it('refuses an invalid sheet payload without poisoning the instance', async () => {
const runtime = new CoreRuntime(nodeCoreSource('pkg'))
await runtime.ready()
const outcome = runtime.sheet('{"status":"draft"}').open()
expect(outcome).toMatchObject({ ok: false, kind: 'invalid_sheet' })
expect(runtime.status).toBe('ready')
expect(runtime.sheet(BUNDLE).open()).toEqual({ ok: true, value: null })
})
})