feat(companion): add waitForAnyEvent runtime helper

This commit is contained in:
William Valentin
2026-02-16 19:27:25 -08:00
parent f7c6947d22
commit 717e5d60e5
5 changed files with 156 additions and 1 deletions
+66
View File
@@ -416,6 +416,72 @@ describe('CompanionRuntimeClient', () => {
await expect(awaited).resolves.toEqual({ thresholdPct: 75, estimatedPct: 88 });
});
it('waitForAnyEvent resolves with event envelope for first matching event', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const awaited = client.waitForAnyEvent<{ active?: boolean; token?: string }>(
['agent.typing', 'agent.stream'],
{ timeoutMs: 2000 },
);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 58,
event: 'agent.typing',
data: { active: true },
}),
);
await expect(awaited).resolves.toEqual({
event: 'agent.typing',
data: { active: true },
});
});
it('waitForAnyEvent supports per-event predicate filtering', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const awaited = client.waitForAnyEvent<{ token?: string }>(
['agent.stream'],
{
timeoutMs: 2000,
predicate: (_event, data) => data.token === 'accept',
},
);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 59,
event: 'agent.stream',
data: { token: 'skip' },
}),
);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 60,
event: 'agent.stream',
data: { token: 'accept' },
}),
);
await expect(awaited).resolves.toEqual({
event: 'agent.stream',
data: { token: 'accept' },
});
});
it('waitForAnyEvent validates input event list', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
expect(() => client.waitForAnyEvent([])).toThrow(
'eventNames must contain at least one event name',
);
});
it('connects and performs node registration + capability discovery', async () => {
if (!LISTEN_ALLOWED) {
return;