Files
faunosteo/apps/app/tests/helpers/importWorker.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

116 lines
3.5 KiB
TypeScript

import { nodeCoreSource } from '@faunosteo/core-wasm/node'
import type { WorkerLike } from '@/core/worker/client'
import { setImportWorkerSpawn } from '@/core/worker/controller'
import {
createWorkerHandler,
type EngineLoader,
type WorkerEngine,
} from '@/core/worker/handler'
import type { WorkerMessage } from '@/core/worker/protocol'
/**
* The import worker run in-process (happy-dom has no `Worker`): a `WorkerLike` whose
* `postMessage` feeds the worker handler and whose answers come back through `onmessage`
* on a later tick, as a real worker's would; `terminate` drops every later answer. The
* engine is the Node module by default; a test passes its own loader for failures.
*/
let instances = 0
/** The shipped module instantiated afresh (one instance per worker, as in the browser). */
export const nodeEngineLoader: EngineLoader = async (module) => {
const source = nodeCoreSource('pkg')
instances += 1
const glue = await source.importGlue(1000 + instances)
const compiled = module ?? (await source.compile())
const exports = (await glue.default({ module_or_path: compiled })) as {
memory?: WebAssembly.Memory
}
return {
SheetHandle: glue.SheetHandle,
FileBytes: glue.FileBytes,
memory: exports.memory ?? null,
}
}
export interface FakeWorker extends WorkerLike {
terminated: boolean
/** Every message the page posted to the worker (the task, as the worker receives it). */
received: unknown[]
/** Every message the worker posted, dropped or delivered. */
posted: WorkerMessage[]
}
export function inProcessWorker(
load: EngineLoader = nodeEngineLoader,
): FakeWorker {
const worker: FakeWorker = {
terminated: false,
received: [],
posted: [],
onmessage: null,
onerror: null,
onmessageerror: null,
terminate() {
worker.terminated = true
},
postMessage(message) {
worker.received.push(message)
void handle(message)
},
}
const handle = createWorkerHandler(load, (message) => {
worker.posted.push(message)
setTimeout(() => {
if (!worker.terminated)
worker.onmessage?.({ data: message } as MessageEvent)
}, 0)
})
return worker
}
/** Routes the page's imports through in-process workers; returns the spawned ones. */
export function useInProcessImportWorker(
load: EngineLoader = nodeEngineLoader,
): { workers: FakeWorker[]; restore: () => void } {
const workers: FakeWorker[] = []
setImportWorkerSpawn(() => {
const worker = inProcessWorker(load)
workers.push(worker)
return worker
})
return { workers, restore: () => setImportWorkerSpawn(null) }
}
/** An engine whose `SheetHandle` throws `error` on every import call (a trap, say). */
export function throwingEngineLoader(error: Error): EngineLoader {
return async (module) => {
const engine = await nodeEngineLoader(module)
class Throwing extends engine.SheetHandle {
import_pdf_bytes(): string {
throw error
}
restore_copy_bytes(): string {
throw error
}
}
return { ...engine, SheetHandle: Throwing } as WorkerEngine
}
}
/** An engine whose import calls answer the envelope text `out` (a core refusal, say). */
export function answeringEngineLoader(out: string): EngineLoader {
return async (module) => {
const engine = await nodeEngineLoader(module)
class Answering extends engine.SheetHandle {
import_pdf_bytes(): string {
return out
}
restore_copy_bytes(): string {
return out
}
}
return { ...engine, SheetHandle: Answering } as WorkerEngine
}
}