|
| 1 | +// Control UI E2E proves dashboard tabs do not multiply server-owned session-list demand. |
| 2 | +import type { Page } from "playwright"; |
| 3 | +import { expect, it } from "vitest"; |
| 4 | +import { |
| 5 | + installMockGateway, |
| 6 | + waitForControlUiRoute, |
| 7 | + type MockGatewayControls, |
| 8 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 9 | +import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts"; |
| 10 | + |
| 11 | +const suite = createControlUiE2eSuite({ |
| 12 | + name: "Control UI dashboard session-list demand", |
| 13 | +}); |
| 14 | + |
| 15 | +const DASHBOARD_REQUEST_PARAMS = { |
| 16 | + archived: "all", |
| 17 | + boardFace: "dashboard", |
| 18 | + configuredAgentsOnly: true, |
| 19 | + includeGlobal: true, |
| 20 | + includeUnknown: true, |
| 21 | + limit: 50, |
| 22 | +} as const; |
| 23 | + |
| 24 | +function sessionsResult(key: string, label: string, updatedAt: number) { |
| 25 | + return { |
| 26 | + count: 1, |
| 27 | + defaults: { contextTokens: null, model: null, modelProvider: null }, |
| 28 | + path: "", |
| 29 | + sessions: [ |
| 30 | + { |
| 31 | + boardFace: "dashboard", |
| 32 | + key, |
| 33 | + kind: "direct", |
| 34 | + label, |
| 35 | + updatedAt, |
| 36 | + }, |
| 37 | + ], |
| 38 | + ts: updatedAt, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +function isDashboardRequest(request: { params?: unknown }): boolean { |
| 43 | + return ( |
| 44 | + typeof request.params === "object" && |
| 45 | + request.params !== null && |
| 46 | + "boardFace" in request.params && |
| 47 | + request.params.boardFace === "dashboard" |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +async function requestCounts(gateways: MockGatewayControls[]) { |
| 52 | + const requests = ( |
| 53 | + await Promise.all(gateways.map((gateway) => gateway.getRequests("sessions.list"))) |
| 54 | + ).flat(); |
| 55 | + const dashboard = requests.filter(isDashboardRequest).length; |
| 56 | + return { canonical: requests.length - dashboard, dashboard, total: requests.length }; |
| 57 | +} |
| 58 | + |
| 59 | +suite.define(() => { |
| 60 | + it("keeps dashboard query demand at one request per real browser tab", async () => { |
| 61 | + const context = await suite.browser.newContext({ viewport: { height: 800, width: 1200 } }); |
| 62 | + const tabs: Array<{ |
| 63 | + gateway: MockGatewayControls; |
| 64 | + page: Page; |
| 65 | + canonicalLabel: string; |
| 66 | + dashboardLabel: string; |
| 67 | + updatedDashboardLabel: string; |
| 68 | + }> = []; |
| 69 | + try { |
| 70 | + for (const index of [1, 2]) { |
| 71 | + const page = await context.newPage(); |
| 72 | + const dashboardLabel = `Dashboard tab ${index}`; |
| 73 | + const gateway = await installMockGateway(page, { |
| 74 | + deferredMethods: ["sessions.list"], |
| 75 | + methodResponses: { |
| 76 | + "sessions.list": { |
| 77 | + cases: [ |
| 78 | + { |
| 79 | + match: { archived: "all", boardFace: "dashboard" }, |
| 80 | + response: sessionsResult(`agent:main:dashboard-${index}`, dashboardLabel, index), |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + const canonicalLabel = `Older canonical tab ${index}`; |
| 87 | + const updatedDashboardLabel = `Updated dashboard tab ${index}`; |
| 88 | + tabs.push({ gateway, page, canonicalLabel, dashboardLabel, updatedDashboardLabel }); |
| 89 | + } |
| 90 | + |
| 91 | + await Promise.all( |
| 92 | + tabs.map(async ({ gateway, page }) => { |
| 93 | + await page.goto(suite.server.baseUrl); |
| 94 | + const canonical = await gateway.waitForRequest("sessions.list"); |
| 95 | + expect(isDashboardRequest(canonical)).toBe(false); |
| 96 | + await page.waitForFunction(() => { |
| 97 | + const app = document.querySelector("openclaw-app") as HTMLElement & { |
| 98 | + runtime?: { context: { agents: { state: { agentsList: unknown } } } }; |
| 99 | + }; |
| 100 | + return app.runtime?.context.agents.state.agentsList != null; |
| 101 | + }); |
| 102 | + await page.evaluate(() => { |
| 103 | + const app = document.querySelector("openclaw-app") as HTMLElement & { |
| 104 | + runtime?: { |
| 105 | + context: { |
| 106 | + navigate: (routeId: string) => void; |
| 107 | + agentSelection: { |
| 108 | + state: { scopeId: string | null }; |
| 109 | + setScope: (agentId: string | null) => void; |
| 110 | + }; |
| 111 | + }; |
| 112 | + }; |
| 113 | + }; |
| 114 | + if (!app.runtime) { |
| 115 | + throw new Error("OpenClaw application runtime is unavailable"); |
| 116 | + } |
| 117 | + app.runtime.context.agentSelection.setScope(null); |
| 118 | + if (app.runtime.context.agentSelection.state.scopeId !== null) { |
| 119 | + throw new Error("Control UI did not enter all-agent scope"); |
| 120 | + } |
| 121 | + app.runtime.context.navigate("dashboards"); |
| 122 | + }); |
| 123 | + await waitForControlUiRoute(page, { pathname: "/dashboards", routeId: "dashboards" }); |
| 124 | + }), |
| 125 | + ); |
| 126 | + await Promise.all( |
| 127 | + tabs.map(({ page, dashboardLabel }) => |
| 128 | + page.getByText(dashboardLabel, { exact: true }).waitFor(), |
| 129 | + ), |
| 130 | + ); |
| 131 | + expect(await requestCounts(tabs.map(({ gateway }) => gateway))).toEqual({ |
| 132 | + canonical: 2, |
| 133 | + dashboard: 2, |
| 134 | + total: 4, |
| 135 | + }); |
| 136 | + for (const tabGateway of tabs.map(({ gateway }) => gateway)) { |
| 137 | + const dashboardRequest = (await tabGateway.getRequests("sessions.list")).find( |
| 138 | + isDashboardRequest, |
| 139 | + ); |
| 140 | + expect(dashboardRequest?.params).toEqual(DASHBOARD_REQUEST_PARAMS); |
| 141 | + expect(dashboardRequest?.params).not.toHaveProperty("agentId"); |
| 142 | + } |
| 143 | + await Promise.all( |
| 144 | + tabs.map(async ({ canonicalLabel, gateway, page }, index) => { |
| 145 | + await gateway.resolveDeferred( |
| 146 | + "sessions.list", |
| 147 | + sessionsResult(`agent:main:canonical-${index + 1}`, canonicalLabel, 10 + index), |
| 148 | + ); |
| 149 | + await page.getByText(canonicalLabel, { exact: true }).first().waitFor(); |
| 150 | + }), |
| 151 | + ); |
| 152 | + |
| 153 | + expect(await requestCounts(tabs.map(({ gateway }) => gateway))).toEqual({ |
| 154 | + canonical: 2, |
| 155 | + dashboard: 2, |
| 156 | + total: 4, |
| 157 | + }); |
| 158 | + |
| 159 | + const beforeWave = await Promise.all( |
| 160 | + tabs.map(({ gateway }) => gateway.getRequests("sessions.list")), |
| 161 | + ); |
| 162 | + await Promise.all( |
| 163 | + tabs.map(async ({ gateway, updatedDashboardLabel }, index) => { |
| 164 | + await gateway.setMethodResponse("sessions.list", { |
| 165 | + cases: [ |
| 166 | + { |
| 167 | + match: { archived: "all", boardFace: "dashboard" }, |
| 168 | + response: sessionsResult( |
| 169 | + `agent:main:updated-dashboard-${index + 1}`, |
| 170 | + updatedDashboardLabel, |
| 171 | + 20 + index, |
| 172 | + ), |
| 173 | + }, |
| 174 | + ], |
| 175 | + }); |
| 176 | + await gateway.emitGatewayEvent("sessions.changed", { |
| 177 | + agentId: "main", |
| 178 | + key: `agent:main:changed-${index + 1}`, |
| 179 | + kind: "direct", |
| 180 | + reason: "update", |
| 181 | + sessionKey: `agent:main:changed-${index + 1}`, |
| 182 | + updatedAt: 20 + index, |
| 183 | + }); |
| 184 | + }), |
| 185 | + ); |
| 186 | + await Promise.all( |
| 187 | + tabs.map(({ gateway }, index) => |
| 188 | + expect |
| 189 | + .poll(async () => { |
| 190 | + const added = (await gateway.getRequests("sessions.list")).slice( |
| 191 | + beforeWave[index]?.length ?? 0, |
| 192 | + ); |
| 193 | + const dashboard = added.filter(isDashboardRequest).length; |
| 194 | + return { canonical: added.length - dashboard, dashboard }; |
| 195 | + }) |
| 196 | + .toEqual({ canonical: 1, dashboard: 1 }), |
| 197 | + ), |
| 198 | + ); |
| 199 | + |
| 200 | + const afterWave = await Promise.all( |
| 201 | + tabs.map(({ gateway }) => gateway.getRequests("sessions.list")), |
| 202 | + ); |
| 203 | + for (const [index, requests] of afterWave.entries()) { |
| 204 | + const added = requests.slice(beforeWave[index]?.length ?? 0); |
| 205 | + expect(added.filter(isDashboardRequest)).toHaveLength(1); |
| 206 | + expect(added.filter((request) => !isDashboardRequest(request))).toHaveLength(1); |
| 207 | + } |
| 208 | + const dashboardRequests = afterWave.flat().filter(isDashboardRequest); |
| 209 | + expect(dashboardRequests).toHaveLength(4); |
| 210 | + for (const request of dashboardRequests) { |
| 211 | + expect(request.params).toEqual(DASHBOARD_REQUEST_PARAMS); |
| 212 | + expect(request.params).not.toHaveProperty("agentId"); |
| 213 | + } |
| 214 | + await Promise.all( |
| 215 | + tabs.map(({ page, updatedDashboardLabel }) => |
| 216 | + page.getByText(updatedDashboardLabel, { exact: true }).waitFor(), |
| 217 | + ), |
| 218 | + ); |
| 219 | + } finally { |
| 220 | + await context.close(); |
| 221 | + } |
| 222 | + }); |
| 223 | +}); |
0 commit comments