返回 CodeWhale
fake-backend.mjs
根目录 / crates / tui / plugins / computer-use / tests / fixtures / fake-backend.mjs
1 // Test-only backend, injected via CODEWHALE_CU_TEST_BACKEND (see
2 // src/transport.mjs). Records every call as JSONL in FAKE_BACKEND_CALLS and
3 // answers with canned raster/element data. resolve_element reads its reply
4 // from the JSON file at FAKE_BACKEND_CONTROL, falling back to element 0's
5 // cached geometry.
6 import fs from "node:fs";
7 import os from "node:os";
8 import path from "node:path";
9 import crypto from "node:crypto";
10
11 // Smallest valid 1x1 PNG.
12 const PNG_1X1 = Buffer.from(
13 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
14 "base64",
15 );
16
17 const ELEMENTS = [
18 { index: 0, path: [0], windowIndex: 0, role: "AXWindow", label: "Main", position: { x: 0, y: 0 }, size: { w: 400, h: 300 } },
19 { index: 1, path: [0, 1], windowIndex: 0, role: "AXButton", label: "OK", position: { x: 10, y: 20 }, size: { w: 60, h: 30 } },
20 { index: 2, path: [], windowIndex: -1, role: "AXMenuBar", actions: [] },
21 { index: 3, path: [0], windowIndex: -1, role: "AXMenuBarItem", label: "File", actions: ["AXPress"] },
22 { index: 4, path: [0, 0], windowIndex: -1, role: "AXMenu", actions: [] },
23 { index: 5, path: [0, 0, 0], windowIndex: -1, role: "AXMenuItem", label: "Save", actions: ["AXPress"] },
24 { index: 6, path: [0], windowIndex: -2, role: "AXMenu", actions: [] },
25 { index: 7, path: [0, 0], windowIndex: -2, role: "AXMenuItem", label: "Choose", actions: ["AXPress"] },
26 { index: 8, path: [0, 2], windowIndex: 0, role: "AXTextField", value: "Fixture text", focused: true, enabled: true, actions: ["AXConfirm"], position: { x: 10, y: 60 }, size: { w: 150, h: 25 } },
27 // Suites that need more controls append them (FAKE_BACKEND_EXTRA_ELEMENTS,
28 // a JSON array) so the shared indices above never shift.
29 ...JSON.parse(process.env.FAKE_BACKEND_EXTRA_ELEMENTS || "[]"),
30 ];
31
32 function tmpPng(prefix) {
33 const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
34 const file = path.join(dir, `${crypto.randomBytes(3).toString("hex")}.png`);
35 fs.writeFileSync(file, PNG_1X1);
36 return file;
37 }
38
39 export function create() {
40 const callsFile = process.env.FAKE_BACKEND_CALLS;
41 const controlFile = process.env.FAKE_BACKEND_CONTROL;
42 const record = (method, args) => {
43 try { fs.appendFileSync(callsFile, JSON.stringify({ method, args }) + "\n"); } catch {}
44 };
45 // One backend instance owns this binding; serve-mode tests read it back to
46 // prove state survives between requests on the same agent process.
47 let boundApp = null;
48
49 return {
50 platform: "fake",
51 async open_application(args = {}) {
52 record("open_application", args);
53 boundApp = { name: args.name ?? "FakeApp", pid: args.pid ?? 4242, bundle_id: args.bundle_id ?? "com.fake.app" };
54 return { launched: true, activate: !!args.activate, resolved: boundApp, keyboard_delivery: args.activate ? "foreground-guarded" : "process", input_scope: args.activate ? "shared-desktop" : "application", shared_pointer: !!args.activate };
55 },
56 async list_apps() {
57 record("list_apps", {});
58 return { apps: [{ name: boundApp?.name ?? "FakeApp", pid: boundApp?.pid ?? 4242, bundle_id: "com.fake.app" }] };
59 },
60 async left_mouse_down(args = {}) { record("left_mouse_down", args); return { action_sent: true, at: { x: args.target?.x, y: args.target?.y } }; },
61 async left_mouse_up(args = {}) { record("left_mouse_up", args); return { action_sent: true }; },
62 async screenshot(args = {}) {
63 const { region } = args;
64 record("screenshot", args);
65 const file = tmpPng("cu-fake-shot-");
66 return region
67 ? { file, points: { x: region[0], y: region[1], w: region[2], h: region[3] }, pixels: { w: region[2] * 2, h: region[3] * 2 }, scale: 2 }
68 : { file, points: { x: 0, y: 0, w: 800, h: 600 }, pixels: { w: 1600, h: 1200 }, scale: 2 };
69 },
70 async zoom(args = {}) {
71 const { region } = args;
72 record("zoom", args);
73 return { file: tmpPng("cu-fake-zoom-"), region };
74 },
75 async get_app_state({ app_ref, detail, include_ocr } = {}) {
76 record("get_app_state", { app_ref, detail, include_ocr });
77 return { found: true, name: app_ref?.name ?? "FakeApp", elements: ELEMENTS, ...(include_ocr ? { ocr: {
78 status: app_ref?.name === "OCR unavailable" ? "unavailable" : "ok",
79 raster: { file: "/fixture/ocr.png", points: { x: 100, y: 50, w: 200, h: 100 }, pixels: { w: 400, h: 200 }, scale: 2 },
80 blocks: app_ref?.name === "OCR unavailable" ? [] : [{ text: "Raster text", confidence: 0.9, bounds: { x: 60, y: 20, w: 40, h: 40 }, target: { type: "coordinate", x: 80, y: 40 } }],
81 } } : {}) };
82 },
83 async resolve_element(args) {
84 record("resolve_element", args);
85 let ctrl = null;
86 try { ctrl = JSON.parse(fs.readFileSync(controlFile, "utf8")); } catch {}
87 if (ctrl) return ctrl;
88 const el = ELEMENTS[1];
89 return { found: true, element: { role: el.role, label: el.label, position: el.position, size: el.size }, reason: null };
90 },
91 async left_click({ target } = {}) { record("left_click", { target }); return { action_sent: true, at: { x: target?.x, y: target?.y } }; },
92 async double_click({ target } = {}) { record("double_click", { target }); return { action_sent: true, at: { x: target?.x, y: target?.y } }; },
93 async mouse_move({ target } = {}) { record("mouse_move", { target }); return { action_sent: true, at: { x: target?.x, y: target?.y } }; },
94 async perform_action(args) { record("perform_action", args); return { action_sent: true, strategy: "a11y" }; },
95 async select_text(args) { record("select_text", args); return { action_sent: true, strategy: "a11y" }; },
96 async set_value(args) { record("set_value", args); return { action_sent: true, strategy: "a11y", verified: true, after: args.value }; },
97 async type(args) { record("type", args); return { action_sent: true, text: args.text, verified: true, bound_app: boundApp?.name ?? null }; },
98 async key(args) { record("key", args); return { action_sent: true, key: args.text ?? "return" }; },
99 async focus(args) { record("focus", args); return { action_sent: true, focused: true, strategy: "a11y" }; },
100 async get_value(args) { record("get_value", args); return { value: "Fixture text", strategy: "a11y" }; },
101 async invoke_menu(args) { record("invoke_menu", args); return { action_sent: true, strategy: "a11y" }; },
102 async app_script(args) { record("app_script", args); return { result: "fake", language: args.language ?? "applescript" }; },
103 };
104 }
105
106 export default { create };
107
107 lines Plain Text