跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

macos-input-smoke.mjs

100 行 · 6.0 KB历史下载
1import assert from "node:assert/strict";
2import { createWorkbench } from "./workbench-fixture.mjs";
3
4const inputs = [], status = { installed: true, running: true, screen: true, input: true, input_protocol: 1, persistent: true };
5let image, geometry = "input-geometry-1";
6const { page, browser, errors } = await createWorkbench({
7 async beforeNavigate(page, state) {
8 state.account.guest_system = state.status.guest_system = "macos";
9 state.status.vm_running = state.status.ready = true;
10 image = await page.evaluate(() => {
11 const canvas = document.createElement("canvas");
12 canvas.width = 640; canvas.height = 480;
13 const context = canvas.getContext("2d");
14 context.fillStyle = "#f7f5f1"; context.fillRect(0, 0, 640, 480);
15 context.fillStyle = "#303030"; context.font = "22px sans-serif";
16 context.fillText("Synthetic Claude input surface", 30, 45);
17 return canvas.toDataURL().split(",")[1];
18 });
19 await page.route("**/api/macos/claude/window", route => route.fulfill({ json: status }));
20 await page.routeWebSocket("**/api/macos/claude/connection**", socket => {
21 socket.onMessage(data => {
22 const { id, request } = JSON.parse(data);
23 let result = { ok: true };
24 if (request.action === "status") result = status;
25 else if (request.action === "frame") result = { image, pid: 23, window: 70, geometry, contentWidth: 640, contentHeight: 480 };
26 else {
27 inputs.push(request);
28 if (request.action === "ime") result = { available: true, context: "a".repeat(32), preedit_supported: false,
29 rect: [80, 70, 1, 24], width: 640, height: 480 };
30 }
31 socket.send(JSON.stringify({ id, result }));
32 });
33 });
34 },
35});
36page.setDefaultTimeout(10000);
37async function until(predicate) {
38 for (let attempt = 0; attempt < 200; attempt++) {
39 if (predicate()) return;
40 await page.waitForTimeout(20);
41 }
42 assert(predicate(), "expected native window input did not arrive");
43}
44try {
45 await page.getByRole("button", { name: "Claude Desktop", exact: true }).click();
46 const surface = page.getByAltText("Claude Desktop 客机窗口");
47 await surface.waitFor();
48 await page.getByLabel("向 Claude Desktop 输入").waitFor({ state: "attached" });
49 const bounds = await surface.boundingBox();
50 await page.mouse.move(bounds.x + 80, bounds.y + 100);
51 await page.mouse.down();
52 await page.mouse.move(bounds.x + 360, bounds.y + 160, { steps: 5 });
53 await page.mouse.up();
54 await until(() => inputs.some(input => input.action === "pointer" && input.phase === "up"));
55 const drag = inputs.filter(input => input.action === "pointer");
56 const firstDown = drag.findIndex(input => input.phase === "down");
57 assert(firstDown >= 0 && drag.slice(firstDown + 1, -1).some(input => input.phase === "move" && input.buttons === 1));
58 assert.equal(drag.at(-1).phase, "up");
59 assert(drag.every(input => input.geometry === geometry && input.pid === 23 && input.window === 70));
60 await surface.click({ button: "right", position: { x: 150, y: 160 } });
61 await surface.dblclick({ position: { x: 200, y: 190 } });
62 await until(() => inputs.some(input => input.button === 2 && input.phase === "up") && inputs.some(input => input.clicks === 2));
63
64 await page.keyboard.press("Meta+f");
65 assert.equal(await page.evaluate(() => window.cageEditAction("selectAll")), true);
66 await page.keyboard.press("F5");
67 await until(() => inputs.some(input => input.action === "key" && input.code === "F5" && !input.down));
68 assert(inputs.some(input => input.code === "KeyF" && input.command));
69 assert(inputs.some(input => input.code === "KeyA" && input.command));
70 await until(() => inputs.some(input => input.action === "ime" && input.mode === "status"));
71 await page.getByLabel("向 Claude Desktop 输入").evaluate(element => {
72 element.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true }));
73 element.value = "nihao";
74 element.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertCompositionText", isComposing: true, data: "nihao" }));
75 element.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true, data: "你好🙂" }));
76 });
77 await page.keyboard.press("Enter");
78 await until(() => inputs.some(input => input.action === "ime" && input.mode === "commit"));
79 const composition = inputs.filter(input => input.action === "ime" && input.mode !== "status");
80 assert.deepEqual(composition.map(input => input.mode), ["begin", "update", "commit"]);
81 assert.equal(composition[2].text, "你好🙂");
82 assert(composition.every(input => input.session === composition[0].session && input.geometry === geometry));
83 assert.deepEqual(composition.map(input => input.sequence), [0, 1, 2]);
84 await until(() => inputs.some(input => input.code === "Enter" && !input.down));
85 assert(inputs.indexOf(composition[2]) < inputs.findIndex(input => input.code === "Enter" && input.down));
86
87 const resetCount = inputs.filter(input => input.action === "input-reset").length;
88 await page.mouse.down();
89 await page.evaluate(() => window.dispatchEvent(new Event("blur")));
90 await until(() => inputs.filter(input => input.action === "input-reset").length > resetCount);
91 await page.mouse.up();
92 const beforeResize = inputs.filter(input => input.action === "input-reset").length;
93 geometry = "input-geometry-2";
94 await until(() => inputs.filter(input => input.action === "input-reset").length > beforeResize);
95 await surface.click({ position: { x: 40, y: 50 } });
96 await page.keyboard.press("ArrowLeft");
97 await until(() => inputs.some(input => input.code === "ArrowLeft" && !input.down && input.geometry === geometry));
98 assert.deepEqual(errors, []);
99 console.log(JSON.stringify({ result: "PASS", checks: ["drag with held left button", "right click", "double click count", "Command find", "host edit menu select-all", "F5", "IME begin/update/commit before Enter", "blur releases input", "geometry change resets and rebinds input"], requests: inputs.filter(input => input.action !== "frame" && input.mode !== "status") }));
100} finally { await browser.close(); }