跳到主要内容

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

浏览文件

macos-resources-smoke.mjs

75 行 · 5.1 KB历史下载
1import assert from "node:assert/strict";
2import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
3import { tmpdir } from "node:os";
4import { join } from "node:path";
5import { createWorkbench } from "./workbench-fixture.mjs";
6
7const output = process.env.CAGE_RESOURCE_QA_OUTPUT || await mkdtemp(join(tmpdir(), "cage-macos-resource-ui-"));
8await mkdir(output, { recursive: true });
9const requests = [];
10let failSave = false, reads = 0;
11const resource = { runtime: "vz-macos", memory_policy: "fixed", automatic_reclaim: false, running: true,
12 automatic: false, memory_gib: 4, cpus: 2, min_memory_gib: 4, min_cpus: 2, max_memory_gib: 12, max_cpus: 5,
13 minimum_requirements: { source: "existing-configuration" }, recommended_memory_bytes: 12 * 2 ** 30,
14 recommended_cpus: 5, configured_memory_bytes: 4 * 2 ** 30, configured_cpus: 2, restart_required: false,
15 observation: { cpus: 2, cpu_percent: null, guest: { total_bytes: 4 * 2 ** 30, available_bytes: 2 * 2 ** 30 } } };
16const fixture = await createWorkbench({ beforeNavigate: async (page, state) => {
17 state.account.guest_system = state.status.guest_system = "macos";
18 state.status.vm_running = state.status.ready = true;
19 await page.route("**/api/environment/resources", route => {
20 if (route.request().method() === "POST") {
21 const body = route.request().postDataJSON(); requests.push(body);
22 if (failSave) return route.fulfill({ status: 400, contentType: "text/plain; charset=utf-8", body: "资源配置保存失败,请检查磁盘权限。" });
23 if (body.allocation) Object.assign(resource, body.allocation, { restart_required: true });
24 return route.fulfill({ json: { ok: true } });
25 }
26 reads++;
27 resource.observation.observed_at = new Date().toISOString();
28 return route.fulfill({ json: resource });
29 });
30} });
31const { page, browser, errors } = fixture;
32try {
33 await page.getByRole("button", { name: "设置", exact: true }).click();
34 await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "环境与恢复", exact: true }).click();
35 const panel = page.getByRole("region", { name: "计算资源", exact: true });
36 await panel.getByText(/macOS 客机使用固定的启动内存容量/).waitFor();
37 const cpu = panel.locator(".setting-summary-row").filter({ hasText: "CPU 使用率" }).locator("strong");
38 assert.equal(await cpu.innerText(), "等待采样");
39 assert.equal(await panel.getByLabel("内存上限(GiB)", { exact: true }).getAttribute("min"), "4");
40 assert.equal(await panel.getByLabel("CPU 逻辑核心数", { exact: true }).getAttribute("min"), "2");
41 await panel.getByText(/当前最低值沿用已有启动配置/).waitFor();
42 await panel.getByLabel("内存上限(GiB)", { exact: true }).fill("3");
43 await panel.getByRole("button", { name: "保存资源配置", exact: true }).click();
44 assert.equal(requests.length, 0);
45 await panel.getByLabel("内存上限(GiB)", { exact: true }).fill("6");
46 await panel.getByLabel("CPU 逻辑核心数", { exact: true }).fill("3");
47 await panel.getByRole("button", { name: "保存资源配置", exact: true }).click();
48 await panel.getByText("已保存,重启环境后生效。", { exact: true }).waitFor();
49 assert.deepEqual(requests[0].allocation, { automatic: false, memory_gib: 6, cpus: 3 });
50 failSave = true;
51 await panel.getByLabel("内存上限(GiB)", { exact: true }).fill("8");
52 await panel.getByRole("button", { name: "保存资源配置", exact: true }).click();
53 const error = panel.getByRole("alert").filter({ hasText: "资源配置保存失败" });
54 await error.waitFor();
55 const before = reads;
56 resource.observation.cpu_percent = 50;
57 await cpu.getByText("50% / 2 个核心", { exact: true }).waitFor({ timeout: 6000 });
58 assert.ok(reads > before);
59 assert.ok(await error.isVisible());
60 failSave = false;
61 await panel.getByRole("button", { name: "重启并应用已保存配置", exact: true }).click();
62 await page.getByRole("dialog", { name: "重启并应用已保存的资源配置?", exact: true }).getByRole("button", { name: "重启并应用", exact: true }).click();
63 await error.waitFor({ state: "hidden" });
64 assert.deepEqual(requests.at(-1), {});
65 await panel.getByRole("heading", { name: "计算资源", exact: true }).scrollIntoViewIfNeeded();
66 await page.screenshot({ path: join(output, "macos-resources.png"), animations: "disabled" });
67 await panel.getByText("资源统计说明", { exact: true }).click();
68 await panel.getByText(/内存估算使用客机物理容量/).scrollIntoViewIfNeeded();
69 await page.screenshot({ path: join(output, "macos-resource-observation.png"), animations: "disabled" });
70 assert.deepEqual(errors, []);
71 const result = { passed: true, browser: browser.browserType().name(), requests, reads,
72 checks: ["Mac fixed-capacity policy and minimum source visible", "first CPU sample unknown", "minimum enforced before save", "saved allocation waits for restart", "polling preserves save failure", "restart uses existing confirmation and shared endpoint"] };
73 await writeFile(join(output, "result.json"), JSON.stringify(result, null, 2));
74 console.log(JSON.stringify(result));
75} finally { await browser.close(); }