| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import fs from "node:fs/promises"; |
| 5 | import os from "node:os"; |
| 6 | import path from "node:path"; |
| 7 | import { spawn } from "node:child_process"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | import { chooseInline, createWorkbench } from "./workbench-fixture.mjs"; |
| 10 | |
| 11 | const root = fileURLToPath(new URL("../../", import.meta.url)); |
| 12 | const output = path.join( |
| 13 | root, |
| 14 | "docs/test-reports/2026-11-01-browser-tabs-favorites", |
| 15 | ); |
| 16 | await fs.mkdir(output, { recursive: true }); |
| 17 | const profile = await fs.mkdtemp(path.join(os.tmpdir(), "cage-favorites-qa-")); |
| 18 | await fs.writeFile(path.join(profile, "config.json"), "{}", { mode: 0o600 }); |
| 19 | await fs.writeFile( |
| 20 | path.join(profile, "workbench.json"), |
| 21 | JSON.stringify({ workspace: path.join(profile, "workspace") }), |
| 22 | { mode: 0o600 }, |
| 23 | ); |
| 24 | let service, origin, cookie, browser; |
| 25 | const checks = []; |
| 26 | async function stop() { |
| 27 | if (!service || service.exitCode !== null) return; |
| 28 | await new Promise((resolve) => { |
| 29 | service.once("exit", resolve); |
| 30 | service.kill("SIGTERM"); |
| 31 | }); |
| 32 | } |
| 33 | async function start() { |
| 34 | await fs.rm(path.join(profile, "control.json"), { force: true }); |
| 35 | service = spawn(path.join(root, "_output/bin/cage"), ["serve"], { |
| 36 | env: { |
| 37 | ...process.env, |
| 38 | CLAUDE_CAGE_CONFIG: path.join(profile, "config.json"), |
| 39 | }, |
| 40 | stdio: "ignore", |
| 41 | }); |
| 42 | let control; |
| 43 | for (let i = 0; i < 100; i++) { |
| 44 | try { |
| 45 | control = JSON.parse( |
| 46 | await fs.readFile(path.join(profile, "control.json"), "utf8"), |
| 47 | ); |
| 48 | break; |
| 49 | } catch { |
| 50 | await new Promise((resolve) => setTimeout(resolve, 100)); |
| 51 | } |
| 52 | } |
| 53 | assert.ok(control, "isolated control service did not start"); |
| 54 | origin = `http://localhost:${control.port}`; |
| 55 | const launch = await fetch(origin + "/api/launch", { |
| 56 | method: "POST", |
| 57 | headers: { Origin: origin, "Content-Type": "application/json" }, |
| 58 | body: JSON.stringify({ token: control.token }), |
| 59 | }); |
| 60 | const { url } = await launch.json(); |
| 61 | const session = await fetch(origin + "/api/session", { |
| 62 | method: "POST", |
| 63 | headers: { Origin: origin, "Content-Type": "application/json" }, |
| 64 | body: JSON.stringify({ token: new URL(url).hash.slice(1) }), |
| 65 | }); |
| 66 | cookie = session.headers.get("set-cookie").split(";")[0]; |
| 67 | const headers = { Origin: origin, Cookie: cookie, "Content-Type": "application/json" }; |
| 68 | const account = await (await fetch(origin + "/api/account", { headers })).json(); |
| 69 | const password = "favorites-fixture-only"; |
| 70 | const unlocked = await fetch(origin + "/api/account/" + (account.exists ? "unlock" : "create"), { |
| 71 | method: "POST", headers, body: JSON.stringify({ username: "收藏测试", password, confirm_password: password }), |
| 72 | }); |
| 73 | assert.ok(unlocked.ok, "the isolated favorites fixture requires an unlocked local account"); |
| 74 | } |
| 75 | try { |
| 76 | await start(); |
| 77 | const fixture = await createWorkbench({ |
| 78 | favoritesRequest: async (request) => { |
| 79 | const response = await fetch(origin + "/api/favorites", { |
| 80 | method: request.method(), |
| 81 | headers: { |
| 82 | Origin: origin, |
| 83 | Cookie: cookie, |
| 84 | "Content-Type": "application/json", |
| 85 | }, |
| 86 | body: request.postData() || undefined, |
| 87 | }); |
| 88 | return { |
| 89 | status: response.status, |
| 90 | contentType: response.headers.get("content-type"), |
| 91 | body: await response.text(), |
| 92 | }; |
| 93 | }, |
| 94 | }); |
| 95 | browser = fixture.browser; |
| 96 | const { page, state, errors } = fixture; |
| 97 | const shot = (name) => |
| 98 | page.screenshot({ |
| 99 | path: path.join(output, name + ".png"), |
| 100 | animations: "disabled", |
| 101 | }); |
| 102 | const tabs = page.getByRole("tablist", { name: "浏览器标签页" }); |
| 103 | await page |
| 104 | .getByRole("button", { name: "启动 Cage 环境", exact: true }) |
| 105 | .click(); |
| 106 | await page |
| 107 | .getByRole("heading", { name: "Cage 环境已就绪", exact: true }) |
| 108 | .waitFor(); |
| 109 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 110 | await tabs.getByRole("tab").waitFor(); |
| 111 | await page.getByRole("button", { name: "新建网页", exact: true }).click(); |
| 112 | await page.waitForFunction( |
| 113 | () => document.querySelectorAll('.browser-tabs [role="tab"]').length === 2, |
| 114 | ); |
| 115 | for (let i = 3; i <= 8; i++) |
| 116 | state.tabs.push({ |
| 117 | id: "qa-" + i, |
| 118 | kind: "web", |
| 119 | title: "设计文档 " + i, |
| 120 | url: `https://example.com/${i}`, |
| 121 | }); |
| 122 | await page.waitForFunction( |
| 123 | () => document.querySelectorAll('.browser-tabs [role="tab"]').length === 8, |
| 124 | ); |
| 125 | await page.waitForFunction(() => { |
| 126 | const list = document.querySelector(".browser-tabs"); |
| 127 | return ( |
| 128 | list.querySelector(".active").getBoundingClientRect().width > |
| 129 | list.querySelector(".browser-tab:not(.active)").getBoundingClientRect() |
| 130 | .width + |
| 131 | 20 |
| 132 | ); |
| 133 | }); |
| 134 | let layout = await page.evaluate(() => { |
| 135 | const list = document.querySelector(".browser-tabs"); |
| 136 | const active = list.querySelector(".active").getBoundingClientRect(); |
| 137 | const add = document |
| 138 | .querySelector(".browser-new-tab") |
| 139 | .getBoundingClientRect(); |
| 140 | return { |
| 141 | overflow: list.scrollWidth > list.clientWidth + 1, |
| 142 | active: active.width, |
| 143 | other: list.querySelector(":not(.active)").getBoundingClientRect().width, |
| 144 | centers: Math.abs(active.y + active.height / 2 - add.y - add.height / 2), |
| 145 | }; |
| 146 | }); |
| 147 | assert.equal(layout.overflow, false); |
| 148 | assert.ok(layout.active > layout.other); |
| 149 | assert.ok(layout.centers < 1); |
| 150 | await shot("desktop-tabs"); |
| 151 | const from = tabs.getByRole("tab", { name: "设计文档 3", exact: true }); |
| 152 | const to = tabs.getByRole("tab", { name: "设计文档 6", exact: true }); |
| 153 | const initial = await from.boundingBox(), |
| 154 | target = await to.boundingBox(); |
| 155 | const before = await to.boundingBox(); |
| 156 | await page.mouse.move(initial.x + initial.width / 2, initial.y + 16); |
| 157 | await page.mouse.down(); |
| 158 | await page.mouse.move(target.x + target.width - 6, target.y + 16, { |
| 159 | steps: 18, |
| 160 | }); |
| 161 | await page.locator(".browser-tab.dragging").waitFor(); |
| 162 | assert.ok( |
| 163 | Math.abs((await from.boundingBox()).x - initial.x) > 100, |
| 164 | "dragged tab must follow pointer before mouseup", |
| 165 | ); |
| 166 | await page.waitForFunction( |
| 167 | (x) => |
| 168 | document.querySelector('[data-tab-id="qa-6"]').getBoundingClientRect().x < |
| 169 | x - 20, |
| 170 | before.x, |
| 171 | ); |
| 172 | await shot("drag-in-progress"); |
| 173 | await page.mouse.up(); |
| 174 | await page.waitForFunction( |
| 175 | () => |
| 176 | [...document.querySelectorAll(".browser-tab")].findIndex( |
| 177 | (e) => e.dataset.tabId === "qa-3", |
| 178 | ) > |
| 179 | [...document.querySelectorAll(".browser-tab")].findIndex( |
| 180 | (e) => e.dataset.tabId === "qa-6", |
| 181 | ), |
| 182 | ); |
| 183 | const ordered = await tabs.getByRole("tab").allTextContents(); |
| 184 | const again = await from.boundingBox(); |
| 185 | await page.mouse.move(again.x + 20, again.y + 16); |
| 186 | await page.mouse.down(); |
| 187 | await page.mouse.move(again.x - 200, again.y + 16, { steps: 12 }); |
| 188 | await page.keyboard.press("Escape"); |
| 189 | await page.mouse.up(); |
| 190 | assert.deepEqual(await tabs.getByRole("tab").allTextContents(), ordered); |
| 191 | await page.reload(); |
| 192 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 193 | assert.deepEqual(await tabs.getByRole("tab").allTextContents(), ordered); |
| 194 | checks.push( |
| 195 | "8 tabs: aligned add button, active expansion, pointer tracking and live neighbor movement, Escape cancellation, persisted order", |
| 196 | ); |
| 197 | |
| 198 | await tabs.getByRole("tab", { name: "设计文档 3", exact: true }).click(); |
| 199 | await page.getByRole("button", { name: "收藏此页", exact: true }).click(); |
| 200 | await page.getByLabel("收藏名称", { exact: true }).fill("Cage 设计资料"); |
| 201 | await page.getByLabel("新收藏夹名称", { exact: true }).fill("工作资料"); |
| 202 | await page.getByRole("button", { name: "保存", exact: true }).click(); |
| 203 | await page |
| 204 | .getByRole("dialog", { name: "添加收藏", exact: true }) |
| 205 | .waitFor({ state: "hidden" }); |
| 206 | await page |
| 207 | .getByRole("button", { name: "编辑此页收藏", exact: true }) |
| 208 | .waitFor(); |
| 209 | let saved = JSON.parse( |
| 210 | await fs.readFile(path.join(profile, "favorites.json"), "utf8"), |
| 211 | ); |
| 212 | assert.equal(saved.items[0].title, "Cage 设计资料"); |
| 213 | assert.equal(saved.folders[0].title, "工作资料"); |
| 214 | await page.keyboard.press("Meta+d"); |
| 215 | await page.getByLabel("收藏名称", { exact: true }).fill("待重试的收藏"); |
| 216 | |
| 217 | await fs.rename( |
| 218 | path.join(profile, "favorites.json"), |
| 219 | path.join(profile, "favorites.backup"), |
| 220 | ); |
| 221 | await fs.mkdir(path.join(profile, "favorites.json")); |
| 222 | await page.getByRole("button", { name: "保存", exact: true }).click(); |
| 223 | await page |
| 224 | .getByRole("dialog", { name: "编辑收藏", exact: true }) |
| 225 | .getByRole("alert") |
| 226 | .waitFor(); |
| 227 | assert.equal( |
| 228 | await page.getByLabel("收藏名称", { exact: true }).inputValue(), |
| 229 | "待重试的收藏", |
| 230 | ); |
| 231 | await fs.rmdir(path.join(profile, "favorites.json")); |
| 232 | await fs.rename( |
| 233 | path.join(profile, "favorites.backup"), |
| 234 | path.join(profile, "favorites.json"), |
| 235 | ); |
| 236 | await page.getByRole("button", { name: "保存", exact: true }).click(); |
| 237 | await page |
| 238 | .getByRole("dialog", { name: "编辑收藏", exact: true }) |
| 239 | .waitFor({ state: "hidden" }); |
| 240 | await stop(); |
| 241 | await start(); |
| 242 | await page.reload(); |
| 243 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 244 | await page.keyboard.press("Meta+Shift+o"); |
| 245 | const panel = page.getByRole("dialog", { name: "收藏夹", exact: true }); |
| 246 | await panel |
| 247 | .getByRole("button", { name: "编辑收藏 待重试的收藏", exact: true }) |
| 248 | .waitFor(); |
| 249 | await shot("favorites"); |
| 250 | await page.getByLabel("搜索收藏", { exact: true }).fill("不存在"); |
| 251 | await panel.getByText("没有匹配的收藏").waitFor(); |
| 252 | await page.getByLabel("搜索收藏", { exact: true }).fill("example.com/3"); |
| 253 | await panel |
| 254 | .getByRole("button", { name: "编辑收藏 待重试的收藏", exact: true }) |
| 255 | .click(); |
| 256 | await chooseInline(page.getByLabel("保存到收藏夹", { exact: true }), ""); |
| 257 | await page.getByRole("button", { name: "保存", exact: true }).click(); |
| 258 | await page |
| 259 | .getByRole("dialog", { name: "编辑收藏", exact: true }) |
| 260 | .waitFor({ state: "hidden" }); |
| 261 | saved = JSON.parse( |
| 262 | await fs.readFile(path.join(profile, "favorites.json"), "utf8"), |
| 263 | ); |
| 264 | assert.equal(saved.items[0].folder_id, ""); |
| 265 | await page.getByLabel("搜索收藏", { exact: true }).fill(""); |
| 266 | await chooseInline(page |
| 267 | .getByLabel("筛选收藏夹", { exact: true }), saved.folders[0].id); |
| 268 | await page.getByRole("button", { name: "重命名收藏夹", exact: true }).click(); |
| 269 | await page.getByLabel("收藏名称", { exact: true }).fill("常用资料"); |
| 270 | await page.getByRole("button", { name: "保存", exact: true }).click(); |
| 271 | await page |
| 272 | .getByRole("dialog", { name: "重命名收藏夹", exact: true }) |
| 273 | .waitFor({ state: "hidden" }); |
| 274 | await page |
| 275 | .getByRole("button", { name: "删除收藏夹,收藏移至未分类", exact: true }) |
| 276 | .click(); |
| 277 | await page |
| 278 | .getByRole("button", { name: "重命名收藏夹", exact: true }) |
| 279 | .waitFor({ state: "hidden" }); |
| 280 | await panel.getByRole("button", { name: /^待重试的收藏/ }).click(); |
| 281 | await panel.waitFor({ state: "hidden" }); |
| 282 | assert.equal(state.tabs.at(-1).url, "https://example.com/3"); |
| 283 | await page.getByRole("button", { name: "编辑此页收藏", exact: true }).click(); |
| 284 | await page.getByRole("button", { name: "删除收藏", exact: true }).click(); |
| 285 | await page.getByRole("button", { name: "收藏此页", exact: true }).waitFor(); |
| 286 | saved = JSON.parse( |
| 287 | await fs.readFile(path.join(profile, "favorites.json"), "utf8"), |
| 288 | ); |
| 289 | assert.equal(saved.items.length, 0); |
| 290 | assert.equal(saved.folders.length, 0); |
| 291 | checks.push( |
| 292 | "Real favorites service: create folder and favorite, edit, failed persistence retains input, retry, restart, search, move, folder rename/delete, open via guest tab API, delete favorite", |
| 293 | ); |
| 294 | |
| 295 | for (let i = 9; i <= 32; i++) |
| 296 | state.tabs.push({ |
| 297 | id: "qa-" + i, |
| 298 | kind: "web", |
| 299 | title: "参考网页 " + i, |
| 300 | url: `https://example.com/${i}`, |
| 301 | }); |
| 302 | await page.waitForFunction( |
| 303 | () => document.querySelectorAll('.browser-tabs [role="tab"]').length === 33, |
| 304 | ); |
| 305 | for (const width of [1280, 820, 375]) { |
| 306 | await page.setViewportSize({ width, height: 850 }); |
| 307 | await page.waitForFunction(() => { |
| 308 | const list = document.querySelector(".browser-tabs"); |
| 309 | return list.scrollWidth <= list.clientWidth + 1; |
| 310 | }); |
| 311 | await shot("many-tabs-" + width); |
| 312 | } |
| 313 | await page.setViewportSize({ width: 820, height: 558 }); |
| 314 | await page.getByRole("button", { name: "搜索标签页", exact: true }).click(); |
| 315 | await page |
| 316 | .getByLabel("搜索已打开的标签页", { exact: true }) |
| 317 | .fill("参考网页 28"); |
| 318 | await page |
| 319 | .getByRole("dialog", { name: "搜索标签页", exact: true }) |
| 320 | .getByRole("button", { name: "参考网页 28", exact: true }) |
| 321 | .click(); |
| 322 | await page.waitForFunction( |
| 323 | () => |
| 324 | document.querySelector('.browser-tabs [aria-selected="true"]') |
| 325 | .textContent === "参考网页 28", |
| 326 | ); |
| 327 | await page.evaluate(() => |
| 328 | document.documentElement.style.setProperty("--ui-font-size", "20px"), |
| 329 | ); |
| 330 | await shot("narrow-large-text"); |
| 331 | checks.push( |
| 332 | "33 tabs: no horizontal scroll at 1280/820/375, tab search selects crowded tabs, 820px large text", |
| 333 | ); |
| 334 | assert.deepEqual(errors, []); |
| 335 | await fs.writeFile( |
| 336 | path.join(output, "result.json"), |
| 337 | JSON.stringify( |
| 338 | { |
| 339 | passed: true, |
| 340 | checks, |
| 341 | pageErrors: errors, |
| 342 | scope: |
| 343 | "Real DOM and favorites Go service; VM pages, navigation and noVNC are simulated.", |
| 344 | }, |
| 345 | null, |
| 346 | 2, |
| 347 | ), |
| 348 | ); |
| 349 | console.log(JSON.stringify({ passed: true, checks }, null, 2)); |
| 350 | } finally { |
| 351 | await browser?.close(); |
| 352 | await stop(); |
| 353 | await fs.rm(profile, { recursive: true, force: true }); |
| 354 | } |