跳到主要内容

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

浏览文件

claude-desktop-smoke.mjs

183 行 · 7.0 KB历史下载
1import assert from "node:assert/strict";
2import fs from "node:fs/promises";
3import { fileURLToPath } from "node:url";
4import { createWorkbench } from "./workbench-fixture.mjs";
5const output = new URL(
6 "../../docs/test-reports/2026-11-02-claude-desktop/",
7 import.meta.url,
8);
9await fs.mkdir(output, { recursive: true });
10const { browser, page, state, errors } = await createWorkbench();
11await page.addInitScript(() => {
12 window.EventSource = class extends EventTarget {
13 constructor() { super(); window.desktopBrowserEvents = this; }
14 close() {}
15 };
16});
17const desktop = {
18 installed: false,
19 running: false,
20 version: "",
21 failed: false,
22};
23let failInstall = true;
24const actions = [];
25await page.route("**/api/claude-desktop**", async (route) => {
26 const path = new URL(route.request().url()).pathname;
27 if (path.endsWith("/vnc-credentials"))
28 return route.fulfill({ json: { password: "fixture-only" } });
29 if (route.request().method() === "GET")
30 return route.fulfill({ json: desktop });
31 if (path.endsWith("/resize")) return route.fulfill({ json: { ok: true } });
32 const action = path.split("/").at(-1);
33 actions.push(action);
34 const job = {
35 id: "desktop-" + actions.length,
36 kind: "claude-desktop-" + action,
37 running: false,
38 message: "已完成",
39 };
40 if (action === "install" && failInstall)
41 job.error = "测试:安装源暂时不可用,请重试";
42 else if (action === "install")
43 Object.assign(desktop, { installed: true, version: "4.4212.0" });
44 else if (action === "start") desktop.running = true;
45 else if (action === "stop") desktop.running = false;
46 state.status.job = job;
47 await route.fulfill({ json: job });
48});
49try {
50 state.status.vm_running = true;
51 state.status.ready = true;
52 await page.reload();
53 await page.getByRole("button", { name: "claude.ai", exact: true }).click();
54 assert.equal(
55 state.requests.find((x) => x.path === "/browser/new").body.url,
56 "https://claude.ai/",
57 );
58 await page
59 .getByRole("button", { name: "Claude Desktop", exact: true })
60 .click();
61 await page.getByRole("button", { name: "下载并安装", exact: true }).waitFor();
62 assert.deepEqual(
63 actions,
64 [],
65 "opening the entry must not download without clicking install",
66 );
67 await page.screenshot({
68 path: fileURLToPath(new URL("desktop-install.png", output)),
69 });
70 await page.getByRole("button", { name: "下载并安装", exact: true }).click();
71 await page
72 .getByRole("alert")
73 .filter({ hasText: "安装源暂时不可用" })
74 .waitFor();
75 assert.equal(desktop.installed, false);
76 failInstall = false;
77 await page.getByRole("button", { name: "下载并安装", exact: true }).click();
78 await page.getByRole("button", { name: "关闭应用", exact: true }).waitFor();
79 assert.deepEqual(actions, ["install", "install", "start"]);
80 const desktopToolbar = page.locator(".claude-desktop-toolbar");
81 const sendBrowserState = (focus) => page.evaluate((snapshot) => {
82 window.desktopBrowserEvents.onmessage({ data: JSON.stringify(snapshot) });
83 }, { tabs: state.tabs, active: state.active, focus });
84 await sendBrowserState(false);
85 await desktopToolbar.waitFor();
86 const loginTab = { id: "desktop-login", kind: "web", title: "Desktop sign in", url: "https://claude.com/cai/login/app-google-auth" };
87 state.tabs.push(loginTab);
88 state.active = loginTab.id;
89 await sendBrowserState(true);
90 await desktopToolbar.waitFor({ state: "hidden" });
91 assert.equal(desktop.running, true, "opening an external login page must preserve Desktop");
92 await page.getByRole("button", { name: "Claude Desktop", exact: true }).click();
93 await sendBrowserState(false);
94 await desktopToolbar.waitFor();
95 state.tabs.push({ ...loginTab, id: "desktop-login-poll" });
96 state.active = "desktop-login-poll";
97 await desktopToolbar.waitFor({ state: "hidden" });
98 assert.equal(await page.getByRole("textbox", { name: "网页地址或搜索" }).inputValue(), loginTab.url);
99 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
100 assert.equal(
101 desktop.running,
102 true,
103 "changing views must not stop the desktop",
104 );
105 await page
106 .getByRole("button", { name: "Claude Desktop", exact: true })
107 .click();
108 await page.getByRole("button", { name: "关闭应用", exact: true }).click();
109 await page
110 .getByRole("dialog")
111 .getByRole("button", { name: "关闭应用", exact: true })
112 .click();
113 await page
114 .getByRole("button", { name: "打开 Claude Desktop", exact: true })
115 .waitFor();
116 assert.equal(desktop.running, false);
117 const stoppedActions = actions.length;
118 await page.getByRole("button", { name: "刷新 Claude Desktop 状态" }).click();
119 assert.equal(
120 actions.length,
121 stoppedActions,
122 "refresh must not reopen a stopped app",
123 );
124 await page.getByRole("button", { name: /^设置/ }).click();
125 await page
126 .getByLabel("Claude 终端启动命令", { exact: true })
127 .fill("claude --continue --model sonnet");
128 await page.getByRole("button", { name: "保存设置", exact: true }).click();
129 await page
130 .getByLabel("Claude 终端启动命令", { exact: true })
131 .waitFor({ state: "hidden" });
132 assert.equal(
133 state.settings.claude_terminal_command,
134 "claude --continue --model sonnet",
135 );
136 await page.getByRole("button", { name: /^在 .* 新建终端$/ }).click();
137 await page
138 .getByRole("menu")
139 .getByRole("menuitem", { name: "Claude 终端", exact: true })
140 .waitFor();
141 await page.screenshot({
142 path: fileURLToPath(new URL("terminal-choice.png", output)),
143 });
144 await page
145 .getByRole("menuitem", { name: "Claude 终端", exact: true })
146 .click();
147 const terminalRequests = () =>
148 state.requests.filter(
149 (x) => x.path === "/terminals" && x.method === "POST",
150 );
151 assert.equal(terminalRequests().at(-1).body.kind, "claude");
152 assert.equal(
153 terminalRequests().at(-1).body.workspace,
154 state.settings.workspace,
155 );
156 const before = terminalRequests().length;
157 await page.reload();
158 await page.getByRole("button", { name: /^在 .* 新建终端$/ }).waitFor();
159 assert.equal(
160 terminalRequests().length,
161 before,
162 "reconnect must not relaunch Claude",
163 );
164 await page.getByRole("button", { name: /^在 .* 新建终端$/ }).click();
165 await page.getByRole("menuitem", { name: "普通终端", exact: true }).click();
166 assert.equal(terminalRequests().at(-1).body.kind, "shell");
167 await page.setViewportSize({ width: 820, height: 558 });
168 await page
169 .getByRole("button", { name: "Claude Desktop", exact: true })
170 .click();
171 await page.screenshot({
172 path: fileURLToPath(new URL("desktop-small.png", output)),
173 });
174 assert.deepEqual(errors, []);
175 console.log(
176 "PASS: sidebar, optional installation, failure/retry, Desktop login handoff via events and polling, background updates, lifecycle, terminal choice, settings and reconnect",
177 );
178} catch (error) {
179 console.error(JSON.stringify({ actions, desktop, status: state.status, errors, body: await page.locator("body").innerText() }));
180 throw error;
181} finally {
182 await browser.close();
183}