From db8af09f86bb0237ddc236727f65c2550eab295b Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 18 May 2026 01:05:24 -0700 Subject: [PATCH] Fix flaky e2e rename and onboarding skip (#2235) --- .../src/components/tab-bar/SortableTab.tsx | 1 + .../terminal-pane/pane-helpers.test.ts | 91 ++++++++++++++++++- .../components/terminal-pane/pane-helpers.ts | 26 ++++++ .../lib/focus-terminal-tab-surface.test.ts | 48 ++++++++++ .../src/lib/focus-terminal-tab-surface.ts | 5 + tests/e2e/onboarding.spec.ts | 7 +- tests/e2e/tab-rename.spec.ts | 37 ++++++-- tests/playwright.config.ts | 2 +- 8 files changed, 202 insertions(+), 15 deletions(-) create mode 100644 src/renderer/src/lib/focus-terminal-tab-surface.test.ts diff --git a/src/renderer/src/components/tab-bar/SortableTab.tsx b/src/renderer/src/components/tab-bar/SortableTab.tsx index 33ad80c23..5d2fa9ef6 100644 --- a/src/renderer/src/components/tab-bar/SortableTab.tsx +++ b/src/renderer/src/components/tab-bar/SortableTab.tsx @@ -280,6 +280,7 @@ export default function SortableTab({ {isEditing ? ( setRenameValue(event.target.value)} diff --git a/src/renderer/src/components/terminal-pane/pane-helpers.test.ts b/src/renderer/src/components/terminal-pane/pane-helpers.test.ts index bda187eb0..8e5987672 100644 --- a/src/renderer/src/components/terminal-pane/pane-helpers.test.ts +++ b/src/renderer/src/components/terminal-pane/pane-helpers.test.ts @@ -1,5 +1,6 @@ -import { describe, expect, it } from 'vitest' -import { isWindowsUserAgent, shellEscapePath } from './pane-helpers' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { PaneManager } from '@/lib/pane-manager/pane-manager' +import { fitAndFocusPanes, isWindowsUserAgent, shellEscapePath } from './pane-helpers' describe('isWindowsUserAgent', () => { it('detects Windows user agents', () => { @@ -46,3 +47,89 @@ describe('shellEscapePath', () => { ) }) }) + +describe('fitAndFocusPanes', () => { + class FakeHTMLElement { + readonly tagName: string + readonly isContentEditable: boolean + readonly classList: { contains: (className: string) => boolean } + + constructor(args: { + tagName: string + classNames?: string[] + isContentEditable?: boolean + closestSelector?: string | null + }) { + this.tagName = args.tagName + this.isContentEditable = args.isContentEditable ?? false + const classNames = new Set(args.classNames ?? []) + this.classList = { contains: (className) => classNames.has(className) } + this.closest = vi.fn((selector: string) => + selector === args.closestSelector ? (this as unknown as Element) : null + ) + } + + closest: (selector: string) => Element | null + } + + afterEach(() => { + vi.unstubAllGlobals() + }) + + function makeManager(): { manager: PaneManager; terminal: { focus: ReturnType } } { + const terminal = { focus: vi.fn() } + const manager = { + fitAllPanes: vi.fn(), + getActivePane: () => ({ terminal }), + getPanes: () => [{ terminal }] + } as unknown as PaneManager + return { manager, terminal } + } + + function stubDocument(activeElement: Element | null, renameInputMounted = false): void { + vi.stubGlobal('HTMLElement', FakeHTMLElement) + vi.stubGlobal('document', { + activeElement, + querySelector: vi.fn((selector: string) => + selector === '[data-tab-rename-input="true"]' && renameInputMounted + ? (new FakeHTMLElement({ tagName: 'INPUT' }) as unknown as Element) + : null + ) + }) + } + + it('fits without stealing focus from an active editable field', () => { + const input = new FakeHTMLElement({ tagName: 'INPUT' }) as unknown as Element + stubDocument(input) + const { manager, terminal } = makeManager() + + fitAndFocusPanes(manager) + + expect(manager.fitAllPanes).toHaveBeenCalled() + expect(terminal.focus).not.toHaveBeenCalled() + }) + + it('does not focus the terminal while inline tab rename is mounting', () => { + stubDocument(null, true) + const { manager, terminal } = makeManager() + + fitAndFocusPanes(manager) + + expect(manager.fitAllPanes).toHaveBeenCalled() + expect(terminal.focus).not.toHaveBeenCalled() + }) + + it('still focuses the active terminal pane when the current focus is xterm', () => { + const textarea = new FakeHTMLElement({ + tagName: 'TEXTAREA', + classNames: ['xterm-helper-textarea'] + }) as unknown as Element + stubDocument(textarea) + const { manager, terminal } = makeManager() + + fitAndFocusPanes(manager) + + expect(manager.fitAllPanes).toHaveBeenCalled() + expect(terminal.focus).toHaveBeenCalled() + }) +}) diff --git a/src/renderer/src/components/terminal-pane/pane-helpers.ts b/src/renderer/src/components/terminal-pane/pane-helpers.ts index 5a03ca095..d33de9781 100644 --- a/src/renderer/src/components/terminal-pane/pane-helpers.ts +++ b/src/renderer/src/components/terminal-pane/pane-helpers.ts @@ -28,6 +28,15 @@ export function hasDimensionsChanged(manager: PaneManager): boolean { } export function focusActivePane(manager: PaneManager): void { + // Why: tab rename focuses the input on the next frame. A queued terminal + // layout focus can land in between mount and focus, blurring rename closed. + if (typeof document !== 'undefined' && document.querySelector('[data-tab-rename-input="true"]')) { + return + } + const activeElement = typeof document === 'undefined' ? null : document.activeElement + if (shouldPreserveEditableFocus(activeElement)) { + return + } const panes = manager.getPanes() const activePane = manager.getActivePane() ?? panes[0] activePane?.terminal.focus() @@ -56,6 +65,23 @@ export function isLinuxUserAgent( return !isMacUserAgent(userAgent) && !isWindowsUserAgent(userAgent) && userAgent.includes('Linux') } +function shouldPreserveEditableFocus(element: Element | null): boolean { + if (!(element instanceof HTMLElement)) { + return false + } + if (element.classList.contains('xterm-helper-textarea') || element.closest('.xterm')) { + return false + } + // Why: deferred fit/focus work can run after inline rename or settings + // fields take focus. Layout maintenance must not blur user edits closed. + return ( + element.isContentEditable || + element.tagName === 'INPUT' || + element.tagName === 'TEXTAREA' || + element.tagName === 'SELECT' + ) +} + // Why: escape rules are a property of the *target* shell receiving the path, // not the client OS. A Windows client dropping onto a Linux SSH worktree must // produce POSIX-quoted output; passing a userAgent string here coupled escape diff --git a/src/renderer/src/lib/focus-terminal-tab-surface.test.ts b/src/renderer/src/lib/focus-terminal-tab-surface.test.ts new file mode 100644 index 000000000..66d733dbd --- /dev/null +++ b/src/renderer/src/lib/focus-terminal-tab-surface.test.ts @@ -0,0 +1,48 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { focusTerminalTabSurface } from './focus-terminal-tab-surface' + +describe('focusTerminalTabSurface', () => { + afterEach(() => { + vi.unstubAllGlobals() + }) + + function flushAnimationFrames(): void { + vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => { + callback(0) + return 1 + }) + } + + it('focuses the scoped xterm helper textarea', () => { + flushAnimationFrames() + const textarea = { focus: vi.fn() } + vi.stubGlobal('document', { + querySelector: vi.fn((selector: string) => + selector === '[data-terminal-tab-id="tab-1"] .xterm-helper-textarea' ? textarea : null + ) + }) + + focusTerminalTabSurface('tab-1') + + expect(textarea.focus).toHaveBeenCalled() + }) + + it('does not steal focus while inline tab rename is open', () => { + flushAnimationFrames() + const textarea = { focus: vi.fn() } + vi.stubGlobal('document', { + querySelector: vi.fn((selector: string) => { + if (selector === '[data-tab-rename-input="true"]') { + return {} + } + return selector === '[data-terminal-tab-id="tab-1"] .xterm-helper-textarea' + ? textarea + : null + }) + }) + + focusTerminalTabSurface('tab-1') + + expect(textarea.focus).not.toHaveBeenCalled() + }) +}) diff --git a/src/renderer/src/lib/focus-terminal-tab-surface.ts b/src/renderer/src/lib/focus-terminal-tab-surface.ts index 060bd8fef..e5d183d87 100644 --- a/src/renderer/src/lib/focus-terminal-tab-surface.ts +++ b/src/renderer/src/lib/focus-terminal-tab-surface.ts @@ -12,6 +12,11 @@ function cssAttributeString(value: string): string { export function focusTerminalTabSurface(tabId: string, leafId?: string | null): void { requestAnimationFrame(() => { requestAnimationFrame(() => { + // Why: this can be queued before inline tab rename mounts. If it runs + // afterward, focusing xterm blurs the rename input and commits it closed. + if (document.querySelector('[data-tab-rename-input="true"]')) { + return + } const escapedTabId = cssAttributeString(tabId) const scopedSelector = leafId ? `[data-terminal-tab-id="${escapedTabId}"] [data-leaf-id="${cssAttributeString(leafId)}"] .xterm-helper-textarea` diff --git a/tests/e2e/onboarding.spec.ts b/tests/e2e/onboarding.spec.ts index 73a689f84..3760933ee 100644 --- a/tests/e2e/onboarding.spec.ts +++ b/tests/e2e/onboarding.spec.ts @@ -326,11 +326,12 @@ test.describe('Onboarding flow', () => { .toBe(1) }) - test('Skip jumps to the repo step without dismissing onboarding', async ({ orcaPage }) => { + test('Skip jumps to the repo step, saves the selected agent, and keeps onboarding open', async ({ + orcaPage + }) => { await expect(orcaPage.getByRole('heading', { name: /Pick your default agent/i })).toBeVisible({ timeout: 15_000 }) - const beforeDefaultAgent = (await getSettings(orcaPage)).defaultTuiAgent const codexButton = orcaPage.getByRole('button', { name: /^Codex\s/ }) const codexVisible = await codexButton .first() @@ -374,7 +375,7 @@ test.describe('Onboarding flow', () => { }) await expect .poll(async () => (await getSettings(orcaPage)).defaultTuiAgent, { timeout: 5_000 }) - .toBe(beforeDefaultAgent) + .toBe('codex') await orcaPage.reload() await waitForSessionReady(orcaPage) diff --git a/tests/e2e/tab-rename.spec.ts b/tests/e2e/tab-rename.spec.ts index cb6a47a4c..542e3305e 100644 --- a/tests/e2e/tab-rename.spec.ts +++ b/tests/e2e/tab-rename.spec.ts @@ -64,6 +64,17 @@ test.describe('Tab Rename (Inline)', () => { return page.locator(`[data-testid="sortable-tab"][data-tab-title="${escaped}"]`).first() } + async function dispatchMiddleClickSequence( + locator: ReturnType[0]['locator']> + ): Promise { + await locator.evaluate((element) => { + const eventInit = { bubbles: true, cancelable: true, button: 1 } + element.dispatchEvent(new MouseEvent('mousedown', { ...eventInit, buttons: 4 })) + element.dispatchEvent(new MouseEvent('mouseup', eventInit)) + element.dispatchEvent(new MouseEvent('auxclick', eventInit)) + }) + } + async function getActiveCustomTitle( page: Parameters[0], worktreeId: string @@ -287,7 +298,7 @@ test.describe('Tab Rename (Inline)', () => { const state = store.getState() const existing = (state.tabsByWorktree[targetWorktreeId] ?? []).length for (let i = existing; i < 15; i++) { - state.createTab(targetWorktreeId) + state.createTab(targetWorktreeId, undefined, undefined, { activate: false }) } }, worktreeId) @@ -295,19 +306,25 @@ test.describe('Tab Rename (Inline)', () => { .poll(async () => (await getWorktreeTabs(orcaPage, worktreeId)).length, { timeout: 5_000 }) .toBeGreaterThanOrEqual(15) - const activeTitle = await getActiveTabTitle(orcaPage, worktreeId) - const tabLocator = tabLocatorByTitle(orcaPage, activeTitle) - await tabLocator.dblclick() + const targetTitle = await getActiveTabTitle(orcaPage, worktreeId) + const tabLocator = tabLocatorByTitle(orcaPage, targetTitle) + await tabLocator.scrollIntoViewIfNeeded() + await expect(tabLocator).toBeVisible() + // Why: once 15 tabs are packed into the strip, the tab center can overlap + // the close affordance. Target the visible title text, which is the rename + // hit area users aim for. + const tabTitle = tabLocator.getByText(targetTitle, { exact: true }) + await expect(tabTitle).toBeVisible() + await tabTitle.dblclick() const renameInput = orcaPage.getByRole('textbox', { - name: `Rename tab ${activeTitle}`, + name: `Rename tab ${targetTitle}`, exact: true }) await expect(renameInput).toBeVisible() - const box = await renameInput.boundingBox() - expect(box).not.toBeNull() - expect(box!.width).toBeGreaterThanOrEqual(60) + const width = await renameInput.evaluate((element) => element.getBoundingClientRect().width) + expect(width).toBeGreaterThanOrEqual(60) }) test('middle-clicking inside the rename input does not close the tab', async ({ orcaPage }) => { @@ -327,10 +344,12 @@ test.describe('Tab Rename (Inline)', () => { // Why: the outer tab's middle-click handler closes the tab. The rename // input stops propagation + preventDefaults middle-click so the tab // isn't closed while the user is editing. - await renameInput.click({ button: 'middle' }) + await dispatchMiddleClickSequence(renameInput) // The tab must still exist — no regression where editing-then-middle-click // accidentally closes the tab out from under the input. + await expect(renameInput).toBeVisible() + await expect(tabLocatorByTitle(orcaPage, originalTitle)).toBeVisible() expect((await getWorktreeTabs(orcaPage, worktreeId)).length).toBe(tabsBefore) }) }) diff --git a/tests/playwright.config.ts b/tests/playwright.config.ts index ecaf4ea57..e5c4dfb3e 100644 --- a/tests/playwright.config.ts +++ b/tests/playwright.config.ts @@ -32,7 +32,7 @@ export default defineConfig({ // don't share state — we can safely fan out to match the runner's vCPU count. workers: process.env.CI ? 4 : undefined, forbidOnly: !!process.env.CI, - retries: process.env.CI ? 1 : 0, + retries: 0, reporter: 'list', use: { // Why: this suite intentionally runs with retries disabled so first-failure