Add canonical websocket method list and runtime coverage test

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 14:14:26 +00:00
1 parent e0a9865d02
commit 2462a90227
4 files changed
+121 -3

No files matched your search

+4 -3
View File
@@ -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<TValue>(
method: string,
method: WsNativeApiMethod,
schema: SchemaLike<TValue>,
params?: unknown,
): Promise<TValue> {
@@ -349,7 +350,7 @@ class WsNativeApiClient {
return parsed.data;
}
private async requestNullResult(method: string, params?: unknown): Promise<void> {
private async requestNullResult(method: WsNativeApiMethod, params?: unknown): Promise<void> {
const value = await this.request(method, params);
if (value !== null) {
throw new Error(`Runtime method '${method}' returned invalid response payload.`);
+75
View File
@@ -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<WsNativeApiMethod, unknown> = {
"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<WsServerMessage>,
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,
+18
View File
@@ -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);
});
});
+24
View File
@@ -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[]]);