67 lines
2.4 KiB
TypeScript
67 lines
2.4 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,
|
|
}
|
|
},
|
|
}
|
|
const runtime = new CoreRuntime(wrapped)
|
|
if (!(await runtime.ready())) throw new Error('trapping runtime failed to load')
|
|
return { runtime, traps }
|
|
}
|