Fix flaky e2e rename and onboarding skip (#2235)

This commit is contained in:
Neil
2026-05-18 01:05:24 -07:00
committed by GitHub
parent 5067d4d3f9
commit db8af09f86
8 changed files with 202 additions and 15 deletions
@@ -280,6 +280,7 @@ export default function SortableTab({
{isEditing ? (
<Input
ref={renameInputRef}
data-tab-rename-input="true"
value={renameValue}
aria-label={`Rename tab ${tab.customTitle ?? tab.title}`}
onChange={(event) => setRenameValue(event.target.value)}
@@ -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<typeof vi.fn> } } {
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()
})
})
@@ -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
@@ -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()
})
})
@@ -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`
+4 -3
View File
@@ -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)
+28 -9
View File
@@ -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<Parameters<typeof getActiveTabId>[0]['locator']>
): Promise<void> {
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<typeof getActiveTabId>[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)
})
})
+1 -1
View File
@@ -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