| 1 | import assert from "node:assert/strict"; |
| 2 | import {mkdir} from "node:fs/promises"; |
| 3 | import {fileURLToPath} from "node:url"; |
| 4 | import {createWorkbench} from "./workbench-fixture.mjs"; |
| 5 | |
| 6 | const out=new URL("../../docs/test-reports/2026-11-08-account-scheduling/",import.meta.url); |
| 7 | await mkdir(out,{recursive:true}); |
| 8 | const scheduling={mode:"balanced",switch_policy:"ask"}; |
| 9 | let profiles=[{id:"a",label:"账号 A",email:"[email protected]",status:"ready",enabled:true},{id:"b",label:"账号 B",email:"[email protected]",status:"ready",enabled:true}]; |
| 10 | const sid="12345678-1234-4321-aaaa-123456789abc"; |
| 11 | const session={id:sid,workspace:"/Users/shihao/.claude-cage-workspace",title:"调度验证",terminal_available:true,desktop_terminal_id:"first",can_send:true,revision:1,account_id:"a",account_label:"账号 A",switch_policy:"ask",turns:[]}; |
| 12 | let switchCalls=[]; |
| 13 | const account=(route,body)=>{ |
| 14 | if(body.action==="policy")session.switch_policy=body.policy; |
| 15 | if(body.action==="switch"){ |
| 16 | switchCalls.push(body);session.account_id="b";session.account_label="账号 B";session.desktop_terminal_id="second"; |
| 17 | session.account_switch={id:body.request_id,phase:"completed",target_label:"账号 B"}; |
| 18 | } |
| 19 | return route.fulfill({json:{session,source_id:"12345678-1234-4321-aaaa-123456789abd",switch_policy:session.switch_policy,supported:true,candidates:[{id:"b",label:"账号 B"}]}}); |
| 20 | }; |
| 21 | const fixture=await createWorkbench(); |
| 22 | const {browser,page,state,errors}=fixture; |
| 23 | try{ |
| 24 | await page.route("**/api/claude-accounts*",async route=>{ |
| 25 | const body=route.request().postDataJSON(); |
| 26 | if(body?.action==="scheduling"){ |
| 27 | Object.assign(scheduling,body.scheduling); |
| 28 | if(body.order)profiles=body.order.map(id=>profiles.find(a=>a.id===id)); |
| 29 | } |
| 30 | if(body?.action==="update")Object.assign(profiles.find(a=>a.id===body.id),body); |
| 31 | return route.fulfill({json:{accounts:profiles,scheduling,detection_status:"offline"}}); |
| 32 | }); |
| 33 | await page.getByRole("button",{name:"设置",exact:true}).click(); |
| 34 | await page.getByRole("button",{name:"Claude 账号",exact:true}).click(); |
| 35 | await page.getByText("调度设置 · 均衡分配",{exact:true}).click(); |
| 36 | const select=page.getByRole("combobox",{name:"新会话分配",exact:true}); |
| 37 | const policy=page.getByRole("combobox",{name:"新会话切换授权",exact:true}); |
| 38 | const before=(await policy.boundingBox()).y; |
| 39 | await select.click(); |
| 40 | assert.ok((await policy.boundingBox()).y>before+20,"options must occupy layout space"); |
| 41 | await select.press("End");await select.press("Enter"); |
| 42 | await page.getByRole("region",{name:"账号优先顺序"}).count(); |
| 43 | assert.equal(scheduling.mode,"sequential"); |
| 44 | await page.getByRole("button",{name:"提高 账号 B 的优先级"}).click(); |
| 45 | await page.waitForFunction(()=>document.querySelector('.claude-account-order')?.textContent.startsWith('1. 账号 B')); |
| 46 | assert.equal(profiles[0].id,"b"); |
| 47 | await policy.click();await page.getByRole("option",{name:"额度耗尽后自动接续",exact:true}).click(); |
| 48 | assert.equal(scheduling.switch_policy,"allow"); |
| 49 | await page.getByRole("textbox",{name:"账号 A 的接续分组"}).fill("本人账号"); |
| 50 | await page.getByRole("article",{name:"账号 A",exact:true}).getByRole("button",{name:"保存分组"}).click(); |
| 51 | assert.equal(profiles.find(a=>a.id==="a").handoff_group,"本人账号"); |
| 52 | await page.screenshot({path:fileURLToPath(new URL("scheduling-desktop.png",out))}); |
| 53 | await page.reload(); |
| 54 | await page.getByRole("button",{name:"设置",exact:true}).click();await page.getByRole("button",{name:"Claude 账号",exact:true}).click(); |
| 55 | await page.getByText("调度设置 · 顺序优先",{exact:true}).waitFor(); |
| 56 | |
| 57 | const phoneContext=await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,userAgent:"CageMobile/1"}); |
| 58 | const phone=await phoneContext.newPage(); |
| 59 | const phoneErrors=[];phone.on("pageerror",e=>phoneErrors.push(e.message)); |
| 60 | await phone.route("**/api/**",async route=>{ |
| 61 | const path=new URL(route.request().url()).pathname;const body=route.request().postDataJSON(); |
| 62 | if(path.endsWith("/account"))return account(route,body); |
| 63 | if(path.endsWith("/projects"))return route.fulfill({json:{host:"测试电脑",ready:true,shared_sessions:true,projects:[{path:session.workspace,name:"默认工作区"}]}}); |
| 64 | if(path.endsWith("/sessions"))return route.fulfill({json:{sessions:[session]}}); |
| 65 | return route.fulfill({json:session}); |
| 66 | }); |
| 67 | await phone.goto((process.env.CAGE_UI_URL || "http://127.0.0.1:5198")+"/mobile.html"); |
| 68 | const drawer=phone.getByRole("dialog",{name:"工作区侧栏"}); |
| 69 | await drawer.waitFor(); |
| 70 | const folder=drawer.getByRole("button",{name:"项目 默认工作区",exact:true}); |
| 71 | if(await folder.getAttribute("aria-expanded")==="false")await folder.click(); |
| 72 | await phone.getByRole("button",{name:"调度验证",exact:true}).click(); |
| 73 | await phone.getByRole("button",{name:"账号 A · 会话绑定",exact:true}).click(); |
| 74 | const switchButton=phone.getByRole("button",{name:"切换并恢复历史",exact:true}); |
| 75 | assert.equal(await switchButton.isDisabled(),true); |
| 76 | await phone.getByRole("combobox",{name:"此会话切换授权"}).click(); |
| 77 | await phone.getByRole("option",{name:"始终等待原账号",exact:true}).click(); |
| 78 | assert.equal(session.switch_policy,"wait"); |
| 79 | await phone.getByRole("checkbox",{name:"允许将此会话历史交给所选账号"}).check(); |
| 80 | await switchButton.click(); |
| 81 | await phone.getByRole("button",{name:"账号 B · 会话绑定",exact:true}).waitFor(); |
| 82 | assert.equal(switchCalls.length,1);assert.equal(switchCalls[0].confirm,true);assert.equal(switchCalls[0].target,"b"); |
| 83 | assert.equal(switchCalls[0].continue,false,"switching must not imply task continuation"); |
| 84 | assert.ok(await phone.evaluate(()=>document.documentElement.scrollWidth<=innerWidth)); |
| 85 | await phone.screenshot({path:fileURLToPath(new URL("session-account-phone.png",out))}); |
| 86 | assert.deepEqual(errors,[]);assert.deepEqual(phoneErrors,[]); |
| 87 | console.log("Scheduling: inline mouse/keyboard selection, ordering, group persistence, session authorization and explicit mobile switch passed."); |
| 88 | }finally{await browser.close();} |