Files
faunosteo/apps/app/tests/helpers/core.ts
T
bchanot 6d1cce6e06 feat(app): R4.3b/c import worker, client, controller; HomeRoute on the disposable worker
Protocol v1 (task → ready → result | failed with phase and kind), the worker entry with
the glue bundled statically and the page's compiled Module posted, the file streamed into
the module's memory; client with start and task timers (paused while hidden), settle-once,
terminate always, error events never masked; module-level controller (one import at a
time, cancel on leaving Home, worker chunk prefetched once the engine is loaded); the
settlement mapped to the B.1.8 messages (OD-5: a trap, a timeout or an unreadable file is
« Erreur de lecture du PDF. », never a corrupt PDF). HomeRoute keeps its routing and size
pre-checks, loads its own index after acceptance, drops a result when the session changed.
Tests: handler and client in-process, the corpus message test (O8 51, O7 stored inputs,
O2/O3/Step 5), the six existing suites migrated to the real worker logic.
2026-10-18 18:10:42 +02:00

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