feat(companion): add runtime event subscription hooks
This commit is contained in:
@@ -96,6 +96,61 @@ afterAll(async () => {
|
||||
});
|
||||
|
||||
describe('CompanionRuntimeClient', () => {
|
||||
it('dispatches gateway events to subscribed handlers and supports unsubscribe', () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
const handler = vi.fn();
|
||||
const unsubscribe = client.subscribeEvents(handler);
|
||||
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 42,
|
||||
event: 'agent.stream',
|
||||
data: { token: 'hello' },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith('agent.stream', { token: 'hello' });
|
||||
|
||||
unsubscribe();
|
||||
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 43,
|
||||
event: 'agent.stream',
|
||||
data: { token: 'world' },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('isolates subscriber callback failures', () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
const badHandler = vi.fn(() => {
|
||||
throw new Error('subscriber failed');
|
||||
});
|
||||
const goodHandler = vi.fn();
|
||||
client.subscribeEvents(badHandler);
|
||||
client.subscribeEvents(goodHandler);
|
||||
|
||||
expect(() => {
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 44,
|
||||
event: 'agent.stream',
|
||||
data: { token: 'safe' },
|
||||
}),
|
||||
);
|
||||
}).not.toThrow();
|
||||
|
||||
expect(badHandler).toHaveBeenCalledOnce();
|
||||
expect(goodHandler).toHaveBeenCalledWith('agent.stream', { token: 'safe' });
|
||||
});
|
||||
|
||||
it('connects and performs node registration + capability discovery', async () => {
|
||||
if (!LISTEN_ALLOWED) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user