跳到主要内容

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

浏览文件

account-remember-live-smoke.mjs

106 行 · 5.4 KB历史下载
1// Production UI + real local controller. Uses only a freshly created profile;
2// never opens or starts a VM, and stores OS trust under its temporary HOME.
3import assert from "node:assert/strict";
4import { randomBytes } from "node:crypto";
5import fs from "node:fs/promises";
6import os from "node:os";
7import path from "node:path";
8import { execFile } from "node:child_process";
9import { promisify } from "node:util";
10import { createRequire } from "node:module";
11import { fileURLToPath } from "node:url";
12
13const cage = process.argv[2];
14assert.ok(path.isAbsolute(cage || ""), "Supply an absolute Cage helper path");
15const run = promisify(execFile);
16const root = await fs.mkdtemp(path.join(os.tmpdir(), "cage-remember-live-"));
17const env = { ...process.env, HOME: root, LOCALAPPDATA: root, CLAUDE_CAGE_CONFIG: path.join(root, "profile", "config.json") };
18const output = new URL("../../docs/test-reports/2026-11-08-remember-device/", import.meta.url);
19await fs.mkdir(output, { recursive: true });
20const require = createRequire(import.meta.url);
21const { chromium, webkit } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
22const engine = process.env.CAGE_TEST_BROWSER === "webkit" ? webkit : chromium;
23const browser = await engine.launch({ headless: true });
24let context, page;
25const errors = [];
26const command = async (...args) => JSON.parse((await run(cage, args, { env, timeout: 60000 })).stdout);
27const account = () => page.evaluate(async () => {
28 const context = await (await fetch("/api/client/context")).json();
29 return (await fetch("/api/account", { headers: { "X-Cage-Connection": context.connection_id } })).json();
30});
31const menu = () => page.getByRole("button", { name: /^账户菜单,/ });
32const remember = () => page.getByRole("checkbox", { name: /^记住我/ });
33const password = randomBytes(24).toString("base64url");
34const restart = async () => {
35 await context?.close();
36 await command("prepare-update", "--json");
37 const entry = await command("desktop", "--json");
38 context = await browser.newContext();
39 page = await context.newPage();
40 page.on("pageerror", e => errors.push(e.message));
41 await page.goto(entry.url);
42};
43const unlock = async (enabled) => {
44 await remember().setChecked(enabled);
45 await page.getByLabel("账户密码", { exact: true }).fill(password);
46 const response = page.waitForResponse(r => r.url().endsWith("/api/client/trusted-login"));
47 await page.getByRole("button", { name: "解锁 Cage", exact: true }).click();
48 assert.equal((await response).status(), 200);
49 await menu().waitFor();
50};
51try {
52 await restart();
53 await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor();
54 await remember().waitFor();
55 assert.equal(await remember().isChecked(), true);
56 await remember().uncheck();
57 await page.getByLabel("用户名", { exact: true }).fill("remember_live_qa");
58 await page.getByLabel("设置密码", { exact: true }).fill(password);
59 await page.getByLabel("确认密码", { exact: true }).fill(password);
60 const created = page.waitForResponse(r => r.url().endsWith("/api/client/trusted-login"));
61 await page.getByRole("button", { name: "创建本地账户", exact: true }).click();
62 assert.equal((await created).status(), 200);
63 await menu().waitFor();
64 assert.equal(Boolean((await account()).trusted_login), false);
65
66 await restart();
67 await remember().waitFor();
68 assert.equal(await remember().isChecked(), false);
69 await unlock(true);
70 assert.equal((await account()).trusted_login, true);
71 const record = JSON.parse(await fs.readFile(path.join(root, "profile", "account.json"), "utf8"));
72 assert.equal(record.devices?.automatic, undefined);
73 assert.equal(record.trusted_unlock.sealed_key.length > 0, true);
74
75 await restart();
76 await menu().waitFor();
77 assert.equal((await account()).unlocked, true);
78 await menu().click();
79 await page.getByRole("menuitem", { name: /锁定 Cage/ }).click();
80 await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor();
81 await restart();
82 await remember().waitFor();
83 assert.equal((await account()).unlocked, false);
84 assert.equal((await account()).require_authentication, true);
85 await page.screenshot({ path: fileURLToPath(new URL(`live-${engine === webkit ? "webkit" : "chromium"}-locked.png`, output)) });
86 await unlock(false);
87 assert.equal(Boolean((await account()).trusted_login), false);
88 await restart();
89 await remember().waitFor();
90 assert.equal(await remember().isChecked(), false);
91 assert.equal((await account()).unlocked, false);
92 await unlock(true);
93 await menu().click();
94 await page.getByRole("menuitem", { name: "退出账户", exact: true }).click();
95 await page.getByRole("dialog").getByRole("button", { name: "停止环境并退出账户", exact: true }).click();
96 await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor();
97 assert.equal(Boolean((await account()).trusted_login), false);
98 const final = JSON.parse(await fs.readFile(path.join(root, "profile", "account.json"), "utf8"));
99 assert.equal(final.sealed_key, record.sealed_key, "workspace password-wrapped key changed");
100 assert.deepEqual(errors, []);
101 console.log("PASS: real production UI/controller, isolated OS trust, create opt-out, password opt-in, controller restart, remembered login, persistent manual lock, opt-out restart, logout revocation and unchanged workspace key");
102} finally {
103 await browser.close();
104 await command("prepare-update", "--json");
105 await fs.rm(root, { recursive: true, force: true });
106}