跳到主要内容

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

浏览文件

subscription-import-smoke.mjs

139 行 · 8.8 KB历史下载
1// Real authenticated workbench and bundled Mihomo, with a new private profile.
2// Uses synthetic local YAML only; never reads the user's profile or subscription.
3import assert from "node:assert/strict";
4import fs from "node:fs/promises";
5import path from "node:path";
6import { spawn, execFile } from "node:child_process";
7import { promisify } from "node:util";
8import { createRequire } from "node:module";
9
10const require = createRequire(import.meta.url);
11const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
12const run = promisify(execFile);
13const executable = process.env.CAGE_SUBSCRIPTION_QA_EXECUTABLE;
14assert.ok(executable, "Set CAGE_SUBSCRIPTION_QA_EXECUTABLE to the newly built cage beside mihomo");
15const output = path.resolve(process.env.CAGE_SUBSCRIPTION_QA_OUTPUT || "/tmp/cage-subscription-ui");
16await fs.mkdir(output, { recursive: true });
17const profile = await fs.mkdtemp("/private/tmp/cage-subscription-qa-");
18const env = { ...process.env, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") };
19await run(executable, ["init"], { env });
20const log = await fs.open(path.join(profile, "server.log"), "w", 0o600);
21const service = spawn(executable, ["serve"], { env, stdio: ["ignore", log.fd, log.fd] });
22let browser;
23const errors = [];
24try {
25 let control;
26 for (let i = 0; i < 100; i++) {
27 try {
28 control = JSON.parse(await fs.readFile(path.join(profile, "control.json")));
29 break;
30 } catch {
31 if (service.exitCode !== null) throw new Error("Private service exited before readiness");
32 await new Promise((resolve) => setTimeout(resolve, 100));
33 }
34 }
35 assert.ok(control, "Private control service did not start");
36 const origin = `http://localhost:${control.port}`;
37 const launch = await fetch(`${origin}/api/launch`, {
38 method: "POST",
39 headers: { Origin: origin, "Content-Type": "application/json" },
40 body: JSON.stringify({ token: control.token }),
41 }).then((response) => response.json());
42 browser = await chromium.launch({ headless: true, executablePath: process.env.CAGE_TEST_BROWSER_PATH });
43 const page = await browser.newPage({ viewport: { width: 1280, height: 1000 } });
44 page.on("pageerror", (error) => errors.push(error.message));
45 await page.goto(launch.url);
46 await page.getByLabel("用户名", { exact: true }).fill("订阅验证");
47 await page.getByLabel("设置密码", { exact: true }).fill("isolated-subscription-qa-password");
48 await page.getByLabel("确认密码", { exact: true }).fill("isolated-subscription-qa-password");
49 await page.getByRole("button", { name: "创建本地账户", exact: true }).click();
50 const enter = async () => {
51 await page.getByRole("button", { name: /^设置/ }).click();
52 await page.getByRole("button", { name: "网络与代理", exact: true }).click();
53 await page.getByRole("heading", { name: "内置代理", exact: true }).waitFor();
54 await page.getByRole("tab", { name: "订阅", exact: true }).click();
55 };
56 const importYAML = async (name, content) => {
57 await page.getByRole("button", { name: "添加来源", exact: true }).click();
58 await page.getByRole("button", { name: "本地 YAML", exact: true }).click();
59 await page.getByLabel("来源名称", { exact: true }).fill(name);
60 await page.getByLabel("节点 YAML", { exact: true }).fill(content);
61 await page.getByRole("button", { name: "导入并验证", exact: true }).click();
62 await page.locator(".proxy-progress").waitFor({ state: "hidden" });
63 };
64 await enter();
65 const pin = "ab".repeat(32);
66 const mixed = `proxies:
67 - {name: 普通节点, type: socks5, server: 127.0.0.1, port: 1080}
68 - {name: 端口跳跃节点, type: hysteria2, server: example.com, port: 35000, password: private-password, mport: 35000-39000, pinSHA256: "${pin}"}
69 - {name: VLESS扩展参数, type: vless, server: example.com, port: 443, uuid: 00000000-0000-4000-8000-000000000000, tls: true, udp: true, xudp: true, future-option: private-field-value, reality-opts: {public-key: CQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, short-id: "0123456789abcdef", support-x25519mlkem768: true}}
70 - {name: 不兼容节点, type: socks5, server: example.com, port: 1080, INTERFACE_NAME: private-field-value}
71 - {name: 依赖不兼容节点, type: http, server: example.com, port: 8080, dialer-proxy: 不兼容节点}
72`;
73 await importYAML("兼容性验证", mixed);
74 await page.locator(".proxy-notice").filter({ hasText: "已导入 3 个节点,跳过 2 个" }).waitFor();
75 const state = () => page.evaluate(() => fetch("/api/proxy").then((response) => response.json()));
76 const imported = await state();
77 assert.equal(imported.sources[0].nodes.length, 3);
78 assert.equal(imported.sources[0].skipped.length, 2);
79 const stored = JSON.parse(await fs.readFile(path.join(profile, "proxy/sources.json")));
80 const hy2 = stored.sources[0].nodes.find((node) => node.type === "hysteria2").config;
81 assert.equal(hy2.ports, "35000-39000");
82 assert.equal(hy2.fingerprint, pin);
83 const vless = stored.sources[0].nodes.find((node) => node.type === "vless").config;
84 assert.equal(vless.xudp, true);
85 assert.equal(vless["future-option"], "private-field-value");
86 assert.equal(vless["reality-opts"]["support-x25519mlkem768"], true);
87 assert.equal(vless.udp, false);
88 for (const secret of [pin, "private-password", "private-field-value", stored.auth]) {
89 assert.ok(!JSON.stringify(imported).includes(secret), "Public import status leaked a field value");
90 }
91 await importYAML("第二来源", "proxies: [{name: 备用节点, type: http, server: 127.0.0.1, port: 8080}]");
92 await page.locator(".proxy-notice").filter({ hasText: "已导入 1 个节点,跳过 0 个" }).waitFor();
93 const details = page.locator(".proxy-source-skipped").first();
94 const summary = details.locator("summary");
95 const secondSource = page.locator(".proxy-source").nth(1);
96 // Measure relative positions so scrolling cannot hide a layout regression.
97 const relativeY = async () => (await secondSource.boundingBox()).y - (await details.boundingBox()).y;
98 await summary.scrollIntoViewIfNeeded();
99 const before = await relativeY();
100 await summary.click();
101 await details.getByText(/节点不能引用本机文件、网络接口或额外路由/).waitFor();
102 await details.getByText(/代理链必须引用同一来源中成功导入/).waitFor();
103 assert.ok((await relativeY()) > before + 30, "Expanded reasons did not push the next source down");
104 await page.locator(".settings-content").evaluate((element) => { element.scrollTop = 0; });
105 await page.screenshot({ path: path.join(output, "partial-import.png") });
106 await summary.focus();
107 await summary.press("Enter");
108 assert.equal(await details.getAttribute("open"), null);
109 assert.ok(Math.abs((await relativeY()) - before) < 2, "Collapsed reasons did not restore layout");
110 await page.reload();
111 await enter();
112 await page.locator(".proxy-source-skipped summary").click();
113 await page.locator(".proxy-source-skipped").getByText(/节点不能引用本机文件、网络接口或额外路由/).waitFor();
114 assert.equal((await state()).sources[0].skipped.length, 2, "Reload lost the import report");
115 await page.setViewportSize({ width: 760, height: 1000 });
116 const bounds = await page.locator(".proxy-source").first().boundingBox();
117 assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= 760, "Import report overflowed the narrow viewport");
118 await page.screenshot({ path: path.join(output, "narrow.png") });
119 await page.setViewportSize({ width: 1280, height: 1000 });
120 await importYAML("全部不兼容", "proxies: [{name: 证书参数无效, type: hysteria2, server: example.com, port: 443, password: private-password, pinSHA256: private-pin}]");
121 await page.getByRole("alert").filter({ hasText: "没有可导入的节点" }).waitFor();
122 assert.equal((await state()).sources.length, 2, "Failed import changed existing sources");
123 assert.ok(!(await page.getByRole("alert").innerText()).includes("private-pin"));
124 await page.screenshot({ path: path.join(output, "all-skipped.png") });
125 assert.deepEqual(errors, []);
126 const result = { passed: true, imported: 3, skipped: 2, checks: ["real UI import", "bundled core validation", "VLESS xudp and nested REALITY options retained", "unknown protocol field retained", "interface key variant rejected", "guest UDP remains disabled", "port hopping and pin retained", "dependent chain skipped", "public status privacy", "success summary", "inline reasons and keyboard collapse", "reload persistence", "narrow viewport", "all-skipped rejection preserves sources"], browserErrors: errors };
127 await fs.writeFile(path.join(output, "result.json"), JSON.stringify(result, null, 2));
128 console.log(JSON.stringify(result));
129} finally {
130 await browser?.close();
131 const stopped = new Promise((resolve) => service.once("exit", resolve));
132 if (service.exitCode === null) {
133 service.kill("SIGTERM");
134 const force = setTimeout(() => service.kill("SIGKILL"), 5000);
135 await stopped;
136 clearTimeout(force);
137 }
138 await log.close();
139}