跳到主要内容

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

浏览文件

environment-storage-smoke.mjs

84 行 · 5.2 KB历史下载
1import assert from "node:assert/strict";
2import { createWorkbench } from "./workbench-fixture.mjs";
3
4const platform = process.env.CAGE_STORAGE_PLATFORM || "windows";
5const { browser, page, state, errors } = await createWorkbench({
6 viewport: { width: Number(process.env.CAGE_STORAGE_WIDTH || 1280), height: 850 },
7 clientContext: { mode: "local", connection_id: "local", client_platform: platform, server_platform: platform, connection: { state: "disconnected" } },
8});
9try {
10 state.status.local_vm_storage = true;
11 const windows = platform === "windows";
12 let location = { directory: windows ? "C:\\Users\\demo\\.claude-cage\\state" : "/home/demo/.claude-cage/state", has_environment: false };
13 const writes = [];
14 const destination = windows ? "D:\\开发工具\\Cage 环境" : "/Volumes/开发工具/Cage 环境";
15 const occupied = windows ? "E:\\occupied" : "/mnt/occupied";
16 const migrated = windows ? "E:\\Cage 环境" : "/mnt/Cage 环境";
17 await page.route("**/api/environment/storage", (route) => {
18 if (route.request().method() === "GET") return route.fulfill({ json: location });
19 const input = route.request().postDataJSON();
20 writes.push(input);
21 const rejected = input.directory.endsWith("occupied");
22 const job = { kind: "storage", id: String(writes.length), running: !rejected && input.migrate, error: rejected ? "目录必须为空" : "", message: "正在复制并校验虚拟磁盘,请保持 Cage 开启。" };
23 state.status.job = job;
24 if (!rejected && !job.running) location = { ...location, directory: input.directory, backup: location.has_environment ? location.directory : "" };
25 return route.fulfill({ json: job });
26 });
27 await page.getByRole("button", { name: "选择环境存储位置" }).click();
28 const panel = page.getByRole("region", { name: "本机环境存储" });
29 const picker = panel.getByRole("button", { name: "选择文件夹" });
30 const target = panel.getByLabel("新存储位置");
31 state.chosenFolder = "";
32 await picker.click();
33 assert.equal(await target.inputValue(), "");
34 state.chosenFolder = destination;
35 await picker.click();
36 await page.waitForFunction((path) => [...document.querySelectorAll("input")].some((input) => input.value === path), destination);
37 await panel.getByRole("button", { name: "使用此位置" }).click();
38 await panel.getByText(destination, { exact: true }).waitFor();
39 assert.equal(writes[0].migrate, false);
40 location.has_environment = true;
41 await page.reload();
42 await page.getByRole("button", { name: "选择环境存储位置" }).click();
43 await panel.getByText(destination, { exact: true }).waitFor();
44 await target.fill(occupied);
45 await panel.getByRole("button", { name: "迁移到此位置" }).click();
46 await page.getByRole("dialog").getByRole("button", { name: "取消" }).click();
47 assert.equal(writes.length, 1);
48 await panel.getByRole("button", { name: "迁移到此位置" }).click();
49 await page.getByRole("dialog").getByRole("button", { name: "迁移环境" }).click();
50 await panel.getByText("目录必须为空", { exact: true }).waitFor();
51 await target.fill(migrated);
52 await panel.getByRole("button", { name: "迁移到此位置" }).click();
53 await page.getByRole("dialog").getByRole("button", { name: "迁移环境" }).click();
54 const progress = panel.getByRole("status");
55 await progress.waitFor();
56 const actions = panel.locator(".storage-actions");
57 await actions.scrollIntoViewIfNeeded();
58 const geometry = await panel.evaluate((element) => {
59 const buttons = element.querySelector(".storage-actions").getBoundingClientRect();
60 const status = element.querySelector('[role="status"]').getBoundingClientRect();
61 return { buttonsBottom: buttons.bottom, statusTop: status.top };
62 });
63 if (process.env.CAGE_STORAGE_SCREENSHOT) {
64 await panel.getByRole("heading", { name: "本机环境存储" }).scrollIntoViewIfNeeded();
65 await page.screenshot({ path: process.env.CAGE_STORAGE_SCREENSHOT });
66 }
67 assert.ok(geometry.statusTop >= geometry.buttonsBottom + 8, `Migration status overlaps actions: ${JSON.stringify(geometry)}`);
68 for (const button of await actions.getByRole("button").all()) {
69 assert.equal(await button.isDisabled(), true);
70 assert.equal(await button.evaluate((element) => getComputedStyle(element).opacity), "1");
71 }
72 assert.equal(await target.isDisabled(), true);
73 assert.equal(writes.length, 3);
74 location = { ...location, directory: writes.at(-1).directory, backup: location.directory };
75 state.status.job = { ...state.status.job, running: false };
76 await panel.getByText(migrated, { exact: true }).waitFor();
77 await page.waitForFunction(() => !document.querySelector('.environment-storage input').disabled);
78 assert.equal(await progress.count(), 0);
79 assert.equal(writes.at(-1).migrate, true);
80 assert.equal(writes.at(-1).previous, destination);
81 assert.equal(await panel.locator("code").textContent(), destination);
82 assert.deepEqual(errors, []);
83 console.log(`Storage selection, picker cancellation, migration confirmation, running layout, failure and backup location passed (${platform}, ${process.env.CAGE_TEST_BROWSER || "chromium"}, ${page.viewportSize().width}px; status gap ${geometry.statusTop - geometry.buttonsBottom}px)`);
84} finally { await browser.close(); }