跳到主要内容

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

浏览文件

browser-download-panel-smoke.mjs

253 行 · 9.3 KB历史下载
1import assert from "node:assert/strict";
2import fs from "node:fs/promises";
3import { createWorkbench } from "./workbench-fixture.mjs";
4
5// Isolated records and native bridge; no personal files or running environment.
6const downloads = [
7 {
8 id: "report",
9 name: "产品需求与界面验收报告.pdf",
10 state: "saved",
11 path: "/Users/test/Cage/Downloads/产品需求与界面验收报告.pdf",
12 },
13 {
14 id: "video",
15 name: "很长的视频文件名称_20261107_项目演示与操作录屏.mp4",
16 state: "downloading",
17 receivedBytes: 42,
18 totalBytes: 100,
19 },
20 {
21 id: "failed",
22 name: "assets.zip",
23 state: "failed",
24 error: "保存失败,请检查连接后重试。",
25 retryable: true,
26 },
27 { id: "canceled", name: "installer.dmg", state: "canceled" },
28 ...Array.from({ length: 10 }, (_, index) => ({
29 id: `history-${index}`,
30 name: `历史文件 ${index + 1}.txt`,
31 state: "saved",
32 path: `/Users/test/Cage/Downloads/历史文件 ${index + 1}.txt`,
33 })),
34];
35let records = downloads;
36const output = process.env.CAGE_DOWNLOAD_CAPTURE || "/tmp/cage-download-panel";
37await fs.mkdir(output, { recursive: true });
38for (const platform of ["darwin", "win32", "linux"]) {
39 const calls = [];
40 const { browser, page, state, errors } = await createWorkbench({
41 clientContext: {
42 mode: "local",
43 connection_id: "local",
44 client_platform: platform,
45 server_platform: platform,
46 connection: { state: "disconnected" },
47 },
48 nativeRequest: async (body) => {
49 calls.push(body);
50 return body.action === "saveBrowserDownload"
51 ? { canceled: true }
52 : { ok: true };
53 },
54 });
55 try {
56 records = downloads;
57 await page.route("**/api/browser/tabs", (route) =>
58 route.fulfill({
59 json: { tabs: state.tabs, active: state.active, downloads: records },
60 }),
61 );
62 await page.route("**/api/browser/files/saved", (route) =>
63 route.fulfill({ json: [] }),
64 );
65 await page.route("**/api/browser/download-retry", (route) => {
66 calls.push({ action: "retry", ...route.request().postDataJSON() });
67 return route.fulfill({ json: { ok: true } });
68 });
69 state.status.ready = state.status.vm_running = true;
70 await page.reload();
71 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
72 const trigger = page.getByRole("button", {
73 name: "浏览器下载",
74 exact: true,
75 });
76 const panel = page.getByRole("dialog", { name: "下载文件", exact: true });
77 const toolbar = page.locator(".browser-chrome .navigation-row");
78 assert.equal(
79 await page
80 .getByRole("button", { name: "设置", exact: true })
81 .locator("svg.lucide-settings")
82 .count(),
83 1,
84 );
85 await page.getByRole("button", { name: "收起侧栏", exact: true }).click();
86 for (const width of [1280, 375]) {
87 await page.setViewportSize({ width, height: 850 });
88 const before = await toolbar.boundingBox();
89 await trigger.focus();
90 await trigger.press("Enter");
91 await panel.waitFor();
92 const box = await panel.boundingBox(),
93 after = await toolbar.boundingBox();
94 assert.ok(
95 box.x >= 0 &&
96 box.x + box.width <= width &&
97 box.y >= after.y + after.height,
98 "download panel stays below the toolbar and inside the viewport",
99 );
100 assert.ok(
101 box.y + box.height <= 850 && Math.abs(after.height - before.height) < 1,
102 "panel stays inside the window without shifting the toolbar",
103 );
104 assert.equal(
105 await page.locator(".modal-scrim").count(),
106 0,
107 "downloads do not dim the page",
108 );
109 assert.equal(
110 await panel.getByRole("progressbar").getAttribute("value"),
111 "42",
112 );
113 const list = panel.locator(".browser-download-list");
114 assert.equal(
115 await list.evaluate((el) => el.scrollHeight > el.clientHeight),
116 true,
117 "long history scrolls inside the panel",
118 );
119 assert.equal(
120 await list.evaluate((el) => el.scrollWidth <= el.clientWidth),
121 true,
122 "long file names and paths do not widen the panel",
123 );
124 const row = panel.locator("article").first();
125 await row.getByRole("button", { name: "打开文件", exact: true }).click();
126 await row
127 .getByRole("button", { name: "在文件夹中显示", exact: true })
128 .click();
129 await row.getByRole("button", { name: "另存为…", exact: true }).click();
130 await panel
131 .getByRole("button", { name: "重试保存", exact: true })
132 .click();
133 await panel
134 .getByRole("button", { name: "重试保存", exact: true })
135 .waitFor({ state: "visible" });
136 await panel.getByRole("button", { name: "关闭面板", exact: true }).focus();
137 await page.screenshot({ path: `${output}/${platform}-${width}.png` });
138 if (platform === "darwin" && width === 1280) await panel.screenshot({ path: `${output}/panel.png` });
139 await list.evaluate((el) => {
140 el.scrollTop = el.scrollHeight;
141 });
142 await panel
143 .getByText("历史文件 10.txt", { exact: true })
144 .scrollIntoViewIfNeeded();
145 await page.keyboard.press("Escape");
146 await panel.waitFor({ state: "hidden" });
147 assert.equal(
148 await trigger.evaluate((el) => el === document.activeElement),
149 true,
150 "Escape restores the download button focus",
151 );
152 await trigger.click();
153 await page.getByRole("textbox", { name: "网页地址或搜索" }).click();
154 await panel.waitFor({ state: "hidden" });
155 assert.equal(
156 await page
157 .getByRole("textbox", { name: "网页地址或搜索" })
158 .evaluate((el) => el === document.activeElement),
159 true,
160 "outside clicks keep their intended focus",
161 );
162 }
163 await page.setViewportSize({ width: 1280, height: 850 });
164 if (platform === "darwin") {
165 await page.emulateMedia({ colorScheme: "dark" });
166 await page.waitForFunction(
167 () => document.documentElement.dataset.theme === "dark",
168 );
169 await trigger.click();
170 await panel.waitFor();
171 await page.screenshot({ path: `${output}/dark.png` });
172 await page.evaluate(() =>
173 document.documentElement.style.setProperty("--ui-font-size", "28px"),
174 );
175 await page.screenshot({ path: `${output}/large-text.png` });
176 assert.equal(
177 await panel
178 .locator(".browser-download-list")
179 .evaluate((el) => el.scrollWidth <= el.clientWidth),
180 true,
181 );
182 await page.setViewportSize({ width: 900, height: 600 });
183 await page.waitForFunction(() => {
184 const box = document.querySelector(".browser-download-panel").getBoundingClientRect();
185 return box.right <= window.innerWidth && box.bottom <= window.innerHeight;
186 });
187 const resized = await panel.boundingBox();
188 assert.ok(
189 resized.x + resized.width <= 900 && resized.y + resized.height <= 600,
190 "open panel follows window resizing",
191 );
192 await page.keyboard.press("Escape");
193 await page.evaluate(() =>
194 document.documentElement.style.removeProperty("--ui-font-size"),
195 );
196 await page.emulateMedia({ colorScheme: "light" });
197 await page.setViewportSize({ width: 1280, height: 850 });
198 }
199 await page.getByRole("button", { name: "浏览器菜单", exact: true }).click();
200 const menu = page.getByRole("menu", { name: "浏览器菜单", exact: true });
201 assert.equal(
202 await menu
203 .getByRole("menuitem", { name: "设置", exact: true })
204 .locator("svg.lucide-settings")
205 .count(),
206 1,
207 );
208 await menu
209 .getByRole("menuitem", { name: "浏览器下载", exact: true })
210 .click();
211 await panel.waitFor();
212 await page.keyboard.press("Escape");
213 await panel.waitFor({ state: "hidden" });
214 await page.getByRole("button", { name: "展开侧栏", exact: true }).click();
215 await page.getByRole("button", { name: "下载文件", exact: true }).click();
216 await panel.waitFor();
217 assert.ok(
218 (await panel.boundingBox()).x > 800,
219 "sidebar also opens the panel at the upper right",
220 );
221 await panel.getByRole("button", { name: "关闭面板", exact: true }).click();
222 records = [];
223 await page.reload();
224 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
225 await trigger.click();
226 await panel.getByText("暂无下载", { exact: true }).waitFor();
227 await page.screenshot({ path: `${output}/${platform}-empty.png` });
228 assert.ok(
229 calls.some(
230 (call) =>
231 call.action === "browserDownloadAction" && call.operation === "open",
232 ),
233 );
234 assert.ok(
235 calls.some(
236 (call) =>
237 call.action === "browserDownloadAction" &&
238 call.operation === "reveal",
239 ),
240 );
241 assert.ok(calls.some((call) => call.action === "saveBrowserDownload"));
242 assert.ok(
243 calls.some((call) => call.action === "retry" && call.id === "failed"),
244 );
245 assert.deepEqual(errors, []);
246 console.log(
247 `PASS: ${platform} context, toolbar/menu/sidebar downloads, 1280/375 layout, progress, scroll, empty state, file actions, retry, Escape/outside dismissal and gear icons.`,
248 );
249 } finally {
250 await browser.close();
251 }
252}
253console.log(`Screenshots: ${output}`);