feat(companion): add hasPendingWork runtime observability

This commit is contained in:
William Valentin
2026-02-16 20:54:23 -08:00
parent 7e556c5815
commit d14f82cd84
6 changed files with 52 additions and 2 deletions
+13
View File
@@ -40,6 +40,7 @@ function createRuntimeMock(): {
eventSubscriptionCount: number;
pendingRequestCount: number;
pendingEventWaitCount: number;
hasPendingWork: boolean;
connected: boolean;
} {
const connect = vi.fn(async () => undefined);
@@ -81,6 +82,7 @@ function createRuntimeMock(): {
const eventSubscriptionCount = 3;
const pendingRequestCount = 2;
const pendingEventWaitCount = 1;
const hasPendingWork = true;
const connected = true;
const runtime = {
@@ -122,6 +124,9 @@ function createRuntimeMock(): {
get pendingEventWaitCount() {
return pendingEventWaitCount;
},
get hasPendingWork() {
return hasPendingWork;
},
get connected() {
return connected;
},
@@ -161,6 +166,7 @@ function createRuntimeMock(): {
eventSubscriptionCount,
pendingRequestCount,
pendingEventWaitCount,
hasPendingWork,
connected,
};
}
@@ -320,6 +326,13 @@ describe('platform companion clients', () => {
expect(client.pendingEventWaitCount).toBe(mock.pendingEventWaitCount);
});
it('platform hasPendingWork forwards runtime getter value', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
expect(client.hasPendingWork).toBe(mock.hasPendingWork);
});
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
@@ -283,6 +283,10 @@ export class MacOSCompanionClient {
return this.runtime.pendingEventWaitCount;
}
get hasPendingWork(): boolean {
return this.runtime.hasPendingWork;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -533,6 +537,10 @@ export class IOSCompanionClient {
return this.runtime.pendingEventWaitCount;
}
get hasPendingWork(): boolean {
return this.runtime.hasPendingWork;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -781,6 +789,10 @@ export class AndroidCompanionClient {
return this.runtime.pendingEventWaitCount;
}
get hasPendingWork(): boolean {
return this.runtime.hasPendingWork;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
+6
View File
@@ -306,12 +306,14 @@ describe('CompanionRuntimeClient', () => {
url: 'ws://127.0.0.1:1',
});
expect(client.pendingEventWaitCount).toBe(0);
expect(client.hasPendingWork).toBe(false);
const awaited = client.waitForEvent<{ seq: number }>('agent.stream', {
timeoutMs: 2000,
predicate: (data) => data.seq === 2,
});
expect(client.pendingEventWaitCount).toBe(1);
expect(client.hasPendingWork).toBe(true);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
@@ -330,6 +332,7 @@ describe('CompanionRuntimeClient', () => {
await expect(awaited).resolves.toEqual({ seq: 2 });
expect(client.pendingEventWaitCount).toBe(0);
expect(client.hasPendingWork).toBe(false);
});
it('waitForEvent rejects on timeout', async () => {
@@ -628,13 +631,16 @@ describe('CompanionRuntimeClient', () => {
});
await client.connect();
expect(client.pendingRequestCount).toBe(0);
expect(client.hasPendingWork).toBe(false);
const pending = client.call('system.capabilities');
expect(client.pendingRequestCount).toBe(1);
expect(client.hasPendingWork).toBe(true);
client.disconnect();
await expect(pending).rejects.toThrow('Disconnected');
expect(client.pendingRequestCount).toBe(0);
expect(client.hasPendingWork).toBe(false);
});
it('connects and performs node registration + capability discovery', async () => {
+4
View File
@@ -310,6 +310,10 @@ export class CompanionRuntimeClient {
return this.pendingEventWaits.size;
}
get hasPendingWork(): boolean {
return this.pendingRequestCount > 0 || this.pendingEventWaitCount > 0;
}
async connect(): Promise<void> {
if (this.connected) {
return;