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.
281 lines
7.9 KiB
TypeScript
281 lines
7.9 KiB
TypeScript
/**
|
|
* R4.3: the page side of one import (plan §B.1.5 items 1, 3, 5): the worker created in a
|
|
* try/catch, listeners before the post, the start timer until `ready`, the task timer
|
|
* after, settle once on the first of result / failed / error event / timer / cancel, the
|
|
* worker terminated in every case, later messages ignored, timers paused while hidden.
|
|
*/
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
runImportTask,
|
|
type ImportSettlement,
|
|
type WorkerLike,
|
|
} from '@/core/worker/client'
|
|
import { PROTOCOL_VERSION } from '@/core/worker/protocol'
|
|
import type { Visibility } from '@/core/worker/timers'
|
|
|
|
/** A scripted worker: the test drives its messages and events. */
|
|
function scriptedWorker() {
|
|
const worker: WorkerLike & { terminated: number; sent: unknown[] } = {
|
|
terminated: 0,
|
|
sent: [],
|
|
onmessage: null,
|
|
onerror: null,
|
|
onmessageerror: null,
|
|
postMessage: (m) => worker.sent.push(m),
|
|
terminate: () => {
|
|
worker.terminated += 1
|
|
},
|
|
}
|
|
const emit = (data: unknown) => worker.onmessage?.({ data } as MessageEvent)
|
|
return {
|
|
worker,
|
|
ready: () => emit({ v: PROTOCOL_VERSION, type: 'ready' }),
|
|
result: (out = '{"ok":true}') =>
|
|
emit({
|
|
v: PROTOCOL_VERSION,
|
|
type: 'result',
|
|
out,
|
|
diag: { ms: 1, linearBytes: 0, chunks: 1 },
|
|
}),
|
|
failed: (phase: 'start' | 'task', kind: string) =>
|
|
emit({
|
|
v: PROTOCOL_VERSION,
|
|
type: 'failed',
|
|
phase,
|
|
kind,
|
|
detail: `${kind}!`,
|
|
}),
|
|
error: (message: string) => worker.onerror?.({ message } as ErrorEvent),
|
|
}
|
|
}
|
|
|
|
function visibility(): Visibility & { set: (hidden: boolean) => void } {
|
|
let hidden = false
|
|
const listeners = new Set<() => void>()
|
|
return {
|
|
hidden: () => hidden,
|
|
onChange: (listener) => {
|
|
listeners.add(listener)
|
|
return () => listeners.delete(listener)
|
|
},
|
|
set: (next) => {
|
|
hidden = next
|
|
for (const listener of listeners) listener()
|
|
},
|
|
}
|
|
}
|
|
|
|
const file = new File([new Uint8Array([1])], 'a.pdf')
|
|
const input = { task: 'pdf' as const, file, sheet: '{}', module: null }
|
|
|
|
async function settled(run: {
|
|
settled: Promise<ImportSettlement>
|
|
}): Promise<ImportSettlement> {
|
|
await vi.advanceTimersByTimeAsync(0)
|
|
return run.settled
|
|
}
|
|
|
|
describe('import client', () => {
|
|
beforeEach(() => vi.useFakeTimers())
|
|
afterEach(() => vi.useRealTimers())
|
|
|
|
it('posts the task after installing the listeners, then settles on the result and terminates', async () => {
|
|
const s = scriptedWorker()
|
|
const run = runImportTask(input, {
|
|
spawn: () => s.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
expect(s.worker.onmessage).not.toBeNull()
|
|
expect(s.worker.sent).toEqual([{ v: 1, type: 'task', ...input }])
|
|
s.ready()
|
|
s.result('{"ok":true}')
|
|
expect(await settled(run)).toMatchObject({
|
|
kind: 'result',
|
|
out: '{"ok":true}',
|
|
})
|
|
expect(s.worker.terminated).toBe(1)
|
|
})
|
|
|
|
it('is worker_unavailable when the spawn throws, fails before ready, or never answers ready', async () => {
|
|
const thrown = runImportTask(input, {
|
|
spawn: () => {
|
|
throw new DOMException('blocked', 'SecurityError')
|
|
},
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
expect(await settled(thrown)).toEqual({
|
|
kind: 'failed',
|
|
reason: 'worker_unavailable',
|
|
detail: 'SecurityError',
|
|
})
|
|
|
|
const early = scriptedWorker()
|
|
const failedEarly = runImportTask(input, {
|
|
spawn: () => early.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
early.failed('start', 'other')
|
|
expect(await settled(failedEarly)).toMatchObject({
|
|
kind: 'failed',
|
|
reason: 'worker_unavailable',
|
|
})
|
|
|
|
const silent = scriptedWorker()
|
|
const timedOut = runImportTask(input, {
|
|
spawn: () => silent.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 1000,
|
|
})
|
|
await vi.advanceTimersByTimeAsync(99)
|
|
expect(silent.worker.terminated).toBe(0)
|
|
await vi.advanceTimersByTimeAsync(2)
|
|
expect(await timedOut.settled).toMatchObject({
|
|
kind: 'failed',
|
|
reason: 'worker_unavailable',
|
|
})
|
|
expect(silent.worker.terminated).toBe(1)
|
|
})
|
|
|
|
it('maps task failures: trap → crashed, read → read, sheet/other → engine, timer → timeout', async () => {
|
|
for (const [kind, reason] of [
|
|
['trap', 'crashed'],
|
|
['read', 'read'],
|
|
['sheet', 'engine'],
|
|
['other', 'engine'],
|
|
]) {
|
|
const s = scriptedWorker()
|
|
const run = runImportTask(input, {
|
|
spawn: () => s.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
s.ready()
|
|
s.failed('task', kind!)
|
|
expect(await settled(run)).toEqual({
|
|
kind: 'failed',
|
|
reason,
|
|
detail: `${kind}!`,
|
|
})
|
|
expect(s.worker.terminated).toBe(1)
|
|
}
|
|
const slow = scriptedWorker()
|
|
const run = runImportTask(input, {
|
|
spawn: () => slow.worker,
|
|
startTimeoutMs: 50,
|
|
taskTimeoutMs: 200,
|
|
})
|
|
await vi.advanceTimersByTimeAsync(40)
|
|
slow.ready()
|
|
await vi.advanceTimersByTimeAsync(199)
|
|
expect(slow.worker.terminated).toBe(0)
|
|
await vi.advanceTimersByTimeAsync(2)
|
|
expect(await run.settled).toMatchObject({
|
|
kind: 'failed',
|
|
reason: 'timeout',
|
|
})
|
|
})
|
|
|
|
it('settles once: a result after the timeout, a failure after the result, are ignored', async () => {
|
|
const s = scriptedWorker()
|
|
const run = runImportTask(input, {
|
|
spawn: () => s.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
s.ready()
|
|
await vi.advanceTimersByTimeAsync(101)
|
|
s.result('{"ok":true}')
|
|
expect(await run.settled).toMatchObject({
|
|
kind: 'failed',
|
|
reason: 'timeout',
|
|
})
|
|
expect(s.worker.terminated).toBe(1)
|
|
|
|
const t = scriptedWorker()
|
|
const second = runImportTask(input, {
|
|
spawn: () => t.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
t.ready()
|
|
t.result('{"ok":true}')
|
|
t.failed('task', 'trap')
|
|
t.error('late')
|
|
expect(await settled(second)).toMatchObject({ kind: 'result' })
|
|
expect(t.worker.terminated).toBe(1)
|
|
})
|
|
|
|
it('never masks an error event: worker_unavailable before ready, crashed after', async () => {
|
|
const before = scriptedWorker()
|
|
const first = runImportTask(input, {
|
|
spawn: () => before.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
before.error('script failed')
|
|
expect(await settled(first)).toEqual({
|
|
kind: 'failed',
|
|
reason: 'worker_unavailable',
|
|
detail: 'script failed',
|
|
})
|
|
const after = scriptedWorker()
|
|
const second = runImportTask(input, {
|
|
spawn: () => after.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
after.ready()
|
|
after.error('boom')
|
|
expect(await settled(second)).toEqual({
|
|
kind: 'failed',
|
|
reason: 'crashed',
|
|
detail: 'boom',
|
|
})
|
|
})
|
|
|
|
it('cancel terminates the worker and settles cancelled, once', async () => {
|
|
const s = scriptedWorker()
|
|
const run = runImportTask(input, {
|
|
spawn: () => s.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
})
|
|
s.ready()
|
|
run.cancel()
|
|
run.cancel()
|
|
s.result()
|
|
expect(await settled(run)).toMatchObject({
|
|
kind: 'failed',
|
|
reason: 'cancelled',
|
|
})
|
|
expect(s.worker.terminated).toBe(1)
|
|
})
|
|
|
|
it('pauses the task timer while the page is hidden', async () => {
|
|
const s = scriptedWorker()
|
|
const vis = visibility()
|
|
const run = runImportTask(input, {
|
|
spawn: () => s.worker,
|
|
startTimeoutMs: 100,
|
|
taskTimeoutMs: 100,
|
|
visibility: vis,
|
|
})
|
|
s.ready()
|
|
await vi.advanceTimersByTimeAsync(50)
|
|
vis.set(true)
|
|
await vi.advanceTimersByTimeAsync(10_000)
|
|
expect(s.worker.terminated).toBe(0)
|
|
vis.set(false)
|
|
await vi.advanceTimersByTimeAsync(49)
|
|
expect(s.worker.terminated).toBe(0)
|
|
await vi.advanceTimersByTimeAsync(2)
|
|
expect(await run.settled).toMatchObject({
|
|
kind: 'failed',
|
|
reason: 'timeout',
|
|
})
|
|
})
|
|
})
|