feat(companion): add waitForEvent runtime helper
This commit is contained in:
@@ -187,6 +187,48 @@ describe('CompanionRuntimeClient', () => {
|
||||
expect(streamHandler).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('waitForEvent resolves using optional predicate filter', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
const awaited = client.waitForEvent<{ seq: number }>('agent.stream', {
|
||||
timeoutMs: 2000,
|
||||
predicate: (data) => data.seq === 2,
|
||||
});
|
||||
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 48,
|
||||
event: 'agent.stream',
|
||||
data: { seq: 1 },
|
||||
}),
|
||||
);
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 49,
|
||||
event: 'agent.stream',
|
||||
data: { seq: 2 },
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(awaited).resolves.toEqual({ seq: 2 });
|
||||
});
|
||||
|
||||
it('waitForEvent rejects on timeout', async () => {
|
||||
vi.useFakeTimers();
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
const awaited = expect(
|
||||
client.waitForEvent('agent.stream', { timeoutMs: 100 }),
|
||||
).rejects.toThrow('Timed out waiting for event agent.stream');
|
||||
await vi.advanceTimersByTimeAsync(100);
|
||||
await awaited;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('connects and performs node registration + capability discovery', async () => {
|
||||
if (!LISTEN_ALLOWED) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user