| 1 | import assert from "node:assert/strict"; |
| 2 | import { readFile } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | |
| 6 | const { browser, page, state, errors } = await createWorkbench(); |
| 7 | const profiles = []; |
| 8 | const starts = []; |
| 9 | let assignedProfile = 0; |
| 10 | let failRefresh = false; |
| 11 | let accountRequests = 0; |
| 12 | const usageRequests = []; |
| 13 | let heldRefresh = null; |
| 14 | let offlineUsage = false; |
| 15 | let panelWorker = false; |
| 16 | let usageUnavailable = ""; |
| 17 | let defaultLogin = null; |
| 18 | let detectionError = ""; |
| 19 | let lastLoginNotice = ""; |
| 20 | const joins = []; |
| 21 | const now = Math.floor(Date.now() / 1000); |
| 22 | const openAccounts = async () => { |
| 23 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 24 | await page.getByRole("button", { name: "Claude 账号", exact: true }).click(); |
| 25 | }; |
| 26 | |
| 27 | async function checkLongAccountList() { |
| 28 | const total = 40; |
| 29 | for (let number = profiles.length + 1; number <= total; number++) { |
| 30 | profiles.push({ |
| 31 | id: "profile-" + number, label: "测试账号 " + number, enabled: true, status: "ready", |
| 32 | email: `user.${number}@example.test`, plan: "Pro", |
| 33 | quota: { observed_at: now, five_hour: { used_percentage: 28, resets_at: now + 6000 }, |
| 34 | seven_day: { used_percentage: 64, resets_at: now + 200000 } }, |
| 35 | }); |
| 36 | } |
| 37 | await openAccounts(); |
| 38 | const list = page.getByRole("region", { name: "账号列表", exact: true }); |
| 39 | const last = list.getByRole("article", { name: "测试账号 " + total, exact: true }); |
| 40 | await last.waitFor({ state: "attached" }); |
| 41 | assert.equal(await list.getByRole("article").count(), total); |
| 42 | const header = page.getByRole("heading", { name: "Claude 账号", exact: true }); |
| 43 | const add = page.getByRole("button", { name: "登录新账号", exact: true }); |
| 44 | const headerY = (await header.boundingBox()).y; |
| 45 | const addY = (await add.boundingBox()).y; |
| 46 | assert.ok(headerY >= 0 && addY >= 0); |
| 47 | assert.ok(await list.evaluate((el) => el.scrollHeight > el.clientHeight * 2 && el.clientHeight > 0)); |
| 48 | await list.hover(); |
| 49 | await page.mouse.wheel(0, 640); |
| 50 | await page.waitForFunction((el) => el.scrollTop > 0, await list.elementHandle()); |
| 51 | assert.ok(Math.abs((await header.boundingBox()).y - headerY) <= 1, "scrolling must not move the panel title"); |
| 52 | assert.ok(Math.abs((await add.boundingBox()).y - addY) <= 1, "the add action must stay in view"); |
| 53 | |
| 54 | await add.focus(); |
| 55 | const reachableControls = await list.locator("button:not(:disabled),input:not(:disabled),[tabindex='0']").count(); |
| 56 | |
| 57 | const nextControl = process.platform === "darwin" && browser.browserType().name() === "webkit" ? "Alt+Tab" : "Tab"; |
| 58 | let reached = false; |
| 59 | for (let step = 0; step < reachableControls + 5; step++) { |
| 60 | await page.keyboard.press(nextControl); |
| 61 | if (await last.evaluate((row) => row.contains(document.activeElement))) { |
| 62 | reached = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | assert.ok(reached, "tab navigation must reach a control in the last account"); |
| 67 | const visibleFocus = await list.evaluate((el) => { |
| 68 | const focused = document.activeElement.getBoundingClientRect(); |
| 69 | const viewport = el.getBoundingClientRect(); |
| 70 | return focused.top >= viewport.top - 1 && focused.bottom <= viewport.bottom + 1; |
| 71 | }); |
| 72 | assert.ok(visibleFocus, "the account list must scroll the focused control into view"); |
| 73 | assert.ok(Math.abs((await add.boundingBox()).y - addY) <= 1); |
| 74 | |
| 75 | for (const width of [1280, 520, 375]) { |
| 76 | await page.setViewportSize({ width, height: 700 }); |
| 77 | const bounds = await list.evaluate((el) => ({ scroll: el.scrollWidth, client: el.clientWidth, |
| 78 | document: document.documentElement.scrollWidth, viewport: window.innerWidth })); |
| 79 | assert.ok(bounds.scroll <= bounds.client + 1 && bounds.document <= bounds.viewport + 1, |
| 80 | `the account list must fit a ${width}px viewport horizontally: ${JSON.stringify(bounds)}`); |
| 81 | const first = await last.getByRole("progressbar", { name: "5h 已用额度" }).boundingBox(); |
| 82 | const second = await last.getByRole("progressbar", { name: "7d 已用额度" }).boundingBox(); |
| 83 | assert.ok(first.width > 0 && Math.abs(first.x - second.x) <= 1 && Math.abs(first.width - second.width) <= 1, |
| 84 | "5h and 7d progress bars must start and end at the same horizontal positions"); |
| 85 | assert.ok(first.y + first.height <= second.y, "the two limits occupy separate rows"); |
| 86 | } |
| 87 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 88 | await page.setViewportSize({ width: 1280, height: 850 }); |
| 89 | } |
| 90 | |
| 91 | async function checkLocalCountdown() { |
| 92 | const frozen = new Date("2030-01-01T08:00:00Z"); |
| 93 | await page.clock.install({ time: new Date(+frozen - 60_000) }); |
| 94 | await page.reload(); |
| 95 | await page.getByRole("tab", { name: "终端 4 · 工作", exact: true }).waitFor(); |
| 96 | await page.clock.pauseAt(frozen); |
| 97 | const seconds = +frozen / 1000; |
| 98 | profiles[0].quota = { observed_at: seconds, five_hour: { used_percentage: 28, observed_at: seconds - 900, resets_at: seconds + 5 }, |
| 99 | seven_day: { used_percentage: 64, observed_at: seconds, resets_at: seconds + 7200 } }; |
| 100 | await openAccounts(); |
| 101 | const row = page.getByRole("article", { name: "工作", exact: true }); |
| 102 | await row.getByText("5秒后", { exact: true }).waitFor(); |
| 103 | await row.getByText("最近更新 刚刚", { exact: true }).waitFor(); |
| 104 | await page.getByText("正在请求账号用量…").waitFor({ state: "hidden" }); |
| 105 | const requestsBefore = accountRequests; |
| 106 | const wavesBefore = usageRequests.filter((r) => r.action === "usage_panel").length; |
| 107 | await page.clock.runFor(2000); |
| 108 | await row.getByText("3秒后", { exact: true }).waitFor(); |
| 109 | await page.clock.runFor(3000); |
| 110 | await row.getByText("等待更新", { exact: true }).waitFor(); |
| 111 | assert.equal(await row.getByText("28%", { exact: true }).count(), 0, "expired quota cannot remain current"); |
| 112 | const expired = row.getByRole("group", { name: "5h 额度" }); |
| 113 | assert.equal(await expired.getByRole("progressbar").count(), 0); |
| 114 | const history = expired.getByText(/^上次 28% ·/); |
| 115 | assert.equal(await history.locator("time").getAttribute("datetime"), new Date((seconds - 900) * 1000).toISOString()); |
| 116 | await row.getByText("64%", { exact: true }).waitFor(); |
| 117 | assert.equal(accountRequests, requestsBefore, "countdown ticks and expiry must not poll the account API"); |
| 118 | const joinedBefore = joins.length; |
| 119 | defaultLogin = { identity: "fresh-session", email: "[email protected]", plan: "Pro" }; |
| 120 | await page.clock.runFor(8000); |
| 121 | await page.getByRole("article", { name: defaultLogin.email }).getByRole("button", { name: "加入调度" }).waitFor(); |
| 122 | assert.equal(joins.length, joinedBefore, "discovering a new default identity during polling cannot enroll it"); |
| 123 | assert.equal(usageRequests.filter((r) => r.action === "usage_panel").length, wavesBefore, "12-second panel polling cannot start another usage query"); |
| 124 | assert.ok(usageRequests.some((r) => !r.action && r.visible), "a visible panel keeps the existing queue alive via GET"); |
| 125 | await page.clock.runFor(47_000); |
| 126 | await row.getByText("最近更新 1分钟前", { exact: true }).waitFor(); |
| 127 | await page.clock.runFor(240_000); |
| 128 | await row.getByText("最近更新 5分钟前", { exact: true }).waitFor(); |
| 129 | } |
| 130 | |
| 131 | async function checkUsagePanel() { |
| 132 | const profile = profiles[0]; |
| 133 | const waves = () => usageRequests.filter((r) => r.action === "usage_panel").length; |
| 134 | const before = waves(); |
| 135 | panelWorker = true; |
| 136 | await openAccounts(); |
| 137 | const row = page.getByRole("article", { name: profile.label, exact: true }); |
| 138 | await page.getByText("正在查询账号用量…", { exact: true }).waitFor(); |
| 139 | profile.usage_checking = true; |
| 140 | await row.getByText("正在查询额度…").waitFor(); |
| 141 | assert.equal(waves(), before + 1, "opening the panel initiates one real usage request"); |
| 142 | assert.equal(await row.getByRole("button", { name: "检查状态" }).isEnabled(), false); |
| 143 | panelWorker = false; |
| 144 | Object.assign(profile, { usage_checking: false, usage_status: "cached", usage_checked_at: now }); |
| 145 | await row.getByText("本次只有缓存读数").waitFor(); |
| 146 | await row.getByText("23%", { exact: true }).waitFor(); |
| 147 | await row.getByText("历史读数", { exact: true }).waitFor(); |
| 148 | await page.setViewportSize({ width: 375, height: 700 }); |
| 149 | const list = page.getByRole("region", { name: "账号列表", exact: true }); |
| 150 | assert.ok(await list.evaluate((el) => el.scrollWidth <= el.clientWidth + 1), "window history fits the narrow account list"); |
| 151 | await page.setViewportSize({ width: 1280, height: 850 }); |
| 152 | assert.equal(waves(), before + 1, "polling the completed query does not start another query"); |
| 153 | const back = page.getByRole("button", { name: "返回工作台", exact: true }); |
| 154 | await back.click(); |
| 155 | offlineUsage = true; |
| 156 | await openAccounts(); |
| 157 | await row.getByText("环境离线,无法查询").waitFor(); |
| 158 | await row.getByText("23%", { exact: true }).waitFor(); |
| 159 | assert.equal(await row.getByRole("button", { name: "检查状态" }).isEnabled(), false); |
| 160 | await back.click(); |
| 161 | offlineUsage = false; |
| 162 | usageUnavailable = "网络已暂停,恢复连接后可查询订阅额度"; |
| 163 | await openAccounts(); |
| 164 | await page.getByText(usageUnavailable).waitFor(); |
| 165 | assert.equal(await row.getByRole("button", { name: "检查状态" }).isEnabled(), false); |
| 166 | assert.equal(await page.getByRole("button", { name: "登录新账号", exact: true }).isEnabled(), true); |
| 167 | await back.click(); |
| 168 | usageUnavailable = ""; |
| 169 | profile.usage_status = "not_applicable"; |
| 170 | await openAccounts(); |
| 171 | await row.getByText("当前登录不提供订阅额度").waitFor(); |
| 172 | await row.getByText("历史读数", { exact: true }).waitFor(); |
| 173 | await back.click(); |
| 174 | |
| 175 | profile.usage_status = "error"; |
| 176 | profile.usage_checked_at = now; |
| 177 | await openAccounts(); |
| 178 | await row.getByText("额度查询失败").waitFor(); |
| 179 | const failedFive = row.getByRole("group", { name: "5h 额度" }); |
| 180 | assert.equal(await failedFive.locator("time").getAttribute("datetime"), new Date((now - 3600) * 1000).toISOString()); |
| 181 | const failedWeek = row.getByRole("group", { name: "7d 额度" }); |
| 182 | assert.equal(await failedWeek.getByRole("progressbar").count(), 0); |
| 183 | assert.equal(await failedWeek.getByText(/^上次 100% ·/).locator("time").getAttribute("datetime"), new Date((now - 7200) * 1000).toISOString()); |
| 184 | await back.click(); |
| 185 | } |
| 186 | try { |
| 187 | await page.route("**/api/claude-accounts*", async (route) => { |
| 188 | accountRequests++; |
| 189 | const body = route.request().postDataJSON(); |
| 190 | usageRequests.push({ ...body, visible: new URL(route.request().url()).searchParams.get("panel") === "visible" }); |
| 191 | if (body) assert.ok(["join", "update", "refresh", "cancel", "refresh_default", "usage_panel"].includes(body.action)); |
| 192 | if (body?.action === "join") { |
| 193 | joins.push(body); |
| 194 | if (!defaultLogin || body.identity !== defaultLogin.identity) |
| 195 | return route.fulfill({ status: 409, contentType: "text/plain; charset=utf-8", body: "普通终端登录已改变,请确认当前身份" }); |
| 196 | profiles.push({ id: "default-" + defaultLogin.identity, source: "default", label: defaultLogin.email, |
| 197 | email: defaultLogin.email, plan: defaultLogin.plan, enabled: true, status: "ready" }); |
| 198 | } |
| 199 | if (body?.action === "cancel") { |
| 200 | const index = profiles.findIndex((profile) => profile.id === body.id); |
| 201 | assert.equal(index, 0); |
| 202 | profiles.splice(index, 1); |
| 203 | } |
| 204 | if (body?.action === "update") Object.assign(profiles.find((a) => a.id === body.id), |
| 205 | body.label !== undefined ? { label: body.label } : { enabled: body.enabled }); |
| 206 | if (body?.action === "refresh" && failRefresh) |
| 207 | return route.fulfill({ status: 503, contentType: "text/plain; charset=utf-8", body: "暂时无法检查登录" }); |
| 208 | if (body?.action === "refresh" && heldRefresh) await heldRefresh.promise; |
| 209 | if (body?.action === "refresh_default") { |
| 210 | assert.equal(body.identity, defaultLogin?.identity); |
| 211 | Object.assign(defaultLogin, { usage_status: "unavailable", usage_checked_at: now }); |
| 212 | } |
| 213 | return route.fulfill({ json: { accounts: profiles, detection_status: offlineUsage ? "offline" : detectionError ? "unavailable" : defaultLogin ? "ready" : "signed_out", |
| 214 | detected: !detectionError && defaultLogin && !profiles.some((p) => p.email === defaultLogin.email) ? defaultLogin : undefined, |
| 215 | detection_error: detectionError, login_notice: lastLoginNotice, usage_panel_running: panelWorker, |
| 216 | usage_unavailable_reason: usageUnavailable } }); |
| 217 | }); |
| 218 | await page.route("**/api/terminals", async (route) => { |
| 219 | const body = route.request().postDataJSON(); |
| 220 | if (!body) return route.fulfill({ json: state.terminals }); |
| 221 | starts.push(body); |
| 222 | if (body.kind === "claude" && profiles.length && !profiles[assignedProfile].enabled) |
| 223 | return route.fulfill({ status: 409, contentType: "text/plain; charset=utf-8", body: "没有可用 Claude 账号" }); |
| 224 | const terminal = { id: String(starts.length), title: "终端 " + starts.length, kind: body.kind, |
| 225 | workspace: body.workspace, claude_account_id: body.kind === "claude-login" ? body.claude_account_id || "one" |
| 226 | : body.kind === "claude" ? profiles[assignedProfile]?.id : undefined, |
| 227 | claude_account_source: body.kind === "claude-login" ? "managed" : profiles[assignedProfile]?.source }; |
| 228 | state.terminals.push(terminal); |
| 229 | return route.fulfill({ json: terminal }); |
| 230 | }); |
| 231 | state.status.vm_running = state.status.ready = true; |
| 232 | await page.reload(); |
| 233 | await page.getByRole("button", { name: /^在 .* 新建终端$/ }).waitFor(); |
| 234 | assert.equal(usageRequests.filter((r) => r.action === "usage_panel").length, 0, "launching the workbench must not query subscription usage"); |
| 235 | const plus = page.getByRole("button", { name: /^在 .* 新建终端$/ }); |
| 236 | const launchClaude = async () => { |
| 237 | await plus.click(); |
| 238 | await page.getByRole("menuitem", { name: "Claude 终端", exact: true }).press("Enter"); |
| 239 | }; |
| 240 | const back = () => page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 241 | const profileCard = (label) => page.getByRole("article", { name: label, exact: true }); |
| 242 | const rename = async (before, label) => { |
| 243 | const row = profileCard(before); |
| 244 | await row.getByRole("button", { name: "修改 " + before + " 的备注" }).click(); |
| 245 | await row.getByRole("textbox", { name: "账号备注" }).fill(label); |
| 246 | await row.getByRole("button", { name: "保存备注" }).click(); |
| 247 | }; |
| 248 | |
| 249 | |
| 250 | profiles.push({ id: "legacy", label: "草稿", status: "pending", enabled: true }); |
| 251 | await openAccounts(); |
| 252 | const draft = profileCard("草稿"); |
| 253 | await draft.getByRole("button", { name: "撤销添加" }).click(); |
| 254 | const confirmation = page.getByRole("dialog", { name: "撤销添加「草稿」?" }); |
| 255 | await confirmation.getByText(/沿用环境原有的登录/).waitFor(); |
| 256 | await confirmation.getByRole("button", { name: "取消", exact: true }).click(); |
| 257 | assert.equal(profiles.length, 1); |
| 258 | await draft.getByRole("button", { name: "撤销添加" }).click(); |
| 259 | await confirmation.getByRole("button", { name: "撤销添加", exact: true }).click(); |
| 260 | await page.getByText("尚无已加入调度的账号。").waitFor(); |
| 261 | await page.getByText(/普通终端尚未登录 Claude 订阅/).waitFor(); |
| 262 | await back(); |
| 263 | await launchClaude(); |
| 264 | await page.getByRole("tab", { name: "终端 1", exact: true }).waitFor(); |
| 265 | assert.deepEqual(starts[0], { workspace: state.settings.workspace, kind: "claude" }); |
| 266 | |
| 267 | await openAccounts(); |
| 268 | assert.equal(await page.getByRole("textbox", { name: "账号名称" }).count(), 0); |
| 269 | await page.getByRole("button", { name: "登录新账号", exact: true }).click(); |
| 270 | await page.getByRole("tab", { name: "终端 2 · 新账号登录", exact: true }).waitFor(); |
| 271 | assert.deepEqual(starts[1], { workspace: state.settings.workspace, kind: "claude-login" }); |
| 272 | assert.equal(profiles.length, 0, "opening a login terminal must not create a pool account"); |
| 273 | state.terminals[1].closed = true; |
| 274 | const email = "[email protected]"; |
| 275 | profiles.push({ id: "one", status: "ready", label: email, email, plan: "Pro", enabled: true, source: "managed", |
| 276 | quota: { observed_at: now, five_hour: { used_percentage: 0, observed_at: now - 3600, resets_at: now + 3600 }, |
| 277 | seven_day: { used_percentage: 100, observed_at: now - 7200, resets_at: now - 80 } } }); |
| 278 | lastLoginNotice = "已加入 " + email; |
| 279 | await openAccounts(); |
| 280 | await page.getByText(lastLoginNotice).waitFor(); |
| 281 | await rename(email, "工作"); |
| 282 | let card = profileCard("工作"); |
| 283 | await card.getByText("0%", { exact: true }).waitFor(); |
| 284 | await card.getByText("历史读数", { exact: true }).waitFor(); |
| 285 | await card.getByText("等待更新", { exact: true }).waitFor(); |
| 286 | assert.equal(await card.getByRole("button", { name: "撤销添加" }).count(), 0); |
| 287 | await card.getByRole("checkbox", { name: "参与自动分配" }).click(); |
| 288 | await card.getByRole("checkbox", { checked: false }).waitFor(); |
| 289 | failRefresh = true; |
| 290 | await card.getByRole("button", { name: "检查状态" }).click(); |
| 291 | await page.getByRole("alert").filter({ hasText: "暂时无法检查登录" }).waitFor(); |
| 292 | await card.getByText("额度查询失败").waitFor(); |
| 293 | await card.getByText("0%", { exact: true }).waitFor(); |
| 294 | assert.equal(await card.getByRole("group", { name: "5h 额度" }).locator("time").getAttribute("datetime"), new Date((now - 3600) * 1000).toISOString()); |
| 295 | assert.equal(await card.getByRole("group", { name: "7d 额度" }).getByText(/^上次 100% ·/).locator("time").getAttribute("datetime"), new Date((now - 7200) * 1000).toISOString()); |
| 296 | failRefresh = false; |
| 297 | heldRefresh = Promise.withResolvers(); |
| 298 | await card.getByRole("button", { name: "检查状态" }).click(); |
| 299 | await card.getByText("正在查询额度…").waitFor(); |
| 300 | assert.equal(await card.getByRole("button", { name: "检查状态" }).isEnabled(), false); |
| 301 | assert.deepEqual(usageRequests.filter((r) => r.action === "refresh").at(-1), { action: "refresh", id: "one", visible: false }); |
| 302 | |
| 303 | Object.assign(profiles[0], { usage_status: "partial", usage_checked_at: now, |
| 304 | quota: { five_hour: { used_percentage: 23, observed_at: now - 3600, resets_at: 0 }, |
| 305 | seven_day: { used_percentage: 100, observed_at: now - 7200, resets_at: now - 80 }, observed_at: now - 3600 } }); |
| 306 | heldRefresh.resolve(); |
| 307 | await card.getByText("本次只返回了部分额度").waitFor(); |
| 308 | await card.getByText("23%", { exact: true }).waitFor(); |
| 309 | await card.getByText(/^读取于 /).waitFor(); |
| 310 | await card.getByText("重置时间未知", { exact: true }).waitFor(); |
| 311 | const oldWeek = card.getByRole("group", { name: "7d 额度" }).getByText(/^上次 100% ·/); |
| 312 | assert.equal(await oldWeek.locator("time").getAttribute("datetime"), new Date((now - 7200) * 1000).toISOString()); |
| 313 | const updated = card.getByText(/^最近更新 /); |
| 314 | assert.equal(await updated.count(), 1); |
| 315 | assert.equal(await updated.locator("time").getAttribute("datetime"), new Date((now - 3600) * 1000).toISOString(), |
| 316 | "a newer query must not make retained quota readings appear fresh"); |
| 317 | assert.equal(await card.getByText(/^(上次查询|最近读数) /).count(), 0); |
| 318 | heldRefresh = null; |
| 319 | await back(); |
| 320 | await launchClaude(); |
| 321 | await page.getByText("没有可用 Claude 账号", { exact: true }).waitFor(); |
| 322 | assert.equal(await page.getByRole("tab", { name: /^终端 / }).count(), 2); |
| 323 | await openAccounts(); |
| 324 | await card.getByRole("checkbox", { name: "参与自动分配" }).click(); |
| 325 | await card.getByRole("checkbox", { checked: true }).waitFor(); |
| 326 | await back(); |
| 327 | await launchClaude(); |
| 328 | await page.getByRole("tab", { name: "终端 4 · 工作", exact: true }).waitFor(); |
| 329 | await page.getByText("Claude · 工作", { exact: true }).waitFor(); |
| 330 | assert.deepEqual(starts.at(-1), { workspace: state.settings.workspace, kind: "claude" }); |
| 331 | |
| 332 | const identityA = { identity: "a-in-org", email: "[email protected]", organization: "Org", plan: "Pro" }; |
| 333 | defaultLogin = identityA; |
| 334 | await openAccounts(); |
| 335 | assert.equal(await card.getByRole("button", { name: "重新登录" }).isEnabled(), false); |
| 336 | const detectedA = profileCard(identityA.email); |
| 337 | await detectedA.getByText(/已发现,未加入/).waitFor(); |
| 338 | const clawd = await readFile(new URL("../src/claude-account-crab.svg", import.meta.url), "utf8"); |
| 339 | assert.ok(await detectedA.locator("img").evaluate(async (img, source) => { |
| 340 | await img.decode(); |
| 341 | const shape = (svg) => { |
| 342 | const root = new DOMParser().parseFromString(svg, "image/svg+xml").documentElement; |
| 343 | return [root.getAttribute("viewBox"), root.getAttribute("fill"), ...Array.from(root.querySelectorAll("path"), (p) => p.getAttribute("d"))]; |
| 344 | }; |
| 345 | const data = /^data:image\/svg\+xml(;base64)?,(.*)$/s.exec(img.currentSrc); |
| 346 | const rendered = data ? data[1] ? atob(data[2]) : decodeURIComponent(data[2]) : await (await fetch(img.currentSrc)).text(); |
| 347 | return img.naturalWidth > 0 && JSON.stringify(shape(rendered)) === JSON.stringify(shape(source)); |
| 348 | }, clawd), "the detected account renders the same Clawd artwork as managed accounts"); |
| 349 | assert.equal(await detectedA.getByRole("checkbox").count(), 0); |
| 350 | await page.setViewportSize({ width: 375, height: 700 }); |
| 351 | const panelList = page.getByRole("region", { name: "账号列表", exact: true }); |
| 352 | assert.ok(await panelList.evaluate((el) => el.scrollWidth <= el.clientWidth + 1), "detected identities fit on a narrow screen"); |
| 353 | await page.setViewportSize({ width: 1280, height: 850 }); |
| 354 | assert.equal(profiles.length, 1, "detected identity is not in the pool"); |
| 355 | await detectedA.getByRole("button", { name: "检查状态" }).click(); |
| 356 | await detectedA.getByText("本次未返回额度").waitFor(); |
| 357 | assert.equal(await detectedA.getByText(/^最近更新 /).locator("time").getAttribute("datetime"), new Date(now * 1000).toISOString(), |
| 358 | "accounts without readings fall back to the latest query time"); |
| 359 | assert.equal(await detectedA.getByText("暂无读数", { exact: true }).count(), 2); |
| 360 | assert.deepEqual(usageRequests.filter((r) => r.action === "refresh_default").at(-1), { action: "refresh_default", identity: identityA.identity, visible: false }); |
| 361 | await detectedA.getByRole("button", { name: "加入调度" }).click(); |
| 362 | await detectedA.getByText("普通终端账号", { exact: true }).waitFor(); |
| 363 | assert.deepEqual(joins.at(-1), { action: "join", identity: identityA.identity }); |
| 364 | assert.equal(await detectedA.getByRole("button", { name: /登录/ }).count(), 0); |
| 365 | await rename(identityA.email, "个人"); |
| 366 | await profileCard("个人").waitFor(); |
| 367 | await rename("个人", ""); |
| 368 | await profileCard(identityA.email).waitFor(); |
| 369 | await rename(identityA.email, "个人"); |
| 370 | await back(); |
| 371 | assignedProfile = 1; |
| 372 | await launchClaude(); |
| 373 | await page.getByRole("tab", { name: "终端 5 · 个人", exact: true }).waitFor(); |
| 374 | defaultLogin = { identity: "b-in-org", email: "[email protected]", plan: "Pro" }; |
| 375 | profiles[1].status = "changed"; |
| 376 | profiles[1].quota = null; |
| 377 | await openAccounts(); |
| 378 | const nextIdentity = profileCard(defaultLogin.email); |
| 379 | await nextIdentity.getByText(/未加入/).waitFor(); |
| 380 | await profileCard("个人").getByText(/请重新打开工作会话确认身份/).waitFor(); |
| 381 | assert.equal(await nextIdentity.getByRole("checkbox").count(), 0); |
| 382 | assert.equal(profiles[1].email, identityA.email); |
| 383 | |
| 384 | defaultLogin = { identity: "c-in-org", email: "[email protected]", plan: "Pro" }; |
| 385 | await nextIdentity.getByRole("button", { name: "加入调度" }).click(); |
| 386 | await page.getByRole("alert").filter({ hasText: "普通终端登录已改变" }).waitFor(); |
| 387 | await profileCard(defaultLogin.email).getByText(/未加入/).waitFor(); |
| 388 | assert.equal(joins.at(-1).identity, "b-in-org"); |
| 389 | assert.equal(profiles.length, 2); |
| 390 | await back(); |
| 391 | |
| 392 | Object.assign(profiles[1], { source: "managed", status: "ready" }); |
| 393 | await openAccounts(); |
| 394 | await profileCard("个人").getByText(/仍使用普通终端的登录/).waitFor(); |
| 395 | await back(); |
| 396 | await page.getByRole("tab", { name: "终端 5 · 个人", exact: true }).click(); |
| 397 | await page.getByText("Claude · 个人(启动时账号)", { exact: true }).waitFor(); |
| 398 | await page.getByRole("tab", { name: "终端 4 · 工作", exact: true }).click(); |
| 399 | await page.getByText("Claude · 工作", { exact: true }).waitFor(); |
| 400 | assert.equal(starts.length, 5, "changing the environment identity must never replay a session"); |
| 401 | detectionError = "无法读取普通终端状态"; |
| 402 | await openAccounts(); |
| 403 | await page.getByRole("alert").filter({ hasText: detectionError }).waitFor(); |
| 404 | assert.equal(await page.getByText(/普通终端尚未登录 Claude 订阅/).count(), 0); |
| 405 | await back(); |
| 406 | detectionError = ""; |
| 407 | defaultLogin = null; |
| 408 | await page.reload(); |
| 409 | await page.getByRole("tab", { name: "终端 4 · 工作", exact: true }).waitFor(); |
| 410 | await page.getByRole("tab", { name: "终端 5 · 个人", exact: true }).waitFor(); |
| 411 | state.terminals.find((t) => t.id === "4").closed = true; |
| 412 | await openAccounts(); |
| 413 | await card.getByRole("checkbox", { name: "参与自动分配" }).click(); |
| 414 | await card.getByRole("checkbox", { checked: false }).waitFor(); |
| 415 | await card.getByRole("button", { name: "重新登录" }).click(); |
| 416 | await page.getByRole("tab", { name: "终端 6 · 工作", exact: true }).waitFor(); |
| 417 | assert.deepEqual(starts.at(-1), { workspace: state.settings.workspace, kind: "claude-login", claude_account_id: "one" }); |
| 418 | assert.ok(!state.requests.some((r) => r.path.startsWith("/claude-desktop") && r.method === "POST")); |
| 419 | await checkUsagePanel(); |
| 420 | await checkLongAccountList(); |
| 421 | await checkLocalCountdown(); |
| 422 | assert.deepEqual(errors, []); |
| 423 | console.log("Claude account UI: independent login, explicit default enrollment, identity change, quota, terminal affinity, accessible scrolling and countdown passed."); |
| 424 | } finally { |
| 425 | await browser.close(); |
| 426 | } |