| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { join } from "node:path"; |
| 4 | import { smokeCaptureDirectory } from "./smoke-captures.mjs"; |
| 5 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 6 | |
| 7 | const base = process.env.CAGE_UI_URL || "http://127.0.0.1:5178"; |
| 8 | const engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 9 | const output = await smokeCaptureDirectory("session-actions-" + engine); |
| 10 | const desktop = await createWorkbench({url:base}); |
| 11 | const {page, state, browser, errors} = desktop; |
| 12 | const workspace = state.settings.workspace; |
| 13 | const id = "12345678-1234-4321-aaaa-123456789abc"; |
| 14 | const sessions = [ |
| 15 | {id, workspace, title:"待处理的会话", revision:1, generation:id, updated_at:"2026-11-07T08:00:00Z", pinned:false}, |
| 16 | {id:"12345678-1234-4321-aaaa-123456789abd", workspace, title:"另一段会话", revision:1, generation:id, updated_at:"2026-11-07T09:00:00Z", pinned:false}, |
| 17 | ]; |
| 18 | let failSave = false; |
| 19 | const update = (target, body) => { |
| 20 | if (body.title) target.title = target.custom_title = body.title; |
| 21 | if (body.pinned !== undefined) target.pinned = body.pinned; |
| 22 | target.metadata_set = true; target.revision++; |
| 23 | state.terminals = state.terminals.map(t => t.cli_session_id === target.id ? {...t, title:target.title, custom_title:target.custom_title, pinned:target.pinned, metadata_set:true} : t); |
| 24 | return {...target, turns:[]}; |
| 25 | }; |
| 26 | const remove = target => { |
| 27 | sessions.splice(sessions.indexOf(target),1); |
| 28 | state.terminals = state.terminals.filter(t => t.cli_session_id !== target.id); |
| 29 | }; |
| 30 | try { |
| 31 | state.status.ready = state.status.vm_running = true; |
| 32 | await page.evaluate(({workspace,title}) => localStorage.setItem("cage-tab-layout",JSON.stringify({[workspace]:{"terminal:fixture-shared":{title,pinned:false}}})),{workspace,title:sessions[0].title}); |
| 33 | state.terminals = [{id:"fixture-shared", kind:"claude", workspace, title:sessions[0].title, cli_session_id:id, metadata_set:false, pinned:false}]; |
| 34 | await page.route("**/api/terminals/*/metadata", async route => route.fulfill({json:update(sessions[0],route.request().postDataJSON())})); |
| 35 | await page.route("**/api/terminals/*", async route => { |
| 36 | if (route.request().method() !== "DELETE") return route.fallback(); |
| 37 | remove(sessions[0]); await route.fulfill({json:{ok:true}}); |
| 38 | }); |
| 39 | await page.route("**/api/cli/sessions/*/archive", async route => { |
| 40 | const target = sessions.find(s => route.request().url().includes(s.id)); |
| 41 | remove(target); await route.fulfill({json:{ok:true}}); |
| 42 | }); |
| 43 | await page.getByRole("tab",{name:/待处理的会话/}).first().hover(); |
| 44 | await page.getByRole("group",{name:"待处理的会话 的操作"}).waitFor(); |
| 45 | assert.equal(sessions[0].metadata_set,true,"desktop local label was not imported into host metadata"); |
| 46 | const phoneContext = await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,userAgent:"CageMobile/1"}); |
| 47 | const phone = await phoneContext.newPage(); |
| 48 | const phoneErrors = []; |
| 49 | phone.on("pageerror",error => phoneErrors.push(error.message)); |
| 50 | await phone.route("**/api/**",async route => { |
| 51 | const path = new URL(route.request().url()).pathname; |
| 52 | if (path.endsWith("/projects")) return route.fulfill({json:{host:"隔离测试主机",ready:true,shared_sessions:true,projects:[{path:workspace,name:"测试项目"}]}}); |
| 53 | if (path.endsWith("/sessions")) return route.fulfill({json:{sessions,notice:""}}); |
| 54 | const target = sessions.find(s => path.includes(s.id)); |
| 55 | if (!target) return route.fulfill({status:404}); |
| 56 | if (path.endsWith("/account")) return route.fulfill({ json: { session: target, supported: false, candidates: [] } }); |
| 57 | if (path.endsWith("/metadata")) { |
| 58 | if (failSave) return route.fulfill({status:500,contentType:"text/plain",body:"测试:磁盘暂时不可写"}); |
| 59 | return route.fulfill({json:update(target,route.request().postDataJSON())}); |
| 60 | } |
| 61 | if (path.endsWith("/archive")) { remove(target); return route.fulfill({json:{ok:true}}); } |
| 62 | return route.fulfill({json:{...target,turns:[]}}); |
| 63 | }); |
| 64 | await phone.goto(base+"/mobile.html"); |
| 65 | const drawer = phone.getByRole("dialog",{name:"工作区侧栏"}); |
| 66 | await drawer.getByRole("button",{name:"待处理的会话",exact:true}).waitFor(); |
| 67 | await phone.waitForFunction(() => document.querySelector(".workspace-drawer")?.dataset.visible === "true" && document.querySelector(".workspace-drawer").getBoundingClientRect().left >= 0); |
| 68 | const row = phone.locator(".swipe-session").filter({has:phone.getByRole("button",{name:"待处理的会话",exact:true})}); |
| 69 | const box = await row.boundingBox(); |
| 70 | |
| 71 | await phone.mouse.move(box.x+30,box.y+20); await phone.mouse.down(); |
| 72 | await phone.mouse.move(box.x+31,box.y+65,{steps:8}); await phone.mouse.up(); |
| 73 | assert.equal(await row.getByRole("group").count(),0); |
| 74 | |
| 75 | if (engine === "chromium") { |
| 76 | const touch = await phoneContext.newCDPSession(phone); |
| 77 | await touch.send("Input.dispatchTouchEvent",{type:"touchStart",touchPoints:[{x:box.x+box.width-24,y:box.y+20}]}); |
| 78 | for(let dx=24;dx<=154;dx+=13) await touch.send("Input.dispatchTouchEvent",{type:"touchMove",touchPoints:[{x:box.x+box.width-dx,y:box.y+20}]}); |
| 79 | await touch.send("Input.dispatchTouchEvent",{type:"touchEnd",touchPoints:[]}); |
| 80 | await touch.detach(); |
| 81 | } else { |
| 82 | await phone.mouse.move(box.x+box.width-40,box.y+20); await phone.mouse.down(); |
| 83 | await phone.mouse.move(box.x+box.width-170,box.y+20,{steps:12}); await phone.mouse.up(); |
| 84 | } |
| 85 | await row.getByRole("button",{name:"置顶",exact:true}).waitFor(); |
| 86 | await row.locator(".session-slide").evaluate(el => Promise.all(el.getAnimations().map(animation => animation.finished))); |
| 87 | assert.equal(await phone.getByRole("textbox",{name:"消息"}).count(),0,"swipe opened chat"); |
| 88 | await phone.screenshot({path:join(output,engine+"-swipe.png")}); |
| 89 | await row.getByRole("button",{name:"置顶",exact:true}).click(); |
| 90 | await row.getByRole("button",{name:"取消置顶",exact:true}).waitFor(); |
| 91 | assert.match(await phone.locator(".swipe-session").first().innerText(),/待处理的会话/); |
| 92 | await page.getByRole("group",{name:"待处理的会话 的操作"}).getByRole("button",{name:"取消置顶",exact:true}).waitFor(); |
| 93 | await row.getByRole("button",{name:"重命名",exact:true}).click(); |
| 94 | let dialog = phone.getByRole("dialog",{name:"重命名会话"}); |
| 95 | await dialog.getByRole("textbox",{name:"会话名称"}).fill("手机改名同步电脑"); |
| 96 | failSave = true; |
| 97 | await dialog.getByRole("button",{name:"保存名称"}).click(); |
| 98 | await dialog.getByRole("alert").filter({hasText:"磁盘暂时不可写"}).waitFor(); |
| 99 | assert.equal(sessions[0].title,"待处理的会话"); |
| 100 | failSave = false; |
| 101 | await dialog.getByRole("button",{name:"保存名称"}).click(); |
| 102 | await dialog.waitFor({state:"hidden"}); |
| 103 | const desktopActions = page.getByRole("group",{name:"手机改名同步电脑 的操作"}); |
| 104 | await desktopActions.waitFor(); |
| 105 | await desktopActions.getByRole("button",{name:"重命名",exact:true}).click(); |
| 106 | await page.getByRole("textbox",{name:"标签名称"}).fill("电脑改名同步手机"); |
| 107 | await page.getByRole("button",{name:"保存名称",exact:true}).click(); |
| 108 | await phone.getByRole("button",{name:"电脑改名同步手机",exact:true}).waitFor(); |
| 109 | await phone.reload(); |
| 110 | await drawer.getByRole("button",{name:"电脑改名同步手机",exact:true}).waitFor(); |
| 111 | const currentRow = phone.locator(".swipe-session").filter({has:phone.getByRole("button",{name:"电脑改名同步手机",exact:true})}); |
| 112 | await currentRow.getByRole("button",{name:"电脑改名同步手机",exact:true}).focus(); |
| 113 | await phone.keyboard.press("ArrowLeft"); |
| 114 | await currentRow.getByRole("button",{name:"取消置顶",exact:true}).waitFor(); |
| 115 | await phone.keyboard.press("Escape"); |
| 116 | assert.equal(await currentRow.getByRole("group").count(),0); |
| 117 | await currentRow.getByRole("button",{name:/更多操作/}).click(); |
| 118 | await currentRow.getByRole("button",{name:"归档",exact:true}).click(); |
| 119 | dialog=phone.getByRole("dialog",{name:"归档会话"}); |
| 120 | await dialog.getByRole("button",{name:"取消",exact:true}).click(); |
| 121 | assert.equal(sessions.length,2); |
| 122 | await currentRow.getByRole("button",{name:"归档",exact:true}).click(); |
| 123 | await dialog.getByRole("button",{name:"归档会话",exact:true}).click(); |
| 124 | await currentRow.waitFor({state:"hidden"}); |
| 125 | await page.getByRole("group",{name:"电脑改名同步手机 的操作"}).waitFor({state:"hidden"}); |
| 126 | await phone.reload(); |
| 127 | await drawer.getByRole("button",{name:"另一段会话",exact:true}).waitFor(); |
| 128 | assert.equal(await drawer.getByRole("button",{name:"电脑改名同步手机",exact:true}).count(),0); |
| 129 | |
| 130 | state.terminals = [{id:"fixture-second",kind:"claude",workspace,title:sessions[0].title,cli_session_id:sessions[0].id,closed:true}]; |
| 131 | await drawer.getByRole("button",{name:"另一段会话",exact:true}).click(); |
| 132 | await phone.getByRole("textbox",{name:"消息"}).waitFor(); |
| 133 | await page.getByRole("tab",{name:"另一段会话",exact:true}).hover(); |
| 134 | await page.getByRole("group",{name:"另一段会话 的操作"}).getByRole("button",{name:"归档",exact:true}).click(); |
| 135 | await page.getByRole("dialog",{name:"归档这段会话?"}).getByRole("button",{name:"归档会话"}).click(); |
| 136 | await drawer.waitFor(); |
| 137 | await drawer.getByText("暂无会话",{exact:true}).waitFor(); |
| 138 | assert.equal(await phone.getByRole("textbox",{name:"消息"}).count(),0); |
| 139 | assert.deepEqual(errors,[]); assert.deepEqual(phoneErrors,[]); |
| 140 | console.log(engine+": left swipe, vertical scroll, keyboard, pin, bidirectional rename, failure/retry, archive/cancel, peer archive, label migration, reload passed"); |
| 141 | } finally { await browser.close(); } |