From 2462a90227b0f57504bfacac832c2942170a5faf Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 14:14:26 +0000 Subject: [PATCH] Add canonical websocket method list and runtime coverage test Co-authored-by: Theo Browne --- apps/renderer/src/wsNativeApi.ts | 7 +-- apps/t3/src/runtimeApiServer.test.ts | 75 ++++++++++++++++++++++++++++ packages/contracts/src/ipc.test.ts | 18 +++++++ packages/contracts/src/ipc.ts | 24 +++++++++ 4 files changed, 121 insertions(+), 3 deletions(-) diff --git a/apps/renderer/src/wsNativeApi.ts b/apps/renderer/src/wsNativeApi.ts index 0e401534b9..e5ef553ab7 100644 --- a/apps/renderer/src/wsNativeApi.ts +++ b/apps/renderer/src/wsNativeApi.ts @@ -3,6 +3,7 @@ import type { NativeApi, OutputChunk, ProviderEvent, + WsNativeApiMethod, WsClientMessage, WsEventMessage, WsResponseMessage, @@ -288,7 +289,7 @@ class WsNativeApiClient { return connectAttempt; } - private async request(method: string, params?: unknown) { + private async request(method: WsNativeApiMethod, params?: unknown) { const socket = await this.connect(); const id = String(this.nextRequestId); this.nextRequestId += 1; @@ -337,7 +338,7 @@ class WsNativeApiClient { } private async requestParsed( - method: string, + method: WsNativeApiMethod, schema: SchemaLike, params?: unknown, ): Promise { @@ -349,7 +350,7 @@ class WsNativeApiClient { return parsed.data; } - private async requestNullResult(method: string, params?: unknown): Promise { + private async requestNullResult(method: WsNativeApiMethod, params?: unknown): Promise { const value = await this.request(method, params); if (value !== null) { throw new Error(`Runtime method '${method}' returned invalid response payload.`); diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index 74f86427a7..167f474178 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -6,6 +6,7 @@ import { afterEach, describe, expect, it } from "vitest"; import { WebSocket } from "ws"; import { + WS_NATIVE_API_METHODS, WS_METHOD_MAX_CHARS, WS_REQUEST_ID_MAX_CHARS, WS_ERROR_MESSAGE_MAX_CHARS, @@ -113,6 +114,38 @@ async function sendRequest( return waitForMatchingResponse(); } +type WsNativeApiMethod = (typeof WS_NATIVE_API_METHODS)[number]; + +const METHOD_COVERAGE_PARAMS: Record = { + "app.bootstrap": undefined, + "app.health": undefined, + "todos.list": undefined, + "todos.add": { text: "coverage todo" }, + "todos.toggle": "missing-todo-id", + "todos.remove": "missing-todo-id", + "dialogs.pickFolder": undefined, + "terminal.run": { command: "echo runtime-method-coverage" }, + "agent.spawn": { + command: process.execPath, + args: ["-e", "process.exit(0)"], + }, + "agent.kill": "missing-session-id", + "agent.write": { + sessionId: "missing-session-id", + data: "coverage", + }, + "providers.startSession": null, + "providers.sendTurn": null, + "providers.interruptTurn": null, + "providers.respondToRequest": null, + "providers.stopSession": null, + "providers.listSessions": undefined, + "shell.openInEditor": { + cwd: process.cwd(), + editor: "file-manager", + }, +}; + async function waitForAgentEvent( nextMessage: () => Promise, channel: string, @@ -1830,6 +1863,48 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it( + "supports every websocket native API method declared in contracts", + async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const responses: WsResponseMessage[] = []; + await WS_NATIVE_API_METHODS.reduce( + (chain, method, index) => + chain.then(async () => { + const response = await sendRequest( + client.socket, + client.nextMessage, + `method-coverage-${index}`, + method, + METHOD_COVERAGE_PARAMS[method], + ); + responses.push(response); + }), + Promise.resolve(), + ); + + for (const response of responses) { + if (response.ok) { + continue; + } + + expect(response.error?.code).toBe("request_failed"); + expect(response.error?.message).not.toContain("Unknown API method"); + } + + client.socket.close(); + }, + 20_000, + ); + it("returns structured errors for unknown methods at max method length", async () => { const server = await startRuntimeApiServer({ port: 0, diff --git a/packages/contracts/src/ipc.test.ts b/packages/contracts/src/ipc.test.ts index e41514f0e5..5a514edae4 100644 --- a/packages/contracts/src/ipc.test.ts +++ b/packages/contracts/src/ipc.test.ts @@ -6,6 +6,8 @@ import { dialogsPickFolderResultSchema, editorIdSchema, shellOpenInEditorInputSchema, + WS_NATIVE_API_METHODS, + wsNativeApiMethodSchema, } from "./ipc"; describe("appBootstrapResultSchema", () => { @@ -188,3 +190,19 @@ describe("editor and shell-open schemas", () => { ).toThrow(); }); }); + +describe("wsNativeApiMethodSchema", () => { + it("accepts every declared websocket native API method", () => { + for (const method of WS_NATIVE_API_METHODS) { + expect(wsNativeApiMethodSchema.parse(method)).toBe(method); + } + }); + + it("rejects unknown websocket native API methods", () => { + expect(() => wsNativeApiMethodSchema.parse("providers.abortTurn")).toThrow(); + }); + + it("keeps declared websocket native API methods unique", () => { + expect(new Set(WS_NATIVE_API_METHODS).size).toBe(WS_NATIVE_API_METHODS.length); + }); +}); diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index b67aee46f7..9e0e2226cd 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -19,6 +19,30 @@ export const EDITORS = [ { id: "file-manager", label: "File Manager", command: null }, ] as const; +export const WS_NATIVE_API_METHODS = [ + "app.bootstrap", + "app.health", + "todos.list", + "todos.add", + "todos.toggle", + "todos.remove", + "dialogs.pickFolder", + "terminal.run", + "agent.spawn", + "agent.kill", + "agent.write", + "providers.startSession", + "providers.sendTurn", + "providers.interruptTurn", + "providers.respondToRequest", + "providers.stopSession", + "providers.listSessions", + "shell.openInEditor", +] as const; + +export type WsNativeApiMethod = (typeof WS_NATIVE_API_METHODS)[number]; +export const wsNativeApiMethodSchema = z.enum(WS_NATIVE_API_METHODS); + export type EditorId = (typeof EDITORS)[number]["id"]; export const editorIdSchema = z.enum(EDITORS.map((entry) => entry.id) as [EditorId, ...EditorId[]]);