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
+13
View File
@@ -31,6 +31,7 @@ function createRuntimeMock(): {
subscribeEvents: ReturnType<typeof vi.fn>;
subscribeEvent: ReturnType<typeof vi.fn>;
clearEventSubscriptions: ReturnType<typeof vi.fn>;
cancelPendingEventWaits: ReturnType<typeof vi.fn>;
listKnownEventNames: ReturnType<typeof vi.fn>;
waitForEvent: ReturnType<typeof vi.fn>;
waitForIdle: ReturnType<typeof vi.fn>;
@@ -76,6 +77,7 @@ function createRuntimeMock(): {
const subscribeEvents = vi.fn(() => () => undefined);
const subscribeEvent = vi.fn(() => () => undefined);
const clearEventSubscriptions = vi.fn(() => undefined);
const cancelPendingEventWaits = vi.fn(() => undefined);
const listKnownEventNames = vi.fn(() => ['agent.stream', 'agent.typing', 'context_warning']);
const waitForEvent = vi.fn(async () => ({ token: 'evented' }));
const waitForIdle = vi.fn(async () => undefined);
@@ -119,6 +121,7 @@ function createRuntimeMock(): {
subscribeEvents,
subscribeEvent,
clearEventSubscriptions,
cancelPendingEventWaits,
listKnownEventNames,
waitForEvent,
waitForIdle,
@@ -172,6 +175,7 @@ function createRuntimeMock(): {
subscribeEvents,
subscribeEvent,
clearEventSubscriptions,
cancelPendingEventWaits,
listKnownEventNames,
waitForEvent,
waitForIdle,
@@ -313,6 +317,15 @@ describe('platform companion clients', () => {
expect(mock.clearEventSubscriptions).toHaveBeenCalledOnce();
});
it('platform cancelPendingEventWaits forwards to runtime client', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
client.cancelPendingEventWaits('manual');
expect(mock.cancelPendingEventWaits).toHaveBeenCalledWith('manual');
});
it('platform listKnownEventNames forwards to runtime client', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
+12
View File
@@ -269,6 +269,10 @@ export class MacOSCompanionClient {
this.runtime.clearEventSubscriptions();
}
cancelPendingEventWaits(reason?: string): void {
this.runtime.cancelPendingEventWaits(reason);
}
listKnownEventNames(): CompanionEventName[] {
return this.runtime.listKnownEventNames();
}
@@ -535,6 +539,10 @@ export class IOSCompanionClient {
this.runtime.clearEventSubscriptions();
}
cancelPendingEventWaits(reason?: string): void {
this.runtime.cancelPendingEventWaits(reason);
}
listKnownEventNames(): CompanionEventName[] {
return this.runtime.listKnownEventNames();
}
@@ -799,6 +807,10 @@ export class AndroidCompanionClient {
this.runtime.clearEventSubscriptions();
}
cancelPendingEventWaits(reason?: string): void {
this.runtime.cancelPendingEventWaits(reason);
}
listKnownEventNames(): CompanionEventName[] {
return this.runtime.listKnownEventNames();
}
+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',
+4
View File
@@ -435,6 +435,10 @@ export class CompanionRuntimeClient {
this.rejectEventWaits(new Error('Event subscriptions cleared'));
}
cancelPendingEventWaits(reason = 'Event waits cancelled'): void {
this.rejectEventWaits(new Error(reason));
}
subscribeEvent<TData = unknown>(
eventName: CompanionEventName | string,
handler: CompanionTypedEventHandler<TData>,