|
| 1 | +import type { ReactNode, RefObject } from "react"; |
| 2 | +import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"; |
| 3 | +import { cleanup, renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import { GlobalWindow } from "happy-dom"; |
| 5 | +import type { ChatInputAPI } from "@/browser/features/ChatInput"; |
| 6 | +import { APIProvider, type APIClient } from "@/browser/contexts/API"; |
| 7 | +import { createTestApiClient, type TestApiOverrides } from "@/browser/testUtils"; |
| 8 | +import { useAIViewKeybinds } from "./useAIViewKeybinds"; |
| 9 | +import { useEscapeToDismiss } from "./useEscapeToDismiss"; |
| 10 | + |
| 11 | +let originalWindow: typeof globalThis.window; |
| 12 | +let originalDocument: typeof globalThis.document; |
| 13 | +let originalHTMLElement: unknown; |
| 14 | + |
| 15 | +// The app mounts the stream-interrupt listener (ChatPane) before an overlay opens, so this |
| 16 | +// harness registers it first too: the order decides which window listener sees Escape first. |
| 17 | +function renderWithStreamInterrupt(overlayOpen: boolean) { |
| 18 | + const interruptStream = mock(() => Promise.resolve({ success: true as const, data: undefined })); |
| 19 | + const client: TestApiOverrides<APIClient> = { workspace: { interruptStream } }; |
| 20 | + const onDismiss = mock(() => undefined); |
| 21 | + const chatInputAPI: RefObject<ChatInputAPI | null> = { current: null }; |
| 22 | + const wrapper = ({ children }: { children: ReactNode }) => ( |
| 23 | + <APIProvider client={createTestApiClient(client)}>{children}</APIProvider> |
| 24 | + ); |
| 25 | + renderHook( |
| 26 | + () => { |
| 27 | + useAIViewKeybinds({ |
| 28 | + workspaceId: "ws", |
| 29 | + canInterrupt: true, |
| 30 | + showRetryBarrier: false, |
| 31 | + chatInputAPI, |
| 32 | + jumpToBottom: () => undefined, |
| 33 | + loadOlderHistory: null, |
| 34 | + handleOpenTerminal: () => undefined, |
| 35 | + handleOpenInEditor: () => undefined, |
| 36 | + aggregator: undefined, |
| 37 | + setEditingMessage: () => undefined, |
| 38 | + vimEnabled: false, |
| 39 | + }); |
| 40 | + useEscapeToDismiss(overlayOpen, onDismiss); |
| 41 | + }, |
| 42 | + { wrapper } |
| 43 | + ); |
| 44 | + return { interruptStream, onDismiss }; |
| 45 | +} |
| 46 | + |
| 47 | +function pressEscape(target: EventTarget, init: KeyboardEventInit = {}): KeyboardEvent { |
| 48 | + const event = new window.KeyboardEvent("keydown", { |
| 49 | + key: "Escape", |
| 50 | + bubbles: true, |
| 51 | + cancelable: true, |
| 52 | + ...init, |
| 53 | + }); |
| 54 | + target.dispatchEvent(event); |
| 55 | + return event; |
| 56 | +} |
| 57 | + |
| 58 | +function addComposer(): HTMLTextAreaElement { |
| 59 | + const composer = document.createElement("textarea"); |
| 60 | + document.body.appendChild(composer); |
| 61 | + composer.focus(); |
| 62 | + return composer; |
| 63 | +} |
| 64 | + |
| 65 | +describe("useEscapeToDismiss", () => { |
| 66 | + beforeEach(() => { |
| 67 | + originalWindow = globalThis.window; |
| 68 | + originalDocument = globalThis.document; |
| 69 | + originalHTMLElement = (globalThis as unknown as { HTMLElement: unknown }).HTMLElement; |
| 70 | + const domWindow = new GlobalWindow() as unknown as Window & typeof globalThis; |
| 71 | + globalThis.window = domWindow; |
| 72 | + globalThis.document = domWindow.document; |
| 73 | + // The keybind helpers check `target instanceof HTMLElement`. |
| 74 | + (globalThis as unknown as { HTMLElement: unknown }).HTMLElement = domWindow.HTMLElement; |
| 75 | + }); |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + cleanup(); |
| 79 | + globalThis.window = originalWindow; |
| 80 | + globalThis.document = originalDocument; |
| 81 | + (globalThis as unknown as { HTMLElement: unknown }).HTMLElement = originalHTMLElement; |
| 82 | + }); |
| 83 | + |
| 84 | + test("with the overlay closed, Escape interrupts the stream exactly as before", async () => { |
| 85 | + const { interruptStream, onDismiss } = renderWithStreamInterrupt(false); |
| 86 | + |
| 87 | + // The composer ignores Escape for the interrupt unless it opts in, and nothing claims it. |
| 88 | + const fromComposer = pressEscape(addComposer()); |
| 89 | + expect(fromComposer.defaultPrevented).toBe(false); |
| 90 | + expect(interruptStream).not.toHaveBeenCalled(); |
| 91 | + |
| 92 | + // Outside an editable element, Escape still interrupts. |
| 93 | + pressEscape(document.body); |
| 94 | + await waitFor(() => expect(interruptStream).toHaveBeenCalledTimes(1)); |
| 95 | + expect(onDismiss).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + test("with the overlay open, Escape from the composer closes it and interrupts nothing", () => { |
| 99 | + const { interruptStream, onDismiss } = renderWithStreamInterrupt(true); |
| 100 | + |
| 101 | + const event = pressEscape(addComposer()); |
| 102 | + |
| 103 | + expect(onDismiss).toHaveBeenCalledTimes(1); |
| 104 | + expect(event.defaultPrevented).toBe(true); |
| 105 | + expect(interruptStream).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + test("with the overlay open, Escape from targets the interrupt accepts closes it and interrupts nothing", async () => { |
| 109 | + const { interruptStream, onDismiss } = renderWithStreamInterrupt(true); |
| 110 | + const optedIn = addComposer(); |
| 111 | + optedIn.setAttribute("data-escape-interrupts-stream", ""); |
| 112 | + |
| 113 | + pressEscape(document.body); |
| 114 | + pressEscape(optedIn); |
| 115 | + |
| 116 | + expect(onDismiss).toHaveBeenCalledTimes(2); |
| 117 | + // stopStream runs asynchronously, so give a wrongly started interrupt time to arrive. |
| 118 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 119 | + expect(interruptStream).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + test("leaves Escape to whatever already handled it", () => { |
| 123 | + const { onDismiss } = renderWithStreamInterrupt(true); |
| 124 | + const composer = addComposer(); |
| 125 | + |
| 126 | + // An open popover, menu or edit mode: they call preventDefault or stop propagation. |
| 127 | + const claim = (e: Event) => e.preventDefault(); |
| 128 | + document.addEventListener("keydown", claim); |
| 129 | + pressEscape(composer); |
| 130 | + document.removeEventListener("keydown", claim); |
| 131 | + const stop = (e: Event) => e.stopPropagation(); |
| 132 | + document.addEventListener("keydown", stop); |
| 133 | + pressEscape(composer); |
| 134 | + document.removeEventListener("keydown", stop); |
| 135 | + |
| 136 | + // IME composition, modified Escape, a terminal, and an open modal dialog. |
| 137 | + pressEscape(composer, { isComposing: true }); |
| 138 | + pressEscape(composer, { ctrlKey: true, shiftKey: true }); |
| 139 | + const terminal = document.createElement("div"); |
| 140 | + terminal.setAttribute("data-terminal-container", ""); |
| 141 | + document.body.appendChild(terminal); |
| 142 | + pressEscape(terminal); |
| 143 | + const modal = document.createElement("div"); |
| 144 | + modal.setAttribute("role", "dialog"); |
| 145 | + modal.setAttribute("aria-modal", "true"); |
| 146 | + document.body.appendChild(modal); |
| 147 | + pressEscape(composer); |
| 148 | + |
| 149 | + expect(onDismiss).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments