跳到主要内容

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

浏览文件

test-macos-install.mjs

123 行 · 6.8 KB历史下载
1// Exercise the manual macOS installation path in a disposable user directory.
2import assert from "node:assert/strict";
3import { execFileSync } from "node:child_process";
4import { createHash, randomUUID } from "node:crypto";
5import fs from "node:fs/promises";
6import { createRequire } from "node:module";
7import os from "node:os";
8import path from "node:path";
9import { seed, verify } from "./personal-data-fixture.mjs";
10
11const source = process.argv[2];
12if (process.platform !== "darwin" || !source || !path.isAbsolute(source) || !source.endsWith(".app")) {
13 throw new Error("Usage on macOS: node scripts/test-macos-install.mjs /absolute/path/to/Cage.app");
14}
15const require = createRequire(import.meta.url);
16const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
17const root = await fs.mkdtemp(path.join(os.tmpdir(), "cage-macos-install-"));
18const qaHome = path.join(root, "user");
19const manifest = path.join(root, "personal-data.json");
20// The 12 preservation fixtures contain marker bytes. Use a separate valid profile for login.
21const profile = path.join(root, "runtime-profile");
22const env = { ...process.env, HOME: qaHome, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") };
23const installed = path.join(qaHome, "Applications", "Cage.app");
24const mount = path.join(root, "mount");
25const image = path.join(root, "manual-install.dmg");
26const stage = path.join(root, "volume");
27const run = (exe, args, options = {}) => execFileSync(exe, args, { encoding: "utf8", timeout: 180000, ...options });
28const cli = (...args) => run(path.join(installed, "Contents/Resources/cage"), args, { env });
29const hash = async file => createHash("sha256").update(await fs.readFile(file)).digest("hex");
30const password = randomUUID() + "7z";
31const username = "install_smoke";
32const evidence = process.env.CAGE_REMOTE_QA_DIR;
33let browser, mounted = false, running = false;
34
35try {
36 await seed(qaHome, manifest);
37 await fs.mkdir(path.dirname(installed), { recursive: true });
38 await fs.mkdir(stage);
39 await fs.mkdir(mount);
40 run("ditto", [source, path.join(stage, "Cage.app")]);
41 run("hdiutil", ["create", "-quiet", "-volname", "Cage-Isolated-QA", "-srcfolder", stage, "-format", "UDZO", image]);
42 run("hdiutil", ["attach", "-nobrowse", "-readonly", "-mountpoint", mount, image]);
43 mounted = true;
44 const result = {
45 dmg_sha256: await hash(image),
46 backend_sha256: await hash(path.join(source, "Contents/Resources/cage")),
47 launcher_sha256: await hash(path.join(source, "Contents/MacOS/Cage")),
48 };
49
50 if (evidence) await fs.mkdir(evidence, { recursive: true });
51 browser = await chromium.launch({ headless: true, executablePath: process.env.CAGE_TEST_BROWSER_PATH });
52 let savedAccountHash;
53 for (const phase of ["install", "reinstall"]) {
54 if (phase === "reinstall") await fs.rm(installed, { recursive: true });
55 run("ditto", [path.join(mount, "Cage.app"), installed]);
56 if (savedAccountHash) assert.equal(await hash(path.join(profile, "account.json")), savedAccountHash, "reinstall changed the account file");
57 run("codesign", ["--verify", "--deep", "--strict", installed]);
58 assert.equal(await hash(path.join(installed, "Contents/Resources/cage")), result.backend_sha256);
59 cli("verify-web-assets");
60 if (process.arch === "arm64") cli("verify-macos-guest");
61 running = true;
62 const entry = JSON.parse(cli("desktop", "--json"));
63 assert.equal(entry.status, "ready");
64 const page = await browser.newPage();
65 const errors = [];
66 page.on("pageerror", error => errors.push(error.message));
67 try {
68 await page.goto(entry.url);
69 if (phase === "install") {
70 await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor();
71 await page.getByRole("checkbox", { name: /^记住我/ }).uncheck();
72 await page.getByLabel("用户名", { exact: true }).fill(username);
73 await page.getByLabel("设置密码", { exact: true }).fill(password);
74 await page.getByLabel("确认密码", { exact: true }).fill(password);
75 await page.getByRole("button", { name: "创建本地账户", exact: true }).click();
76 } else {
77 await page.getByLabel("账户密码", { exact: true }).fill(password);
78 await page.getByRole("button", { name: "解锁 Cage", exact: true }).click();
79 }
80 await page.getByRole("heading", { name: "为 Cage 准备独立环境", exact: true }).waitFor();
81 assert.equal(await page.getByRole("navigation", { name: "主导航" }).count(), 0, "workbench must remain hidden before initialization");
82 if (evidence) await page.screenshot({ animations: "disabled", path: path.join(evidence, `${phase}-initialization.png`) });
83 await page.getByRole("button", { name: "下一步:配置代理", exact: true }).click();
84 await page.getByRole("heading", { name: "连接你的网络代理", exact: true }).waitFor();
85 assert.ok(await page.getByRole("button", { name: "开始安装", exact: true }).isDisabled(), "a fresh profile must require a proxy before installing");
86 // Do not start a VM or configure real network credentials in package-preservation QA.
87 await page.getByRole("button", { name: "上一步", exact: true }).click();
88 await page.getByRole("heading", { name: "为 Cage 准备独立环境", exact: true }).waitFor();
89 const account = await page.evaluate(async () => {
90 const context = await (await fetch("/api/client/context")).json();
91 return (await fetch("/api/account", { headers: { "X-Cage-Connection": context.connection_id } })).json();
92 });
93 assert.equal(account.username, username);
94 assert.equal(account.unlocked, true);
95 const status = await page.evaluate(async () => (await fetch("/api/client/local-status")).json());
96 assert.ok(status.known && status.service_running && !status.running, "installation QA must leave the VM stopped");
97 assert.deepEqual(errors, []);
98 result[phase] = { original_account: true, initialization_wizard: true, proxy_required: true, vm_stopped: true, preserved: await verify(qaHome, manifest) };
99 } finally {
100 await page.close();
101 }
102 cli("prepare-update", "--json");
103 running = false;
104 savedAccountHash = await hash(path.join(profile, "account.json"));
105 }
106 const accountHash = await hash(path.join(profile, "account.json"));
107 await fs.rm(installed, { recursive: true });
108 assert.equal(await hash(path.join(profile, "account.json")), accountHash);
109 result.uninstall = { account_preserved: true, preserved: await verify(qaHome, manifest) };
110 if (evidence) await fs.writeFile(path.join(evidence, "result.json"), JSON.stringify(result, null, 2));
111 console.log(JSON.stringify(result, null, 2));
112} finally {
113 try {
114 if (running) cli("prepare-update", "--json");
115 } finally {
116 try {
117 if (browser) await browser.close();
118 } finally {
119 if (mounted) run("hdiutil", ["detach", mount]);
120 await fs.rm(root, { recursive: true });
121 }
122 }
123}