跳到主要内容

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

浏览文件

resources-smoke.mjs

79 行 · 5.2 KB历史下载
1import assert from "node:assert/strict";
2import { createWorkbench } from "./workbench-fixture.mjs";
3
4const { browser, page, errors } = await createWorkbench();
5const windowsRuntime = process.env.CAGE_RESOURCE_RUNTIME === "hyperv";
6let allocation = { automatic: true, memory_gib: 12, cpus: 5 };
7let rejectSave = false;
8const saves = [];
9await page.route("**/api/environment/resources", async (route) => {
10 if (route.request().method() === "POST") {
11 if (rejectSave) return route.fulfill({ status: 400, contentType: "text/plain; charset=utf-8", body: "资源配置保存失败" });
12 const body = route.request().postDataJSON();
13 saves.push(body);
14 if (body.allocation) allocation = body.allocation.automatic ? { automatic: true, memory_gib: 12, cpus: 5 } : body.allocation;
15 return route.fulfill({ json: { ok: true } });
16 }
17 return route.fulfill({ json: { ...allocation, ...(windowsRuntime ? { runtime: "hyperv", min_memory_gib: 4, min_cpus: 2 } : {}), running: false, recommended_memory_bytes: 12 * 2 ** 30, recommended_cpus: 5, max_memory_gib: 12, max_cpus: 5 } });
18});
19async function open() {
20 await page.getByRole("button", { name: "设置", exact: true }).click();
21 await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "环境与恢复", exact: true }).click();
22 await page.getByLabel("内存上限(GiB)", { exact: true }).waitFor();
23}
24try {
25 await open();
26 assert.equal(await page.getByRole("button", { name: "保存资源配置", exact: true }).isDisabled(), true);
27 assert.equal(await page.getByRole("button", { name: "恢复默认", exact: true }).isDisabled(), true);
28 if (windowsRuntime) {
29 assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).getAttribute("min"), "4");
30 assert.equal(await page.getByLabel("CPU 逻辑核心数", { exact: true }).getAttribute("min"), "2");
31 await page.getByLabel("内存上限(GiB)", { exact: true }).fill("3");
32 await page.getByRole("button", { name: "保存资源配置", exact: true }).click();
33 assert.equal(saves.length, 0);
34
35 }
36 await page.getByLabel("内存上限(GiB)", { exact: true }).fill("4");
37 await page.getByLabel("CPU 逻辑核心数", { exact: true }).fill("2");
38 assert.equal(await page.getByText("没有未保存的修改", { exact: true }).count(), 0);
39 await page.getByRole("button", { name: "返回工作台", exact: true }).click();
40 const discard = page.getByRole("dialog", { name: "放弃尚未保存的修改?", exact: true });
41 await discard.waitFor();
42 await discard.getByRole("button", { name: "取消", exact: true }).click();
43 assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "4");
44 await page.screenshot({ path: "/tmp/cage-resource-settings.png", fullPage: true });
45 await page.getByRole("button", { name: "保存资源配置", exact: true }).click();
46 await page.getByText("已保存,下次启动环境时生效。", { exact: true }).waitFor();
47 assert.deepEqual(allocation, { automatic: false, memory_gib: 4, cpus: 2 });
48 await page.reload();
49 await open();
50 assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "4");
51 assert.equal(await page.getByLabel("CPU 逻辑核心数", { exact: true }).inputValue(), "2");
52 const count = saves.length;
53 await page.getByLabel("内存上限(GiB)", { exact: true }).fill("13");
54 await page.getByRole("button", { name: "保存资源配置", exact: true }).click();
55 assert.equal(saves.length, count);
56 await page.getByLabel("内存上限(GiB)", { exact: true }).fill("6");
57 rejectSave = true;
58 await page.getByRole("button", { name: "保存资源配置", exact: true }).click();
59 await page.getByRole("alert").filter({ hasText: "资源配置保存失败" }).waitFor();
60 await page.waitForResponse(response => response.url().endsWith("/api/environment/resources") && response.request().method() === "GET" && response.ok());
61
62 assert.equal(await page.getByRole("alert").filter({ hasText: "资源配置保存失败" }).isVisible(), true);
63 assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "6");
64 rejectSave = false;
65 await page.getByRole("button", { name: "恢复默认", exact: true }).click();
66 assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "12");
67 assert.equal(await page.getByLabel("CPU 逻辑核心数", { exact: true }).inputValue(), "5");
68 assert.equal(allocation.automatic, false, "reset stays a draft until saved");
69 await page.getByRole("button", { name: "保存资源配置", exact: true }).click();
70 await page.getByText("已保存,下次启动环境时生效。", { exact: true }).waitFor();
71 assert.equal(allocation.automatic, true);
72 await page.reload();
73 await open();
74 assert.equal(await page.getByRole("button", { name: "恢复默认", exact: true }).isDisabled(), true);
75 assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "12");
76 assert.equal(await page.getByRole("alert").filter({ hasText: "资源配置保存失败" }).count(), 0);
77 assert.deepEqual(errors, []);
78 console.log("Resource settings: manual save, reload, bounds, failure retention, defaults, editing, reset and persistence passed.");
79} finally { await browser.close(); }