|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { act, type ComponentProps } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +vi.mock("~/hooks/useSettings", () => ({ |
| 8 | + useEnvironmentIdentificationMode: () => "none", |
| 9 | +})); |
| 10 | +vi.mock("../SidebarStageBackdrop", () => ({ |
| 11 | + StageBackdropButtonArt: () => null, |
| 12 | + useSidebarStageBackdropVariant: () => null, |
| 13 | +})); |
| 14 | + |
| 15 | +import { ComposerPrimaryActions } from "./ComposerPrimaryActions"; |
| 16 | + |
| 17 | +const baseProps: ComponentProps<typeof ComposerPrimaryActions> = { |
| 18 | + compact: false, |
| 19 | + canOperateThread: true, |
| 20 | + pendingAction: null, |
| 21 | + isRunning: false, |
| 22 | + canInterrupt: false, |
| 23 | + showPlanFollowUpPrompt: false, |
| 24 | + promptHasText: true, |
| 25 | + isSendBusy: false, |
| 26 | + sendDisabledReason: null, |
| 27 | + isConnecting: false, |
| 28 | + isEnvironmentUnavailable: false, |
| 29 | + isPreparingWorktree: false, |
| 30 | + hasSendableContent: true, |
| 31 | + preserveComposerFocusOnPointerDown: true, |
| 32 | + onPreviousPendingQuestion: () => {}, |
| 33 | + onInterrupt: () => {}, |
| 34 | + onImplementPlanInNewThread: () => {}, |
| 35 | +}; |
| 36 | + |
| 37 | +// Chromium skips the compatibility mouse events after a canceled pointerdown |
| 38 | +// but still fires click. jsdom does not, so the press is spelled out here. |
| 39 | +function pressWithMouse(target: Element) { |
| 40 | + const init = { bubbles: true, cancelable: true, button: 0 }; |
| 41 | + const pointerDown = new MouseEvent("pointerdown", init); |
| 42 | + Object.defineProperty(pointerDown, "pointerType", { value: "mouse" }); |
| 43 | + target.dispatchEvent(pointerDown); |
| 44 | + if (!pointerDown.defaultPrevented) target.dispatchEvent(new MouseEvent("mousedown", init)); |
| 45 | + const pointerUp = new MouseEvent("pointerup", init); |
| 46 | + Object.defineProperty(pointerUp, "pointerType", { value: "mouse" }); |
| 47 | + target.dispatchEvent(pointerUp); |
| 48 | + if (!pointerDown.defaultPrevented) target.dispatchEvent(new MouseEvent("mouseup", init)); |
| 49 | + target.dispatchEvent(new MouseEvent("click", init)); |
| 50 | +} |
| 51 | + |
| 52 | +it.each([ |
| 53 | + { |
| 54 | + trigger: "Send options", |
| 55 | + item: "Send with full history (180k tokens)", |
| 56 | + props: { compactBeforeSendTokens: 180_000, onSendWithFullHistory: vi.fn() }, |
| 57 | + handler: "onSendWithFullHistory", |
| 58 | + }, |
| 59 | + { |
| 60 | + trigger: "Implementation actions", |
| 61 | + item: "Implement in a new thread", |
| 62 | + props: { |
| 63 | + showPlanFollowUpPrompt: true, |
| 64 | + promptHasText: false, |
| 65 | + hasSendableContent: false, |
| 66 | + onImplementPlanInNewThread: vi.fn(), |
| 67 | + }, |
| 68 | + handler: "onImplementPlanInNewThread", |
| 69 | + }, |
| 70 | +] as const)( |
| 71 | + "opens the $trigger menu with a mouse while the composer is resting", |
| 72 | + async ({ trigger, item, props, handler }) => { |
| 73 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 74 | + const container = document.createElement("div"); |
| 75 | + document.body.append(container); |
| 76 | + const root = createRoot(container); |
| 77 | + try { |
| 78 | + await act(async () => root.render(<ComposerPrimaryActions {...baseProps} {...props} />)); |
| 79 | + const button = container.querySelector(`[aria-label="${trigger}"]`)!; |
| 80 | + await act(async () => pressWithMouse(button)); |
| 81 | + // Base UI opens a mousedown-triggered menu on the next animation frame. |
| 82 | + await act(async () => new Promise((resolve) => requestAnimationFrame(resolve))); |
| 83 | + const menuItem = Array.from(document.querySelectorAll<HTMLElement>('[role="menuitem"]')).find( |
| 84 | + (element) => element.textContent === item, |
| 85 | + ); |
| 86 | + expect(menuItem).toBeDefined(); |
| 87 | + await act(async () => menuItem!.click()); |
| 88 | + expect(props[handler as keyof typeof props]).toHaveBeenCalledTimes(1); |
| 89 | + } finally { |
| 90 | + await act(async () => root.unmount()); |
| 91 | + container.remove(); |
| 92 | + vi.unstubAllGlobals(); |
| 93 | + } |
| 94 | + }, |
| 95 | +); |
0 commit comments