feat(companion): add cancellable pending event wait helper

This commit is contained in:
William Valentin
2026-02-16 22:18:17 -08:00
parent c41332a643
commit c4a8d099d6
6 changed files with 69 additions and 2 deletions
+23
View File
@@ -389,6 +389,29 @@ describe('CompanionRuntimeClient', () => {
await awaited;
});
it('cancelPendingEventWaits rejects waiters without clearing subscriptions', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const handler = vi.fn();
client.subscribeEvents(handler);
const awaited = expect(
client.waitForEvent('agent.stream', { timeoutMs: 10_000 }),
).rejects.toThrow('manually cancelled');
client.cancelPendingEventWaits('manually cancelled');
await awaited;
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 99,
event: 'agent.stream',
data: { token: 'still-subscribed' },
}),
);
expect(handler).toHaveBeenCalledWith('agent.stream', { token: 'still-subscribed' });
});
it('waitForEvent rejects immediately on disconnect', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',