跳到主要内容

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

浏览文件

file-transfer.test.mjs

56 行 · 2.9 KB历史下载
1import test from "node:test";
2import assert from "node:assert/strict";
3import { readFileSync } from "node:fs";
4import vm from "node:vm";
5
6const source = readFileSync(new URL("./file-transfer.js", import.meta.url), "utf8")
7 .replace(/^import .*;\n/, "").replaceAll("export ", "");
8function fixture({ fail, switchAfterUpload = false } = {}) {
9 const calls = []; let active = true;
10 const functions = vm.runInNewContext(source + "\n({sendFiles,shellFilePaths})", {
11 URLSearchParams,
12 api: async (path, body) => { calls.push([path, body]); return { id: "upload-id" }; },
13 uploadFile: async (path, file) => {
14 calls.push([path, file]); if (fail === file.name) throw Error("connection lost");
15 if (switchAfterUpload) active = false;
16 return { ok: true, path: "/home/cage/attachments/" + file.name };
17 },
18 native: async (action, body) => { calls.push([action, body]); return { ok: true, paths: ["/home/cage/attachments/video.mp4"] }; },
19 });
20 return { ...functions, calls, valid: () => active };
21}
22
23test("video and document files remain one ordered batch and bypass image conversion", async () => {
24 const f = fixture();
25 const files = [{ name: "原视频.mp4", size: 8 << 20 }, { name: "中文.txt", size: 13 }];
26 const result = await f.sendFiles({ files, kind: "drop", x: 0.2, y: 0.8 }, "browser", "original-tab");
27 assert.equal(f.calls[0][1].id, "original-tab");
28 assert.equal(f.calls[1][1], files[0]); assert.equal(f.calls[2][1], files[1]);
29 assert.equal(f.calls.at(-1)[0], "/input/files/finish");
30 assert.equal(result.paths.length, 2);
31});
32
33test("a failed file or switched target cancels a batch without delivering partial attachments", async () => {
34 for (const options of [{ fail: "second.mp4" }, { switchAfterUpload: true }]) {
35 const f = fixture(options);
36 await assert.rejects(f.sendFiles({ files: [{ name: "first.txt", size: 1 }, { name: "second.mp4", size: 2 }] }, "browser", "tab", { valid: f.valid }));
37 assert.equal(f.calls.at(-1)[0], "/input/files/cancel");
38 assert.equal(f.calls.some(([path]) => path === "/input/files/finish"), false);
39 }
40});
41
42test("native copied files consume an opaque selection handle for the original destination", async () => {
43 const f = fixture();
44 const result = await f.sendFiles({ fileSelection: "selected-files" }, "terminal", "original-terminal");
45 assert.equal(f.calls[1][0], "uploadHostFiles");
46 assert.equal(f.calls[1][1].selection, "selected-files");
47 assert.equal(f.calls[1][1].target, "terminal");
48 assert.equal(f.calls[1][1].id, "upload-id");
49 assert.equal(result.paths[0], "/home/cage/attachments/video.mp4");
50});
51
52test("attachment paths preserve apostrophes in both PowerShell and POSIX shells", () => {
53 const f = fixture();
54 assert.equal(f.shellFilePaths(["C:\\Users\\Cage\\O'Brien.txt"]), "'C:\\Users\\Cage\\O''Brien.txt' ");
55 assert.equal(f.shellFilePaths(["/home/cage/O'Brien.txt"]), "'/home/cage/O'\\''Brien.txt' ");
56});