跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

session-order-smoke.mjs

85 行 · 5.5 KB历史下载
1import assert from "node:assert/strict";
2import { mkdir, writeFile } from "node:fs/promises";
3import { createWorkbench } from "./workbench-fixture.mjs";
4
5const engine = process.env.CAGE_TEST_BROWSER || "chromium";
6const output = process.env.CAGE_TEST_CAPTURE || "/tmp/cage-session-order";
7let state, opens = 0;
8const 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];
13const 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});
27state = fixture.state;
28const {page,browser,errors} = fixture;
29try {
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();}