| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const base = process.env.CAGE_UI_URL || "http://127.0.0.1:5209"; |
| 6 | const output = process.env.CAGE_TEST_CAPTURE || "../docs/test-reports/2026-11-07-cli-session-sync"; |
| 7 | const workspace = "/Users/shihao/.claude-cage-workspace"; |
| 8 | const id = "12345678-1234-4321-aaaa-123456789abc"; |
| 9 | const otherID = "12345678-1234-4321-aaaa-123456789abd"; |
| 10 | const otherProject = "/Users/shihao/Projects/mobile"; |
| 11 | const sessions = [ |
| 12 | {id, workspace, title:"电脑保留的历史", terminal_available:true, revision:1, updated_at:"2026-11-07T08:00:00Z", |
| 13 | turns:[{id:"turn-1", prompt:"原来的问题", text:"原来的回答", status:"completed"}]}, |
| 14 | {id:otherID, workspace, title:"另一段会话", revision:1, updated_at:"2026-11-07T07:00:00Z", turns:[]}, |
| 15 | {id:"12345678-1234-4321-aaaa-123456789abe", workspace, title:"准备发布说明", archived:true, pinned:true, updated_at:"2026-11-06T09:00:00Z", turns:[]}, |
| 16 | {id:"12345678-1234-4321-aaaa-123456789abf", workspace, title:"核对接口", archived:true, updated_at:"2026-11-05T08:00:00Z", turns:[]}, |
| 17 | {id:"12345678-1234-4321-aaaa-123456789ac0", workspace:otherProject, title:"移动端布局检查", archived:true, updated_at:"2026-11-04T08:00:00Z", turns:[]}, |
| 18 | ]; |
| 19 | let state, failPurge = true, failOpen = true, opens = 0, releaseOpen; |
| 20 | const handle = async (route, path, body) => { |
| 21 | const archived = new URL(route.request().url()).searchParams.get("archived") === "true"; |
| 22 | if (path === "/cli/sessions") return route.fulfill({json:{sessions:sessions.filter(s => Boolean(s.archived) === archived),notice:""}}); |
| 23 | const session = sessions.find(s => path.includes(s.id)); |
| 24 | if (!session) return route.fulfill({status:404}); |
| 25 | if (path.endsWith("/open")) { |
| 26 | opens++; |
| 27 | if (failOpen) return route.fulfill({status:409,contentType:"text/plain; charset=utf-8",body:"测试:原终端仍在运行,请先退出其中的 Claude Code"}); |
| 28 | await new Promise(resolve => { releaseOpen = resolve; }); |
| 29 | const terminal = {id:"restored-terminal", kind:"claude", workspace, title:session.title, cli_session_id:session.id}; |
| 30 | state.terminals = [terminal]; |
| 31 | return route.fulfill({json:{...session,desktop_terminal_id:terminal.id}}); |
| 32 | } |
| 33 | if (path.endsWith("/archive")) { |
| 34 | session.archived = true; session.revision++; |
| 35 | state.terminals = state.terminals.filter(t => t.cli_session_id !== session.id); |
| 36 | } else if (path.endsWith("/restore")) { |
| 37 | session.archived = false; session.revision++; |
| 38 | } else if (path.endsWith("/purge")) { |
| 39 | assert.equal(body.confirm,true); |
| 40 | if (failPurge) return route.fulfill({status:503,contentType:"text/plain; charset=utf-8",body:"删除连接中断,请重试"}); |
| 41 | sessions.splice(sessions.indexOf(session),1); |
| 42 | } else if (path.endsWith("/metadata")) Object.assign(session,body); |
| 43 | return route.fulfill({json:session}); |
| 44 | }; |
| 45 | const desktop = await createWorkbench({url:base, cliRequest:handle}); |
| 46 | state = desktop.state; |
| 47 | const {page,browser,errors} = desktop; |
| 48 | try { |
| 49 | state.status.ready = state.status.vm_running = true; |
| 50 | state.status.projects = [workspace, otherProject]; |
| 51 | const history = page.getByRole("button",{name:"电脑保留的历史",exact:true}); |
| 52 | await history.waitFor(); |
| 53 | await history.click(); |
| 54 | await page.getByRole("status").filter({hasText:"测试:原终端仍在运行"}).waitFor(); |
| 55 | assert.equal(await page.getByRole("dialog",{name:"电脑保留的历史"}).count(),0,"session click opened an intermediate preview"); |
| 56 | assert.equal(await history.isEnabled(),true,"failed session open could not be retried"); |
| 57 | failOpen = false; |
| 58 | await history.click(); |
| 59 | await page.waitForFunction(() => document.querySelector('.sidebar-session[aria-busy="true"]')?.disabled); |
| 60 | assert.equal(await page.getByRole("button",{name:"另一段会话",exact:true}).isDisabled(),true,"another open was allowed during terminal restore"); |
| 61 | releaseOpen(); |
| 62 | await page.getByRole("tab",{name:"电脑保留的历史",exact:true}).waitFor(); |
| 63 | assert.equal(await history.count(),0,"history duplicated its live terminal"); |
| 64 | assert.equal(opens,2,"clicking one session sent duplicate open requests"); |
| 65 | assert.equal(state.terminalConnections,1,"restored terminal did not connect"); |
| 66 | assert.equal(state.requests.filter(r => r.path === "/cli/sessions/" + id).length,0,"desktop fetched a history preview"); |
| 67 | await page.getByRole("tab",{name:"电脑保留的历史",exact:true}).hover(); |
| 68 | await page.getByRole("group",{name:"电脑保留的历史 的操作"}).getByRole("button",{name:"归档",exact:true}).click(); |
| 69 | await page.getByRole("dialog",{name:"归档这段会话?"}).getByRole("button",{name:"归档会话",exact:true}).click(); |
| 70 | await page.getByRole("tab",{name:"电脑保留的历史",exact:true}).waitFor({state:"hidden"}); |
| 71 | assert.deepEqual(errors,[],"desktop archive"); |
| 72 | |
| 73 | const context = await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,userAgent:"CageMobile/1"}); |
| 74 | const phone = await context.newPage(); |
| 75 | const phoneErrors = []; |
| 76 | phone.on("pageerror",error => phoneErrors.push(error.message)); |
| 77 | await phone.route("**/api/**",async route => { |
| 78 | const path = new URL(route.request().url()).pathname.replace("/api/mobile/cli","/cli"); |
| 79 | if (path.endsWith("/projects")) return route.fulfill({json:{host:"测试电脑",ready:true,shared_sessions:true,projects:[{path:workspace,name:"默认工作区"},{path:otherProject,name:"mobile"}]}}); |
| 80 | return handle(route,path,route.request().postDataJSON()); |
| 81 | }); |
| 82 | await phone.goto(base+"/mobile.html"); |
| 83 | const drawer = phone.getByRole("dialog",{name:"工作区侧栏"}); |
| 84 | await drawer.getByRole("button",{name:"设置",exact:true}).click(); |
| 85 | await phone.getByRole("button",{name:"已归档会话",exact:true}).click(); |
| 86 | let archived = phone.getByRole("article").filter({has:phone.getByRole("heading",{name:"电脑保留的历史",exact:true})}); |
| 87 | await archived.getByRole("button",{name:"取消归档"}).click(); |
| 88 | await history.waitFor(); |
| 89 | await phone.getByRole("button",{name:"返回设置"}).click(); |
| 90 | await phone.getByRole("button",{name:"返回会话"}).click(); |
| 91 | const row = drawer.locator(".swipe-session").filter({has:phone.getByRole("button",{name:"电脑保留的历史",exact:true})}); |
| 92 | await row.getByRole("button",{name:"电脑保留的历史的更多操作"}).click(); |
| 93 | await row.getByRole("button",{name:"归档",exact:true}).click(); |
| 94 | await phone.getByRole("dialog",{name:"归档会话",exact:true}).getByRole("button",{name:"归档会话",exact:true}).click(); |
| 95 | await history.waitFor({state:"hidden"}); |
| 96 | assert.deepEqual(errors,[],"phone archive"); |
| 97 | |
| 98 | await page.getByRole("button",{name:"设置",exact:true}).click(); |
| 99 | await page.getByRole("navigation",{name:"设置分组"}).getByRole("button",{name:"已归档会话",exact:true}).click(); |
| 100 | const panel = page.getByRole("region",{name:"已归档会话",exact:true}); |
| 101 | const projectFilter = panel.getByRole("combobox",{name:"项目筛选"}); |
| 102 | const group = panel.locator(".archive-project").first(); |
| 103 | await group.waitFor(); |
| 104 | const before = await group.boundingBox(); |
| 105 | await projectFilter.click(); |
| 106 | const options = page.getByRole("listbox",{name:"项目筛选选项"}); |
| 107 | const opened = await options.boundingBox(); |
| 108 | const shifted = await group.boundingBox(); |
| 109 | assert.ok(Math.abs(shifted.y-before.y)<1 && opened.height>0,"floating project filter moved content"); |
| 110 | await projectFilter.press("Escape"); |
| 111 | assert.ok(Math.abs((await group.boundingBox()).y - before.y) < 2,"filter did not restore layout"); |
| 112 | await projectFilter.press("End"); await projectFilter.press("Enter"); |
| 113 | await panel.getByRole("heading",{name:"移动端布局检查",exact:true}).waitFor(); |
| 114 | assert.equal(await panel.getByRole("heading",{name:"电脑保留的历史",exact:true}).count(),0); |
| 115 | await projectFilter.press("Home"); await projectFilter.press("Enter"); |
| 116 | const filter = panel.getByRole("combobox",{name:"会话筛选"}); |
| 117 | await filter.click(); await page.getByRole("option",{name:"已置顶",exact:true}).click(); |
| 118 | assert.equal(await panel.getByRole("article").count(),1); |
| 119 | await filter.press("Home"); await filter.press("Enter"); |
| 120 | const search = panel.getByRole("searchbox",{name:"搜索已归档的会话"}); |
| 121 | await search.fill("核对"); |
| 122 | assert.equal(await panel.getByRole("article").count(),1); |
| 123 | await search.fill(""); |
| 124 | await panel.getByRole("heading",{name:".claude-cage-workspace",exact:true}).click(); |
| 125 | await mkdir(output,{recursive:true}); |
| 126 | await page.screenshot({path:output+"/"+(process.env.CAGE_TEST_BROWSER||"chromium")+"-archive-layout.png"}); |
| 127 | const target = panel.getByRole("article").filter({has:page.getByRole("heading",{name:"电脑保留的历史",exact:true})}); |
| 128 | await target.getByRole("button",{name:"删除会话",exact:true}).click(); |
| 129 | await target.getByRole("button",{name:"永久删除",exact:true}).click(); |
| 130 | await panel.getByRole("alert").filter({hasText:"删除连接中断"}).waitFor(); |
| 131 | assert.equal(sessions.length,5,"failed purge removed history"); |
| 132 | await mkdir(output,{recursive:true}); |
| 133 | await page.screenshot({path:output+"/"+(process.env.CAGE_TEST_BROWSER||"chromium")+"-archived-desktop.png"}); |
| 134 | failPurge = false; |
| 135 | await target.getByRole("button",{name:"永久删除",exact:true}).click(); |
| 136 | await target.waitFor({state:"hidden"}); |
| 137 | await phone.evaluate(() => window.dispatchEvent(new Event("cage-open-archives"))); |
| 138 | await drawer.waitFor({state:"hidden"}); |
| 139 | await phone.getByRole("heading",{name:"准备发布说明",exact:true}).waitFor(); |
| 140 | assert.equal(await phone.getByRole("heading",{name:"电脑保留的历史",exact:true}).count(),0); |
| 141 | const phoneGroup = phone.locator(".archive-project").first(); |
| 142 | const phoneBefore = await phoneGroup.boundingBox(); |
| 143 | const phoneFilter = phone.getByRole("combobox",{name:"项目筛选"}); |
| 144 | await phoneFilter.click(); |
| 145 | const phoneOptions = await phone.getByRole("listbox",{name:"项目筛选选项"}).boundingBox(); |
| 146 | assert.ok(Math.abs((await phoneGroup.boundingBox()).y-phoneBefore.y)<1 && phoneOptions.height>0,"floating phone filter moved content"); |
| 147 | await phoneFilter.press("Escape"); |
| 148 | assert.ok(Math.abs((await phoneGroup.boundingBox()).y-phoneBefore.y)<2); |
| 149 | await phone.getByRole("heading",{name:"已归档会话",exact:true}).click(); |
| 150 | await phone.screenshot({path:output+"/"+(process.env.CAGE_TEST_BROWSER||"chromium")+"-archived-mobile.png"}); |
| 151 | assert.deepEqual(errors,[],"before reload"); |
| 152 | await page.waitForLoadState("networkidle"); |
| 153 | await page.reload(); |
| 154 | await page.getByRole("button",{name:"另一段会话",exact:true}).waitFor(); |
| 155 | assert.equal(await history.count(),0); |
| 156 | assert.equal(sessions.length,4); |
| 157 | assert.equal(sessions[0].id,otherID); |
| 158 | assert.deepEqual(errors,[]); |
| 159 | assert.deepEqual(phoneErrors,[]); |
| 160 | console.log("Persistent desktop history, terminal resume, bidirectional archive/restore, purge confirmation/failure/retry, reload passed"); |
| 161 | } finally { await browser.close(); } |