Files
faunosteo/apps/app/tests/helpers/core.ts
T
bchanot 8ae756534c feat(domain): V1-2.2 wasm facade, contract types, module proofs
PreviewBuilder, PreviewSheet and clean_svg on the browser module, the
31 ts-rs types and their index, the glue interface, and the proofs on
the shipped module; the module stays under its 600 KB budget.
2026-10-29 13:14:22 +01:00

69 lines
2.5 KiB
TypeScript

import { CoreRuntime, type CoreSource } from '@faunosteo/core-wasm'
import { nodeCoreRuntime, nodeCoreSource } from '@faunosteo/core-wasm/node'
/** A loaded runtime on the shipped build (the package's Node source). */
export function testCoreRuntime(): Promise<CoreRuntime> {
return nodeCoreRuntime('pkg')
}
/**
* Makes `runtime.stopped` follow a flag the test controls; the returned setter flips it and
* notifies the subscribers, as the runtime does on its own transition to stopped.
*/
export function controlStopped(runtime: CoreRuntime, initial: boolean) {
let stopped = initial
Object.defineProperty(runtime, 'stopped', { get: () => stopped, configurable: true })
return (next: boolean) => {
stopped = next
;(runtime as unknown as { notify(): void }).notify()
}
}
/** A runtime whose module never loads (CSP refusal, network failure). */
export function unavailableCoreRuntime(): CoreRuntime {
return new CoreRuntime({
compile: () => Promise.reject(new WebAssembly.CompileError('blocked')),
importGlue: () => Promise.reject(new TypeError('blocked')),
})
}
/**
* A real runtime on the shipped module whose `SheetHandle.call` throws a
* `WebAssembly.RuntimeError` (as an out-of-memory trap does) for `draft.restore`
* calls whose input contains `marker`; `traps.count` counts them. The runtime
* poisons and recovers exactly as in production (three recoveries).
*/
export async function trappingCoreRuntime(marker: string) {
const source = nodeCoreSource('pkg')
const traps = { count: 0 }
const wrapped: CoreSource = {
compile: () => source.compile(),
importGlue: async (instance) => {
const glue = await source.importGlue(instance)
const Real = glue.SheetHandle
class Trapping extends Real {
call(op: string, input: string): string {
if (op === 'draft.restore' && input.includes(marker)) {
traps.count += 1
throw new WebAssembly.RuntimeError('unreachable')
}
return super.call(op, input)
}
}
return {
default: glue.default,
core_version: glue.core_version,
call: glue.call,
SheetHandle: Trapping,
FileBytes: glue.FileBytes,
ExportSession: glue.ExportSession,
PreviewBuilder: glue.PreviewBuilder,
clean_svg: glue.clean_svg,
}
},
}
const runtime = new CoreRuntime(wrapped)
if (!(await runtime.ready())) throw new Error('trapping runtime failed to load')
return { runtime, traps }
}