feat(companion): expose pending request observability

This commit is contained in:
William Valentin
2026-02-16 20:51:57 -08:00
parent 5277fed220
commit df86fbd08e
6 changed files with 84 additions and 1 deletions
+13
View File
@@ -38,6 +38,7 @@ function createRuntimeMock(): {
waitForContextWarning: ReturnType<typeof vi.fn>;
waitForAnyEvent: ReturnType<typeof vi.fn>;
eventSubscriptionCount: number;
pendingRequestCount: number;
connected: boolean;
} {
const connect = vi.fn(async () => undefined);
@@ -77,6 +78,7 @@ function createRuntimeMock(): {
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
const waitForAnyEvent = vi.fn(async () => ({ event: 'agent.stream', data: { token: 'any' } }));
const eventSubscriptionCount = 3;
const pendingRequestCount = 2;
const connected = true;
const runtime = {
@@ -112,6 +114,9 @@ function createRuntimeMock(): {
get eventSubscriptionCount() {
return eventSubscriptionCount;
},
get pendingRequestCount() {
return pendingRequestCount;
},
get connected() {
return connected;
},
@@ -149,6 +154,7 @@ function createRuntimeMock(): {
waitForContextWarning,
waitForAnyEvent,
eventSubscriptionCount,
pendingRequestCount,
connected,
};
}
@@ -294,6 +300,13 @@ describe('platform companion clients', () => {
expect(client.eventSubscriptionCount).toBe(mock.eventSubscriptionCount);
});
it('platform pendingRequestCount forwards runtime getter value', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
expect(client.pendingRequestCount).toBe(mock.pendingRequestCount);
});
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
@@ -275,6 +275,10 @@ export class MacOSCompanionClient {
return this.runtime.eventSubscriptionCount;
}
get pendingRequestCount(): number {
return this.runtime.pendingRequestCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -517,6 +521,10 @@ export class IOSCompanionClient {
return this.runtime.eventSubscriptionCount;
}
get pendingRequestCount(): number {
return this.runtime.pendingRequestCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -757,6 +765,10 @@ export class AndroidCompanionClient {
return this.runtime.eventSubscriptionCount;
}
get pendingRequestCount(): number {
return this.runtime.pendingRequestCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
+38
View File
@@ -593,6 +593,44 @@ describe('CompanionRuntimeClient', () => {
);
});
it('tracks pendingRequestCount for in-flight RPCs', async () => {
class FakeWebSocket extends EventEmitter {
readyState: number = WebSocket.CONNECTING;
constructor() {
super();
queueMicrotask(() => {
this.readyState = WebSocket.OPEN;
this.emit('open');
});
}
send(_payload: string, callback?: (error?: Error) => void): void {
callback?.();
}
close(_code?: number, _reason?: string): void {
this.readyState = WebSocket.CLOSED;
this.emit('close');
}
}
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
websocketFactory: () => new FakeWebSocket() as unknown as WebSocket,
requestTimeoutMs: 10_000,
});
await client.connect();
expect(client.pendingRequestCount).toBe(0);
const pending = client.call('system.capabilities');
expect(client.pendingRequestCount).toBe(1);
client.disconnect();
await expect(pending).rejects.toThrow('Disconnected');
expect(client.pendingRequestCount).toBe(0);
});
it('connects and performs node registration + capability discovery', async () => {
if (!LISTEN_ALLOWED) {
return;
+4
View File
@@ -302,6 +302,10 @@ export class CompanionRuntimeClient {
return this.eventHandlers.size;
}
get pendingRequestCount(): number {
return this.pending.size;
}
async connect(): Promise<void> {
if (this.connected) {
return;