| 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 | ]; |
| 28 | |
| 29 | function tmpPng(prefix) { |
| 30 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 31 | const file = path.join(dir, `${crypto.randomBytes(3).toString("hex")}.png`); |
| 32 | fs.writeFileSync(file, PNG_1X1); |
| 33 | return file; |
| 34 | } |
| 35 | |
| 36 | export function create() { |
| 37 | const callsFile = process.env.FAKE_BACKEND_CALLS; |
| 38 | const controlFile = process.env.FAKE_BACKEND_CONTROL; |
| 39 | const record = (method, args) => { |
| 40 | try { fs.appendFileSync(callsFile, JSON.stringify({ method, args }) + "\n"); } catch {} |
| 41 | }; |
| 42 | // One backend instance owns this binding; serve-mode tests read it back to |
| 43 | // prove state survives between requests on the same agent process. |
| 44 | let boundApp = null; |
| 45 | |
| 46 | return { |
| 47 | platform: "fake", |
| 48 | async open_application(args = {}) { |
| 49 | record("open_application", args); |
| 50 | boundApp = { name: args.name ?? "FakeApp", pid: args.pid ?? 4242, bundle_id: args.bundle_id ?? "com.fake.app" }; |
| 51 | 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 }; |
| 52 | }, |
| 53 | async list_apps() { |
| 54 | record("list_apps", {}); |
| 55 | return { apps: [{ name: boundApp?.name ?? "FakeApp", pid: boundApp?.pid ?? 4242, bundle_id: "com.fake.app" }] }; |
| 56 | }, |
| 57 | async left_mouse_down(args = {}) { record("left_mouse_down", args); return { action_sent: true, at: { x: args.target?.x, y: args.target?.y } }; }, |
| 58 | async left_mouse_up(args = {}) { record("left_mouse_up", args); return { action_sent: true }; }, |
| 59 | async screenshot({ region } = {}) { |
| 60 | record("screenshot", { region }); |
| 61 | const file = tmpPng("cu-fake-shot-"); |
| 62 | return region |
| 63 | ? { 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 } |
| 64 | : { file, points: { x: 0, y: 0, w: 800, h: 600 }, pixels: { w: 1600, h: 1200 }, scale: 2 }; |
| 65 | }, |
| 66 | async zoom({ region } = {}) { |
| 67 | record("zoom", { region }); |
| 68 | return { file: tmpPng("cu-fake-zoom-"), region }; |
| 69 | }, |
| 70 | async get_app_state({ app_ref, detail, include_ocr } = {}) { |
| 71 | record("get_app_state", { app_ref, detail, include_ocr }); |
| 72 | return { found: true, name: app_ref?.name ?? "FakeApp", elements: ELEMENTS, ...(include_ocr ? { ocr: { |
| 73 | status: app_ref?.name === "OCR unavailable" ? "unavailable" : "ok", |
| 74 | raster: { file: "/fixture/ocr.png", points: { x: 100, y: 50, w: 200, h: 100 }, pixels: { w: 400, h: 200 }, scale: 2 }, |
| 75 | 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 } }], |
| 76 | } } : {}) }; |
| 77 | }, |
| 78 | async resolve_element(args) { |
| 79 | record("resolve_element", args); |
| 80 | let ctrl = null; |
| 81 | try { ctrl = JSON.parse(fs.readFileSync(controlFile, "utf8")); } catch {} |
| 82 | if (ctrl) return ctrl; |
| 83 | const el = ELEMENTS[1]; |
| 84 | return { found: true, element: { role: el.role, label: el.label, position: el.position, size: el.size }, reason: null }; |
| 85 | }, |
| 86 | async left_click({ target } = {}) { record("left_click", { target }); return { action_sent: true, at: { x: target?.x, y: target?.y } }; }, |
| 87 | async double_click({ target } = {}) { record("double_click", { target }); return { action_sent: true, at: { x: target?.x, y: target?.y } }; }, |
| 88 | async mouse_move({ target } = {}) { record("mouse_move", { target }); return { action_sent: true, at: { x: target?.x, y: target?.y } }; }, |
| 89 | async perform_action(args) { record("perform_action", args); return { action_sent: true, strategy: "a11y" }; }, |
| 90 | async select_text(args) { record("select_text", args); return { action_sent: true, strategy: "a11y" }; }, |
| 91 | async set_value(args) { record("set_value", args); return { action_sent: true, strategy: "a11y", verified: true, after: args.value }; }, |
| 92 | async type(args) { record("type", args); return { action_sent: true, text: args.text, verified: true, bound_app: boundApp?.name ?? null }; }, |
| 93 | async key(args) { record("key", args); return { action_sent: true, key: args.text ?? "return" }; }, |
| 94 | async focus(args) { record("focus", args); return { action_sent: true, focused: true, strategy: "a11y" }; }, |
| 95 | async get_value(args) { record("get_value", args); return { value: "Fixture text", strategy: "a11y" }; }, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | export default { create }; |
| 100 |