跳到主要内容

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

浏览文件

remote-hosts-smoke.mjs

105 行 · 7.3 KB历史下载
1// Real client service and browser interaction, using only a fresh temp profile.
2// No cloud host or personal Cage configuration is accessed.
3import assert from "node:assert/strict";
4import { spawn } from "node:child_process";
5import { createRequire } from "node:module";
6import { mkdtemp, readFile, mkdir, writeFile } from "node:fs/promises";
7import { tmpdir } from "node:os";
8import { join, resolve } from "node:path";
9
10const require = createRequire(import.meta.url);
11const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
12const executable = process.env.CAGE_CLIENT_BINARY;
13if (!executable) throw new Error("Set CAGE_CLIENT_BINARY to the newly built client binary");
14const profile = await mkdtemp(join(tmpdir(), "cage-remote-host-ui-"));
15const artifacts = resolve(process.env.CAGE_REMOTE_QA_DIR || join(profile, "artifacts"));
16await mkdir(artifacts, { recursive: true });
17const service = spawn(executable, ["serve"], { env: { ...process.env, CLAUDE_CAGE_CONFIG: join(profile, "config.json") }, stdio: "ignore" });
18let browser;
19let page;
20try {
21 let control;
22 for (let i = 0; i < 200; i++) {
23 try { control = JSON.parse(await readFile(join(profile, "control.json"), "utf8")); break; }
24 catch { if (service.exitCode !== null) throw new Error("Client service exited before publishing its private control record"); await new Promise((r) => setTimeout(r, 50)); }
25 }
26 assert.ok(control, "client service did not start");
27 const origin = `http://localhost:${control.port}`;
28 const launched = await fetch(`${origin}/api/launch`, { method: "POST", headers: { "Content-Type": "application/json", Origin: origin }, body: JSON.stringify({ token: control.token }) });
29 assert.equal(launched.status, 200);
30 const entry = await launched.json();
31 browser = await chromium.launch({ headless: true, executablePath: process.env.CAGE_TEST_BROWSER_PATH });
32 page = await browser.newPage({ viewport: { width: 1280, height: 850 } });
33 const errors = [];
34 page.on("pageerror", (error) => errors.push(error.message));
35 await page.goto(entry.url);
36 await page.getByRole("complementary", { name: "SSH 主机", exact: true }).waitFor();
37 assert.equal(await page.getByRole("button", { name: "使用本机" }).count(), 0, "thin client must not offer local workloads");
38 assert.equal(await page.getByRole("button", { name: "创建本地账户" }).count(), 0, "local account must not block remote setup");
39 await page.getByRole("button", { name: "添加远程主机" }).click();
40 await page.getByLabel("主机名称", { exact: true }).fill("隔离 UI 测试主机");
41 await page.getByLabel("服务器地址", { exact: true }).fill("127.0.0.1");
42 await page.getByLabel("SSH 端口", { exact: true }).fill("9");
43 await page.getByLabel("SSH 用户名", { exact: true }).fill("cage-test");
44 const auth = page.getByRole("combobox", { name: "SSH 登录方式" });
45 const keyField = page.getByLabel("本机私钥文件", { exact: true });
46 // Opening the floating list keeps both the dialog and its fields in place.
47 const gap = async () => (await keyField.boundingBox()).y - (await auth.boundingBox()).y;
48 const before = await gap();
49 await auth.click();
50 await page.getByRole("option", { name: "本机 SSH agent" }).waitFor();
51 const expanded = await keyField.boundingBox();
52 assert.ok(Math.abs(await gap() - before) < 1, "floating choices must not move subsequent fields");
53 const list = await page.getByRole("listbox").boundingBox();
54 assert.ok(list.height > 0 && Math.abs(expanded.y - (await keyField.boundingBox()).y) < 1);
55 await auth.press("Escape");
56 assert.equal(await auth.getAttribute("aria-expanded"), "false");
57 await page.getByRole("listbox").waitFor({state:"hidden"});
58 assert.ok(Math.abs(await gap() - before) < 1, "closing choices restores layout");
59 await auth.press("ArrowDown");
60 await auth.press("ArrowDown");
61 await auth.press("Enter");
62 assert.equal(await auth.getAttribute("aria-expanded"), "false");
63 assert.equal(await keyField.count(), 0, "keyboard selection switches authentication method");
64 await auth.click();
65 await page.getByRole("option", { name: "本机密钥文件" }).click();
66 await page.screenshot({ path: join(artifacts, "remote-host-form-desktop.png"), fullPage: true });
67 await page.setViewportSize({ width: 375, height: 900 });
68 assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false, "mobile form should not overflow horizontally");
69 await page.screenshot({ path: join(artifacts, "remote-host-form-mobile.png"), fullPage: true });
70 await page.setViewportSize({ width: 1280, height: 850 });
71 await page.getByLabel("本机私钥文件", { exact: true }).fill(join(profile, "missing-test-key"));
72 await page.getByRole("button", { name: "保存主机", exact: true }).click();
73 await page.getByText("隔离 UI 测试主机", { exact: true }).waitFor();
74 await page.reload();
75 await page.getByText("隔离 UI 测试主机", { exact: true }).waitFor();
76 const saved = JSON.parse(await readFile(join(profile, "remote-hosts.json"), "utf8"));
77 assert.equal(saved.hosts.length, 1); assert.equal(saved.hosts[0].port, 9);
78 await page.getByRole("button", { name: "连接此主机" }).click();
79 await page.getByText("SSH 密钥不存在或文件权限不安全", { exact: true }).waitFor();
80 assert.equal(await page.getByRole("complementary", { name: "SSH 主机", exact: true }).count(), 1, "failed remote connection must retain host management");
81 assert.equal(await page.getByRole("button", { name: "创建本地账户" }).count(), 0, "failure must not fall back to a local account");
82 await page.screenshot({ path: join(artifacts, "remote-host-error.png"), fullPage: true });
83 await page.getByRole("button", { name: "编辑", exact: true }).click();
84 await page.getByLabel("主机名称", { exact: true }).fill("修改后的测试主机");
85 await page.getByRole("button", { name: "保存主机", exact: true }).click();
86 await page.getByText("修改后的测试主机", { exact: true }).waitFor();
87 await page.getByRole("button", { name: "移除连接", exact: true }).click();
88 await page.getByRole("button", { name: "取消", exact: true }).click();
89 assert.equal(await page.getByText("修改后的测试主机", { exact: true }).count(), 1);
90 await page.getByRole("button", { name: "移除连接", exact: true }).click();
91 await page.getByRole("button", { name: "确认移除", exact: true }).click();
92 await page.getByText("还没有远程主机", { exact: true }).waitFor();
93 assert.deepEqual(JSON.parse(await readFile(join(profile, "remote-hosts.json"), "utf8")).hosts, []);
94 assert.deepEqual(errors, []);
95 const result = { ok: true, scope: "真实本地 cage-client 服务和 Chromium;独立临时配置;连接失败路径", checks: ["thin-client-no-local-account", "host-crud-and-persistence", "inline-dropdown-layout", "keyboard-choice", "mobile-layout", "connection-error-no-local-fallback", "remove-cancel-and-confirm"], notTested: ["真实云端服务器连接与容器工作负载", "macOS、Windows、Linux 原生窗口交互"], pageErrors: errors };
96 await writeFile(join(artifacts, "result.json"), JSON.stringify(result, null, 2));
97 console.log(JSON.stringify({ ...result, artifacts }));
98} catch (error) {
99 if (page) await page.screenshot({ path: join(artifacts, "failure.png"), fullPage: true }).catch(() => {});
100 console.error(`UI evidence: ${artifacts}`);
101 throw error;
102} finally {
103 await browser?.close();
104 service.kill("SIGTERM");
105}