跳到主要内容

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

浏览文件

macos-project-recovery-smoke.mjs

133 行 · 8.8 KB历史下载
1import assert from "node:assert/strict";
2import { fileURLToPath } from "node:url";
3import { createWorkbench } from "./workbench-fixture.mjs";
4
5const project = "/Users/fixture/Cage Projects/macOS 示例";
6const calls = [];
7let loggedIn = false, maintenanceClosed = false, geometry = "maintenance-frame-a", images;
8const { page, browser, state, errors } = await createWorkbench({
9 viewport: { width: 1280, height: 900 },
10 async beforeNavigate(page, state) {
11 state.status.vm_running = state.status.ready = true;
12 state.account.guest_system = state.status.guest_system = "macos";
13 state.status.projects = [project];
14 state.status.workspace = state.settings.workspace = project;
15 state.status.project_error = "部分项目文件夹尚未连接,环境和浏览器可继续使用。";
16 state.status.project_errors = [{ path: project, recovery: "guest-login", message: "请先登录虚拟 Mac 的客机账户。登录完成后,Cage 会自动连接项目。" }];
17 images = await page.evaluate(() => {
18 const make = (width, height, color) => {
19 const canvas = document.createElement("canvas");
20 canvas.width = width; canvas.height = height;
21 const context = canvas.getContext("2d");
22 context.fillStyle = color; context.fillRect(0, 0, width, height);
23 context.fillStyle = "#fff"; context.font = "22px sans-serif";
24 context.fillText("Synthetic Apple authorization window", 20, 45);
25 return canvas.toDataURL("image/jpeg").split(",")[1];
26 };
27 return { login: make(1920, 1200, "#485570"), a: make(600, 400, "#334252"), b: make(480, 360, "#465835") };
28 });
29 await page.route("**/api/macos/login", route => {
30 const request = route.request().postDataJSON();
31 calls.push({ mode: "login", ...request });
32 if (request.action === "open") return route.fulfill({ json: { session: "login-fixture" } });
33 if (request.action === "close") return route.fulfill({ json: { closed: true } });
34 if (request.action === "frame") {
35 if (loggedIn) {
36 state.status.project_errors = []; state.status.project_error = "";
37 return route.fulfill({ json: { closed: true } });
38 }
39 return route.fulfill({ json: { image: images.login, mime: "image/jpeg" } });
40 }
41 return route.fulfill({ json: {} });
42 });
43 await page.route("**/api/projects/open", route => {
44 if (!loggedIn) return route.fulfill({ status: 400, contentType: "text/plain; charset=utf-8", headers: { "X-Cage-Recovery": "guest-login" }, body: "请先登录虚拟 Mac 的客机账户。" });
45 return route.fulfill({ json: { workspace: project } });
46 });
47 await page.route("**/api/macos/maintenance", route => {
48 const request = route.request().postDataJSON();
49 calls.push({ mode: "maintenance", ...request });
50 assert(["projects", "screen", "input"].includes(request.maintenance));
51 assert.equal(request.initial_setup, undefined);
52 assert.equal(request.password, undefined);
53 if (request.action === "open") return route.fulfill({ json: { session: "maintenance-fixture", maintenance_version: 1, administrator: "cageprovision" } });
54 if (request.action === "close") return route.fulfill({ json: { closed: true, maintenance_version: 1 } });
55 if (request.action === "frame") return route.fulfill({ json: maintenanceClosed
56 ? { closed: true, maintenance_version: 1, error: "客机前台应用已变化" }
57 : { image: geometry.endsWith("a") ? images.a : images.b, mime: "image/jpeg", geometry,
58 region: geometry.endsWith("a") ? [120, 90, 600, 400] : [240, 150, 480, 360], maintenance_version: 1 } });
59 return route.fulfill({ json: { maintenance_version: 1 } });
60 });
61 },
62});
63page.setDefaultTimeout(12000);
64async function until(predicate) {
65 for (let attempt = 0; attempt < 150; attempt++) {
66 if (predicate()) return;
67 await page.waitForTimeout(20);
68 }
69 assert(predicate(), "expected recovery operation did not arrive");
70}
71try {
72 const failures = page.locator(".project-connection-errors");
73 await failures.getByRole("button", { name: "登录客机账户", exact: true }).click();
74 const login = page.getByRole("dialog", { name: "登录虚拟 Mac", exact: true });
75 await login.getByAltText("Apple 客机登录屏幕").waitFor();
76 await login.getByLabel(/^客机登录键盘/).press("Enter");
77 await until(() => calls.some(call => call.mode === "login" && call.action === "key" && call.code === 36));
78 assert(await login.isVisible(), "Return closed the view before macOS reported a completed login");
79 loggedIn = true;
80 await login.waitFor({ state: "detached" });
81 await failures.waitFor({ state: "detached" });
82
83 state.status.project_error = "部分项目文件夹尚未连接,环境和浏览器可继续使用。";
84 state.status.project_errors = [{ path: project, recovery: "filesystem-permission", message: "虚拟 Mac 的文件系统扩展尚未启用。" }];
85 await page.reload();
86 await failures.getByRole("button", { name: "打开客机授权设置", exact: true }).click();
87 const maintenance = page.getByRole("dialog", { name: "恢复客机系统权限", exact: true });
88 const image = maintenance.getByAltText("客机系统设置或授权窗口");
89 await page.waitForFunction(() => document.querySelector('[data-mode="maintenance"] img')?.naturalWidth === 600);
90 const first = await image.boundingBox();
91 await image.click({ position: { x: first.width / 2, y: first.height / 2 } });
92 await until(() => calls.some(call => call.mode === "maintenance" && call.action === "click"));
93 const click = calls.find(call => call.mode === "maintenance" && call.action === "click");
94 assert.equal(click.geometry, "maintenance-frame-a");
95 assert(Math.abs(click.x - 420) <= 1 && Math.abs(click.y - 290) <= 1);
96 await maintenance.getByLabel(/^客机登录键盘/).press("Meta+a");
97 await maintenance.getByRole("button", { name: "填写管理员密码", exact: true }).click();
98 await until(() => calls.some(call => call.action === "select-all") && calls.some(call => call.action === "fill-password"));
99 assert(calls.filter(call => call.mode === "maintenance" && ["select-all", "fill-password"].includes(call.action))
100 .every(call => call.geometry === "maintenance-frame-a" && call.session === "maintenance-fixture"));
101 assert(!calls.some(call => call.mode === "maintenance" && call.action === "key" && call.code === 36), "password fill submitted authorization without the user");
102 await page.screenshot({ path: fileURLToPath(new URL("../../docs/test-reports/2026-11-09-architecture-fixes/evidence/mac-maintenance-ui.png", import.meta.url)) });
103
104 geometry = "maintenance-frame-b";
105 await page.waitForFunction(() => document.querySelector('[data-mode="maintenance"] img')?.naturalWidth === 480);
106 const next = await image.boundingBox();
107 await image.click({ position: { x: next.width / 2, y: next.height / 2 } });
108 await until(() => calls.some(call => call.action === "click" && call.geometry === geometry));
109 const moved = calls.find(call => call.action === "click" && call.geometry === geometry);
110 assert(Math.abs(moved.x - 480) <= 1 && Math.abs(moved.y - 330) <= 1);
111 maintenanceClosed = true;
112 await maintenance.waitFor({ state: "detached" });
113 assert.equal(await page.locator('[data-mode="maintenance"] img').count(), 0);
114
115 maintenanceClosed = false;
116 state.status.project_error = "";
117 state.status.project_errors = [];
118 await page.reload();
119 await page.getByRole("button", { name: "设置", exact: true }).click();
120 await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "环境与恢复", exact: true }).click();
121 const permissions = page.getByRole("region", { name: "客机系统权限", exact: true });
122 for (const [label, purpose] of [["文件共享权限", "projects"], ["屏幕录制权限", "screen"], ["辅助功能权限", "input"]]) {
123 const start = calls.length;
124 await permissions.getByRole("button", { name: label, exact: true }).click();
125 await image.waitFor();
126 assert(calls.slice(start).some(call => call.action === "open" && call.maintenance === purpose));
127 await maintenance.getByRole("button", { name: "返回工作台", exact: true }).click();
128 await maintenance.waitFor({ state: "detached" });
129 await until(() => calls.slice(start).some(call => call.action === "close" && call.maintenance === purpose));
130 }
131 assert.deepEqual(errors, []);
132 console.log(JSON.stringify({ result: "PASS", browser: browser.version(), checks: ["actionable project login", "Return waits for macOS", "restored project removes warning", "separate maintenance purpose", "cropped window input coordinates", "published geometry binding", "explicit administrator fill intent without password data or submission", "geometry change rebinds crop", "foreground change removes maintenance image", "three permission recovery entries remain available without a project error"] }));
133} finally { await browser.close(); }