feat(companion): add event surface snapshot helper

This commit is contained in:
William Valentin
2026-02-16 22:22:18 -08:00
parent a6e9daaaef
commit 29523bcdac
7 changed files with 86 additions and 2 deletions
+1
View File
@@ -14,6 +14,7 @@ export type {
CompanionRuntimeClientOptions,
WaitForIdleOptions,
PendingWorkSnapshot,
EventSurfaceSnapshot,
CompanionEventHandler,
CompanionTypedEventHandler,
CompanionEventName,
+22
View File
@@ -36,6 +36,7 @@ function createRuntimeMock(): {
waitForEvent: ReturnType<typeof vi.fn>;
waitForIdle: ReturnType<typeof vi.fn>;
getPendingWorkSnapshot: ReturnType<typeof vi.fn>;
getEventSurfaceSnapshot: ReturnType<typeof vi.fn>;
waitForAgentStream: ReturnType<typeof vi.fn>;
waitForAgentTyping: ReturnType<typeof vi.fn>;
waitForContextWarning: ReturnType<typeof vi.fn>;
@@ -86,6 +87,11 @@ function createRuntimeMock(): {
pendingEventWaitCount: 1,
hasPendingWork: true,
}));
const getEventSurfaceSnapshot = vi.fn(() => ({
knownEventNames: ['agent.stream', 'agent.typing', 'context_warning'],
eventSubscriptionCount: 3,
pendingEventWaitCount: 1,
}));
const waitForAgentStream = vi.fn(async () => ({ token: 'streamed' }));
const waitForAgentTyping = vi.fn(async () => ({ active: true }));
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
@@ -126,6 +132,7 @@ function createRuntimeMock(): {
waitForEvent,
waitForIdle,
getPendingWorkSnapshot,
getEventSurfaceSnapshot,
waitForAgentStream,
waitForAgentTyping,
waitForContextWarning,
@@ -180,6 +187,7 @@ function createRuntimeMock(): {
waitForEvent,
waitForIdle,
getPendingWorkSnapshot,
getEventSurfaceSnapshot,
waitForAgentStream,
waitForAgentTyping,
waitForContextWarning,
@@ -395,6 +403,20 @@ describe('platform companion clients', () => {
});
});
it('platform getEventSurfaceSnapshot forwards to runtime client', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
const snapshot = client.getEventSurfaceSnapshot();
expect(mock.getEventSurfaceSnapshot).toHaveBeenCalledOnce();
expect(snapshot).toEqual({
knownEventNames: ['agent.stream', 'agent.typing', 'context_warning'],
eventSubscriptionCount: 3,
pendingEventWaitCount: 1,
});
});
it('macOS client forwards canvas methods to runtime client', async () => {
const mock = createRuntimeMock();
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
+13
View File
@@ -1,6 +1,7 @@
import type {
CompanionEventName,
CompanionEventEnvelope,
EventSurfaceSnapshot,
CompanionEventHandler,
CanvasClearResult,
CanvasDeleteResult,
@@ -301,6 +302,10 @@ export class MacOSCompanionClient {
return this.runtime.getPendingWorkSnapshot();
}
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
return this.runtime.getEventSurfaceSnapshot();
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -571,6 +576,10 @@ export class IOSCompanionClient {
return this.runtime.getPendingWorkSnapshot();
}
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
return this.runtime.getEventSurfaceSnapshot();
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -839,6 +848,10 @@ export class AndroidCompanionClient {
return this.runtime.getPendingWorkSnapshot();
}
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
return this.runtime.getEventSurfaceSnapshot();
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
+18
View File
@@ -703,6 +703,24 @@ describe('CompanionRuntimeClient', () => {
});
});
it('returns event surface snapshot', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const pendingWait = client.waitForEvent('agent.stream', { timeoutMs: 10_000 }).catch(() => undefined);
const unsubscribe = client.subscribeEvents(() => undefined);
expect(client.getEventSurfaceSnapshot()).toEqual({
knownEventNames: ['agent.stream', 'agent.typing', 'context_warning'],
eventSubscriptionCount: 2,
pendingEventWaitCount: 1,
});
unsubscribe();
client.clearEventSubscriptions();
await pendingWait;
});
it('waitForIdle resolves immediately when no work is pending', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
+14
View File
@@ -53,6 +53,12 @@ export interface PendingWorkSnapshot {
hasPendingWork: boolean;
}
export interface EventSurfaceSnapshot {
knownEventNames: CompanionEventName[];
eventSubscriptionCount: number;
pendingEventWaitCount: number;
}
export type CompanionEventHandler = (event: string, data: unknown) => void;
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
export type CompanionEventPredicate<TData = unknown> = (data: TData) => boolean;
@@ -338,6 +344,14 @@ export class CompanionRuntimeClient {
};
}
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
return {
knownEventNames: this.listKnownEventNames(),
eventSubscriptionCount: this.eventSubscriptionCount,
pendingEventWaitCount: this.pendingEventWaitCount,
};
}
async connect(): Promise<void> {
if (this.connected) {
return;