|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { expect, it } from "vitest"; |
| 4 | +import { installMockGateway } from "../test-helpers/control-ui-e2e.ts"; |
| 5 | +import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts"; |
| 6 | + |
| 7 | +const suite = createControlUiE2eSuite({ |
| 8 | + browserLaunchOptions: { ignoreDefaultArgs: ["--hide-scrollbars"] }, |
| 9 | + name: "Control UI composer capability menu height", |
| 10 | +}); |
| 11 | + |
| 12 | +function skill(index: number) { |
| 13 | + const name = `Skill ${String(index).padStart(2, "0")}`; |
| 14 | + return { |
| 15 | + name, |
| 16 | + description: `${name} skill`, |
| 17 | + source: "test", |
| 18 | + filePath: `/tmp/openclaw-e2e/skills/${name}/SKILL.md`, |
| 19 | + baseDir: `/tmp/openclaw-e2e/skills/${name}`, |
| 20 | + skillKey: name.toLowerCase().replaceAll(" ", "-"), |
| 21 | + always: false, |
| 22 | + disabled: false, |
| 23 | + blockedByAllowlist: false, |
| 24 | + eligible: true, |
| 25 | + requirements: { anyBins: [], bins: [], env: [], config: [], os: [] }, |
| 26 | + missing: { anyBins: [], bins: [], env: [], config: [], os: [] }, |
| 27 | + configChecks: [], |
| 28 | + install: [], |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +function sessionsList() { |
| 33 | + return { |
| 34 | + count: 1, |
| 35 | + defaults: { contextTokens: 200_000, model: "gpt-5.5", modelProvider: "openai" }, |
| 36 | + path: "", |
| 37 | + sessions: [ |
| 38 | + { |
| 39 | + key: "main", |
| 40 | + kind: "direct", |
| 41 | + model: "gpt-5.5", |
| 42 | + modelProvider: "openai", |
| 43 | + status: "done", |
| 44 | + updatedAt: Date.now(), |
| 45 | + }, |
| 46 | + ], |
| 47 | + ts: Date.now(), |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +function configResponse() { |
| 52 | + const servers = Object.fromEntries( |
| 53 | + Array.from({ length: 24 }, (_, index) => [ |
| 54 | + `connector-${String(index).padStart(2, "0")}`, |
| 55 | + { enabled: true, url: `https://connector-${index}.example.test` }, |
| 56 | + ]), |
| 57 | + ); |
| 58 | + const config = { mcp: { servers }, tools: { web: { search: { enabled: false } } } }; |
| 59 | + return { |
| 60 | + raw: JSON.stringify(config), |
| 61 | + hash: "capability-menu-height-config", |
| 62 | + sourceConfig: config, |
| 63 | + runtimeConfig: config, |
| 64 | + config, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function toolsEffectiveResponse() { |
| 69 | + return { |
| 70 | + agentId: "main", |
| 71 | + profile: "full", |
| 72 | + groups: [ |
| 73 | + { |
| 74 | + id: "mcp", |
| 75 | + label: "MCP", |
| 76 | + source: "mcp", |
| 77 | + tools: Array.from({ length: 28 }, (_, index) => { |
| 78 | + const number = String(index + 1).padStart(2, "0"); |
| 79 | + return { |
| 80 | + id: `mcp_connector_00_tool_${number}`, |
| 81 | + label: `Project tool ${number}`, |
| 82 | + description: `Operate on project resource ${number}`, |
| 83 | + rawDescription: `Operate on project resource ${number}`, |
| 84 | + source: "mcp", |
| 85 | + mcpServer: "connector-00", |
| 86 | + mcpToolName: `project-tool-${number}`, |
| 87 | + }; |
| 88 | + }), |
| 89 | + }, |
| 90 | + ], |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +suite.define(() => { |
| 95 | + it("caps every long capability view at 420px and keeps keyboard focus visible", async () => { |
| 96 | + await suite.withPage({ viewport: { width: 1280, height: 900 } }, async ({ page }) => { |
| 97 | + const gateway = await installMockGateway(page, { |
| 98 | + featureMethods: ["chat.metadata", "chat.startup", "sessions.patch", "tools.effective"], |
| 99 | + historyMessages: [ |
| 100 | + { |
| 101 | + role: "user", |
| 102 | + content: [ |
| 103 | + { |
| 104 | + type: "text", |
| 105 | + text: "Review the release dashboard and flag anything that needs attention.", |
| 106 | + }, |
| 107 | + ], |
| 108 | + timestamp: Date.now() - 60_000, |
| 109 | + }, |
| 110 | + { |
| 111 | + role: "assistant", |
| 112 | + content: [ |
| 113 | + { |
| 114 | + type: "text", |
| 115 | + text: "The rollout is healthy. I found two follow-ups:\n\n- Confirm the mobile smoke lane\n- Review the connector permission changes", |
| 116 | + }, |
| 117 | + ], |
| 118 | + timestamp: Date.now() - 30_000, |
| 119 | + }, |
| 120 | + ], |
| 121 | + methodResponses: { |
| 122 | + "config.get": configResponse(), |
| 123 | + "sessions.list": sessionsList(), |
| 124 | + "skills.status": { |
| 125 | + workspaceDir: "/tmp/openclaw-e2e/workspace", |
| 126 | + managedSkillsDir: "/tmp/openclaw-e2e/skills", |
| 127 | + skills: Array.from({ length: 36 }, (_, index) => skill(index + 1)), |
| 128 | + }, |
| 129 | + "tools.effective": toolsEffectiveResponse(), |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + await page.goto(`${suite.server.baseUrl}chat`); |
| 134 | + await gateway.waitForRequest("chat.startup"); |
| 135 | + const composer = page.locator(".agent-chat__input"); |
| 136 | + await expect.poll(() => composer.isVisible()).toBe(true); |
| 137 | + const dropdown = composer.locator("wa-dropdown.agent-chat__capability-menu"); |
| 138 | + const attach = composer.locator("button.agent-chat__input-btn--attach"); |
| 139 | + await expect.poll(() => attach.isVisible()).toBe(true); |
| 140 | + await attach.click(); |
| 141 | + await dropdown.locator('[value="open-skills"]').click(); |
| 142 | + await expect.poll(() => dropdown.getAttribute("data-view")).toBe("skills"); |
| 143 | + |
| 144 | + const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim(); |
| 145 | + const captureStage = process.env.OPENCLAW_UI_E2E_CAPTURE_STAGE?.trim(); |
| 146 | + const capture = async (view: string, theme: "dark" | "light") => { |
| 147 | + if (!artifactDir || !captureStage) { |
| 148 | + return; |
| 149 | + } |
| 150 | + await fs.mkdir(artifactDir, { recursive: true }); |
| 151 | + await page.evaluate((mode) => { |
| 152 | + document.documentElement.dataset.themeMode = mode; |
| 153 | + }, theme); |
| 154 | + await page.waitForTimeout(50); |
| 155 | + const menuCenter = await dropdown.evaluate((node) => { |
| 156 | + const menu = node.shadowRoot?.querySelector<HTMLElement>('[part="menu"]'); |
| 157 | + if (!menu) { |
| 158 | + throw new Error("expected capability menu bounds"); |
| 159 | + } |
| 160 | + const rect = menu.getBoundingClientRect(); |
| 161 | + return { x: rect.right - 8, y: rect.top + rect.height / 2 }; |
| 162 | + }); |
| 163 | + await page.mouse.move(menuCenter.x, menuCenter.y); |
| 164 | + await page.mouse.wheel(0, 1); |
| 165 | + await page.screenshot({ |
| 166 | + path: path.join(artifactDir, `${view}-${theme}-${captureStage}.png`), |
| 167 | + }); |
| 168 | + }; |
| 169 | + |
| 170 | + const inspectView = async (view: string) => { |
| 171 | + const layout = await dropdown.evaluate((node) => { |
| 172 | + const menu = node.shadowRoot?.querySelector<HTMLElement>('[part="menu"]'); |
| 173 | + const composerElement = node.closest<HTMLElement>(".agent-chat__input"); |
| 174 | + const back = node.querySelector<HTMLElement>('[value="back"]'); |
| 175 | + if (!menu || !composerElement || !back) { |
| 176 | + throw new Error("expected capability menu layout"); |
| 177 | + } |
| 178 | + menu.scrollTop = Math.floor((menu.scrollHeight - menu.clientHeight) / 2); |
| 179 | + const menuRect = menu.getBoundingClientRect(); |
| 180 | + const backRect = back.getBoundingClientRect(); |
| 181 | + const style = getComputedStyle(menu); |
| 182 | + return { |
| 183 | + backOffset: backRect.top - menuRect.top, |
| 184 | + clientHeight: menu.clientHeight, |
| 185 | + maxHeight: Number.parseFloat(style.maxHeight), |
| 186 | + overscrollY: style.overscrollBehaviorY, |
| 187 | + scrollHeight: menu.scrollHeight, |
| 188 | + scrollTop: menu.scrollTop, |
| 189 | + token: Number.parseFloat( |
| 190 | + getComputedStyle(composerElement).getPropertyValue( |
| 191 | + "--chat-composer-popover-max-height", |
| 192 | + ), |
| 193 | + ), |
| 194 | + viewportHeight: window.innerHeight, |
| 195 | + }; |
| 196 | + }); |
| 197 | + await capture(view, "dark"); |
| 198 | + await capture(view, "light"); |
| 199 | + return { ...layout, view }; |
| 200 | + }; |
| 201 | + |
| 202 | + const layouts = [await inspectView("skills")]; |
| 203 | + |
| 204 | + const back = dropdown.locator('[value="back"]'); |
| 205 | + const interactionTarget = dropdown.locator('[value="skill:19"]'); |
| 206 | + const captureInteraction = async (state: "focus" | "hover", theme: "dark" | "light") => { |
| 207 | + await page.evaluate((mode) => { |
| 208 | + document.documentElement.dataset.themeMode = mode; |
| 209 | + }, theme); |
| 210 | + await interactionTarget.scrollIntoViewIfNeeded(); |
| 211 | + |
| 212 | + if (state === "hover") { |
| 213 | + await interactionTarget.hover(); |
| 214 | + await expect |
| 215 | + .poll(() => interactionTarget.evaluate((node) => node.matches(":hover"))) |
| 216 | + .toBe(true); |
| 217 | + } else { |
| 218 | + await back.focus(); |
| 219 | + await page.keyboard.press("Home"); |
| 220 | + for (let index = 0; index < 20; index += 1) { |
| 221 | + await page.keyboard.press("ArrowDown"); |
| 222 | + } |
| 223 | + await page.mouse.move(900, 500); |
| 224 | + await expect |
| 225 | + .poll(() => |
| 226 | + interactionTarget.evaluate( |
| 227 | + (node) => document.activeElement === node && node.matches(":focus-visible"), |
| 228 | + ), |
| 229 | + ) |
| 230 | + .toBe(true); |
| 231 | + } |
| 232 | + |
| 233 | + await expect |
| 234 | + .poll(() => |
| 235 | + dropdown.evaluate((node, target) => { |
| 236 | + const menu = node.shadowRoot?.querySelector<HTMLElement>('[part="menu"]'); |
| 237 | + const backRow = node.querySelector<HTMLElement>('[value="back"]'); |
| 238 | + const targetRow = node.querySelector<HTMLElement>(target); |
| 239 | + if (!menu || !backRow || !targetRow) { |
| 240 | + return false; |
| 241 | + } |
| 242 | + const menuRect = menu.getBoundingClientRect(); |
| 243 | + const backRect = backRow.getBoundingClientRect(); |
| 244 | + const targetRect = targetRow.getBoundingClientRect(); |
| 245 | + return ( |
| 246 | + menu.scrollTop > 0 && |
| 247 | + menu.scrollHeight > menu.clientHeight && |
| 248 | + backRect.top >= menuRect.top && |
| 249 | + backRect.bottom <= menuRect.bottom && |
| 250 | + targetRect.top >= menuRect.top && |
| 251 | + targetRect.bottom <= menuRect.bottom |
| 252 | + ); |
| 253 | + }, '[value="skill:19"]'), |
| 254 | + ) |
| 255 | + .toBe(true); |
| 256 | + |
| 257 | + await expect |
| 258 | + .poll(() => |
| 259 | + interactionTarget.evaluate((node) => { |
| 260 | + const probe = document.createElement("div"); |
| 261 | + probe.style.backgroundColor = "var(--bg-hover)"; |
| 262 | + probe.style.color = "var(--text)"; |
| 263 | + document.body.append(probe); |
| 264 | + const rowStyle = getComputedStyle(node); |
| 265 | + const probeStyle = getComputedStyle(probe); |
| 266 | + const matches = |
| 267 | + rowStyle.backgroundColor === probeStyle.backgroundColor && |
| 268 | + rowStyle.color === probeStyle.color; |
| 269 | + probe.remove(); |
| 270 | + return matches; |
| 271 | + }), |
| 272 | + ) |
| 273 | + .toBe(true); |
| 274 | + |
| 275 | + if (artifactDir && captureStage === "after") { |
| 276 | + await page.waitForTimeout(50); |
| 277 | + await page.screenshot({ |
| 278 | + path: path.join(artifactDir, `skill-20-${state}-${theme}-after.png`), |
| 279 | + }); |
| 280 | + } |
| 281 | + }; |
| 282 | + |
| 283 | + await captureInteraction("hover", "dark"); |
| 284 | + await captureInteraction("hover", "light"); |
| 285 | + await captureInteraction("focus", "dark"); |
| 286 | + await captureInteraction("focus", "light"); |
| 287 | + |
| 288 | + await back.click(); |
| 289 | + await dropdown.locator('[value="open-connectors"]').click(); |
| 290 | + await expect.poll(() => dropdown.getAttribute("data-view")).toBe("connectors"); |
| 291 | + layouts.push(await inspectView("connectors")); |
| 292 | + |
| 293 | + await dropdown.locator('[value="tools:0"]').click(); |
| 294 | + await expect.poll(() => dropdown.getAttribute("data-view")).toBe("tools:connector-00"); |
| 295 | + await expect.poll(() => dropdown.getByText("28 of 28 tools on").isVisible()).toBe(true); |
| 296 | + layouts.push(await inspectView("tool-access")); |
| 297 | + |
| 298 | + if (artifactDir && captureStage) { |
| 299 | + await fs.writeFile( |
| 300 | + path.join(artifactDir, `${captureStage}-layout.json`), |
| 301 | + `${JSON.stringify(layouts, null, 2)}\n`, |
| 302 | + ); |
| 303 | + } |
| 304 | + |
| 305 | + for (const layout of layouts) { |
| 306 | + const compactHeightCap = Math.min(layout.token, 420, layout.viewportHeight * 0.5); |
| 307 | + expect(compactHeightCap).toBe(420); |
| 308 | + expect(layout.scrollHeight).toBeGreaterThan(layout.clientHeight); |
| 309 | + expect(layout.scrollTop).toBeGreaterThan(0); |
| 310 | + expect(layout.maxHeight).toBeGreaterThanOrEqual(compactHeightCap - 1); |
| 311 | + expect(layout.maxHeight).toBeLessThanOrEqual(compactHeightCap + 1); |
| 312 | + expect(layout.clientHeight).toBeLessThanOrEqual(compactHeightCap + 1); |
| 313 | + expect(layout.overscrollY).toBe("contain"); |
| 314 | + expect(layout.backOffset).toBeGreaterThanOrEqual(0); |
| 315 | + expect(layout.backOffset).toBeLessThanOrEqual(1); |
| 316 | + } |
| 317 | + }); |
| 318 | + }); |
| 319 | +}); |
0 commit comments