返回 CodeWhale
server-routes.test.mjs
根目录 / crates / tui / plugins / computer-use / tests / server-routes.test.mjs
1 // Real MCP + real transports, with HDC/SSH executables or local backends replaced.
2 // Every catalog, downloaded byte and command log belongs to this fixture.
3 import { test } from "node:test";
4 import assert from "node:assert/strict";
5 import { spawn, spawnSync } from "node:child_process";
6 import { pathToFileURL } from "node:url";
7 import { once } from "node:events";
8 import { createInterface } from "node:readline";
9 import fs from "node:fs";
10 import os from "node:os";
11 import path from "node:path";
12 import { setTimeout as delay } from "node:timers/promises";
13 import { routeFingerprint } from "../src/transport.mjs";
14
15 const ROOT = path.resolve(import.meta.dirname, "..");
16
17 // Every control/catalog write the child or server reads must be atomic:
18 // a plain writeFileSync is observable mid-write by the polling readers and
19 // surfaces as "Unexpected end of JSON input" instead of the fixture's error.
20 function writeJsonAtomic(file, value) {
21 const tmp = `${file}.${process.pid}.tmp`;
22 fs.writeFileSync(tmp, JSON.stringify(value));
23 fs.renameSync(tmp, file);
24 }
25
26 function fixture(t, backendSource, sshSource) {
27 const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cu-route-"));
28 const log = path.join(dir, "calls.jsonl");
29 const control = path.join(dir, "control.json");
30 const bin = path.join(dir, "bin");
31 fs.mkdirSync(bin);
32 fs.writeFileSync(path.join(bin, "hdc.cjs"), `#!${process.execPath}
33 const fs = require('node:fs');
34 const args = process.argv.slice(2);
35 const target = args[0] === '-t' ? args.splice(0, 2)[1] : 'default';
36 fs.appendFileSync(process.env.ROUTE_LOG, JSON.stringify({target,args}) + '\\n');
37 const control = fs.existsSync(process.env.ROUTE_CONTROL) ? JSON.parse(fs.readFileSync(process.env.ROUTE_CONTROL)) : {};
38 if (control.fail === target) process.exit(7);
39 if (args[0] === 'list') console.log(target);
40 if (args[0] === 'file' && args[1] === 'recv') {
41 if (control.changeTo) {
42 const file = process.env.CODEWHALE_CU_STATE_DIR + '/computers.json';
43 const catalog = JSON.parse(fs.readFileSync(file));
44 catalog.computers.pad.target = control.changeTo;
45 const tmp = file + '.' + process.pid + '.tmp';
46 fs.writeFileSync(tmp, JSON.stringify(catalog));
47 fs.renameSync(tmp, file);
48 }
49 const layout = { attributes: { bundleName: target, type: 'Button', text: 'OK', bounds: '[0,0][20,20]' } };
50 const jpeg = Buffer.from([255,216,255,192,0,11,8,0,120,0,168,1,1,17,0,255,217]);
51 fs.writeFileSync(args[3], args[2].includes('layout') ? JSON.stringify(layout) : jpeg);
52 }
53 `, { mode: 0o755 });
54 if (sshSource) fs.writeFileSync(path.join(bin, "ssh.cjs"), `#!${process.execPath}\n${sshSource}`, { mode: 0o755 });
55 const env = { ...process.env, PATH: `${bin}${path.delimiter}${process.env.PATH}`,
56 CU_COMMAND_FIXTURES: bin,
57 NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ""} --import=${pathToFileURL(path.join(ROOT, "tests/fixtures/command-shims.mjs")).href}`,
58 ROUTE_LOG: log, ROUTE_CONTROL: control, CODEWHALE_CU_APP: "off", CODEWHALE_CU_APP_WARM: "off",
59 CODEWHALE_CU_STATE_DIR: dir, CODEWHALE_CU_RECORDINGS_DIR: dir };
60 delete env.CODEWHALE_CU_TEST_REMOTE;
61 delete env.CODEWHALE_CU_TEST_BACKEND;
62 if (backendSource) {
63 env.CODEWHALE_CU_TEST_BACKEND = path.join(dir, "backend.mjs");
64 fs.writeFileSync(env.CODEWHALE_CU_TEST_BACKEND, backendSource);
65 }
66 const child = spawn(process.execPath, [path.join(ROOT, "mcp/server.mjs")], { env, stdio: ["pipe", "pipe", "pipe"] });
67 let nextId = 0;
68 const pending = new Map();
69 const lines = createInterface({ input: child.stdout });
70 lines.on("line", line => {
71 const response = JSON.parse(line);
72 pending.get(response.id)?.(response);
73 });
74 let stderr = "";
75 child.stderr.on("data", data => { stderr += data; });
76 t.after(async () => {
77 const exited = once(child, "exit");
78 child.stdin.end();
79 await exited;
80 fs.rmSync(dir, { recursive: true, force: true });
81 assert.equal(stderr, "");
82 });
83 return {
84 dir, env,
85 control(value) { writeJsonAtomic(control, value); },
86 calls() { return fs.existsSync(log) ? fs.readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map(JSON.parse) : []; },
87 async tool(name, args = {}) {
88 const id = ++nextId;
89 let timer;
90 try {
91 const response = await new Promise((resolve, reject) => {
92 timer = setTimeout(() => reject(new Error(`${name} timed out: ${stderr}`)), 8_000);
93 pending.set(id, resolve);
94 child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method: "tools/call", params: { name, arguments: args } }) + "\n");
95 });
96 assert.ok(response.result, JSON.stringify(response));
97 return JSON.parse(response.result.content[0].text);
98 } finally { clearTimeout(timer); pending.delete(id); }
99 },
100 async register(target, label = "Fixture") {
101 const result = await this.tool("computer_register", { computer: "pad", transport: "hdc", target, label });
102 assert.equal(result.ok, true, JSON.stringify(result));
103 },
104 externalRegister(target) {
105 const result = spawnSync(process.execPath, ["--input-type=module", "-e",
106 "import {register} from './src/registry.mjs'; register({id:'pad', transport:'hdc', target:process.argv[1]});", target],
107 { cwd: ROOT, env, encoding: "utf8" });
108 assert.equal(result.status, 0, result.stderr);
109 },
110 };
111 }
112
113 const point = { type: "coordinate", x: 10, y: 10 };
114
115 test("a warmed HDC backend cannot send input to A after registration reports B", async t => {
116 const f = fixture(t);
117 await f.register("A");
118 assert.equal((await f.tool("request_access", { computer: "pad" })).connected, true);
119 await f.register("B");
120 const result = await f.tool("key", { text: "ENTER" });
121 const input = f.calls().filter(call => call.args.includes("uiInput"));
122 assert.equal(result.error?.code, "computer_observation_required", JSON.stringify({ result, input }));
123 assert.deepEqual(input, []);
124 });
125
126 test("same-ID HDC replacement requires fresh observation and dispatches only to B", async t => {
127 const f = fixture(t);
128 await f.register("A");
129 const initial = await f.tool("screenshot", { computer: "pad" });
130 assert.equal(initial.ok, true, JSON.stringify(initial));
131 const state = await f.tool("get_app_state");
132 assert.equal((await f.tool("left_click", { target: point })).ok, true);
133 await f.register("B");
134 const before = f.calls().length;
135 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "computer_observation_required");
136 assert.equal(f.calls().length, before);
137 assert.equal((await f.tool("get_app_state")).bundle_id, "B");
138 assert.equal((await f.tool("left_click", { target: point })).error.code, "no_raster");
139 assert.equal((await f.tool("perform_action", { target: { type: "element", state_id: state.state_id, index: 0 }, action: "click" })).error.code, "unknown_state");
140 assert.equal((await f.tool("screenshot")).ok, true);
141 assert.equal((await f.tool("left_click", { target: point })).ok, true);
142 const input = f.calls().filter(call => call.args.includes("uiInput"));
143 assert.deepEqual(input.map(call => call.target), ["A", "B"]);
144 assert.ok(f.calls().slice(before).every(call => call.target === "B"));
145 });
146
147 test("a different catalog writer invalidates the active host's HDC route on use", async t => {
148 const f = fixture(t);
149 await f.register("A");
150 await f.tool("screenshot", { computer: "pad" });
151 f.externalRegister("B");
152 assert.equal((await f.tool("type", { text: "fixture" })).error.code, "computer_observation_required");
153 assert.equal((await f.tool("computer_list")).active, "pad");
154 await f.tool("screenshot");
155 assert.equal((await f.tool("key", { text: "ENTER" })).ok, true);
156 assert.deepEqual(f.calls().filter(call => call.args.includes("uiInput")).map(call => call.target), ["B"]);
157 });
158
159 test("label-only catalog changes reuse cached backend geometry and observation", async t => {
160 const f = fixture(t);
161 await f.register("A");
162 await f.tool("screenshot", { computer: "pad" });
163 await f.register("A", "Renamed");
164 const before = f.calls().length;
165 assert.equal((await f.tool("list_displays")).ok, true);
166 assert.equal(f.calls().length, before, "cached display geometry proves backend reuse");
167 assert.equal((await f.tool("left_click", { target: point })).ok, true);
168 });
169
170 test("failed observation of a replacement never falls back to the old backend", async t => {
171 const f = fixture(t);
172 await f.register("A");
173 await f.tool("screenshot", { computer: "pad" });
174 f.externalRegister("B");
175 f.control({ fail: "B" });
176 const before = f.calls().length;
177 assert.equal((await f.tool("screenshot")).ok, false);
178 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "computer_observation_required");
179 assert.ok(f.calls().slice(before).every(call => call.target === "B"));
180 assert.equal(f.calls().filter(call => call.args.includes("uiInput")).length, 0);
181 });
182
183 test("an observation completed after an external route change cannot authorize input", async t => {
184 const f = fixture(t);
185 await f.register("A");
186 f.control({ changeTo: "B" });
187 const stale = await f.tool("get_app_state", { computer: "pad" });
188 assert.equal(stale.error.code, "computer_route_changed");
189 assert.equal(stale.request_dispatched, true);
190 assert.equal(stale.outcome_unknown, true);
191 f.control({});
192 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "computer_observation_required");
193 assert.equal((await f.tool("get_app_state")).bundle_id, "B");
194 assert.equal((await f.tool("key", { text: "ENTER" })).ok, true);
195 assert.deepEqual(f.calls().filter(call => call.args.includes("uiInput")).map(call => call.target), ["B"]);
196 });
197
198 test("remove and re-register cannot reuse observations even for the same route", async t => {
199 const f = fixture(t);
200 await f.register("A");
201 await f.tool("screenshot", { computer: "pad" });
202 await f.tool("computer_remove", { computer: "pad" });
203 await f.register("A");
204 assert.equal((await f.tool("left_click", { computer: "pad", target: point })).error.code, "computer_observation_required");
205 });
206
207 test("route fingerprints include transport fields and effective defaults only", () => {
208 const base = { transport: "ssh", host: "a" };
209 assert.equal(routeFingerprint(base), routeFingerprint({ ...base, label: "renamed", registeredAt: "later", platformHint: "linux", agentPath: ".codewhale-cu/agent/agent.mjs" }));
210 for (const changed of [{ host: "b" }, { port: 2222 }, { user: "other" }, { agentPath: "other/agent.mjs" }, { platformHint: "darwin" }, { transport: "hdc" }]) {
211 assert.notEqual(routeFingerprint(base), routeFingerprint({ ...base, ...changed }));
212 }
213 assert.equal(routeFingerprint({ transport: "hdc" }), routeFingerprint({ transport: "hdc", target: "", platform: "harmonyos" }));
214 assert.notEqual(routeFingerprint({ transport: "hdc", target: "A" }), routeFingerprint({ transport: "hdc", target: "B" }));
215 });
216
217 test("failed cleanup and catalog rollback cannot resurrect the retired backend", async t => {
218 const f = fixture(t, `
219 import fs from 'node:fs';
220 let instance = 0;
221 export function create() {
222 const id = ++instance;
223 const record = method => fs.appendFileSync(process.env.ROUTE_LOG, JSON.stringify({method,id}) + '\\n');
224 return {
225 get_app_state: async () => ({found:true, elements:[], instance:id}),
226 key: async () => { record('key'); return {action_sent:true}; },
227 releaseInput: async () => {
228 record('releaseInput');
229 if (JSON.parse(fs.readFileSync(process.env.ROUTE_CONTROL)).failRelease)
230 throw Object.assign(new Error('fixture release failed'), {code:'release_failed'});
231 },
232 closeSession: async () => {
233 record('closeSession');
234 if (JSON.parse(fs.readFileSync(process.env.ROUTE_CONTROL)).failCleanup)
235 throw Object.assign(new Error('fixture cleanup failed'), {code:'cleanup_failed'});
236 }
237 };
238 }
239 `);
240 f.control({ failCleanup: false });
241 assert.equal((await f.tool("computer_register", { computer: "pad", transport: "local" })).ok, true);
242 assert.equal((await f.tool("get_app_state", { computer: "pad" })).instance, 1);
243 f.control({ failRelease: true });
244 assert.equal((await f.tool("computer_register", { computer: "pad", transport: "hdc", target: "B" })).error.code, "release_failed");
245 assert.deepEqual(f.calls().slice(-2).map(call => call.method), ["releaseInput", "closeSession"], "recorder cleanup is attempted even when input release fails");
246 f.control({ failCleanup: true });
247 assert.equal((await f.tool("computer_register", { computer: "pad", transport: "hdc", target: "B" })).error.code, "cleanup_failed");
248 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "cleanup_failed");
249 // Roll back the catalog exactly, bypassing the host which still owns A.
250 const file = path.join(f.dir, "computers.json");
251 const catalog = JSON.parse(fs.readFileSync(file));
252 catalog.computers.pad = { id: "pad", transport: "local" };
253 writeJsonAtomic(file, catalog);
254 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "cleanup_failed");
255 assert.equal(f.calls().filter(call => call.method === "key").length, 0);
256 f.control({ failCleanup: false });
257 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "computer_observation_required");
258 assert.equal((await f.tool("get_app_state")).instance, 2);
259 assert.equal((await f.tool("key", { text: "ENTER" })).ok, true);
260 assert.deepEqual(f.calls().filter(call => call.method === "key").map(call => call.id), [2]);
261 assert.deepEqual(f.calls().slice(0, 2).map(call => call.method), ["releaseInput", "closeSession"]);
262 });
263
264 test("a route change during element revalidation refuses dispatch to the old backend", async t => {
265 const f = fixture(t, `
266 import fs from 'node:fs';
267 export function create() {
268 const element = {index:0, path:[0], role:'Button', label:'OK', position:{x:0,y:0}, size:{w:20,h:20}};
269 return {
270 get_app_state: async () => ({found:true, elements:[element]}),
271 resolve_element: async () => {
272 const file = process.env.CODEWHALE_CU_STATE_DIR + '/computers.json';
273 const catalog = JSON.parse(fs.readFileSync(file));
274 catalog.computers.pad = {id:'pad',transport:'hdc',target:'B'};
275 const tmp = file + '.' + process.pid + '.tmp';
276 fs.writeFileSync(tmp, JSON.stringify(catalog));
277 fs.renameSync(tmp, file);
278 return {found:true,element};
279 },
280 perform_action: async () => { throw new Error('must never dispatch stale action'); }
281 };
282 }
283 `);
284 await f.tool("computer_register", { computer: "pad", transport: "local" });
285 const state = await f.tool("get_app_state", { computer: "pad" });
286 const action = await f.tool("perform_action", { target: { type: "element", state_id: state.state_id, index: 0 }, action: "click" });
287 assert.equal(action.error.code, "computer_route_changed");
288 assert.equal(action.request_dispatched, undefined);
289 assert.equal((await f.tool("key", { text: "ENTER" })).error.code, "computer_observation_required");
290 assert.deepEqual(f.calls(), []);
291 });
292
293 const dispatchFailureBackend = `
294 import fs from 'node:fs';
295 import {setTimeout as delay} from 'node:timers/promises';
296 export function create() {
297 const record = method => fs.appendFileSync(process.env.ROUTE_LOG, JSON.stringify({method}) + '\\n');
298 return {
299 get_app_state: async () => ({found:true,elements:[]}),
300 key: async () => {
301 record('dispatched');
302 while (!JSON.parse(fs.readFileSync(process.env.ROUTE_CONTROL)).release) await delay(5);
303 throw Object.freeze(Object.assign(new Error('fixture failed after dispatch'), {code:'fixture_dispatch_failed'}));
304 },
305 releaseInput: async () => { record('releaseInput'); },
306 closeSession: async () => {
307 record('closeSession');
308 if (JSON.parse(fs.readFileSync(process.env.ROUTE_CONTROL)).failCleanup)
309 throw Object.assign(new Error('fixture cleanup failed'), {code:'fixture_cleanup_failed'});
310 }
311 };
312 }
313 `;
314
315 const dispatchFailureSSH = `
316 const fs = require('node:fs');
317 const {setTimeout:delay} = require('node:timers/promises');
318 const request = JSON.parse(Buffer.from(process.argv.at(-1), 'base64'));
319 const reply = value => process.stdout.write(JSON.stringify(value) + '\\n');
320 (async () => {
321 if (request.tool === 'platform') return reply({ok:true,platform:'linux'});
322 if (request.tool === 'get_app_state') return reply({ok:true,data:{found:true,elements:[]}});
323 if (request.tool !== 'key') throw new Error('unexpected fixture tool');
324 fs.appendFileSync(process.env.ROUTE_LOG, JSON.stringify({method:'dispatched',host:process.argv.find(arg => arg.startsWith('fixture-'))}) + '\\n');
325 let control;
326 while (!(control=JSON.parse(fs.readFileSync(process.env.ROUTE_CONTROL))).release) await delay(5);
327 if (control.failure === 'reply') return reply({ok:false,error:{code:'fixture_dispatch_failed',message:'fixture failed after dispatch'}});
328 process.stderr.write('fixture connection lost after dispatch');
329 process.exitCode = 7;
330 })().catch(error => { process.stderr.write(error.message); process.exitCode = 9; });
331 `;
332
333 for (const mode of ["backend", "reply", "connection"]) {
334 for (const change of ["changed", "removed", "unchanged", ...(mode === "backend" ? ["cleanup-failed"] : [])]) {
335 test(`${mode} dispatch failure preserves the original error when the route is ${change}`, async t => {
336 const local = mode === "backend";
337 const f = fixture(t, local ? dispatchFailureBackend : null, local ? null : dispatchFailureSSH);
338 f.control({ release: false, failure: mode });
339 const registration = await f.tool("computer_register", local
340 ? { computer: "pad", transport: "local" }
341 : { computer: "pad", transport: "ssh", host: "fixture-a.test", installAgent: false });
342 assert.equal(registration.ok, true, JSON.stringify(registration));
343 assert.equal((await f.tool("get_app_state", { computer: "pad" })).ok, true);
344 const pending = f.tool("key", { text: "ENTER" });
345 let failed;
346 try {
347 // The child has entered dispatch before the independent catalog writer
348 // changes anything. Release only after that change is visible in-fixture.
349 const deadline = Date.now() + 2_000;
350 while (!f.calls().some(call => call.method === "dispatched") && Date.now() < deadline) await delay(5);
351 assert.equal(f.calls().filter(call => call.method === "dispatched").length, 1);
352 const file = path.join(f.dir, "computers.json");
353 const catalog = JSON.parse(fs.readFileSync(file));
354 if (change === "removed") delete catalog.computers.pad;
355 else if (change !== "unchanged") {
356 if (local) catalog.computers.pad = { id: "pad", transport: "hdc", target: "B" };
357 else catalog.computers.pad.host = "fixture-b.test";
358 }
359 writeJsonAtomic(file, catalog);
360 f.control({ release: true, failure: mode, failCleanup: change === "cleanup-failed" });
361 failed = await pending;
362 const expected = mode === "connection"
363 ? { code: "tool_error", message: "ssh fixture-a.test exited 7: fixture connection lost after dispatch" }
364 : { code: "fixture_dispatch_failed", message: "fixture failed after dispatch" };
365 assert.equal(failed.ok, false);
366 assert.deepEqual(failed.error, expected, "route reconciliation must not mask the dispatch error");
367 if (change === "unchanged") {
368 assert.equal(Object.hasOwn(failed, "request_dispatched"), false);
369 assert.equal(Object.hasOwn(failed, "outcome_unknown"), false);
370 assert.equal(Object.hasOwn(failed, "note"), false);
371 assert.deepEqual(f.calls().map(call => call.method), ["dispatched"], "unchanged routes keep their resources");
372 } else {
373 assert.equal(failed.request_dispatched, true, JSON.stringify({ receipt: failed, calls: f.calls() }));
374 assert.equal(failed.outcome_unknown, true);
375 assert.match(failed.note, /effect is unconfirmed/);
376 assert.match(failed.note, /do not automatically retry/);
377 if (local) assert.deepEqual(f.calls().map(call => call.method), ["dispatched", "releaseInput", "closeSession"]);
378 const next = await f.tool("key", { text: "must remain blocked" });
379 assert.equal(next.error.code, change === "removed" ? "unknown_computer"
380 : change === "cleanup-failed" ? "fixture_cleanup_failed" : "computer_observation_required");
381 }
382 assert.equal(f.calls().filter(call => call.method === "dispatched").length, 1, "no automatic replay or new-target input");
383 } finally {
384 // Also unblock the owned child when an assertion fails; no live helper,
385 // device, or network endpoint participates in this fixture.
386 f.control({ release: true, failure: mode });
387 await pending;
388 }
389 });
390 }
391 }
392
392 lines Plain Text