| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import fs from "node:fs/promises"; |
| 5 | import path from "node:path"; |
| 6 | import { spawn, execFile } from "node:child_process"; |
| 7 | import { promisify } from "node:util"; |
| 8 | import { createRequire } from "node:module"; |
| 9 | |
| 10 | const require = createRequire(import.meta.url); |
| 11 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 12 | const run = promisify(execFile); |
| 13 | const executable = process.env.CAGE_SUBSCRIPTION_QA_EXECUTABLE; |
| 14 | assert.ok(executable, "Set CAGE_SUBSCRIPTION_QA_EXECUTABLE to the newly built cage beside mihomo"); |
| 15 | const output = path.resolve(process.env.CAGE_SUBSCRIPTION_QA_OUTPUT || "/tmp/cage-subscription-ui"); |
| 16 | await fs.mkdir(output, { recursive: true }); |
| 17 | const profile = await fs.mkdtemp("/private/tmp/cage-subscription-qa-"); |
| 18 | const env = { ...process.env, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") }; |
| 19 | await run(executable, ["init"], { env }); |
| 20 | const log = await fs.open(path.join(profile, "server.log"), "w", 0o600); |
| 21 | const service = spawn(executable, ["serve"], { env, stdio: ["ignore", log.fd, log.fd] }); |
| 22 | let browser; |
| 23 | const errors = []; |
| 24 | try { |
| 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 | |
| 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 | } |