| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import { randomBytes } from "node:crypto"; |
| 5 | import fs from "node:fs/promises"; |
| 6 | import os from "node:os"; |
| 7 | import path from "node:path"; |
| 8 | import { execFile } from "node:child_process"; |
| 9 | import { promisify } from "node:util"; |
| 10 | import { createRequire } from "node:module"; |
| 11 | import { fileURLToPath } from "node:url"; |
| 12 | |
| 13 | const cage = process.argv[2]; |
| 14 | assert.ok(path.isAbsolute(cage || ""), "Supply an absolute Cage helper path"); |
| 15 | const run = promisify(execFile); |
| 16 | const root = await fs.mkdtemp(path.join(os.tmpdir(), "cage-remember-live-")); |
| 17 | const env = { ...process.env, HOME: root, LOCALAPPDATA: root, CLAUDE_CAGE_CONFIG: path.join(root, "profile", "config.json") }; |
| 18 | const output = new URL("../../docs/test-reports/2026-11-08-remember-device/", import.meta.url); |
| 19 | await fs.mkdir(output, { recursive: true }); |
| 20 | const require = createRequire(import.meta.url); |
| 21 | const { chromium, webkit } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 22 | const engine = process.env.CAGE_TEST_BROWSER === "webkit" ? webkit : chromium; |
| 23 | const browser = await engine.launch({ headless: true }); |
| 24 | let context, page; |
| 25 | const errors = []; |
| 26 | const command = async (...args) => JSON.parse((await run(cage, args, { env, timeout: 60000 })).stdout); |
| 27 | const account = () => page.evaluate(async () => { |
| 28 | const context = await (await fetch("/api/client/context")).json(); |
| 29 | return (await fetch("/api/account", { headers: { "X-Cage-Connection": context.connection_id } })).json(); |
| 30 | }); |
| 31 | const menu = () => page.getByRole("button", { name: /^账户菜单,/ }); |
| 32 | const remember = () => page.getByRole("checkbox", { name: /^记住我/ }); |
| 33 | const password = randomBytes(24).toString("base64url"); |
| 34 | const restart = async () => { |
| 35 | await context?.close(); |
| 36 | await command("prepare-update", "--json"); |
| 37 | const entry = await command("desktop", "--json"); |
| 38 | context = await browser.newContext(); |
| 39 | page = await context.newPage(); |
| 40 | page.on("pageerror", e => errors.push(e.message)); |
| 41 | await page.goto(entry.url); |
| 42 | }; |
| 43 | const unlock = async (enabled) => { |
| 44 | await remember().setChecked(enabled); |
| 45 | await page.getByLabel("账户密码", { exact: true }).fill(password); |
| 46 | const response = page.waitForResponse(r => r.url().endsWith("/api/client/trusted-login")); |
| 47 | await page.getByRole("button", { name: "解锁 Cage", exact: true }).click(); |
| 48 | assert.equal((await response).status(), 200); |
| 49 | await menu().waitFor(); |
| 50 | }; |
| 51 | try { |
| 52 | await restart(); |
| 53 | await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor(); |
| 54 | await remember().waitFor(); |
| 55 | assert.equal(await remember().isChecked(), true); |
| 56 | await remember().uncheck(); |
| 57 | await page.getByLabel("用户名", { exact: true }).fill("remember_live_qa"); |
| 58 | await page.getByLabel("设置密码", { exact: true }).fill(password); |
| 59 | await page.getByLabel("确认密码", { exact: true }).fill(password); |
| 60 | const created = page.waitForResponse(r => r.url().endsWith("/api/client/trusted-login")); |
| 61 | await page.getByRole("button", { name: "创建本地账户", exact: true }).click(); |
| 62 | assert.equal((await created).status(), 200); |
| 63 | await menu().waitFor(); |
| 64 | assert.equal(Boolean((await account()).trusted_login), false); |
| 65 | |
| 66 | await restart(); |
| 67 | await remember().waitFor(); |
| 68 | assert.equal(await remember().isChecked(), false); |
| 69 | await unlock(true); |
| 70 | assert.equal((await account()).trusted_login, true); |
| 71 | const record = JSON.parse(await fs.readFile(path.join(root, "profile", "account.json"), "utf8")); |
| 72 | assert.equal(record.devices?.automatic, undefined); |
| 73 | assert.equal(record.trusted_unlock.sealed_key.length > 0, true); |
| 74 | |
| 75 | await restart(); |
| 76 | await menu().waitFor(); |
| 77 | assert.equal((await account()).unlocked, true); |
| 78 | await menu().click(); |
| 79 | await page.getByRole("menuitem", { name: /锁定 Cage/ }).click(); |
| 80 | await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor(); |
| 81 | await restart(); |
| 82 | await remember().waitFor(); |
| 83 | assert.equal((await account()).unlocked, false); |
| 84 | assert.equal((await account()).require_authentication, true); |
| 85 | await page.screenshot({ path: fileURLToPath(new URL(`live-${engine === webkit ? "webkit" : "chromium"}-locked.png`, output)) }); |
| 86 | await unlock(false); |
| 87 | assert.equal(Boolean((await account()).trusted_login), false); |
| 88 | await restart(); |
| 89 | await remember().waitFor(); |
| 90 | assert.equal(await remember().isChecked(), false); |
| 91 | assert.equal((await account()).unlocked, false); |
| 92 | await unlock(true); |
| 93 | await menu().click(); |
| 94 | await page.getByRole("menuitem", { name: "退出账户", exact: true }).click(); |
| 95 | await page.getByRole("dialog").getByRole("button", { name: "停止环境并退出账户", exact: true }).click(); |
| 96 | await page.getByRole("region", { name: "解锁 Cage", exact: true }).waitFor(); |
| 97 | assert.equal(Boolean((await account()).trusted_login), false); |
| 98 | const final = JSON.parse(await fs.readFile(path.join(root, "profile", "account.json"), "utf8")); |
| 99 | assert.equal(final.sealed_key, record.sealed_key, "workspace password-wrapped key changed"); |
| 100 | assert.deepEqual(errors, []); |
| 101 | console.log("PASS: real production UI/controller, isolated OS trust, create opt-out, password opt-in, controller restart, remembered login, persistent manual lock, opt-out restart, logout revocation and unchanged workspace key"); |
| 102 | } finally { |
| 103 | await browser.close(); |
| 104 | await command("prepare-update", "--json"); |
| 105 | await fs.rm(root, { recursive: true, force: true }); |
| 106 | } |