| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir, writeFile } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const output = process.env.CAGE_TEST_CAPTURE || "/tmp/cage-sidebar-session-error"; |
| 6 | await mkdir(output, { recursive: true }); |
| 7 | const original = "Windows Claude Code 尚未就绪,请检查原生组件、图形登录和官方账号登录"; |
| 8 | const current = "未能连接 Windows 会话服务,请确认 Windows 环境已启动;持续失败时在设置中导出诊断包"; |
| 9 | const longError = (current + "。").repeat(4); |
| 10 | const evidence = []; |
| 11 | for (const [platform, guestSystem] of [["windows", "windows-11"], ["darwin", "macos"], ["linux", "debian-13"]]) { |
| 12 | let message = original, httpFailure = false, pending = false, release, requests = 0; |
| 13 | const fixture = await createWorkbench({ |
| 14 | url: process.env.CAGE_UI_URL || "http://127.0.0.1:5217", |
| 15 | clientContext: { mode: "local", connection_id: "local", client_platform: platform, server_platform: platform, connection: { state: "disconnected" } }, |
| 16 | beforeNavigate: async (_page, state) => { |
| 17 | state.account.guest_system = state.status.guest_system = guestSystem; |
| 18 | state.status.ready = state.status.vm_running = true; |
| 19 | }, |
| 20 | cliRequest: async route => { |
| 21 | requests++; |
| 22 | if (pending) await new Promise(resolve => { release = resolve; }); |
| 23 | if (httpFailure) return route.fulfill({ status: 503, contentType: "text/plain; charset=utf-8", body: message }); |
| 24 | return route.fulfill({ json: { sessions: [{ id: "error-history", title: "保留的历史会话", workspace: "/Users/shihao/.claude-cage-workspace" }], notice: message } }); |
| 25 | }, |
| 26 | }); |
| 27 | const { page, browser, errors } = fixture; |
| 28 | try { |
| 29 | const card = page.locator(".sidebar-session-error"); |
| 30 | const alert = card.getByRole("alert"); |
| 31 | await alert.getByText(original, { exact: true }).waitFor(); |
| 32 | const retry = card.getByRole("button", { name: "重新读取会话", exact: true }); |
| 33 | await page.getByRole("button", { name: "保留的历史会话", exact: true }).waitFor(); |
| 34 | await retry.waitFor(); |
| 35 | await page.locator(".sidebar-scroll").screenshot({ path: `${output}/${platform}-legacy-message.png` }); |
| 36 | |
| 37 | for (const [theme, sidebarWidth, fontSize, error] of [["light", 248, 14, current], ["dark", 220, 14, longError], ["light", 248, 28, current]]) { |
| 38 | message = error; |
| 39 | await page.emulateMedia({ colorScheme: theme }); |
| 40 | await page.evaluate(({sidebarWidth, fontSize}) => { |
| 41 | document.documentElement.style.setProperty("--sidebar-width", `${sidebarWidth}px`); |
| 42 | document.documentElement.style.setProperty("font-size", `${fontSize}px`); |
| 43 | }, {sidebarWidth, fontSize}); |
| 44 | await retry.click(); |
| 45 | await alert.getByText(error, { exact: true }).waitFor(); |
| 46 | await page.waitForFunction(() => document.querySelector(".sidebar-session-error > button")?.disabled === false); |
| 47 | const layout = await card.evaluate(element => { |
| 48 | const box = node => { const r = node.getBoundingClientRect(); return { top:r.top, bottom:r.bottom, left:r.left, right:r.right }; }; |
| 49 | const button = element.querySelector("button"), paragraph = element.querySelector("p"); |
| 50 | return { card:box(element), paragraph:box(paragraph), button:box(button), |
| 51 | project:box(element.nextElementSibling), width:element.clientWidth, scrollWidth:element.scrollWidth, |
| 52 | buttonHeight:button.clientHeight, buttonScrollHeight:button.scrollHeight, |
| 53 | text:paragraph.textContent }; |
| 54 | }); |
| 55 | assert.ok(layout.button.top >= layout.paragraph.bottom + 8, "retry overlaps the explanation"); |
| 56 | assert.ok(layout.project.top >= layout.card.bottom, "error covers project controls"); |
| 57 | assert.ok(layout.scrollWidth <= layout.width + 1, "error overflows narrow sidebar"); |
| 58 | assert.ok(layout.buttonScrollHeight <= layout.buttonHeight + 1, "retry label is clipped"); |
| 59 | assert.equal(layout.text, error, "long error was truncated"); |
| 60 | |
| 61 | |
| 62 | if (fontSize === 28) await page.setViewportSize({ width:1280, height:1400 }); |
| 63 | await card.screenshot({ path: `${output}/${platform}-${theme}-${fontSize}.png` }); |
| 64 | if (fontSize === 14 && theme === "light") await page.locator(".sidebar-scroll").screenshot({ path: `${output}/${platform}-after.png` }); |
| 65 | if (fontSize === 28) await page.setViewportSize({ width:1280, height:850 }); |
| 66 | evidence.push({ platform, theme, sidebarWidth, fontSize, layout }); |
| 67 | } |
| 68 | |
| 69 | await page.evaluate(() => document.documentElement.style.setProperty("font-size", "14px")); |
| 70 | message = "会话服务连接中断,请重试"; |
| 71 | httpFailure = true; |
| 72 | await retry.click(); |
| 73 | await alert.getByText(message, { exact: true }).waitFor(); |
| 74 | await page.waitForFunction(() => document.querySelector(".sidebar-session-error > button")?.disabled === false); |
| 75 | assert.equal(await page.getByRole("button", { name: "保留的历史会话", exact: true }).count(), 1, "HTTP failure discarded saved history"); |
| 76 | |
| 77 | pending = true; |
| 78 | await retry.focus(); |
| 79 | await page.keyboard.press("Enter"); |
| 80 | await card.getByRole("button", { name: "正在读取…", exact: true }).waitFor(); |
| 81 | assert.equal(await card.getByRole("button").isDisabled(), true); |
| 82 | assert.equal(await card.getByRole("button").getAttribute("aria-busy"), "true"); |
| 83 | await alert.getByText(message, { exact: true }).waitFor(); |
| 84 | const inFlight = requests; |
| 85 | await page.keyboard.press("Enter"); |
| 86 | assert.equal(requests, inFlight, "keyboard submitted a duplicate retry"); |
| 87 | await card.screenshot({ path: `${output}/${platform}-retry.png` }); |
| 88 | httpFailure = pending = false; |
| 89 | message = ""; |
| 90 | release(); |
| 91 | await card.waitFor({ state: "hidden" }); |
| 92 | await page.getByRole("button", { name: "保留的历史会话", exact: true }).waitFor(); |
| 93 | await page.reload(); |
| 94 | await page.getByRole("button", { name: "保留的历史会话", exact: true }).waitFor(); |
| 95 | assert.equal(await card.count(), 0, "successful reload restored a stale error"); |
| 96 | assert.deepEqual(errors, []); |
| 97 | console.log(`${platform}: notice/HTTP failure, long text, narrow sidebar, dark theme, 200% text, keyboard retry and recovery passed`); |
| 98 | } finally { release?.(); await browser.close(); } |
| 99 | } |
| 100 | await writeFile(`${output}/result.json`, JSON.stringify(evidence, null, 2)); |