feat(companion): add pending work snapshot helper
This commit is contained in:
@@ -13,6 +13,7 @@ export { CompanionHeartbeatLoop } from './heartbeatLoop.js';
|
||||
export type {
|
||||
CompanionRuntimeClientOptions,
|
||||
WaitForIdleOptions,
|
||||
PendingWorkSnapshot,
|
||||
CompanionEventHandler,
|
||||
CompanionTypedEventHandler,
|
||||
CompanionEventName,
|
||||
|
||||
@@ -34,6 +34,7 @@ function createRuntimeMock(): {
|
||||
listKnownEventNames: ReturnType<typeof vi.fn>;
|
||||
waitForEvent: ReturnType<typeof vi.fn>;
|
||||
waitForIdle: ReturnType<typeof vi.fn>;
|
||||
getPendingWorkSnapshot: ReturnType<typeof vi.fn>;
|
||||
waitForAgentStream: ReturnType<typeof vi.fn>;
|
||||
waitForAgentTyping: ReturnType<typeof vi.fn>;
|
||||
waitForContextWarning: ReturnType<typeof vi.fn>;
|
||||
@@ -77,6 +78,11 @@ function createRuntimeMock(): {
|
||||
const listKnownEventNames = vi.fn(() => ['agent.stream', 'agent.typing', 'context_warning']);
|
||||
const waitForEvent = vi.fn(async () => ({ token: 'evented' }));
|
||||
const waitForIdle = vi.fn(async () => undefined);
|
||||
const getPendingWorkSnapshot = vi.fn(() => ({
|
||||
pendingRequestCount: 2,
|
||||
pendingEventWaitCount: 1,
|
||||
hasPendingWork: true,
|
||||
}));
|
||||
const waitForAgentStream = vi.fn(async () => ({ token: 'streamed' }));
|
||||
const waitForAgentTyping = vi.fn(async () => ({ active: true }));
|
||||
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
|
||||
@@ -114,6 +120,7 @@ function createRuntimeMock(): {
|
||||
listKnownEventNames,
|
||||
waitForEvent,
|
||||
waitForIdle,
|
||||
getPendingWorkSnapshot,
|
||||
waitForAgentStream,
|
||||
waitForAgentTyping,
|
||||
waitForContextWarning,
|
||||
@@ -163,6 +170,7 @@ function createRuntimeMock(): {
|
||||
listKnownEventNames,
|
||||
waitForEvent,
|
||||
waitForIdle,
|
||||
getPendingWorkSnapshot,
|
||||
waitForAgentStream,
|
||||
waitForAgentTyping,
|
||||
waitForContextWarning,
|
||||
@@ -346,6 +354,20 @@ describe('platform companion clients', () => {
|
||||
expect(mock.waitForIdle).toHaveBeenCalledWith({ timeoutMs: 250, pollIntervalMs: 10 });
|
||||
});
|
||||
|
||||
it('platform getPendingWorkSnapshot forwards to runtime client', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
|
||||
|
||||
const snapshot = client.getPendingWorkSnapshot();
|
||||
|
||||
expect(mock.getPendingWorkSnapshot).toHaveBeenCalledOnce();
|
||||
expect(snapshot).toEqual({
|
||||
pendingRequestCount: 2,
|
||||
pendingEventWaitCount: 1,
|
||||
hasPendingWork: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('macOS client forwards canvas methods to runtime client', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
|
||||
|
||||
@@ -18,6 +18,7 @@ import type {
|
||||
NodeRegisterResult,
|
||||
NodeStatusSetResult,
|
||||
PutCanvasArtifactInput,
|
||||
PendingWorkSnapshot,
|
||||
NodePushTokenSetResult,
|
||||
SetNodeLocationInput,
|
||||
SystemCapabilitiesResult,
|
||||
@@ -288,6 +289,10 @@ export class MacOSCompanionClient {
|
||||
return this.runtime.hasPendingWork;
|
||||
}
|
||||
|
||||
getPendingWorkSnapshot(): PendingWorkSnapshot {
|
||||
return this.runtime.getPendingWorkSnapshot();
|
||||
}
|
||||
|
||||
waitForAnyEvent<TData = unknown>(
|
||||
eventNames: readonly (CompanionEventName | string)[],
|
||||
options?: {
|
||||
@@ -546,6 +551,10 @@ export class IOSCompanionClient {
|
||||
return this.runtime.hasPendingWork;
|
||||
}
|
||||
|
||||
getPendingWorkSnapshot(): PendingWorkSnapshot {
|
||||
return this.runtime.getPendingWorkSnapshot();
|
||||
}
|
||||
|
||||
waitForAnyEvent<TData = unknown>(
|
||||
eventNames: readonly (CompanionEventName | string)[],
|
||||
options?: {
|
||||
@@ -802,6 +811,10 @@ export class AndroidCompanionClient {
|
||||
return this.runtime.hasPendingWork;
|
||||
}
|
||||
|
||||
getPendingWorkSnapshot(): PendingWorkSnapshot {
|
||||
return this.runtime.getPendingWorkSnapshot();
|
||||
}
|
||||
|
||||
waitForAnyEvent<TData = unknown>(
|
||||
eventNames: readonly (CompanionEventName | string)[],
|
||||
options?: {
|
||||
|
||||
@@ -643,6 +643,33 @@ describe('CompanionRuntimeClient', () => {
|
||||
expect(client.hasPendingWork).toBe(false);
|
||||
});
|
||||
|
||||
it('returns pending work snapshot', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
expect(client.getPendingWorkSnapshot()).toEqual({
|
||||
pendingRequestCount: 0,
|
||||
pendingEventWaitCount: 0,
|
||||
hasPendingWork: false,
|
||||
});
|
||||
|
||||
const pendingWait = client.waitForEvent('agent.stream', { timeoutMs: 10_000 }).catch(() => undefined);
|
||||
expect(client.getPendingWorkSnapshot()).toEqual({
|
||||
pendingRequestCount: 0,
|
||||
pendingEventWaitCount: 1,
|
||||
hasPendingWork: true,
|
||||
});
|
||||
|
||||
client.clearEventSubscriptions();
|
||||
await pendingWait;
|
||||
expect(client.getPendingWorkSnapshot()).toEqual({
|
||||
pendingRequestCount: 0,
|
||||
pendingEventWaitCount: 0,
|
||||
hasPendingWork: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('waitForIdle resolves immediately when no work is pending', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
|
||||
@@ -47,6 +47,12 @@ export interface WaitForIdleOptions {
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export interface PendingWorkSnapshot {
|
||||
pendingRequestCount: number;
|
||||
pendingEventWaitCount: number;
|
||||
hasPendingWork: boolean;
|
||||
}
|
||||
|
||||
export type CompanionEventHandler = (event: string, data: unknown) => void;
|
||||
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
||||
export type CompanionEventPredicate<TData = unknown> = (data: TData) => boolean;
|
||||
@@ -320,6 +326,14 @@ export class CompanionRuntimeClient {
|
||||
return this.pendingRequestCount > 0 || this.pendingEventWaitCount > 0;
|
||||
}
|
||||
|
||||
getPendingWorkSnapshot(): PendingWorkSnapshot {
|
||||
return {
|
||||
pendingRequestCount: this.pendingRequestCount,
|
||||
pendingEventWaitCount: this.pendingEventWaitCount,
|
||||
hasPendingWork: this.hasPendingWork,
|
||||
};
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this.connected) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user