feat(companion): expose pending event wait observability

This commit is contained in:
William Valentin
2026-02-16 20:52:49 -08:00
parent df86fbd08e
commit 21a57c88b9
6 changed files with 49 additions and 2 deletions
+13
View File
@@ -39,6 +39,7 @@ function createRuntimeMock(): {
waitForAnyEvent: ReturnType<typeof vi.fn>;
eventSubscriptionCount: number;
pendingRequestCount: number;
pendingEventWaitCount: number;
connected: boolean;
} {
const connect = vi.fn(async () => undefined);
@@ -79,6 +80,7 @@ function createRuntimeMock(): {
const waitForAnyEvent = vi.fn(async () => ({ event: 'agent.stream', data: { token: 'any' } }));
const eventSubscriptionCount = 3;
const pendingRequestCount = 2;
const pendingEventWaitCount = 1;
const connected = true;
const runtime = {
@@ -117,6 +119,9 @@ function createRuntimeMock(): {
get pendingRequestCount() {
return pendingRequestCount;
},
get pendingEventWaitCount() {
return pendingEventWaitCount;
},
get connected() {
return connected;
},
@@ -155,6 +160,7 @@ function createRuntimeMock(): {
waitForAnyEvent,
eventSubscriptionCount,
pendingRequestCount,
pendingEventWaitCount,
connected,
};
}
@@ -307,6 +313,13 @@ describe('platform companion clients', () => {
expect(client.pendingRequestCount).toBe(mock.pendingRequestCount);
});
it('platform pendingEventWaitCount forwards runtime getter value', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
expect(client.pendingEventWaitCount).toBe(mock.pendingEventWaitCount);
});
it('macOS client forwards canvas methods to runtime client', async () => {
const mock = createRuntimeMock();
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
+12
View File
@@ -279,6 +279,10 @@ export class MacOSCompanionClient {
return this.runtime.pendingRequestCount;
}
get pendingEventWaitCount(): number {
return this.runtime.pendingEventWaitCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -525,6 +529,10 @@ export class IOSCompanionClient {
return this.runtime.pendingRequestCount;
}
get pendingEventWaitCount(): number {
return this.runtime.pendingEventWaitCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -769,6 +777,10 @@ export class AndroidCompanionClient {
return this.runtime.pendingRequestCount;
}
get pendingEventWaitCount(): number {
return this.runtime.pendingEventWaitCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
+3
View File
@@ -305,11 +305,13 @@ describe('CompanionRuntimeClient', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
expect(client.pendingEventWaitCount).toBe(0);
const awaited = client.waitForEvent<{ seq: number }>('agent.stream', {
timeoutMs: 2000,
predicate: (data) => data.seq === 2,
});
expect(client.pendingEventWaitCount).toBe(1);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
@@ -327,6 +329,7 @@ describe('CompanionRuntimeClient', () => {
);
await expect(awaited).resolves.toEqual({ seq: 2 });
expect(client.pendingEventWaitCount).toBe(0);
});
it('waitForEvent rejects on timeout', async () => {
+4
View File
@@ -306,6 +306,10 @@ export class CompanionRuntimeClient {
return this.pending.size;
}
get pendingEventWaitCount(): number {
return this.pendingEventWaits.size;
}
async connect(): Promise<void> {
if (this.connected) {
return;