feat(companion): add waitForAnyEvent runtime helper
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user