Files
faunosteo/apps/app/tests/helpers/core.ts
T
bchanot 9bd9acaf58 fix(app): rescue copy importable, trapping draft held, replace confirmed
The JSON rescue copy could not be restored in the app, a draft that traps
the engine left the tab on a reload screen and spent a recovery on every
read, and an unsaved or in-progress report could be lost by a reload
outside the editor or by New report / Import without warning.

Home import reads the copy (content-sniffed, 16 MiB cap, strict UTF-8)
through the core's draft.restore before any session change; a trapping
(draft, archive) pair is held in the resolution panel and only re-read
on Retry; ReplaceSessionDialog guards New report and Import; the
beforeunload guard is app-wide while unsaved. Supersedes BDR-055's
"never held" for `internal` only.
2026-10-17 12:58:57 +02:00

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