|
| 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 | + name: "Control UI composer accessory focus", |
| 9 | +}); |
| 10 | + |
| 11 | +suite.define(() => { |
| 12 | + it("keeps focus in place when pointer-opening passive composer popovers", async () => { |
| 13 | + await suite.withPage({ viewport: { width: 1440, height: 900 } }, async ({ page }) => { |
| 14 | + const gateway = await installMockGateway(page, { |
| 15 | + models: [{ id: "gpt-5.6", name: "GPT-5.6", provider: "openai" }], |
| 16 | + methodResponses: { |
| 17 | + "sessions.list": { |
| 18 | + count: 1, |
| 19 | + defaults: { |
| 20 | + contextTokens: 200_000, |
| 21 | + model: "gpt-5.6", |
| 22 | + modelProvider: "openai", |
| 23 | + thinkingDefault: "medium", |
| 24 | + thinkingLevels: [ |
| 25 | + { id: "off", label: "off" }, |
| 26 | + { id: "low", label: "low" }, |
| 27 | + { id: "medium", label: "medium" }, |
| 28 | + { id: "high", label: "high" }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + path: "", |
| 32 | + sessions: [ |
| 33 | + { |
| 34 | + contextTokens: 200_000, |
| 35 | + key: "main", |
| 36 | + kind: "direct", |
| 37 | + model: "gpt-5.6", |
| 38 | + modelProvider: "openai", |
| 39 | + status: "done", |
| 40 | + totalTokens: 42_000, |
| 41 | + totalTokensFresh: true, |
| 42 | + updatedAt: Date.now(), |
| 43 | + }, |
| 44 | + ], |
| 45 | + ts: Date.now(), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }); |
| 49 | + await page.goto(`${suite.server.baseUrl}chat`); |
| 50 | + await gateway.waitForRequest("chat.startup"); |
| 51 | + |
| 52 | + const composer = page.locator(".agent-chat__input"); |
| 53 | + const textarea = composer.locator("textarea"); |
| 54 | + await composer.waitFor({ state: "visible" }); |
| 55 | + await page.evaluate(() => { |
| 56 | + const outside = document.createElement("button"); |
| 57 | + outside.id = "composer-accessory-focus-sentinel"; |
| 58 | + outside.textContent = "Outside focus sentinel"; |
| 59 | + document.body.prepend(outside); |
| 60 | + }); |
| 61 | + const outside = page.locator("#composer-accessory-focus-sentinel"); |
| 62 | + |
| 63 | + for (const triggerSelector of [ |
| 64 | + ".context-usage > details > summary", |
| 65 | + ".chat-controls__effort-picker > summary", |
| 66 | + ]) { |
| 67 | + const trigger = composer.locator(triggerSelector); |
| 68 | + await trigger.waitFor({ state: "visible" }); |
| 69 | + |
| 70 | + await outside.focus(); |
| 71 | + await trigger.click(); |
| 72 | + expect(await outside.evaluate((element) => document.activeElement === element)).toBe(true); |
| 73 | + const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim(); |
| 74 | + if (artifactDir && triggerSelector.startsWith(".context-usage")) { |
| 75 | + await fs.mkdir(artifactDir, { recursive: true }); |
| 76 | + const composerBox = await composer.boundingBox(); |
| 77 | + const popoverBox = await composer.locator(".context-usage__popover").boundingBox(); |
| 78 | + if (!composerBox || !popoverBox) { |
| 79 | + throw new Error("expected composer and context popover bounds for focus proof"); |
| 80 | + } |
| 81 | + const y = Math.max(0, popoverBox.y - 16); |
| 82 | + const clip = { |
| 83 | + x: Math.max(0, composerBox.x - 16), |
| 84 | + y, |
| 85 | + width: composerBox.width + 32, |
| 86 | + height: composerBox.y + composerBox.height + 16 - y, |
| 87 | + }; |
| 88 | + await page.screenshot({ |
| 89 | + path: path.join(artifactDir, "after-context-click-no-focus.png"), |
| 90 | + clip, |
| 91 | + }); |
| 92 | + await trigger.focus(); |
| 93 | + await page.screenshot({ |
| 94 | + path: path.join(artifactDir, "before-context-trigger-focused.png"), |
| 95 | + clip, |
| 96 | + }); |
| 97 | + await outside.focus(); |
| 98 | + } |
| 99 | + await trigger.click(); |
| 100 | + |
| 101 | + await textarea.focus(); |
| 102 | + await trigger.click(); |
| 103 | + expect(await textarea.evaluate((element) => document.activeElement === element)).toBe(true); |
| 104 | + await trigger.click(); |
| 105 | + |
| 106 | + await trigger.focus(); |
| 107 | + await trigger.press("Enter"); |
| 108 | + expect(await trigger.evaluate((element) => document.activeElement === element)).toBe(true); |
| 109 | + expect( |
| 110 | + await trigger.evaluate( |
| 111 | + (element) => element.closest<HTMLDetailsElement>("details")?.open ?? false, |
| 112 | + ), |
| 113 | + ).toBe(true); |
| 114 | + await trigger.press("Enter"); |
| 115 | + } |
| 116 | + |
| 117 | + for (const popover of [ |
| 118 | + { |
| 119 | + focus: ".chat-controls__model-search", |
| 120 | + trigger: ".chat-controls__model-picker > summary", |
| 121 | + }, |
| 122 | + { |
| 123 | + focus: ".agent-chat__attach-menu-option", |
| 124 | + trigger: ".agent-chat__input-btn--attach", |
| 125 | + }, |
| 126 | + ]) { |
| 127 | + await outside.focus(); |
| 128 | + await composer.locator(popover.trigger).click(); |
| 129 | + await expect |
| 130 | + .poll(() => |
| 131 | + page |
| 132 | + .locator(popover.focus) |
| 133 | + .first() |
| 134 | + .evaluate((element) => document.activeElement === element), |
| 135 | + ) |
| 136 | + .toBe(true); |
| 137 | + await page.keyboard.press("Escape"); |
| 138 | + } |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments