跳到主要内容

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

浏览文件

macos-desktop-files-smoke.mjs

103 行 · 6.7 KB历史下载
1import assert from "node:assert/strict";
2import { createRequire } from "node:module";
3import { fileURLToPath } from "node:url";
4import { createServer } from "vite";
5
6const require = createRequire(import.meta.url);
7const engine = process.env.CAGE_TEST_BROWSER || "chromium";
8const playwright = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
9const vite = await createServer({ root: fileURLToPath(new URL("..", import.meta.url)), configFile: fileURLToPath(new URL("../vite.config.js", import.meta.url)), server: { host: "127.0.0.1", port: 0 } });
10await vite.listen();
11const origin = `http://127.0.0.1:${vite.httpServer.address().port}`;
12const browser = await playwright[engine].launch({ headless: true });
13const page = await browser.newPage({ viewport: { width: 1280, height: 850 } });
14const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScLbtAAAAABJRU5ErkJggg==";
15const operations = [], errors = [];
16let sequence = 0, failUpload = false, chooser = null;
17page.on("pageerror", error => errors.push(error.message));
18await page.route("**/mac-files-test", async route => route.fulfill({ contentType: "text/html", body: await vite.transformIndexHtml("/mac-files-test", '<div id="root"></div><script type="module" src="/scripts/macos-desktop-files-fixture.jsx"></script>') }));
19await page.route("**/api/**", async route => {
20 const request = route.request(), url = new URL(request.url());
21 if (url.pathname === "/api/macos/claude/window") {
22 const value = request.postDataJSON();
23 if (value.action === "status") return route.fulfill({ json: { installed: true, running: true, input: true, screen: true, persistent: false, version: "fixture" } });
24 if (value.action === "frame") return route.fulfill({ json: { image: png, pid: 100, window: 7, geometry: "stable-window", contentWidth: 640, contentHeight: 480 } });
25 return route.fulfill({ json: { ok: true } });
26 }
27 if (url.pathname === "/api/input/files/desktop/requests") return route.fulfill({ json: { available: true, uploads: chooser ? [chooser] : [], downloads: [], opens: [] } });
28 if (url.pathname === "/api/input/files/start") {
29 const body = request.postDataJSON();
30 operations.push({ action: "start", body });
31 return route.fulfill({ json: { ok: true, id: `11111111-2222-4333-8444-${String(++sequence).padStart(12, "0")}` } });
32 }
33 if (url.pathname.startsWith("/api/input/files/desktop/")) {
34 operations.push({ action: "upload", name: url.searchParams.get("name"), bytes: request.postDataBuffer()?.length });
35 if (failUpload) { failUpload = false; return route.fulfill({ status: 500, contentType: "text/plain", body: "测试附件传递中断,请重新选择文件" }); }
36 return route.fulfill({ json: { ok: true } });
37 }
38 if (["/api/input/files/finish", "/api/input/files/cancel"].includes(url.pathname)) {
39 operations.push({ action: url.pathname.split("/").at(-1), body: request.postDataJSON() });
40 chooser = null;
41 return route.fulfill({ json: { ok: true } });
42 }
43 if (url.pathname === "/api/input/image") {
44 operations.push({ action: "image", body: request.postDataJSON() });
45 return route.fulfill({ json: { ok: true } });
46 }
47 if (url.pathname === "/api/input/paste/start") return route.fulfill({ json: { available: true, transfer: "22222222-3333-4444-8555-666666666666" } });
48 return route.fulfill({ json: { running: true, ready: true, available: true } });
49});
50const waitForCount = async (action, count) => {
51 for (let attempt = 0; attempt < 150; attempt++) {
52 if (operations.filter(item => item.action === action).length >= count) return;
53 await new Promise(resolve => setTimeout(resolve, 40));
54 }
55 throw new Error(`Did not observe ${count} ${action} operations`);
56};
57const choose = async name => {
58 const selected = page.waitForEvent("filechooser");
59 await page.getByRole("button", { name: "添加附件", exact: true }).click();
60 await (await selected).setFiles({ name, mimeType: "text/plain", buffer: Buffer.from("file bytes 中文") });
61};
62try {
63 await page.goto(origin + "/mac-files-test");
64 await page.getByAltText("Claude Desktop 客机窗口").waitFor();
65 await choose("paper.txt");
66 await waitForCount("finish", 1);
67 assert.deepEqual(operations.slice(0, 3).map(item => item.action), ["start", "upload", "finish"]);
68 assert.equal(operations[0].body.target, "desktop");
69 assert.equal(operations[0].body.pid, 100);
70 assert.equal(operations[0].body.window, 7);
71 assert.equal(operations[1].name, "paper.txt");
72 await page.locator(".mac-claude-frame").evaluate(element => {
73 const data = new DataTransfer();
74 data.items.add(new File(["drop data"], "dropped.txt", { type: "text/plain" }));
75 const box = element.getBoundingClientRect();
76 element.dispatchEvent(new DragEvent("drop", { bubbles: true, cancelable: true, dataTransfer: data, clientX: box.x + box.width / 2, clientY: box.y + box.height / 2 }));
77 });
78 await waitForCount("finish", 2);
79 assert.equal(operations.filter(item => item.action === "start").at(-1).body.kind, "drop");
80 await page.getByLabel("向 Claude Desktop 输入").evaluate((element, png) => {
81 const data = new DataTransfer();
82 const bytes = Uint8Array.from(atob(png), value => value.charCodeAt(0));
83 data.items.add(new File([bytes], "image.png", { type: "image/png" }));
84 element.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData: data }));
85 }, png);
86 await waitForCount("image", 1);
87 assert.equal(operations.find(item => item.action === "image").body.target, "desktop");
88 assert.equal(operations.find(item => item.action === "image").body.pid, 100);
89 assert.equal(operations.find(item => item.action === "image").body.window, 7);
90 failUpload = true;
91 await choose("failed.txt");
92 await waitForCount("cancel", 1);
93 await page.getByRole("alert").filter({ hasText: "测试附件传递中断" }).waitFor();
94 await choose("retry.txt");
95 await waitForCount("finish", 3);
96 chooser = { id: "99999999-2222-4333-8444-111111111111", kind: "chooser", multiple: true, target: "desktop" };
97 await page.getByText("上传本机文件", { exact: true }).waitFor();
98 await page.locator('.browser-upload-body input[type="file"]').setInputFiles({ name: "native-dialog.txt", mimeType: "text/plain", buffer: Buffer.from("chooser bytes") });
99 await waitForCount("finish", 4);
100 assert.equal(operations.filter(item => item.action === "finish").at(-1).body.id, "99999999-2222-4333-8444-111111111111");
101 assert.deepEqual(errors, []);
102 console.log(JSON.stringify({ result: "PASS", engine, checks: ["attachment button", "file drop", "PNG paste", "failed upload cancels", "retry after failure", "native chooser request"], operations: operations.map(({ body, ...item }) => ({ ...item, ...(body ? { target: body.target, kind: body.kind } : {}) })) }));
103} finally { await browser.close(); await vite.close(); }