test(companion): cover waitForIdle pending RPC lifecycle

This commit is contained in:
William Valentin
2026-02-16 21:58:36 -08:00
parent c2395ced75
commit 164db42d0f
2 changed files with 54 additions and 0 deletions
+43
View File
@@ -730,6 +730,49 @@ describe('CompanionRuntimeClient', () => {
await pendingWait;
});
it('waitForIdle resolves after pending RPC requests are rejected', 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,
requestTimeoutMs: 10_000,
});
await client.connect();
const pendingCall = client.call('system.capabilities').catch(() => undefined);
expect(client.pendingRequestCount).toBe(1);
const idle = client.waitForIdle({ timeoutMs: 1_000, pollIntervalMs: 5 });
setTimeout(() => {
client.disconnect();
}, 20);
await expect(idle).resolves.toBeUndefined();
await pendingCall;
expect(client.pendingRequestCount).toBe(0);
expect(client.hasPendingWork).toBe(false);
});
it('waitForIdle supports AbortSignal cancellation', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',