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
+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',