| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir, writeFile } from "node:fs/promises"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 5 | const engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 6 | const output = new URL(`../../docs/test-reports/2026-11-09-tailscale-redesign/${engine}/`, import.meta.url); |
| 7 | await mkdir(output, { recursive: true }); |
| 8 | let copied = "", fail = false, delayed = false, failKey = false, failService = false, failBrowser = true; |
| 9 | let tail = { installed: true, version: "1.104.5", enabled: false, backend: "Stopped", ips: [], peers: [], keys: [], forwards: [], user: "cage", ssh_port: 22 }; |
| 10 | const { browser, page, state, errors } = await createWorkbench({ nativeRequest: body => { if (body.action === "clipboardWrite") { copied = body.text; return true; } } }); |
| 11 | const checks = []; |
| 12 | const open = page.getByRole("button", { name: "打开 Tailscale", exact: true }); |
| 13 | const panel = page.getByRole("dialog", { name: "Tailscale", exact: true }); |
| 14 | const refresh = () => panel.getByRole("button", { name: "刷新 Tailscale 状态" }).click(); |
| 15 | try { |
| 16 | await page.route("**/api/browser/new", route => failBrowser ? route.fulfill({ status: 502, body: "浏览器暂未响应" }) : route.fallback()); |
| 17 | await page.route("**/api/tailscale**", async route => { |
| 18 | const request = route.request(), action = new URL(request.url()).pathname.split("/").at(-1), body = request.postDataJSON(); |
| 19 | if (fail) return route.fulfill({ status: 502, contentType: "text/plain", body: "Tailscale 暂未响应,请稍后重试" }); |
| 20 | if (["connect", "login"].includes(action)) tail = { ...tail, enabled: true, backend: "NeedsLogin", ...(delayed ? {} : { auth_url: "https://login.tailscale.com/a/fixture-login" }) }; |
| 21 | if (["disconnect", "logout"].includes(action)) tail = { ...tail, enabled: false, backend: "Stopped", proxy_url: "", ips: [], peers: [], forwards: [], auth_url: "" }; |
| 22 | if (action === "add-key") { |
| 23 | if (failKey) return route.fulfill({ status: 502, body: "请粘贴一条有效的 SSH 公钥" }); |
| 24 | tail.keys.push(body.key); |
| 25 | } |
| 26 | if (action === "remove-key") tail.keys = tail.keys.filter(key => key !== body.key); |
| 27 | if (action === "open-service") { |
| 28 | if (failService) return route.fulfill({ status: 502, body: "设备未出现在当前 Tailscale 网络中" }); |
| 29 | assert.equal(body.host, "100.88.2.4"); assert.equal(body.port, 8080); |
| 30 | tail.forwards = [{ host: body.host, port: body.port, address: "127.0.0.1:32123" }]; |
| 31 | } |
| 32 | return route.fulfill({ json: tail }); |
| 33 | }); |
| 34 | state.status.ready = state.status.vm_running = true; |
| 35 | await page.reload(); await open.waitFor(); |
| 36 | const position = await open.boundingBox(), browserPosition = await page.getByRole("button", { name: "打开浏览器", exact: true }).boundingBox(); |
| 37 | assert.ok(position.y > browserPosition.y && position.y < browserPosition.y + browserPosition.height * 2 + 8); |
| 38 | await open.focus(); await page.keyboard.press("Enter"); await panel.getByText("已断开", { exact: true }).waitFor(); |
| 39 | assert.equal(await panel.locator("textarea").count(), 0); |
| 40 | await panel.getByText("连接后查看你的设备", { exact: true }).waitFor(); |
| 41 | await page.screenshot({ animations: "disabled", path: fileURLToPath(new URL("disconnected.png", output)) }); |
| 42 | await panel.getByRole("button", { name: "连接并登录", exact: true }).click(); |
| 43 | await panel.getByRole("alert").filter({ hasText: "登录页面未能打开" }).waitFor(); |
| 44 | failBrowser = false; |
| 45 | await panel.getByRole("button", { name: "打开登录页面", exact: true }).click(); |
| 46 | await panel.waitFor({ state: "hidden" }); |
| 47 | assert.ok(state.requests.some(r => r.path === "/browser/new" && r.body.url === "https://login.tailscale.com/a/fixture-login" && r.body.incognito === false)); |
| 48 | checks.push("keyboard entry; enrollment precedes SSH; browser launch failure keeps login retry; official login opens in regular Cage browser"); |
| 49 | const connected = { ...tail, enabled: true, backend: "Running", tailnet: "林间私网", account: "[email protected]", name: "cage-workbench.test.ts.net", hostname: "cage-workbench", os: "macOS", auth_url: "", ips: ["100.88.2.3"], proxy_url: "http://cage:[email protected]:32100", peers: [{ name: "offline-server.test.ts.net", hostname: "offline-server", os: "linux", ip: "100.88.2.5", online: false }, { name: "my-computer.test.ts.net", hostname: "my-computer", os: "macOS", ip: "100.88.2.4", online: true }] }; |
| 50 | tail = structuredClone(connected); |
| 51 | await open.click(); await panel.getByText("已连接", { exact: true }).waitFor(); |
| 52 | await panel.getByText("林间私网", { exact: true }).waitFor(); |
| 53 | await panel.getByText("登录账号 · [email protected]", { exact: true }).waitFor(); |
| 54 | assert.equal(await panel.locator(".tailscale-peer").first().getByText("my-computer", { exact: true }).count(), 1); |
| 55 | await panel.getByLabel("搜索设备").fill("100.88.2.5"); assert.equal(await panel.locator(".tailscale-peer").count(), 1); |
| 56 | await panel.getByLabel("搜索设备").fill("missing"); await panel.getByText("没有匹配的设备,请尝试其他名称或地址。", { exact: true }).waitFor(); |
| 57 | await panel.getByLabel("搜索设备").fill(""); |
| 58 | await page.screenshot({ animations: "disabled", path: fileURLToPath(new URL("connected.png", output)) }); |
| 59 | await panel.getByRole("button", { name: "复制地址", exact: true }).click(); assert.equal(copied, "100.88.2.3"); |
| 60 | await panel.getByRole("button", { name: "复制 my-computer 的地址", exact: true }).click(); assert.equal(copied, "100.88.2.4"); |
| 61 | checks.push("network/account/self identity; online-first device list; name and IP search with empty feedback; address copy"); |
| 62 | const peer = panel.locator(".tailscale-peer").first(); |
| 63 | await peer.getByRole("button", { name: "使用设备", exact: true }).focus(); await page.keyboard.press("Enter"); |
| 64 | await peer.getByLabel("SSH 连接", { exact: true }).fill("local-user"); |
| 65 | await peer.getByRole("button", { name: "复制 SSH 命令", exact: true }).click(); assert.equal(copied, "ssh -l 'local-user' 100.88.2.4"); |
| 66 | await peer.getByLabel("服务端口", { exact: true }).fill("8080"); |
| 67 | failService = true; |
| 68 | await peer.getByRole("button", { name: "创建连接地址", exact: true }).click(); await panel.getByRole("alert").waitFor(); |
| 69 | assert.equal(await peer.getByLabel("服务端口").inputValue(), "8080"); |
| 70 | failService = false; |
| 71 | await peer.getByRole("button", { name: "创建连接地址", exact: true }).click(); |
| 72 | await peer.getByText("127.0.0.1:32123", { exact: true }).waitFor(); |
| 73 | await peer.getByRole("button", { name: "复制端口 8080 的连接地址", exact: true }).click(); assert.equal(copied, "127.0.0.1:32123"); |
| 74 | await peer.getByRole("button", { name: "打开 HTTP 页面", exact: true }).click(); await panel.waitFor({ state: "hidden" }); |
| 75 | assert.ok(state.requests.some(r => r.path === "/browser/new" && r.body.url === "http://127.0.0.1:32123")); |
| 76 | checks.push("per-device SSH uses supplied username; service mapping errors preserve port; TCP mapping copied and HTTP page opened"); |
| 77 | await open.click(); await panel.getByText("已连接", { exact: true }).waitFor(); |
| 78 | await panel.getByRole("button", { name: "应用访问私网", exact: true }).click(); |
| 79 | await panel.getByRole("button", { name: "复制私网代理地址", exact: true }).click(); assert.equal(copied, tail.proxy_url); |
| 80 | assert.equal(await panel.getByText(tail.proxy_url, { exact: true }).count(), 0); |
| 81 | await panel.getByRole("button", { name: "从其他设备 SSH 到 Cage", exact: true }).focus(); await page.keyboard.press("Enter"); |
| 82 | await panel.getByRole("button", { name: "复制连接 Cage 命令" }).click(); assert.equal(copied, "ssh [email protected]"); |
| 83 | await panel.getByLabel("连接设备的 SSH 公钥", { exact: true }).fill("ssh-ed25519 AAAAFixture local"); |
| 84 | failKey = true; await panel.getByRole("button", { name: "添加公钥", exact: true }).click(); await panel.getByRole("alert").waitFor(); |
| 85 | assert.equal(await panel.getByLabel("连接设备的 SSH 公钥").inputValue(), "ssh-ed25519 AAAAFixture local"); |
| 86 | failKey = false; await panel.getByRole("button", { name: "添加公钥", exact: true }).click(); await panel.locator(".tailscale-key").waitFor(); |
| 87 | await page.reload(); await open.click(); await panel.getByText("已连接", { exact: true }).waitFor(); |
| 88 | await panel.getByRole("button", { name: "从其他设备 SSH 到 Cage", exact: true }).click(); |
| 89 | await panel.locator(".tailscale-key").waitFor(); |
| 90 | await panel.getByRole("button", { name: "移除 SSH 公钥" }).click(); await panel.locator(".tailscale-key").waitFor({ state: "hidden" }); |
| 91 | checks.push("application proxy credential copied without rendering; SSH opt-in keyboard disclosure; failed key keeps input; saved key survives refresh and can be revoked"); |
| 92 | await page.keyboard.press("Escape"); await panel.waitFor({ state: "hidden" }); |
| 93 | await open.click(); await panel.getByRole("button", { name: "断开连接", exact: true }).click(); |
| 94 | await page.getByRole("dialog", { name: "断开 Tailscale?", exact: true }).getByRole("button", { name: "取消", exact: true }).click(); assert.equal(tail.enabled, true); |
| 95 | await panel.getByRole("button", { name: "断开连接", exact: true }).click(); |
| 96 | await page.getByRole("dialog", { name: "断开 Tailscale?", exact: true }).getByRole("button", { name: "断开连接", exact: true }).click(); |
| 97 | await panel.getByText("已断开", { exact: true }).waitFor(); assert.equal(await panel.locator("textarea").count(), 0); |
| 98 | delayed = true; delete tail.auth_url; |
| 99 | await panel.getByRole("button", { name: "连接并登录", exact: true }).click(); await panel.getByText("等待登录", { exact: true }).waitFor(); |
| 100 | tail.auth_url = "https://login.tailscale.com/a/delayed-login"; await panel.waitFor({ state: "hidden" }); |
| 101 | assert.ok(state.requests.some(r => r.path === "/browser/new" && r.body.url === tail.auth_url)); |
| 102 | tail = { ...tail, auth_url: "", backend: "NeedsMachineAuth" }; await open.click(); await panel.getByText("等待管理员批准", { exact: true }).waitFor(); |
| 103 | assert.equal(await panel.getByRole("button", { name: "重新发起登录", exact: true }).count(), 0); |
| 104 | checks.push("disconnect cancellation preserves network; confirmation clears device controls; delayed auth URL opens automatically; approval is distinct from login"); |
| 105 | tail = { ...connected, peers: [], forwards: [] }; await refresh(); await panel.getByText("私网已连接,还没有发现其他设备", { exact: true }).waitFor(); |
| 106 | fail = true; await refresh(); await panel.getByRole("alert").waitFor(); fail = false; await refresh(); await panel.getByRole("alert").waitFor({ state: "hidden" }); |
| 107 | tail = structuredClone(connected); await refresh(); |
| 108 | await page.setViewportSize({ width: 820, height: 600 }); |
| 109 | await page.screenshot({ animations: "disabled", path: fileURLToPath(new URL("compact.png", output)) }); |
| 110 | assert.ok(await panel.evaluate(node => node.scrollWidth <= node.clientWidth + 1)); |
| 111 | await page.setViewportSize({ width: 520, height: 700 }); |
| 112 | await panel.locator(".tailscale-peer").first().getByRole("button", { name: "使用设备", exact: true }).click(); |
| 113 | await page.screenshot({ animations: "disabled", path: fileURLToPath(new URL("narrow.png", output)) }); |
| 114 | assert.ok(await panel.evaluate(node => node.scrollWidth <= node.clientWidth + 1)); |
| 115 | assert.deepEqual(errors, []); |
| 116 | checks.push("connected empty list guides adding device; API error refresh recovery; compact and narrow layouts have no horizontal overflow; no browser exceptions"); |
| 117 | await writeFile(new URL("result.json", output), JSON.stringify({ engine, checks, errors }, null, 2)); |
| 118 | console.log(JSON.stringify({ engine, checks, errors }, null, 2)); |
| 119 | } finally { await browser.close(); } |