|
| 1 | +import type WaTooltip from "@awesome.me/webawesome/dist/components/tooltip/tooltip.js"; |
| 2 | +import { render } from "lit"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { installTitleTooltips } from "../../../components/tooltip-title.ts"; |
| 5 | +import { i18n } from "../../../i18n/index.ts"; |
| 6 | +import "../../../styles.css"; |
| 7 | +import "../../../styles/chat/split-view.css"; |
| 8 | +import { mountChatPaneHeader } from "./chat-pane-header.test-support.ts"; |
| 9 | +import { renderChatSidebarEditorMenu } from "./chat-sidebar-editor-menu.ts"; |
| 10 | + |
| 11 | +describe.skipIf(typeof HTMLElement.prototype.checkVisibility !== "function")( |
| 12 | + "chat pane branch tooltip positioning", |
| 13 | + () => { |
| 14 | + const containers: HTMLElement[] = []; |
| 15 | + let dispose: () => void; |
| 16 | + let page: (typeof import("vitest/browser"))["page"]; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + ({ page } = await import("vitest/browser")); |
| 20 | + await page.viewport(1200, 800); |
| 21 | + await i18n.setLocale("en"); |
| 22 | + dispose = installTitleTooltips(document); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + dispose(); |
| 27 | + containers.splice(0).forEach((container) => container.remove()); |
| 28 | + }); |
| 29 | + |
| 30 | + it.each(["idle", "busy", "editor"] as const)( |
| 31 | + "anchors the %s hint to its menu button", |
| 32 | + async (state) => { |
| 33 | + const busy = state === "busy"; |
| 34 | + const editor = state === "editor"; |
| 35 | + const onOpenEditor = vi.fn(); |
| 36 | + const reason = "Branch switch is unavailable while the agent is working."; |
| 37 | + const { container, props } = mountChatPaneHeader(containers, { |
| 38 | + branchSwitchDisabledReason: busy ? reason : null, |
| 39 | + branches: [ |
| 40 | + { leafEntryId: "active", headline: "Current work", messageCount: 4, active: true }, |
| 41 | + { leafEntryId: "other", headline: "Earlier idea", messageCount: 2, active: false }, |
| 42 | + ], |
| 43 | + }); |
| 44 | + if (editor) { |
| 45 | + render( |
| 46 | + renderChatSidebarEditorMenu({ |
| 47 | + absolutePath: "/repo/example.ts", |
| 48 | + open: false, |
| 49 | + onOpenChange: () => undefined, |
| 50 | + onOpenEditor, |
| 51 | + }), |
| 52 | + container, |
| 53 | + ); |
| 54 | + } |
| 55 | + container.style.cssText = "position: fixed; top: 80px; left: 500px; width: 650px"; |
| 56 | + const trigger = container.querySelector<HTMLButtonElement>( |
| 57 | + editor ? ".sidebar-file-view__action" : ".chat-pane__branches-trigger", |
| 58 | + )!; |
| 59 | + await page.elementLocator(trigger).hover(); |
| 60 | + const tooltip = () => |
| 61 | + [...document.querySelectorAll("openclaw-tooltip")] |
| 62 | + .map((element) => element.shadowRoot?.querySelector<WaTooltip>("wa-tooltip")) |
| 63 | + .find((element) => element?.open); |
| 64 | + await expect |
| 65 | + .poll(() => tooltip()?.textContent) |
| 66 | + .toContain(editor ? "Open in editor" : busy ? reason : "Session branches"); |
| 67 | + await expect |
| 68 | + .poll(() => { |
| 69 | + const body = tooltip()?.shadowRoot?.querySelector<HTMLElement>('[part="body"]'); |
| 70 | + if (!body) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + const hint = body.getBoundingClientRect(); |
| 74 | + const button = trigger.getBoundingClientRect(); |
| 75 | + return ( |
| 76 | + hint.width > 0 && |
| 77 | + hint.left < button.right && |
| 78 | + hint.right > button.left && |
| 79 | + Math.min(Math.abs(hint.bottom - button.top), Math.abs(hint.top - button.bottom)) < 24 |
| 80 | + ); |
| 81 | + }) |
| 82 | + .toBe(true); |
| 83 | + |
| 84 | + if (!busy) { |
| 85 | + await page.elementLocator(trigger).click(); |
| 86 | + await expect.poll(() => tooltip()).toBeUndefined(); |
| 87 | + await page.getByRole("menuitem", { name: editor ? "VS Code" : /Earlier idea/ }).click(); |
| 88 | + expect(editor ? onOpenEditor : props.onBranchSelect).toHaveBeenCalledWith( |
| 89 | + editor ? "vscode" : "other", |
| 90 | + ); |
| 91 | + } |
| 92 | + }, |
| 93 | + ); |
| 94 | + }, |
| 95 | +); |
0 commit comments