| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const output = process.env.CAGE_TEST_CAPTURE || "/tmp/cage-sidebar-session-running"; |
| 6 | await mkdir(output, {recursive:true}); |
| 7 | for (const platform of ["darwin", "windows", "linux"]) { |
| 8 | const sessions = [ |
| 9 | {id:"external",title:"其他窗口中的会话",running:true}, |
| 10 | {id:"owned",title:"Cage 中已打开的会话",running:true}, |
| 11 | {id:"waiting",title:"等待打开的会话"}, |
| 12 | {id:"available",title:"可以接续的会话"}, |
| 13 | ]; |
| 14 | const releases = [], opens = []; |
| 15 | let behavior = "hold", state; |
| 16 | const fixture = await createWorkbench({ |
| 17 | url:process.env.CAGE_UI_URL || "http://127.0.0.1:5228", |
| 18 | clientContext:{mode:"local",connection_id:"local",client_platform:platform,server_platform:platform}, |
| 19 | beforeNavigate:async (_page, value) => { |
| 20 | state = value; |
| 21 | state.status.ready = state.status.vm_running = true; |
| 22 | sessions.forEach(session=>{session.workspace=state.settings.workspace;}); |
| 23 | state.terminals = [{id:"owned-terminal",kind:"claude",workspace:state.settings.workspace,title:sessions[1].title,cli_session_id:"owned"}]; |
| 24 | }, |
| 25 | cliRequest:async (route,path) => { |
| 26 | if (path === "/cli/sessions") return route.fulfill({json:{sessions}}); |
| 27 | if (path.endsWith("/account")) return route.fulfill({json:{session:{},supported:false,candidates:[]}}); |
| 28 | if (path.endsWith("/open")) { |
| 29 | const session = sessions.find(item=>path.includes("/"+item.id+"/")); |
| 30 | opens.push(session.id); |
| 31 | const mode = behavior; |
| 32 | if (mode === "hold") await new Promise(resolve=>releases.push(resolve)); |
| 33 | if (mode === "error") return route.fulfill({status:409,contentType:"text/plain; charset=utf-8",body:"原会话仍在其他窗口运行,请先退出原会话"}); |
| 34 | const terminal = {id:"terminal-"+session.id,kind:"claude",workspace:session.workspace,title:session.title,cli_session_id:session.id}; |
| 35 | state.terminals.push(terminal); |
| 36 | return route.fulfill({json:{desktop_terminal_id:terminal.id}}); |
| 37 | } |
| 38 | return route.fulfill({json:{}}); |
| 39 | }, |
| 40 | }); |
| 41 | const {page,browser,errors} = fixture; |
| 42 | try { |
| 43 | const external = page.getByRole("button",{name:sessions[0].title,exact:true}); |
| 44 | const waiting = page.getByRole("button",{name:sessions[2].title,exact:true}); |
| 45 | const available = page.getByRole("button",{name:sessions[3].title,exact:true}); |
| 46 | const owned = page.getByRole("tab",{name:sessions[1].title,exact:true}); |
| 47 | await external.waitFor(); |
| 48 | await owned.waitFor(); |
| 49 | assert.equal(await external.isDisabled(),true,"external running history must be disabled before clicking"); |
| 50 | await external.getByRole("img",{name:"其他窗口运行中",exact:true}).waitFor(); |
| 51 | assert.match(await external.getAttribute("title"),/退出原会话/); |
| 52 | const box = await external.boundingBox(); |
| 53 | await page.mouse.click(box.x+box.width/2,box.y+box.height/2); |
| 54 | assert.deepEqual(opens,[],"disabled external session sent an open request"); |
| 55 | assert.equal(await owned.isEnabled(),true,"owned live terminal was disabled"); |
| 56 | await owned.click(); |
| 57 | await owned.press("ArrowDown"); |
| 58 | assert.equal(await waiting.evaluate(element=>element===document.activeElement),true,"keyboard focus got stuck at a disabled session"); |
| 59 | assert.equal(await available.isEnabled(),true); |
| 60 | await page.locator(".sidebar-scroll").screenshot({path:`${output}/${platform}-initial.png`}); |
| 61 | |
| 62 | await waiting.click(); |
| 63 | await waiting.getByRole("img",{name:"正在打开会话",exact:true}).waitFor(); |
| 64 | assert.equal(await waiting.isDisabled(),true); |
| 65 | assert.equal(await available.isEnabled(),true,"unrelated history flickered disabled"); |
| 66 | assert.equal(await owned.isEnabled(),true); |
| 67 | const sent = opens.length; |
| 68 | await page.mouse.click((await waiting.boundingBox()).x+20,(await waiting.boundingBox()).y+20); |
| 69 | assert.equal(opens.length,sent,"pending session sent a duplicate open"); |
| 70 | behavior = "error"; |
| 71 | await available.focus(); |
| 72 | await page.keyboard.press("Enter"); |
| 73 | const failure = page.getByRole("alert").filter({hasText:"原会话仍在其他窗口运行"}); |
| 74 | await failure.waitFor(); |
| 75 | assert.equal(await waiting.isEnabled(),true,"cancelled request kept its row disabled"); |
| 76 | assert.equal(await available.isEnabled(),true,"failed open kept its row disabled"); |
| 77 | releases.shift()(); |
| 78 | await page.waitForFunction(id=>Boolean(document.querySelector(`[data-tab-id="${id}"]`)),"terminal:terminal-waiting"); |
| 79 | assert.equal(await owned.getAttribute("aria-selected"),"true","a cancelled response stole focus"); |
| 80 | |
| 81 | |
| 82 | sessions[0].running = false; |
| 83 | await page.evaluate(()=>window.dispatchEvent(new Event("cage-cli-sessions-changed"))); |
| 84 | await page.waitForFunction(()=>document.querySelector('[aria-label="其他窗口中的会话"]')?.disabled === false); |
| 85 | assert.equal(await external.getByRole("img",{name:"其他窗口运行中"}).count(),0); |
| 86 | behavior = "success"; |
| 87 | await external.click(); |
| 88 | await page.getByRole("tab",{name:sessions[0].title,exact:true}).waitFor(); |
| 89 | assert.equal(await failure.count(),0); |
| 90 | |
| 91 | |
| 92 | behavior = "hold"; |
| 93 | await page.clock.install(); |
| 94 | await available.click(); |
| 95 | await available.getByRole("img",{name:"正在打开会话",exact:true}).waitFor(); |
| 96 | await page.clock.fastForward(60001); |
| 97 | await page.getByRole("alert").filter({hasText:"打开会话超时,历史已保留"}).waitFor(); |
| 98 | assert.equal(await available.isEnabled(),true); |
| 99 | assert.deepEqual(errors,[]); |
| 100 | await page.locator(".sidebar-scroll").screenshot({path:`${output}/${platform}-timeout.png`}); |
| 101 | console.log(`${platform}: initial disabled/spinner, owned terminal, isolated pending state, cancellation, error, exit/reopen and timeout passed`); |
| 102 | } finally { |
| 103 | releases.forEach(release=>release()); |
| 104 | await browser.close(); |
| 105 | } |
| 106 | } |