| 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-archive-position"; |
| 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 sessions = Array.from({ length: 18 }, (_, i) => ({ |
| 12 | id: `archive-position-${i}`, workspace, title: i ? `历史会话 ${i}` : "正在处理的会话", |
| 13 | updated_at: "2026-11-08T08:00:00Z", |
| 14 | })); |
| 15 | const evidence = []; |
| 16 | let failArchive = false; |
| 17 | let holdArchive = false, releaseArchive; |
| 18 | let archiveRequests = 0; |
| 19 | let archived = 0; |
| 20 | const dialog = page.getByRole("dialog", { name: "归档这段会话?", exact: true }); |
| 21 | const actions = title => page.getByRole("group", { name: `${title} 的操作`, exact: true, includeHidden: true }); |
| 22 | const open = async title => { |
| 23 | const row = page.locator(".sidebar-tab").filter({ has: actions(title) }); |
| 24 | await row.hover(); |
| 25 | const trigger = actions(title).getByRole("button", { name: "归档", exact: true }); |
| 26 | await trigger.click(); |
| 27 | await dialog.waitFor(); |
| 28 | return trigger; |
| 29 | }; |
| 30 | try { |
| 31 | await page.route("**/api/cli/sessions", route => route.fulfill({ json: { sessions } })); |
| 32 | await page.route("**/api/cli/sessions/*/archive", async route => { |
| 33 | archiveRequests++; |
| 34 | if (holdArchive) await new Promise(resolve => { releaseArchive = resolve; }); |
| 35 | if (failArchive) return route.fulfill({ status: 400, contentType: "text/plain", body: "环境正在执行其他操作" }); |
| 36 | const id = route.request().url().split("/").at(-2); |
| 37 | sessions.splice(sessions.findIndex(session => session.id === id), 1); |
| 38 | state.terminals = state.terminals.filter(terminal => terminal.cli_session_id !== id); |
| 39 | archived++; |
| 40 | await route.fulfill({ json: { ok: true } }); |
| 41 | }); |
| 42 | state.status.ready = state.status.vm_running = true; |
| 43 | state.terminals = [{ id: "archive-live", kind: "claude", workspace, |
| 44 | title: sessions[0].title, cli_session_id: sessions[0].id }]; |
| 45 | await page.getByRole("tab", { name: sessions[0].title, exact: true }).waitFor(); |
| 46 | await page.getByRole("button", { name: sessions.at(-1).title, exact: true }).waitFor(); |
| 47 | await mkdir(output, { recursive: true }); |
| 48 | |
| 49 | |
| 50 | const firstActions = actions(sessions[0].title); |
| 51 | await page.locator(".sidebar-tab").filter({ has: firstActions }).hover(); |
| 52 | await firstActions.getByRole("button", { name: "归档", exact: true }).hover(); |
| 53 | await page.screenshot({ path: `${output}/${engine}-archive-label.png` }); |
| 54 | await firstActions.locator(".terminal-action-label").filter({ hasText: /^归档$/ }).click(); |
| 55 | await dialog.waitFor({ timeout: 2000 }); |
| 56 | await dialog.getByRole("button", { name: "取消", exact: true }).click(); |
| 57 | assert.equal(archived, 0, "action text bypassed archive confirmation"); |
| 58 | for (const width of [1280, 820]) { |
| 59 | await page.setViewportSize({ width, height: 850 }); |
| 60 | const trigger = await open(sessions[0].title); |
| 61 | const anchor = await trigger.boundingBox(); |
| 62 | const box = await dialog.boundingBox(); |
| 63 | assert.ok(Math.abs(box.y - anchor.y - anchor.height - 6) < 1, "confirmation is not below archive trigger"); |
| 64 | assert.ok(box.width <= 305 && box.x >= 8 && box.x + box.width <= width - 8, "confirmation overflows window"); |
| 65 | assert.ok(Math.abs(box.x + box.width / 2 - anchor.x) < 100, "confirmation is too far from trigger"); |
| 66 | await page.screenshot({ path: `${output}/${engine}-${width}.png` }); |
| 67 | await dialog.getByRole("button", { name: "取消", exact: true }).click(); |
| 68 | await dialog.waitFor({ state: "hidden" }); |
| 69 | assert.equal(archived, 0, "cancel archived a session"); |
| 70 | assert.equal(await trigger.evaluate(node => node === document.activeElement), true, "cancel did not restore trigger focus"); |
| 71 | await trigger.press("Enter"); |
| 72 | await dialog.waitFor(); |
| 73 | await page.keyboard.press("Shift+Tab"); |
| 74 | assert.equal(await dialog.getByRole("button", { name: "归档会话", exact: true }).evaluate(node => node === document.activeElement), true); |
| 75 | await page.keyboard.press("Tab"); |
| 76 | assert.equal(await dialog.getByRole("button", { name: "关闭面板", exact: true }).evaluate(node => node === document.activeElement), true); |
| 77 | await page.keyboard.press("Escape"); |
| 78 | await dialog.waitFor({ state: "hidden" }); |
| 79 | assert.equal(await trigger.evaluate(node => node === document.activeElement), true); |
| 80 | await trigger.press("Enter"); |
| 81 | await dialog.waitFor(); |
| 82 | await page.mouse.click(width - 20, 20); |
| 83 | await dialog.waitFor({ state: "hidden" }); |
| 84 | assert.equal(archived, 0); |
| 85 | evidence.push({ width, anchor, box }); |
| 86 | } |
| 87 | |
| 88 | const last = sessions.at(-1).title; |
| 89 | const bottomTrigger = await open(last); |
| 90 | const bottomAnchor = await bottomTrigger.boundingBox(); |
| 91 | const bottomBox = await dialog.boundingBox(); |
| 92 | assert.ok(bottomBox.y >= 8 && bottomBox.y + bottomBox.height <= 842, "bottom confirmation is clipped"); |
| 93 | assert.ok(bottomBox.y + bottomBox.height <= bottomAnchor.y, "bottom confirmation did not move above the trigger"); |
| 94 | await page.screenshot({ path: `${output}/${engine}-bottom.png` }); |
| 95 | await page.keyboard.press("Escape"); |
| 96 | await open(sessions[0].title); |
| 97 | failArchive = true; |
| 98 | await dialog.getByRole("button", { name: "归档会话", exact: true }).click(); |
| 99 | await dialog.getByRole("alert").filter({ hasText: "环境正在执行其他操作" }).waitFor(); |
| 100 | assert.equal(archived, 0, "failed archive removed a session"); |
| 101 | failArchive = false; |
| 102 | holdArchive = true; |
| 103 | await dialog.getByRole("button", { name: "归档会话", exact: true }).press("Enter"); |
| 104 | await dialog.getByRole("button", { name: "正在归档…", exact: true }).waitFor(); |
| 105 | assert.equal(await dialog.getByRole("button", { name: "取消", exact: true }).isDisabled(), true); |
| 106 | assert.equal(await dialog.getByRole("button", { name: "正在归档…", exact: true }).isDisabled(), true); |
| 107 | const pendingRequests = archiveRequests; |
| 108 | await page.keyboard.press("Enter"); |
| 109 | await page.keyboard.press("Escape"); |
| 110 | assert.equal(await dialog.isVisible(), true, "pending archive was dismissed"); |
| 111 | assert.equal(archiveRequests, pendingRequests, "pending archive was submitted twice"); |
| 112 | await page.screenshot({ path: `${output}/${engine}-pending.png` }); |
| 113 | releaseArchive(); holdArchive = false; |
| 114 | await actions("正在处理的会话").waitFor({ state: "hidden" }); |
| 115 | assert.equal(archived, 1, "confirm should archive exactly once"); |
| 116 | await open(last); |
| 117 | await dialog.getByRole("button", { name: "归档会话", exact: true }).click(); |
| 118 | await actions(last).waitFor({ state: "hidden" }); |
| 119 | assert.equal(archived, 2, "history session archive failed"); |
| 120 | assert.deepEqual(errors, []); |
| 121 | await writeFile(`${output}/${engine}-result.json`, JSON.stringify(evidence, null, 2)); |
| 122 | console.log(`${engine}: archive positioning, viewport edges, focus, keyboard, cancel, outside dismissal, failure/retry and live/history archive passed`); |
| 123 | } finally { |
| 124 | await browser.close(); |
| 125 | } |