|
| 1 | +// Control UI tests cover contextual scrollbars and native-style text selection. |
| 2 | +import { mkdir } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { chromium, type Browser, type Locator, type Page } from "playwright"; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + canRunPlaywrightChromium, |
| 8 | + installMockGateway, |
| 9 | + resolvePlaywrightChromiumExecutablePath, |
| 10 | + startControlUiE2eServer, |
| 11 | + type ControlUiE2eServer, |
| 12 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 13 | + |
| 14 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 15 | +const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath); |
| 16 | +const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1"; |
| 17 | +const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip; |
| 18 | +const captureUiProofEnabled = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1"; |
| 19 | +const uiProofArtifactDir = path.join( |
| 20 | + process.cwd(), |
| 21 | + ".artifacts", |
| 22 | + "control-ui-e2e", |
| 23 | + "app-chrome-interaction", |
| 24 | +); |
| 25 | + |
| 26 | +let browser: Browser; |
| 27 | +let server: ControlUiE2eServer; |
| 28 | + |
| 29 | +async function dragAcross(page: Page, locator: Locator): Promise<string> { |
| 30 | + await locator.scrollIntoViewIfNeeded(); |
| 31 | + const box = await locator.boundingBox(); |
| 32 | + if (!box) { |
| 33 | + throw new Error("Expected a visible text-selection target"); |
| 34 | + } |
| 35 | + const y = box.y + box.height / 2; |
| 36 | + await page.mouse.move(box.x + 2, y); |
| 37 | + await page.mouse.down(); |
| 38 | + await page.mouse.move(box.x + Math.max(3, box.width - 2), y, { steps: 8 }); |
| 39 | + await page.mouse.up(); |
| 40 | + return page.evaluate(() => globalThis.getSelection()?.toString() ?? ""); |
| 41 | +} |
| 42 | + |
| 43 | +async function captureUiProof(page: Page, fileName: string) { |
| 44 | + if (!captureUiProofEnabled) { |
| 45 | + return; |
| 46 | + } |
| 47 | + await mkdir(uiProofArtifactDir, { recursive: true }); |
| 48 | + await page.screenshot({ |
| 49 | + animations: "disabled", |
| 50 | + path: path.join(uiProofArtifactDir, fileName), |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +describeControlUiE2e("Control UI app chrome interaction mocked Gateway E2E", () => { |
| 55 | + beforeAll(async () => { |
| 56 | + if (!chromiumAvailable) { |
| 57 | + throw new Error( |
| 58 | + `Playwright Chromium is not installed or cannot start at ${chromiumExecutablePath}. Run \`pnpm --dir ui exec playwright install --with-deps chromium\`, or set OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM=1 only when intentionally skipping this lane.`, |
| 59 | + ); |
| 60 | + } |
| 61 | + server = await startControlUiE2eServer(); |
| 62 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterAll(async () => { |
| 66 | + await browser?.close(); |
| 67 | + await server?.close(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("uses compact sidebar scrollbars and keeps selection in chat and inputs", async () => { |
| 71 | + if (captureUiProofEnabled) { |
| 72 | + await mkdir(uiProofArtifactDir, { recursive: true }); |
| 73 | + } |
| 74 | + const context = await browser.newContext({ |
| 75 | + locale: "en-US", |
| 76 | + recordVideo: captureUiProofEnabled |
| 77 | + ? { dir: path.join(uiProofArtifactDir, "video"), size: { height: 900, width: 1440 } } |
| 78 | + : undefined, |
| 79 | + serviceWorkers: "block", |
| 80 | + viewport: { height: 900, width: 1440 }, |
| 81 | + }); |
| 82 | + const page = await context.newPage(); |
| 83 | + await installMockGateway(page, { |
| 84 | + historyMessages: [ |
| 85 | + { |
| 86 | + role: "assistant", |
| 87 | + content: [{ type: "text", text: "Selectable transcript content" }], |
| 88 | + }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + |
| 92 | + try { |
| 93 | + await page.goto(`${server.baseUrl}chat`); |
| 94 | + const transcript = page.getByText("Selectable transcript content", { exact: true }); |
| 95 | + await transcript.waitFor(); |
| 96 | + const chatStyles = await page.evaluate(() => { |
| 97 | + const sidebar = document.querySelector<HTMLElement>(".sidebar"); |
| 98 | + const sessions = document.querySelector<HTMLElement>(".sidebar-recent-sessions"); |
| 99 | + const thread = document.querySelector<HTMLElement>(".chat-thread"); |
| 100 | + if (!sidebar || !sessions || !thread) { |
| 101 | + throw new Error("Missing chat interaction surface"); |
| 102 | + } |
| 103 | + return { |
| 104 | + chatSelection: getComputedStyle(thread).userSelect, |
| 105 | + chatScrollbar: getComputedStyle(thread, "::-webkit-scrollbar").width, |
| 106 | + sidebarSelection: getComputedStyle(sidebar).userSelect, |
| 107 | + sidebarScrollbar: getComputedStyle(sessions, "::-webkit-scrollbar").width, |
| 108 | + }; |
| 109 | + }); |
| 110 | + expect(chatStyles).toEqual({ |
| 111 | + chatSelection: "text", |
| 112 | + chatScrollbar: "12px", |
| 113 | + sidebarSelection: "none", |
| 114 | + sidebarScrollbar: "6px", |
| 115 | + }); |
| 116 | + expect(await dragAcross(page, transcript)).toContain("Selectable transcript"); |
| 117 | + await captureUiProof(page, "01-chat-selectable-transcript.png"); |
| 118 | + |
| 119 | + await page.goto(`${server.baseUrl}settings/general`); |
| 120 | + const settingsSidebar = page.locator(".settings-sidebar"); |
| 121 | + const settingsTitle = settingsSidebar.locator(".settings-sidebar__title"); |
| 122 | + const settingsSearch = settingsSidebar.locator(".settings-sidebar__search-input"); |
| 123 | + const content = page.locator(".content"); |
| 124 | + await settingsSidebar.waitFor(); |
| 125 | + await expect |
| 126 | + .poll(() => content.evaluate((element) => element.scrollHeight)) |
| 127 | + .toBeGreaterThan(await content.evaluate((element) => element.clientHeight)); |
| 128 | + |
| 129 | + const settingsStyles = await page.evaluate(() => { |
| 130 | + const contentNode = document.querySelector<HTMLElement>(".content"); |
| 131 | + const nav = document.querySelector<HTMLElement>(".settings-sidebar__nav"); |
| 132 | + const search = document.querySelector<HTMLElement>(".settings-sidebar__search-input"); |
| 133 | + const sidebar = document.querySelector<HTMLElement>(".settings-sidebar"); |
| 134 | + if (!contentNode || !nav || !search || !sidebar) { |
| 135 | + throw new Error("Missing settings interaction surface"); |
| 136 | + } |
| 137 | + return { |
| 138 | + contentScrollbar: getComputedStyle(contentNode, "::-webkit-scrollbar").width, |
| 139 | + contentSelection: getComputedStyle(contentNode).userSelect, |
| 140 | + inputSelection: getComputedStyle(search).userSelect, |
| 141 | + sidebarScrollbar: getComputedStyle(nav, "::-webkit-scrollbar").width, |
| 142 | + sidebarSelection: getComputedStyle(sidebar).userSelect, |
| 143 | + }; |
| 144 | + }); |
| 145 | + expect(settingsStyles).toEqual({ |
| 146 | + contentScrollbar: "12px", |
| 147 | + contentSelection: "none", |
| 148 | + inputSelection: "text", |
| 149 | + sidebarScrollbar: "6px", |
| 150 | + sidebarSelection: "none", |
| 151 | + }); |
| 152 | + |
| 153 | + await page.evaluate(() => globalThis.getSelection()?.removeAllRanges()); |
| 154 | + expect(await dragAcross(page, settingsTitle)).toBe(""); |
| 155 | + await settingsSearch.selectText(); |
| 156 | + expect( |
| 157 | + await settingsSearch.evaluate( |
| 158 | + (element) => |
| 159 | + element instanceof HTMLInputElement && |
| 160 | + element.selectionStart === 0 && |
| 161 | + element.selectionEnd === element.value.length, |
| 162 | + ), |
| 163 | + ).toBe(true); |
| 164 | + await content.evaluate((element) => { |
| 165 | + element.scrollTop = Math.min(160, element.scrollHeight - element.clientHeight); |
| 166 | + }); |
| 167 | + await captureUiProof(page, "02-settings-contextual-scrollbars.png"); |
| 168 | + } finally { |
| 169 | + await context.close(); |
| 170 | + } |
| 171 | + }); |
| 172 | +}); |
0 commit comments