From 2069bdbe27adc4493bc7f087e1addc97f2f19ebc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Mar 2026 15:56:27 -0700 Subject: [PATCH] Fix chat.new shortcut by ignoring detached terminal focus - Treat detached `xterm-helper-textarea` nodes as unfocused in `isTerminalFocused` - Add unit tests for connected vs detached terminal focus behavior - Harden ChatView shortcut test flow by waiting for keybindings/config and retrying route transition --- apps/web/src/components/ChatView.browser.tsx | 69 ++++++++++++++++---- apps/web/src/lib/terminalFocus.test.ts | 56 ++++++++++++++++ apps/web/src/lib/terminalFocus.ts | 1 + 3 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 apps/web/src/lib/terminalFocus.test.ts diff --git a/apps/web/src/components/ChatView.browser.tsx b/apps/web/src/components/ChatView.browser.tsx index 18181b2e1c..687670571f 100644 --- a/apps/web/src/components/ChatView.browser.tsx +++ b/apps/web/src/components/ChatView.browser.tsx @@ -587,6 +587,58 @@ async function waitForInteractionModeButton( ); } +async function waitForServerConfigToApply(): Promise { + await vi.waitFor( + () => { + expect(wsRequests.some((request) => request._tag === WS_METHODS.serverGetConfig)).toBe(true); + }, + { timeout: 8_000, interval: 16 }, + ); + await waitForLayout(); +} + +function dispatchChatNewShortcut(): void { + const useMetaForMod = isMacPlatform(navigator.platform); + window.dispatchEvent( + new KeyboardEvent("keydown", { + key: "o", + shiftKey: true, + metaKey: useMetaForMod, + ctrlKey: !useMetaForMod, + bubbles: true, + cancelable: true, + }), + ); +} + +async function triggerChatNewShortcutUntilPath( + router: ReturnType, + predicate: (pathname: string) => boolean, + errorMessage: string, +): Promise { + let pathname = router.state.location.pathname; + const deadline = Date.now() + 8_000; + while (Date.now() < deadline) { + dispatchChatNewShortcut(); + await waitForLayout(); + pathname = router.state.location.pathname; + if (predicate(pathname)) { + return pathname; + } + } + throw new Error(`${errorMessage} Last path: ${pathname}`); +} + +async function waitForNewThreadShortcutLabel(): Promise { + const newThreadButton = page.getByTestId("new-thread-button"); + await expect.element(newThreadButton).toBeInTheDocument(); + await newThreadButton.hover(); + const shortcutLabel = isMacPlatform(navigator.platform) + ? "New thread (⇧⌘O)" + : "New thread (Ctrl+Shift+O)"; + await expect.element(page.getByText(shortcutLabel)).toBeInTheDocument(); +} + async function waitForImagesToLoad(scope: ParentNode): Promise { const images = Array.from(scope.querySelectorAll("img")); if (images.length === 0) { @@ -1677,7 +1729,6 @@ describe("ChatView timeline estimator parity (full app)", () => { await mounted.cleanup(); } }); - it("creates a fresh draft after the previous draft thread is promoted", async () => { const mounted = await mountChatView({ viewport: DEFAULT_VIEWPORT, @@ -1712,6 +1763,8 @@ describe("ChatView timeline estimator parity (full app)", () => { try { const newThreadButton = page.getByTestId("new-thread-button"); await expect.element(newThreadButton).toBeInTheDocument(); + await waitForNewThreadShortcutLabel(); + await waitForServerConfigToApply(); await newThreadButton.click(); const promotedThreadPath = await waitForURL( @@ -1725,19 +1778,7 @@ describe("ChatView timeline estimator parity (full app)", () => { syncServerReadModel(addThreadToSnapshot(fixture.snapshot, promotedThreadId)); useComposerDraftStore.getState().clearDraftThread(promotedThreadId); - const useMetaForMod = isMacPlatform(navigator.platform); - window.dispatchEvent( - new KeyboardEvent("keydown", { - key: "o", - shiftKey: true, - metaKey: useMetaForMod, - ctrlKey: !useMetaForMod, - bubbles: true, - cancelable: true, - }), - ); - - const freshThreadPath = await waitForURL( + const freshThreadPath = await triggerChatNewShortcutUntilPath( mounted.router, (path) => UUID_ROUTE_RE.test(path) && path !== promotedThreadPath, "Shortcut should create a fresh draft instead of reusing the promoted thread.", diff --git a/apps/web/src/lib/terminalFocus.test.ts b/apps/web/src/lib/terminalFocus.test.ts new file mode 100644 index 0000000000..832324ff14 --- /dev/null +++ b/apps/web/src/lib/terminalFocus.test.ts @@ -0,0 +1,56 @@ +import { afterEach, describe, expect, it } from "vitest"; + +import { isTerminalFocused } from "./terminalFocus"; + +class MockHTMLElement { + isConnected = false; + className = ""; + + readonly classList = { + contains: (value: string) => this.className.split(/\s+/).includes(value), + }; + + closest(selector: string): MockHTMLElement | null { + return selector === ".thread-terminal-drawer .xterm" && this.isConnected ? this : null; + } +} + +const originalDocument = globalThis.document; +const originalHTMLElement = globalThis.HTMLElement; + +afterEach(() => { + if (originalDocument === undefined) { + delete (globalThis as { document?: Document }).document; + } else { + globalThis.document = originalDocument; + } + + if (originalHTMLElement === undefined) { + delete (globalThis as { HTMLElement?: typeof HTMLElement }).HTMLElement; + } else { + globalThis.HTMLElement = originalHTMLElement; + } +}); + +describe("isTerminalFocused", () => { + it("returns false for detached xterm helper textareas", () => { + const detached = new MockHTMLElement(); + detached.className = "xterm-helper-textarea"; + + globalThis.HTMLElement = MockHTMLElement as unknown as typeof HTMLElement; + globalThis.document = { activeElement: detached } as Document; + + expect(isTerminalFocused()).toBe(false); + }); + + it("returns true for connected xterm helper textareas", () => { + const attached = new MockHTMLElement(); + attached.className = "xterm-helper-textarea"; + attached.isConnected = true; + + globalThis.HTMLElement = MockHTMLElement as unknown as typeof HTMLElement; + globalThis.document = { activeElement: attached } as Document; + + expect(isTerminalFocused()).toBe(true); + }); +}); diff --git a/apps/web/src/lib/terminalFocus.ts b/apps/web/src/lib/terminalFocus.ts index d24c9572a3..d4edd9b14e 100644 --- a/apps/web/src/lib/terminalFocus.ts +++ b/apps/web/src/lib/terminalFocus.ts @@ -1,6 +1,7 @@ export function isTerminalFocused(): boolean { const activeElement = document.activeElement; if (!(activeElement instanceof HTMLElement)) return false; + if (!activeElement.isConnected) return false; if (activeElement.classList.contains("xterm-helper-textarea")) return true; return activeElement.closest(".thread-terminal-drawer .xterm") !== null; }