跳到主要内容

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

浏览文件

account-remember-smoke.mjs

159 行 · 8.0 KB历史下载
1import assert from "node:assert/strict";
2import fs from "node:fs/promises";
3import { fileURLToPath } from "node:url";
4import { createWorkbench } from "./workbench-fixture.mjs";
5
6const output = new URL("../../docs/test-reports/2026-11-08-remember-device/", import.meta.url);
7await fs.mkdir(output, { recursive: true });
8
9for (const platform of ["windows", "darwin", "linux"]) {
10 let fixture, failTrust = false;
11 const actions = [];
12 const clientContext = { mode: "local", desktop_gateway: true, connection_id: "local", client_platform: platform, server_platform: platform, connection: { state: "connected" } };
13 fixture = await createWorkbench({
14 clientContext,
15 // Automatic trust must work without the macOS-only Keychain bridge.
16 nativeRequest: async ({ action }) => action === "accountDevice" ? { supported: false } : null,
17 clientRequest: async (route, path, body) => {
18 if (path === "/client/context") return route.fulfill({ json: clientContext });
19 if (path !== "/client/trusted-login") return route.fulfill({ json: {} });
20 const a = fixture.state.account;
21 actions.push(body.action);
22 if (failTrust) return route.fulfill({ status: 409, contentType: "text/plain; charset=utf-8", body: "测试:无法保存本机信任凭据" });
23 if (body.action === "enable") {
24 assert.equal(body.password, fixture.state.accountPassword);
25 a.trusted_login = true;
26 delete (a.devices ||= {}).automatic;
27 } else if (body.action === "disable") {
28 a.trusted_login = false;
29 delete (a.devices ||= {}).automatic;
30 } else if (body.action === "resume") {
31 assert.equal(a.require_authentication, false, "automatic login bypassed manual lock");
32 assert.equal(a.trusted_login, true);
33 a.unlocked = true;
34 } else throw Error(`Unexpected trust action: ${body.action}`);
35 return route.fulfill({ json: { ok: true } });
36 },
37 });
38 const { browser, page, state, errors } = fixture;
39 const workbenchURL = page.url();
40 const reopenLocked = async () => {
41 await page.goto("about:blank");
42 state.account.unlocked = false;
43 await page.goto(workbenchURL);
44 };
45 const menu = () => page.getByRole("button", { name: /^账户菜单,/ });
46 const remember = () => page.getByRole("checkbox", { name: /^记住我/ });
47 const trustReply = () => page.waitForResponse(response => response.url().endsWith("/api/client/trusted-login"));
48 const login = async (password = "fixture-password") => {
49 await page.getByLabel("账户密码", { exact: true }).fill(password);
50 const saved = password === "fixture-password" ? trustReply() : null;
51 await page.getByRole("button", { name: "解锁 Cage", exact: true }).click();
52 if (saved) await (await saved).finished();
53 };
54 const settings = async () => {
55 await menu().click();
56 await page.getByRole("menuitem", { name: "设置", exact: true }).click();
57 };
58 try {
59 Object.assign(state.account, { unlocked: false, require_authentication: false });
60 await page.reload();
61 await remember().waitFor();
62 assert.equal(await remember().isChecked(), false, "existing account was opted in without consent");
63 await remember().check();
64 await login("incorrect-password");
65 await page.getByRole("alert").filter({ hasText: "密码不正确" }).waitFor();
66 assert.deepEqual(actions, []);
67 await page.screenshot({ path: fileURLToPath(new URL(`${platform}-login.png`, output)) });
68 await login();
69 assert.equal(state.account.trusted_login, true);
70 await menu().waitFor();
71 await reopenLocked();
72 await menu().waitFor();
73 assert.equal(actions.filter(a => a === "resume").length, 1);
74
75 await menu().click();
76 await page.getByRole("menuitem", { name: /锁定 Cage/ }).click();
77 await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor();
78 await page.reload();
79 await remember().waitFor();
80 assert.equal(await remember().isChecked(), true);
81 assert.equal(actions.filter(a => a === "resume").length, 1);
82 await remember().uncheck();
83 assert.equal(await remember().isChecked(), false);
84 await login();
85 assert.equal(state.account.trusted_login, false);
86 await reopenLocked();
87 await remember().waitFor();
88 assert.equal(await remember().isChecked(), false);
89 assert.equal(actions.filter(a => a === "resume").length, 1);
90 await login();
91 await settings();
92 await page.getByLabel("验证账户密码", { exact: true }).fill("fixture-password");
93 await page.getByRole("button", { name: "信任此电脑", exact: true }).click();
94 await page.getByRole("button", { name: "关闭记住我", exact: true }).waitFor();
95 await page.screenshot({ path: fileURLToPath(new URL(`${platform}-settings.png`, output)) });
96 await page.getByRole("button", { name: "关闭记住我", exact: true }).click();
97 await page.getByLabel("验证账户密码", { exact: true }).waitFor();
98
99 // Existing macOS grants share the same opt-out control.
100 state.account.devices = { automatic: "legacy-keychain", biometric: "touch-id" };
101 await page.reload();
102 await settings();
103 await page.getByRole("button", { name: "关闭记住我", exact: true }).click();
104 await page.getByLabel("验证账户密码", { exact: true }).waitFor();
105 assert.deepEqual(state.account.devices, { biometric: "touch-id" });
106
107 // A trust-storage failure must not undo a successful password login.
108 await reopenLocked();
109 await remember().check();
110 failTrust = true;
111 await login();
112 await menu().waitFor();
113 await settings();
114 await page.getByText(/已登录,记住我未能开启/).waitFor();
115 assert.equal(Boolean(state.account.trusted_login), false);
116 failTrust = false;
117
118 Object.assign(state.account, { exists: false, unlocked: false, username: "", devices: {}, require_authentication: false });
119 await page.reload();
120 await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor();
121 await remember().waitFor();
122 assert.equal(await remember().isChecked(), true);
123 await page.getByLabel("用户名", { exact: true }).fill("测试工作区");
124 await page.getByLabel("设置密码", { exact: true }).fill("fixture-password");
125 await page.getByLabel("确认密码", { exact: true }).fill("fixture-password");
126 const createdTrust = trustReply();
127 await page.getByRole("button", { name: "创建本地账户", exact: true }).click();
128 await (await createdTrust).finished();
129 assert.equal(state.account.trusted_login, true);
130 await menu().click();
131 await page.getByRole("menuitem", { name: "退出账户", exact: true }).click();
132 await page.getByRole("dialog").getByRole("button", { name: "停止环境并退出账户", exact: true }).click();
133 // The real backend revokes the grant after a successful environment stop.
134 state.account.trusted_login = false;
135 await page.reload();
136 await remember().waitFor();
137 assert.equal(await remember().isChecked(), false);
138 assert.deepEqual(errors, []);
139 console.log(`PASS ${platform}: create, password login, remember, reopen, manual lock, opt-out, legacy migration, settings, storage failure and logout`);
140 } catch (error) {
141 await page.screenshot({ path: fileURLToPath(new URL(`${platform}-failure.png`, output)) });
142 console.error(await page.locator("body").innerText());
143 console.error({ actions, account: state.account });
144 throw error;
145 } finally { await browser.close(); }
146}
147
148const remote = await createWorkbench({
149 clientContext: { mode: "remote", desktop_gateway: true, connection_id: "remote", client_platform: "windows", server_platform: "linux", connection: { state: "connected" } },
150 nativeRequest: async ({ action }) => action === "accountDevice" ? { supported: false } : null,
151});
152try {
153 remote.state.account.unlocked = false;
154 await remote.page.reload();
155 await remote.page.getByLabel("账户密码", { exact: true }).waitFor();
156 assert.equal(await remote.page.getByRole("checkbox", { name: /^记住我/ }).count(), 0);
157 assert.deepEqual(remote.errors, []);
158 console.log("PASS remote: local computer trust is unavailable for a remote host");
159} finally { await remote.browser.close(); }