* Fake worker speaking the same {type:'ready'|'result'} protocol as the real * one. `behavior` decides per call whether to return a result, crash (exit≠0), * hang (never reply — exercises the backstop), or wait on a promise (lets a test * hold a call in-flight to observe concurrency). Emits 'read
| 32 | * the pool has wired its listeners first. |
| 33 | */ |
| 34 | class FakeWorker implements PoolWorker { |
| 35 | private msgCb?: (m: unknown) => void; |
| 36 | private errorCb?: (e: Error) => void; |
| 37 | private exitCb?: (code: number) => void; |
| 38 | alive = true; |
| 39 | terminated = false; |
| 40 | exitCode: number | undefined; |
| 41 | /** Ignore the pool's 'close', to exercise its terminate fallback. */ |
| 42 | ignoresClose = false; |
| 43 | /** What a trim reports. Too much to keep unless a test says otherwise, so a used worker is replaced. */ |
| 44 | heldBytes = Number.MAX_SAFE_INTEGER; |
| 45 | /** Hold the 'trimmed' reply until the test sends it. */ |
| 46 | holdsTrimReport = false; |
| 47 | trims = 0; |
| 48 | constructor(private behavior: (m: CallMsg) => Action, readyOk: boolean | null = true) { |
| 49 | setTimeout(() => { if (this.alive && readyOk !== null) this.emitMessage({ type: 'ready', ok: readyOk }); }, 0); |
| 50 | } |
| 51 | on(event: string, cb: (...args: any[]) => void): void { |
| 52 | if (event === 'message') this.msgCb = cb; |
| 53 | else if (event === 'exit') this.exitCb = cb; |
| 54 | else if (event === 'error') this.errorCb = cb; |
| 55 | } |
| 56 | emitMessage(m: unknown): void { this.msgCb?.(m); } |
| 57 | emitError(): void { this.errorCb?.(new Error('worker failed')); } |
| 58 | emitExit(): void { this.exitCb?.(13); } |
| 59 | private reply(id: number, result: ToolResult): void { |
| 60 | if (this.alive) this.msgCb?.({ type: 'result', id, result }); |
| 61 | } |
| 62 | postMessage(msg: unknown): void { |
| 63 | const m = msg as CallMsg | { type: 'close' } | { type: 'trim' }; |
| 64 | if (m?.type === 'trim') { |
| 65 | this.trims++; |
| 66 | if (!this.holdsTrimReport && this.alive) this.emitMessage({ type: 'trimmed', heldBytes: this.heldBytes }); |
| 67 | return; |
| 68 | } |
| 69 | if (m?.type === 'close') { |
| 70 | // Like the real worker: close up and exit by itself, with code 0. |
| 71 | if (this.ignoresClose) return; |
| 72 | this.alive = false; |
| 73 | setTimeout(() => { this.exitCode = 0; this.exitCb?.(0); }, 0); |
| 74 | return; |
| 75 | } |
| 76 | if (!m || m.type !== 'call') return; |
| 77 | const action = this.behavior(m); |
| 78 | if ('crash' in action) { |
| 79 | this.alive = false; |
| 80 | setTimeout(() => this.exitCb?.(13), 0); // simulate a crash exit |
| 81 | return; |
| 82 | } |
| 83 | if ('hang' in action) return; // never reply |
| 84 | if ('wait' in action) { void action.wait.then((r) => this.reply(m.id, r)); return; } |
| 85 | setTimeout(() => this.reply(m.id, action.result), 0); |
| 86 | } |
| 87 | terminate(): Promise<number> { this.alive = false; this.terminated = true; return Promise.resolve(0); } |
| 88 | } |
| 89 | |
| 90 | const ok = (text: string): ToolResult => ({ content: [{ type: 'text', text }] }); |
| 91 |
nothing calls this directly
no outgoing calls
no test coverage detected