跳到主要内容

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

浏览文件

browser-files-smoke.mjs

168 行 · 5.8 KB历史下载
1import assert from "node:assert/strict";
2import { createWorkbench } from "./workbench-fixture.mjs";
3const id = "34e399c1-78fe-4cf1-93b0-d7fef431b211";
4const chooser = "61c716bb-5e8f-4a32-b872-cfa099a695ef";
5const nativeCalls = [],
6 uploads = [];
7let saved = [],
8 selections = [],
9 cancelSave = true;
10const download = {
11 id,
12 name: "video.mp4",
13 state: "downloading",
14 receivedBytes: 3,
15 totalBytes: 100,
16};
17const fixture = await createWorkbench({
18 nativeRequest: async (body) => {
19 if (body.action === "chooseUploadFiles") { nativeCalls.push(body); return { fileSelection: "fixture-selection", count: 2 }; }
20 if (body.action === "uploadHostFiles") { nativeCalls.push(body); assert.equal(body.target, "browser"); assert.equal(body.id, chooser); assert.equal(body.selection, "fixture-selection"); return {ok:true}; }
21 if (body.action === "saveBrowserDownload") {
22 nativeCalls.push(body);
23 if (cancelSave) return { canceled: true };
24 saved = [
25 { id, name: "视频.mp4", path: "/Users/test/Downloads/视频.mp4" },
26 ];
27 return saved[0];
28 }
29 if (body.action === "browserDownloadAction") {
30 nativeCalls.push(body);
31 return { ok: true };
32 }
33 return null;
34 },
35});
36const { browser, page, state, errors } = fixture;
37await page.route("**/api/browser/tabs", (route) =>
38 route.fulfill({
39 json: {
40 tabs: state.tabs,
41 active: state.active,
42 downloads: [download],
43 uploads: selections,
44 },
45 }),
46);
47await page.route("**/api/browser/files/saved", (route) =>
48 route.fulfill({ json: saved }),
49);
50await page.route("**/api/browser/files/uploads/**", async (route) => {
51 uploads.push({
52 name: new URL(route.request().url()).searchParams.get("name"),
53 bytes: route.request().postDataBuffer(),
54 });
55 await route.fulfill({ json: { ok: true } });
56});
57await page.route("**/api/browser/upload-finish", (route) => {
58 assert.equal(route.request().postDataJSON().id, chooser);
59 selections = [];
60 return route.fulfill({ json: { ok: true } });
61});
62await page.route("**/api/browser/upload-cancel", (route) => {
63 selections = [];
64 return route.fulfill({ json: { ok: true } });
65});
66const poll = async (condition) => {
67 for (let i = 0; i < 120; i++) {
68 if (condition()) return;
69 await new Promise((resolve) => setTimeout(resolve, 50));
70 }
71 throw new Error("UI state timeout");
72};
73try {
74 state.status.ready = true;
75 state.status.vm_running = true;
76 await page.reload();
77 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
78 await page.getByRole("button", { name: "浏览器下载", exact: true }).click();
79 await page.getByText("正在下载", { exact: false }).waitFor();
80 await page
81 .getByRole("dialog", { name: "下载文件" })
82 .getByRole("button", { name: "关闭面板" })
83 .click();
84 selections = [{ id: chooser, tabId: "tab-1", multiple: true }];
85 await page.getByRole("dialog", { name: "上传本机文件" }).waitFor();
86 Object.assign(download, {
87 state: "saved",
88 path: "/Users/test/Cage/Downloads/video.mp4",
89 });
90 await page.getByRole("button", { name: "选择本机文件", exact: true }).click();
91 await page.getByRole("dialog", { name: "上传本机文件" }).waitFor({ state: "hidden" });
92 assert.ok(nativeCalls.some(call => call.action === "uploadHostFiles"));
93 await poll(() =>
94 nativeCalls.some((call) => call.action === "saveBrowserDownload"),
95 );
96 assert.equal(saved.length, 0, "cancel must not invent a save destination");
97 await page.getByRole("button", { name: "浏览器下载", exact: true }).click();
98 await page
99 .getByText("/Users/test/Cage/Downloads/video.mp4", { exact: true })
100 .waitFor();
101 cancelSave = false;
102 await page.getByRole("button", { name: "另存为…", exact: true }).click();
103 await page
104 .getByText("/Users/test/Downloads/视频.mp4", { exact: true })
105 .waitFor();
106 await page
107 .getByRole("dialog", { name: "下载文件" })
108 .getByRole("button", { name: "打开文件", exact: true })
109 .click();
110 await page
111 .getByRole("button", { name: "在文件夹中显示", exact: true })
112 .click();
113 await poll(
114 () =>
115 nativeCalls.filter((call) => call.action === "browserDownloadAction")
116 .length === 2,
117 );
118 assert.deepEqual(
119 nativeCalls
120 .filter((call) => call.action === "browserDownloadAction")
121 .map((call) => [call.operation, call.id]),
122 [
123 ["open", id],
124 ["reveal", id],
125 ],
126 );
127 assert.ok(
128 nativeCalls.every((call) => !("path" in call)),
129 "web UI must not send arbitrary host paths",
130 );
131 await page
132 .getByRole("dialog", { name: "下载文件" })
133 .getByRole("button", { name: "关闭面板" })
134 .click();
135 selections = [{ id: "a8af5851-5837-45ca-a820-1a2565bc7ec6", tabId: "tab-1", multiple: false }];
136 await page
137 .getByRole("dialog", { name: "上传本机文件" })
138 .getByRole("button", { name: "取消上传" })
139 .click();
140 await page
141 .getByRole("dialog", { name: "上传本机文件" })
142 .waitFor({ state: "hidden" });
143 state.status.ready = false;
144 state.status.vm_running = false;
145 await page.waitForFunction(
146 () =>
147 document.querySelector('[aria-label="打开浏览器"]')?.disabled === true,
148 );
149 await page.getByRole("button", { name: "下载文件", exact: true }).click();
150 await page
151 .getByText("/Users/test/Downloads/视频.mp4", { exact: true })
152 .waitFor();
153 await page
154 .getByRole("dialog", { name: "下载文件" })
155 .getByRole("button", { name: "打开文件", exact: true })
156 .click();
157 await poll(
158 () =>
159 nativeCalls.filter((call) => call.action === "browserDownloadAction")
160 .length === 3,
161 );
162 assert.deepEqual(errors, []);
163 console.log(
164 "PASS: workbench native picker with opaque selection handle, multiple files, cancel, deferred Save As, canceled Save As keeps fallback, saved path, Open/Reveal IDs",
165 );
166} finally {
167 await browser.close();
168}