| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import fs from "node:fs/promises"; |
| 4 | import os from "node:os"; |
| 5 | import path from "node:path"; |
| 6 | import { spawn } from "node:child_process"; |
| 7 | import { createRequire } from "node:module"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | const require = createRequire(import.meta.url); |
| 10 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 11 | const root = fileURLToPath(new URL("../", import.meta.url)); |
| 12 | const profile = await fs.mkdtemp(path.join(os.tmpdir(), "cage-vault-e2e-")); |
| 13 | await fs.writeFile(path.join(profile, "config.json"), JSON.stringify({ proxy_url: "socks5://127.0.0.1:1", dns_url: "https://1.1.1.1/dns-query", timezone: "Etc/UTC", locale: "en_US.UTF-8", browser_language: "en-US" }), { mode: 0o600 }); |
| 14 | await fs.writeFile(path.join(profile, "workbench.json"), JSON.stringify({ workspace: path.join(profile, "workspace") }), { mode: 0o600 }); |
| 15 | let service, browser, page; |
| 16 | async function start() { |
| 17 | await fs.rm(path.join(profile, "control.json"), { force: true }); |
| 18 | service = spawn(path.join(root, "_output/vault-qa/cage"), ["serve"], { env: { ...process.env, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") }, stdio: "ignore" }); |
| 19 | let control; |
| 20 | for (let i = 0; i < 100; i++) { |
| 21 | try { control = JSON.parse(await fs.readFile(path.join(profile, "control.json"), "utf8")); break; } |
| 22 | catch { await new Promise(resolve => setTimeout(resolve, 100)); } |
| 23 | } |
| 24 | assert.ok(control, "isolated service did not start"); |
| 25 | const origin = `http://localhost:${control.port}`; |
| 26 | const response = await fetch(origin + "/api/launch", { method: "POST", headers: { Origin: origin, "Content-Type": "application/json" }, body: JSON.stringify({ token: control.token }) }); |
| 27 | assert.ok(response.ok); |
| 28 | return (await response.json()).url; |
| 29 | } |
| 30 | async function stop() { |
| 31 | if (service && service.exitCode === null) await new Promise(resolve => { service.once("exit", resolve); service.kill("SIGTERM"); }); |
| 32 | } |
| 33 | try { |
| 34 | browser = await chromium.launch({ headless: true }); |
| 35 | page = await browser.newPage(); |
| 36 | const errors = []; |
| 37 | page.on("pageerror", error => errors.push(error.message)); |
| 38 | await page.goto(await start()); |
| 39 | await page.getByLabel("用户名", { exact: true }).fill("保险箱验证"); |
| 40 | await page.getByLabel("设置密码", { exact: true }).fill("isolated-vault-account-password"); |
| 41 | await page.getByLabel("确认密码", { exact: true }).fill("isolated-vault-account-password"); |
| 42 | await page.getByRole("button", { name: "创建本地账户", exact: true }).click(); |
| 43 | const openVault = async () => { |
| 44 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 45 | await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "密码保险箱", exact: true }).click(); |
| 46 | await page.getByRole("heading", { name: "密码保险箱", exact: true }).waitFor(); |
| 47 | }; |
| 48 | await openVault(); |
| 49 | await page.getByRole("button", { name: "添加密码", exact: true }).click(); |
| 50 | await page.getByLabel("网站地址", { exact: true }).fill("https://mail.example.test/login"); |
| 51 | await page.getByLabel("账号或邮箱").fill("[email protected]"); |
| 52 | await page.getByLabel("网站密码", { exact: true }).fill("isolated-website-secret"); |
| 53 | await page.getByRole("button", { name: "保存记录", exact: true }).click(); |
| 54 | await page.getByRole("status").filter({ hasText: "密码记录已保存" }).waitFor(); |
| 55 | const raw = await fs.readFile(path.join(profile, "password-vault.json"), "utf8"); |
| 56 | for (const value of ["[email protected]", "isolated-website-secret", "mail.example.test"]) assert.ok(!raw.includes(value), "plaintext persisted"); |
| 57 | const list = await page.evaluate(async () => (await fetch("/api/vault")).json()); |
| 58 | assert.equal(list.entries[0].origin, "https://mail.example.test"); |
| 59 | assert.equal(list.entries[0].password, undefined); |
| 60 | await stop(); |
| 61 | await page.goto(await start()); |
| 62 | await page.getByLabel("账户密码", { exact: true }).fill("isolated-vault-account-password"); |
| 63 | await page.getByRole("button", { name: "解锁 Cage", exact: true }).click(); |
| 64 | await openVault(); |
| 65 | await page.getByText("[email protected]", { exact: true }).waitFor(); |
| 66 | await page.getByRole("button", { name: "查看密码", exact: true }).click(); |
| 67 | await page.getByLabel("Cage 账户密码", { exact: true }).fill("isolated-vault-account-password"); |
| 68 | await page.getByRole("button", { name: "验证并查看" }).click(); |
| 69 | await page.getByLabel("已保存的网站密码").waitFor(); |
| 70 | assert.equal(await page.getByLabel("已保存的网站密码").textContent(), "isolated-website-secret"); |
| 71 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 72 | await page.getByRole("button", { name: /^账户菜单,/ }).click(); |
| 73 | await page.getByRole("menuitem", { name: /锁定 Cage/ }).click(); |
| 74 | await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor(); |
| 75 | assert.equal(await page.evaluate(async () => (await fetch("/api/vault")).status), 423); |
| 76 | assert.equal(await fs.readFile(path.join(profile, "password-vault.json"), "utf8"), raw, "restart/lock modified vault data"); |
| 77 | assert.deepEqual(errors, []); |
| 78 | console.log("PASS: real UI and Go service create/save, encrypted disk, redacted API, service restart, unlock/reveal, lock and data preservation"); |
| 79 | } catch (error) { |
| 80 | console.error((await page?.locator("body").innerText())?.slice(0, 3000)); |
| 81 | throw error; |
| 82 | } finally { |
| 83 | await browser?.close(); await stop(); |
| 84 | await fs.rm(profile, { recursive: true, force: true }); |
| 85 | } |