| 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-layout"; |
| 6 | const engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 7 | const { page, state, browser, errors } = await createWorkbench({ |
| 8 | url: process.env.CAGE_UI_URL || "http://127.0.0.1:5217", |
| 9 | }); |
| 10 | const workspace = state.settings.workspace; |
| 11 | const titles = ["你好", "软件宣传视频制作", "这是一段用于检查窄侧栏省略显示的很长很长的会话标题"]; |
| 12 | const sessions = titles.map((title, index) => ({ |
| 13 | id: `layout-session-${index}`, workspace, title, pinned: index === 2, |
| 14 | updated_at: "2026-11-07T08:00:00Z", |
| 15 | })); |
| 16 | const evidence = []; |
| 17 | try { |
| 18 | await page.route("**/api/cli/sessions", route => route.fulfill({ json: { sessions } })); |
| 19 | state.status.ready = state.status.vm_running = true; |
| 20 | state.terminals = [{ id: "layout-live", workspace, title: "已打开的会话" }]; |
| 21 | const project = page.getByRole("region", { name: "项目 默认工作区", exact: true }); |
| 22 | const history = project.locator(".sidebar-session"); |
| 23 | await history.last().waitFor(); |
| 24 | await project.getByRole("tab", { name: "已打开的会话", exact: true }).waitFor(); |
| 25 | await mkdir(output, { recursive: true }); |
| 26 | for (const width of [1280, 820]) { |
| 27 | await page.setViewportSize({ width, height: 850 }); |
| 28 | await page.mouse.move(width - 10, 10); |
| 29 | const layout = await project.evaluate(section => { |
| 30 | const rows = [...section.querySelectorAll('.sidebar-tab > button[role="tab"], .sidebar-session')]; |
| 31 | return rows.map(button => { |
| 32 | const box = button.getBoundingClientRect(); |
| 33 | const icon = button.querySelector("svg")?.getBoundingClientRect(); |
| 34 | const title = button.querySelector("span").getBoundingClientRect(); |
| 35 | return { title: button.textContent, x: box.x, iconX: icon?.x, textX: title.x, height: box.height, |
| 36 | right: box.right, titleRight: title.right, clipped: button.querySelector("span").scrollWidth > title.width }; |
| 37 | }); |
| 38 | }); |
| 39 | await page.screenshot({ path: `${output}/${engine}-${width}.png` }); |
| 40 | assert.equal(layout.length, 4); |
| 41 | for (const row of layout) { |
| 42 | assert.ok(row.iconX !== undefined, `missing session icon: ${row.title}`); |
| 43 | assert.ok(Math.abs(row.iconX - layout[0].iconX) < 1, `session icons misaligned: ${JSON.stringify(layout)}`); |
| 44 | assert.ok(Math.abs(row.textX - layout[0].textX) < 1, `session titles misaligned: ${JSON.stringify(layout)}`); |
| 45 | assert.ok(row.height >= 40, `session row too short: ${row.title}`); |
| 46 | assert.ok(row.titleRight <= row.right, `title overflow: ${row.title}`); |
| 47 | } |
| 48 | assert.ok(layout.find(row => row.title.startsWith("这是一段")).clipped, "long session title was not truncated"); |
| 49 | const short = project.getByRole("button", { name: "你好", exact: true }); |
| 50 | const before = await short.locator("svg").first().boundingBox(); |
| 51 | await short.hover(); |
| 52 | await project.getByRole("group", { name: "你好 的操作", exact: true }).getByRole("button", { name: "重命名", exact: true }).waitFor(); |
| 53 | const after = await short.locator("svg").first().boundingBox(); |
| 54 | assert.ok(Math.abs(before.x - after.x) < 1, "hover shifted the session icon"); |
| 55 | await short.focus(); |
| 56 | if (engine !== "webkit") { |
| 57 | await page.keyboard.press("Tab"); |
| 58 | assert.equal(await page.locator(":focus").getAttribute("aria-label"), "置顶"); |
| 59 | } |
| 60 | const rename = project.getByRole("group", { name: "你好 的操作", exact: true }).getByRole("button", { name: "重命名", exact: true }); |
| 61 | await rename.focus(); |
| 62 | await page.keyboard.press("Enter"); |
| 63 | const dialog = page.getByRole("dialog"); |
| 64 | await dialog.getByRole("textbox", { name: "标签名称" }).waitFor(); |
| 65 | await dialog.getByRole("button", { name: "取消", exact: true }).click(); |
| 66 | evidence.push({ width, layout }); |
| 67 | } |
| 68 | const folder = project.locator(".project-disclosure"); |
| 69 | await folder.click(); |
| 70 | await history.first().waitFor({ state: "hidden" }); |
| 71 | assert.equal(await folder.getAttribute("aria-expanded"), "false"); |
| 72 | await folder.press("Enter"); |
| 73 | await history.first().waitFor(); |
| 74 | await page.reload(); |
| 75 | await page.getByRole("button", { name: "你好", exact: true }).waitFor(); |
| 76 | assert.deepEqual(errors, []); |
| 77 | await writeFile(`${output}/${engine}-result.json`, JSON.stringify(evidence, null, 2)); |
| 78 | console.log(`${engine}: live/history alignment, short/long/pinned titles, hover, keyboard, collapse and reload passed`); |
| 79 | } finally { |
| 80 | await browser.close(); |
| 81 | } |