| 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 engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 6 | const output = process.env.CAGE_TEST_CAPTURE || "/tmp/cage-session-order"; |
| 7 | let state, opens = 0; |
| 8 | const sessions = [ |
| 9 | { id:"order-idle", title:"已打开的空闲会话", updated_at:"2026-11-07T10:00:00Z" }, |
| 10 | { id:"order-active", title:"正在执行的会话", active:true, updated_at:"2026-11-07T09:00:00Z" }, |
| 11 | { id:"order-saved", title:"保存的会话", updated_at:"2026-11-07T08:00:00Z" }, |
| 12 | ]; |
| 13 | const fixture = await createWorkbench({ url: process.env.CAGE_UI_URL || "http://127.0.0.1:5217", |
| 14 | cliRequest: async (route, path) => { |
| 15 | if (path === "/cli/sessions") return route.fulfill({json:{sessions}}); |
| 16 | if (path.endsWith("/account")) return route.fulfill({json:{session:{},supported:false,candidates:[]}}); |
| 17 | const session = sessions.find(s => path.includes(s.id)); |
| 18 | if (path.endsWith("/open")) { |
| 19 | opens++; |
| 20 | const terminal = {id:"opened-history", workspace:state.settings.workspace, title:session.title, cli_session_id:session.id}; |
| 21 | state.terminals.push(terminal); |
| 22 | return route.fulfill({json:{...session,desktop_terminal_id:terminal.id}}); |
| 23 | } |
| 24 | return route.fulfill({json:session}); |
| 25 | }, |
| 26 | }); |
| 27 | state = fixture.state; |
| 28 | const {page,browser,errors} = fixture; |
| 29 | try { |
| 30 | sessions.forEach(session => {session.workspace = state.settings.workspace;}); |
| 31 | state.status.ready = state.status.vm_running = true; |
| 32 | state.terminals = [{id:"idle", workspace:state.settings.workspace, title:sessions[0].title, cli_session_id:sessions[0].id}]; |
| 33 | const project = page.getByRole("region",{name:"项目 默认工作区",exact:true}); |
| 34 | const order = () => project.locator(".sidebar-tab").evaluateAll(rows => rows.map(row => row.querySelector(":scope > button").textContent)); |
| 35 | const saved = project.getByRole("button",{name:"保存的会话",exact:true}); |
| 36 | await saved.waitFor(); |
| 37 | assert.deepEqual(await order(),[sessions[1].title,sessions[0].title,sessions[2].title]); |
| 38 | await project.getByRole("img",{name:"会话正在运行"}).waitFor(); |
| 39 | await mkdir(output,{recursive:true}); |
| 40 | await page.screenshot({path:`${output}/${engine}-active.png`}); |
| 41 | const phoneContext = await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,userAgent:"CageMobile/1"}); |
| 42 | const phone = await phoneContext.newPage(); |
| 43 | await phone.route("**/api/**", async route => { |
| 44 | const pathname = new URL(route.request().url()).pathname; |
| 45 | return route.fulfill({json:pathname.endsWith("/projects") |
| 46 | ? {host:"隔离测试",ready:true,shared_sessions:true,projects:[{path:state.settings.workspace,name:"默认工作区"}]} |
| 47 | : {sessions}}); |
| 48 | }); |
| 49 | await phone.goto((process.env.CAGE_UI_URL || "http://127.0.0.1:5217")+"/mobile.html"); |
| 50 | const drawer = phone.getByRole("dialog",{name:"工作区侧栏"}); |
| 51 | await drawer.getByRole("img",{name:"会话正在运行"}).waitFor(); |
| 52 | assert.equal(await drawer.locator(".session-select").first().getAttribute("aria-label"),sessions[1].title); |
| 53 | await phoneContext.close(); |
| 54 | const target = project.getByRole("button",{name:sessions[1].title,exact:true}); |
| 55 | const from = await saved.boundingBox(), to = await target.boundingBox(); |
| 56 | await page.mouse.move(from.x+35,from.y+from.height/2); await page.mouse.down(); |
| 57 | await page.mouse.move(to.x+35,to.y+to.height/2,{steps:12}); await page.mouse.up(); |
| 58 | assert.equal(opens,0,"drag opened a conversation"); |
| 59 | assert.deepEqual(await order(),[sessions[2].title,sessions[1].title,sessions[0].title]); |
| 60 | await saved.click(); |
| 61 | await project.getByRole("tab",{name:sessions[2].title,exact:true}).waitFor(); |
| 62 | assert.deepEqual(await order(),[sessions[2].title,sessions[1].title,sessions[0].title],"opening changed order"); |
| 63 | await project.getByRole("tab",{name:sessions[0].title,exact:true}).click(); |
| 64 | assert.deepEqual(await order(),[sessions[2].title,sessions[1].title,sessions[0].title],"click changed order"); |
| 65 | sessions[1].active = false; |
| 66 | await page.evaluate(() => window.dispatchEvent(new Event("cage-cli-sessions-changed"))); |
| 67 | await project.getByRole("img",{name:"会话正在运行"}).waitFor({state:"hidden"}); |
| 68 | assert.deepEqual(await order(),[sessions[2].title,sessions[1].title,sessions[0].title],"activity changed manual order"); |
| 69 | await page.reload(); |
| 70 | await project.getByRole("tab",{name:sessions[2].title,exact:true}).waitFor(); |
| 71 | assert.deepEqual(await order(),[sessions[2].title,sessions[1].title,sessions[0].title],"reload lost manual order"); |
| 72 | const selected = project.getByRole("tab",{name:sessions[2].title,exact:true}); |
| 73 | await selected.focus(); await selected.press("Alt+ArrowDown"); |
| 74 | assert.deepEqual(await order(),[sessions[1].title,sessions[2].title,sessions[0].title],"keyboard reorder failed"); |
| 75 | const before = await order(); |
| 76 | const a = await selected.boundingBox(), b = await project.getByRole("tab",{name:sessions[0].title,exact:true}).boundingBox(); |
| 77 | await page.mouse.move(a.x+30,a.y+15); await page.mouse.down(); |
| 78 | await page.mouse.move(b.x+30,b.y+15,{steps:8}); await page.keyboard.press("Escape"); await page.mouse.up(); |
| 79 | assert.deepEqual(await order(),before,"Escape committed a drag"); |
| 80 | assert.deepEqual(errors,[]); |
| 81 | await mkdir(output,{recursive:true}); |
| 82 | await page.screenshot({path:`${output}/${engine}.png`}); |
| 83 | await writeFile(`${output}/${engine}.json`,JSON.stringify({order:await order(),opens,errors})); |
| 84 | console.log(`${engine}: active priority/spinner, mixed live/history drag, no click reorder, manual priority, reload, keyboard and cancellation passed`); |
| 85 | } finally {await browser.close();} |