| 1 | import assert from "node:assert/strict"; |
| 2 | import fs from "node:fs/promises"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 5 | const output = process.env.CAGE_VAULT_CAPTURE || fileURLToPath(new URL("../../docs/test-reports/2026-11-05-password-vault/", import.meta.url)); |
| 6 | await fs.mkdir(output, { recursive: true }); |
| 7 | let clipboard = ""; |
| 8 | const { browser, page, state, errors } = await createWorkbench({ nativeRequest: async ({ action, text }) => { |
| 9 | if (action === "clipboardWrite") { clipboard = text; return true; } |
| 10 | return null; |
| 11 | } }); |
| 12 | let entries = [], blocked = [], candidate = null, filled = null, sequence = 0; |
| 13 | await page.route("**/api/vault**", async route => { |
| 14 | const req = route.request(), input = req.method() === "POST" ? req.postDataJSON() : {}; |
| 15 | if (!state.account.unlocked) return route.fulfill({ status: 423, body: "Cage 已锁定" }); |
| 16 | const redacted = value => { const { password, ...rest } = value; return rest; }; |
| 17 | if (new URL(req.url()).pathname === "/api/vault/browser") { |
| 18 | if (input.action === "status") return route.fulfill({ json: { |
| 19 | available: true, origin: "https://mail.example.test", document: "fixture-document", |
| 20 | entries: entries.filter(e => e.origin === "https://mail.example.test").map(redacted), |
| 21 | candidates: candidate ? [{ ...redacted(candidate), update: entries.some(e => e.origin === candidate.origin && e.username === candidate.username) }] : [], |
| 22 | } }); |
| 23 | if (input.action === "fill") { assert.equal(input.document, "fixture-document"); filled = input.id; } |
| 24 | if (input.action === "save") { |
| 25 | const existing = entries.find(e => e.origin === candidate.origin && e.username === candidate.username); |
| 26 | if (existing) existing.password = candidate.password; |
| 27 | else entries.push({ ...candidate, id: `saved-${++sequence}` }); |
| 28 | candidate = null; |
| 29 | } |
| 30 | if (input.action === "never") { blocked.push(candidate.origin); candidate = null; } |
| 31 | if (input.action === "dismiss") candidate = null; |
| 32 | return route.fulfill({ json: { ok: true } }); |
| 33 | } |
| 34 | if (req.method() === "GET") return route.fulfill({ json: { entries: entries.map(redacted), blocked } }); |
| 35 | if (input.action === "save") { |
| 36 | const entry = input.entry; |
| 37 | const existing = entries.find(e => e.id === entry.id); |
| 38 | if (existing) Object.assign(existing, entry, { password: entry.password || existing.password }); |
| 39 | else entries.push({ ...entry, id: `saved-${++sequence}` }); |
| 40 | } |
| 41 | if (input.action === "reveal") { |
| 42 | if (input.account_password !== state.accountPassword) return route.fulfill({ status: 400, body: "密码不正确" }); |
| 43 | return route.fulfill({ json: { password: entries.find(e => e.id === input.id).password } }); |
| 44 | } |
| 45 | if (input.action === "delete") entries = entries.filter(e => e.id !== input.id); |
| 46 | if (input.action === "unblock") blocked = blocked.filter(e => e !== input.origin); |
| 47 | return route.fulfill({ json: { ok: true } }); |
| 48 | }); |
| 49 | const settings = async () => { |
| 50 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 51 | await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "密码保险箱", exact: true }).click(); |
| 52 | await page.getByRole("heading", { name: "密码保险箱", exact: true }).waitFor(); |
| 53 | }; |
| 54 | try { |
| 55 | await settings(); |
| 56 | await page.getByText("还没有保存密码", { exact: true }).waitFor(); |
| 57 | await page.getByRole("button", { name: "添加密码", exact: true }).click(); |
| 58 | await page.getByLabel("网站地址", { exact: true }).fill("https://mail.example.test"); |
| 59 | await page.getByLabel("账号或邮箱").fill("[email protected]"); |
| 60 | await page.getByLabel("网站密码", { exact: true }).fill("fixture-mail-secret"); |
| 61 | await page.getByRole("button", { name: "保存记录", exact: true }).click(); |
| 62 | await page.getByRole("status").filter({ hasText: "密码记录已保存" }).waitFor(); |
| 63 | assert.equal(entries.length, 1); |
| 64 | assert.equal(await page.getByText("fixture-mail-secret", { exact: true }).count(), 0); |
| 65 | await page.getByRole("button", { name: "查看密码", exact: true }).click(); |
| 66 | await page.getByLabel("Cage 账户密码", { exact: true }).fill("wrong-password"); |
| 67 | await page.getByRole("button", { name: "验证并查看" }).click(); |
| 68 | await page.getByRole("alert").filter({ hasText: "密码不正确" }).waitFor(); |
| 69 | assert.equal(await page.getByLabel("已保存的网站密码").count(), 0); |
| 70 | await page.getByLabel("Cage 账户密码", { exact: true }).fill(state.accountPassword); |
| 71 | await page.getByRole("button", { name: "验证并查看" }).click(); |
| 72 | await page.getByLabel("已保存的网站密码").waitFor(); |
| 73 | assert.equal(await page.getByLabel("已保存的网站密码").textContent(), "fixture-mail-secret"); |
| 74 | await page.getByRole("button", { name: "隐藏密码" }).click(); |
| 75 | await page.getByRole("button", { name: "复制密码", exact: true }).click(); |
| 76 | await page.getByLabel("Cage 账户密码", { exact: true }).fill(state.accountPassword); |
| 77 | await page.getByRole("button", { name: "验证并复制" }).click(); |
| 78 | await page.getByRole("status").filter({ hasText: "密码已复制" }).waitFor(); |
| 79 | assert.equal(clipboard, "fixture-mail-secret"); |
| 80 | await page.getByRole("button", { name: "编辑", exact: true }).click(); |
| 81 | await page.getByLabel("账号或邮箱").fill("[email protected]"); |
| 82 | await page.getByRole("button", { name: "保存记录", exact: true }).click(); |
| 83 | await page.getByRole("status").filter({ hasText: "密码记录已保存" }).waitFor(); |
| 84 | assert.equal(entries[0].password, "fixture-mail-secret"); |
| 85 | await page.getByLabel("搜索网站或账号").fill("missing"); |
| 86 | await page.getByText("没有找到匹配的账号").waitFor(); |
| 87 | await page.getByLabel("搜索网站或账号").fill(""); |
| 88 | await page.screenshot({ path: output + "vault-settings.png" }); |
| 89 | await page.setViewportSize({ width: 820, height: 558 }); |
| 90 | await page.screenshot({ path: output + "vault-settings-small.png" }); |
| 91 | assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true); |
| 92 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 93 | state.status.ready = true; state.status.vm_running = true; |
| 94 | state.tabs = [{ id: "web-vault", title: "邮箱", kind: "web", url: "https://mail.example.test/login" }]; state.active = "web-vault"; |
| 95 | await page.reload(); |
| 96 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 97 | await page.getByRole("button", { name: "浏览器菜单", exact: true }).click(); |
| 98 | await page.getByRole("menuitem", { name: /密码 · 1 个账号/ }).waitFor(); |
| 99 | const trigger = page.getByRole("menuitem", { name: /密码 · 1 个账号/ }); |
| 100 | const before = await page.locator(".browser-chrome").boundingBox(); |
| 101 | await trigger.press("Enter"); |
| 102 | const picker = page.getByRole("region", { name: "当前网站的密码" }); |
| 103 | await picker.waitFor(); |
| 104 | const after = await page.locator(".browser-chrome").boundingBox(); |
| 105 | assert.ok(after.height > before.height + 60, "password picker did not occupy layout space"); |
| 106 | await picker.getByRole("button", { name: /[email protected]/ }).focus(); |
| 107 | await page.keyboard.press("Enter"); |
| 108 | await page.getByRole("status").filter({ hasText: "已填写" }).waitFor(); |
| 109 | assert.equal(filled, entries[0].id); |
| 110 | const filledHeight = (await page.locator(".browser-chrome").boundingBox()).height; |
| 111 | const pickerHeight = (await picker.boundingBox()).height; |
| 112 | await trigger.press("Escape"); |
| 113 | await picker.waitFor({ state: "hidden" }); |
| 114 | assert.equal(Math.round((await page.locator(".browser-chrome").boundingBox()).height), Math.round(filledHeight - pickerHeight), |
| 115 | "closing the password picker releases its space while preserving the fill confirmation"); |
| 116 | candidate = { id: "pending", tab: "web-vault", origin: "https://mail.example.test", username: "[email protected]", password: "new-fixture-secret" }; |
| 117 | await page.getByText("更新此账号的密码?", { exact: true }).waitFor(); |
| 118 | await page.screenshot({ path: output + "vault-save-prompt.png" }); |
| 119 | await page.getByRole("button", { name: "更新密码", exact: true }).click(); |
| 120 | await page.getByRole("status").filter({ hasText: "密码已保存" }).waitFor(); |
| 121 | assert.equal(entries[0].password, "new-fixture-secret"); |
| 122 | candidate = { id: "ignored", tab: "web-vault", origin: "https://mail.example.test", username: "other", password: "ignored" }; |
| 123 | await page.getByRole("button", { name: "此网站不再提示", exact: true }).click(); |
| 124 | await page.getByRole("status").filter({ hasText: "不再提示保存" }).waitFor(); |
| 125 | await settings(); |
| 126 | await page.getByRole("button", { name: "恢复提示", exact: true }).click(); |
| 127 | await page.getByRole("status").filter({ hasText: "已恢复" }).waitFor(); |
| 128 | assert.deepEqual(blocked, []); |
| 129 | await page.getByRole("button", { name: "删除", exact: true }).click(); |
| 130 | await page.getByRole("button", { name: "删除密码", exact: true }).click(); |
| 131 | await page.getByText("还没有保存密码", { exact: true }).waitFor(); |
| 132 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 133 | await page.getByRole("button", { name: /^账户菜单,/ }).click(); |
| 134 | await page.getByRole("menuitem", { name: /锁定 Cage/ }).click(); |
| 135 | await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor(); |
| 136 | assert.equal(await page.locator(".vault-browser").count(), 0); |
| 137 | assert.deepEqual(errors, []); |
| 138 | console.log("PASS: vault UI CRUD, search, reauthentication, clipboard, inline keyboard picker, save/update/never, lock and small viewport"); |
| 139 | } finally { await browser.close(); } |