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
This commit is contained in:
1 parent
f152e3c53a
commit
2069bdbe27
3 files changed
+112
-14
No files matched your search
@@ -587,6 +587,58 @@ async function waitForInteractionModeButton(
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForServerConfigToApply(): Promise<void> {
|
||||
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<typeof getRouter>,
|
||||
predicate: (pathname: string) => boolean,
|
||||
errorMessage: string,
|
||||
): Promise<string> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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.",
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user