feat(companion): add typed canvas RPC helpers for runtime clients

This commit is contained in:
William Valentin
2026-02-16 18:33:59 -08:00
parent 2e07ae44a3
commit 97afc39e01
8 changed files with 268 additions and 2 deletions
+45
View File
@@ -18,6 +18,11 @@ function createRuntimeMock(): {
setNodePushToken: ReturnType<typeof vi.fn>;
getSystemCapabilities: ReturnType<typeof vi.fn>;
listSystemNodes: ReturnType<typeof vi.fn>;
putCanvasArtifact: ReturnType<typeof vi.fn>;
getCanvasArtifact: ReturnType<typeof vi.fn>;
listCanvasArtifacts: ReturnType<typeof vi.fn>;
deleteCanvasArtifact: ReturnType<typeof vi.fn>;
clearCanvasArtifacts: ReturnType<typeof vi.fn>;
} {
const connect = vi.fn(async () => undefined);
const disconnect = vi.fn(() => undefined);
@@ -29,6 +34,11 @@ function createRuntimeMock(): {
const setNodePushToken = vi.fn(async () => ({ updated: true }));
const getSystemCapabilities = vi.fn(async () => ({ protocol: { version: 1 }, nodes: { enabled: true, locationEnabled: true, pushEnabled: true, allowedRoles: ['companion'], registered: true }, featureGates: {} }));
const listSystemNodes = vi.fn(async () => ({ nodes: [], summary: { total: 0 } }));
const putCanvasArtifact = vi.fn(async () => ({ upserted: true, artifact: { id: 'a1' } }));
const getCanvasArtifact = vi.fn(async () => ({ artifact: { id: 'a1' } }));
const listCanvasArtifacts = vi.fn(async () => ({ artifacts: [{ id: 'a1' }] }));
const deleteCanvasArtifact = vi.fn(async () => ({ deleted: true }));
const clearCanvasArtifacts = vi.fn(async () => ({ cleared: 1 }));
const runtime = {
connect,
@@ -41,6 +51,11 @@ function createRuntimeMock(): {
setNodePushToken,
getSystemCapabilities,
listSystemNodes,
putCanvasArtifact,
getCanvasArtifact,
listCanvasArtifacts,
deleteCanvasArtifact,
clearCanvasArtifacts,
} as unknown as CompanionRuntimeClient;
return {
@@ -55,6 +70,11 @@ function createRuntimeMock(): {
setNodePushToken,
getSystemCapabilities,
listSystemNodes,
putCanvasArtifact,
getCanvasArtifact,
listCanvasArtifacts,
deleteCanvasArtifact,
clearCanvasArtifacts,
};
}
@@ -106,4 +126,29 @@ describe('platform companion clients', () => {
expect(mock.setNodePushToken).toHaveBeenCalledWith({ provider: 'fcm', token: 'c'.repeat(64) });
expect(mock.listSystemNodes).toHaveBeenCalledWith({ platform: 'android', role: 'companion' });
});
it('macOS client forwards canvas methods to runtime client', async () => {
const mock = createRuntimeMock();
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
await client.putCanvasArtifact({
sessionId: 'ws:test-canvas',
type: 'markdown',
content: { body: 'hello' },
});
await client.listCanvasArtifacts('ws:test-canvas');
await client.getCanvasArtifact({ sessionId: 'ws:test-canvas', artifactId: 'a1' });
await client.deleteCanvasArtifact({ sessionId: 'ws:test-canvas', artifactId: 'a1' });
await client.clearCanvasArtifacts('ws:test-canvas');
expect(mock.putCanvasArtifact).toHaveBeenCalledWith({
sessionId: 'ws:test-canvas',
type: 'markdown',
content: { body: 'hello' },
});
expect(mock.listCanvasArtifacts).toHaveBeenCalledWith('ws:test-canvas');
expect(mock.getCanvasArtifact).toHaveBeenCalledWith({ sessionId: 'ws:test-canvas', artifactId: 'a1' });
expect(mock.deleteCanvasArtifact).toHaveBeenCalledWith({ sessionId: 'ws:test-canvas', artifactId: 'a1' });
expect(mock.clearCanvasArtifacts).toHaveBeenCalledWith('ws:test-canvas');
});
});