| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const base = process.env.CAGE_UI_URL || "http://127.0.0.1:5209"; |
| 6 | const engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 7 | const output = process.env.CAGE_TEST_CAPTURE || "../output/archive-bulk-20261108"; |
| 8 | const alpha = "/fixture/alpha", beta = "/fixture/beta"; |
| 9 | const seed = [ |
| 10 | {id:"a1",workspace:alpha,title:"项目方案",pinned:true}, |
| 11 | {id:"a2",workspace:alpha,title:"接口检查"}, |
| 12 | {id:"b1",workspace:beta,title:"移动端检查"}, |
| 13 | {id:"b2",workspace:beta,title:"待完成删除",deleting:true}, |
| 14 | ].map(s => ({...s,archived:true,updated_at:"2026-11-08T00:00:00Z"})); |
| 15 | let sessions = structuredClone(seed), failID = "", holdID = "", release; |
| 16 | const operations = []; |
| 17 | const handle = async (route, path, body) => { |
| 18 | if (path === "/cli/sessions") return route.fulfill({json:{sessions:sessions.filter(s => s.archived === (new URL(route.request().url()).searchParams.get("archived") === "true"))}}); |
| 19 | const [, id, operation] = path.match(/\/sessions\/([^/]+)\/(restore|purge)$/) || []; |
| 20 | if (!id) return route.fulfill({json:{}}); |
| 21 | operations.push({id,operation}); |
| 22 | if (operation === "purge") assert.equal(body.confirm,true); |
| 23 | if (id === holdID) await new Promise(resolve => { release = resolve; }); |
| 24 | if (id === failID) return route.fulfill({status:503,contentType:"text/plain; charset=utf-8",body:"测试连接中断"}); |
| 25 | const session = sessions.find(s => s.id === id); |
| 26 | if (operation === "restore") session.archived = false; |
| 27 | else sessions = sessions.filter(s => s.id !== id); |
| 28 | return route.fulfill({json:{ok:true}}); |
| 29 | }; |
| 30 | const desktop = await createWorkbench({url:base,cliRequest:handle}); |
| 31 | const {page,browser,state,errors} = desktop; |
| 32 | const checkbox = (scope,name) => scope.getByRole("checkbox",{name,exact:true}); |
| 33 | const allName = "全选当前筛选结果"; |
| 34 | const projectName = path => `全选项目 ${path} 的当前筛选结果`; |
| 35 | try { |
| 36 | state.status.ready = state.status.vm_running = true; |
| 37 | await page.getByRole("button",{name:"设置",exact:true}).click(); |
| 38 | await page.getByRole("navigation",{name:"设置分组"}).getByRole("button",{name:"已归档会话",exact:true}).click(); |
| 39 | const panel = page.getByRole("region",{name:"已归档会话",exact:true}); |
| 40 | await checkbox(panel,allName).waitFor(); |
| 41 | const inputBox = await checkbox(panel,allName).boundingBox(); |
| 42 | const labelBox = await panel.locator(".archive-selection").first().locator("span").boundingBox(); |
| 43 | assert.ok(labelBox.x > inputBox.x && Math.abs(labelBox.y + labelBox.height / 2 - inputBox.y - inputBox.height / 2) < 2,"select-all label did not align with its checkbox"); |
| 44 | |
| 45 | await checkbox(panel,"选择会话 项目方案").focus(); await page.keyboard.press("Space"); |
| 46 | assert.equal(await checkbox(panel,projectName(alpha)).evaluate(el => el.indeterminate),true); |
| 47 | assert.equal(await checkbox(panel,allName).evaluate(el => el.indeterminate),true); |
| 48 | await checkbox(panel,projectName(alpha)).check(); |
| 49 | assert.equal(await checkbox(panel,"选择会话 接口检查").isChecked(),true); |
| 50 | assert.equal(await checkbox(panel,"选择会话 移动端检查").isChecked(),false); |
| 51 | await checkbox(panel,allName).check(); |
| 52 | assert.equal(await panel.getByRole("button",{name:"批量取消归档",exact:true}).isDisabled(),true); |
| 53 | await panel.getByRole("button",{name:"清空选择",exact:true}).click(); |
| 54 | assert.equal(await checkbox(panel,allName).isChecked(),false); |
| 55 | const search = panel.getByRole("searchbox"); |
| 56 | await checkbox(panel,allName).check(); |
| 57 | await search.fill("接口"); |
| 58 | assert.equal(await checkbox(panel,allName).isChecked(),false,"filter kept hidden selections"); |
| 59 | await checkbox(panel,allName).check(); |
| 60 | await panel.getByRole("button",{name:"批量取消归档",exact:true}).click(); |
| 61 | await panel.getByRole("status").filter({hasText:"已取消归档 1 个会话"}).waitFor(); |
| 62 | assert.deepEqual(operations.splice(0),[{id:"a2",operation:"restore"}],"filtered select all affected hidden sessions"); |
| 63 | await search.fill(""); |
| 64 | |
| 65 | await checkbox(panel,allName).check(); |
| 66 | const filter = panel.getByRole("combobox",{name:"项目筛选"}); |
| 67 | const toolbar = panel.getByRole("group",{name:"批量处理会话"}); |
| 68 | const before = await toolbar.boundingBox(); |
| 69 | await filter.click(); |
| 70 | const list = await page.getByRole("listbox",{name:"项目筛选选项"}).boundingBox(); |
| 71 | assert.ok(Math.abs((await toolbar.boundingBox()).y-before.y)<1 && list.height>0); |
| 72 | await filter.press("Escape"); |
| 73 | assert.ok(Math.abs((await toolbar.boundingBox()).y-before.y)<2); |
| 74 | await filter.press("End"); await filter.press("Enter"); |
| 75 | assert.equal(await checkbox(panel,allName).isChecked(),false); |
| 76 | await filter.press("Home"); await filter.press("Enter"); |
| 77 | |
| 78 | sessions = structuredClone(seed); |
| 79 | |
| 80 | await panel.getByRole("heading",{name:"接口检查",exact:true}).waitFor(); |
| 81 | failID = "a2"; holdID = "a1"; |
| 82 | await checkbox(panel,projectName(alpha)).check(); |
| 83 | await panel.getByRole("button",{name:"批量取消归档",exact:true}).click(); |
| 84 | await panel.getByRole("status").filter({hasText:"正在处理"}).waitFor(); |
| 85 | assert.equal(await search.isDisabled(),true); |
| 86 | assert.equal(await checkbox(panel,allName).isDisabled(),true); |
| 87 | assert.equal(await panel.getByRole("button",{name:"批量删除",exact:true}).isDisabled(),true); |
| 88 | release(); holdID = ""; |
| 89 | await panel.getByRole("alert").filter({hasText:"1 个会话处理失败"}).waitFor(); |
| 90 | assert.equal(await panel.getByRole("heading",{name:"项目方案",exact:true}).count(),0); |
| 91 | assert.equal(await checkbox(panel,"选择会话 接口检查").isChecked(),true); |
| 92 | assert.equal(await checkbox(panel,"选择会话 移动端检查").isChecked(),false); |
| 93 | failID = ""; |
| 94 | await panel.getByRole("button",{name:"批量取消归档",exact:true}).click(); |
| 95 | await panel.getByRole("heading",{name:"接口检查",exact:true}).waitFor({state:"hidden"}); |
| 96 | |
| 97 | await checkbox(panel,projectName(beta)).check(); |
| 98 | await panel.getByRole("button",{name:"批量删除",exact:true}).click(); |
| 99 | let confirm = panel.getByRole("group",{name:"确认批量永久删除会话",exact:true}); |
| 100 | await confirm.getByRole("button",{name:"取消",exact:true}).click(); |
| 101 | assert.equal(operations.filter(o => o.operation === "purge").length,0); |
| 102 | failID = "b2"; |
| 103 | await panel.getByRole("button",{name:"批量删除",exact:true}).click(); |
| 104 | await confirm.getByRole("button",{name:"永久删除所选会话",exact:true}).click(); |
| 105 | await panel.getByRole("alert").filter({hasText:"待完成删除"}).waitFor(); |
| 106 | assert.equal(await checkbox(panel,"选择会话 待完成删除").isChecked(),true); |
| 107 | assert.equal(await panel.getByRole("heading",{name:"移动端检查",exact:true}).count(),0); |
| 108 | failID = ""; |
| 109 | await panel.getByRole("button",{name:"批量删除",exact:true}).click(); |
| 110 | await confirm.getByText(/所选的 1 个会话/).waitFor(); |
| 111 | await confirm.getByRole("button",{name:"永久删除所选会话",exact:true}).click(); |
| 112 | await panel.getByText("暂无已归档会话。",{exact:true}).waitFor(); |
| 113 | assert.deepEqual(operations.filter(o => o.operation === "purge").map(o => o.id),["b1","b2","b2"]); |
| 114 | |
| 115 | sessions = structuredClone(seed); |
| 116 | const context = await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,userAgent:"CageMobile/1"}); |
| 117 | const phone = await context.newPage(); |
| 118 | const phoneErrors = []; phone.on("pageerror",e => phoneErrors.push(e.message)); |
| 119 | let ready = false; |
| 120 | await phone.route("**/api/**", async route => { |
| 121 | const path = new URL(route.request().url()).pathname.replace("/api/mobile/cli","/cli"); |
| 122 | if (path.endsWith("/projects")) return route.fulfill({json:{host:"隔离测试主机",ready,shared_sessions:true,projects:[{path:alpha,name:"alpha"},{path:beta,name:"beta"}]}}); |
| 123 | return handle(route,path,route.request().postDataJSON()); |
| 124 | }); |
| 125 | await phone.goto(base+"/mobile.html"); |
| 126 | await phone.getByRole("dialog",{name:"工作区侧栏"}).getByRole("button",{name:"设置",exact:true}).click(); |
| 127 | await phone.getByRole("button",{name:"已归档会话",exact:true}).click(); |
| 128 | await checkbox(phone,projectName(alpha)).check(); |
| 129 | assert.equal(await phone.getByRole("button",{name:"批量删除",exact:true}).isDisabled(),true); |
| 130 | assert.equal(await phone.getByRole("button",{name:"批量取消归档",exact:true}).isEnabled(),true); |
| 131 | await phone.getByRole("button",{name:"批量取消归档",exact:true}).click(); |
| 132 | await phone.getByRole("heading",{name:"接口检查",exact:true}).waitFor({state:"hidden"}); |
| 133 | ready = true; |
| 134 | await phone.getByRole("button",{name:"返回设置",exact:true}).click(); |
| 135 | await phone.getByRole("button",{name:"已归档会话",exact:true}).click(); |
| 136 | await checkbox(phone,allName).check(); |
| 137 | await phone.getByRole("button",{name:"批量删除",exact:true}).click(); |
| 138 | await phone.getByRole("group",{name:"确认批量永久删除会话"}).waitFor(); |
| 139 | await mkdir(output,{recursive:true}); |
| 140 | await phone.screenshot({path:`${output}/${engine}-mobile.png`,fullPage:true}); |
| 141 | assert.equal(await phone.evaluate(() => document.documentElement.scrollWidth <= innerWidth),true,"mobile layout overflowed"); |
| 142 | await phone.getByRole("button",{name:"永久删除所选会话",exact:true}).click(); |
| 143 | await phone.getByText("暂无已归档会话。",{exact:true}).waitFor(); |
| 144 | await phone.reload(); |
| 145 | await phone.getByRole("heading",{name:"已归档会话",exact:true}).waitFor(); |
| 146 | await phone.getByText("暂无已归档会话。",{exact:true}).waitFor(); |
| 147 | assert.equal(sessions.filter(s => s.archived).length,0); |
| 148 | |
| 149 | sessions = structuredClone(seed); |
| 150 | await checkbox(panel,projectName(alpha)).check(); |
| 151 | await page.screenshot({path:`${output}/${engine}-desktop.png`}); |
| 152 | assert.deepEqual(errors,[]); assert.deepEqual(phoneErrors,[]); |
| 153 | console.log(`${engine}: item/project/all selection, mixed state, keyboard, filter scope, progress/locking, restore/purge partial failure and retry, delete cancellation, mobile/offline/reload passed`); |
| 154 | } finally { await browser.close(); } |