test(companion): cover waitForAnyEvent socket-close rejection

This commit is contained in:
William Valentin
2026-02-16 19:42:52 -08:00
parent 8837843df1
commit 4f6904c395
2 changed files with 49 additions and 0 deletions
+11
View File
@@ -785,6 +785,17 @@
],
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/heartbeatLoop.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"companion-runtime-wait-for-any-close-rejection-coverage": {
"status": "completed",
"date": "2026-02-17",
"updated": "2026-02-17",
"summary": "Added regression coverage proving `waitForAnyEvent()` rejects with `WebSocket closed` when the runtime socket closes unexpectedly.",
"files_modified": [
"src/companion/runtimeClient.test.ts",
"docs/plans/state.json"
],
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/heartbeatLoop.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"browser-tools-activation-clarity": {
"status": "completed",
"date": "2026-02-17",
+38
View File
@@ -418,6 +418,44 @@ describe('CompanionRuntimeClient', () => {
expect(client.connected).toBe(false);
});
it('waitForAnyEvent rejects when websocket closes unexpectedly', async () => {
class FakeWebSocket extends EventEmitter {
readyState: number = WebSocket.CONNECTING;
constructor() {
super();
queueMicrotask(() => {
this.readyState = WebSocket.OPEN;
this.emit('open');
});
}
send(_payload: string, callback?: (error?: Error) => void): void {
callback?.();
}
close(_code?: number, _reason?: string): void {
this.readyState = WebSocket.CLOSED;
this.emit('close');
}
}
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
websocketFactory: () => new FakeWebSocket() as unknown as WebSocket,
});
await client.connect();
const awaited = expect(
client.waitForAnyEvent(['agent.stream', 'agent.typing'], { timeoutMs: 10_000 }),
).rejects.toThrow('WebSocket closed');
const ws = (client as unknown as { ws: WebSocket | null }).ws;
ws?.close();
await awaited;
expect(client.connected).toBe(false);
});
it('waitForAgentStream resolves on agent.stream events', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',