import type { Page } from '@stablyai/playwright-test' import { test, expect } from './helpers/orca-app' import { execInTerminal, waitForActivePanePtyId, waitForActiveTerminalManager } from './helpers/terminal' import { ensureTerminalVisible, getActiveTabId, waitForActiveWorktree, waitForSessionReady } from './helpers/store' import { getRendererTitleLog, installRendererTitleLog } from './helpers/terminal-title-log' import { waitForPtyShellEcho } from './terminal-pty-readiness' test.describe.configure({ mode: 'serial' }) async function countRenderedTabs(page: Page): Promise { return page.locator('[data-testid="sortable-tab"]').count() } async function createTerminalTab(page: Page): Promise { const tabsBefore = await countRenderedTabs(page) const activeBefore = await getActiveTabId(page) await page.getByRole('button', { name: 'New tab' }).click() await page .getByRole('menuitem', { name: /New Terminal/i }) .first() .click() await expect .poll(() => countRenderedTabs(page), { timeout: 5_000, message: 'New Terminal did not render a new tab in the tab bar' }) .toBe(tabsBefore + 1) let tabId: string | null = null await expect .poll( async () => { tabId = await getActiveTabId(page) return Boolean(tabId && tabId !== activeBefore) }, { timeout: 5_000, message: 'New Terminal did not become the active tab' } ) .toBe(true) if (!tabId) { throw new Error('createTerminalTab: active tab id was unavailable after creating terminal') } return tabId } async function activateTerminalTab(page: Page, tabId: string): Promise { await page.evaluate((targetTabId) => { const store = window.__store if (!store) { throw new Error('activateTerminalTab: window.__store is unavailable') } const state = store.getState() state.setActiveTabType('terminal', state.activeWorktreeId) state.setActiveTab(targetTabId) }, tabId) await expect .poll(async () => getActiveTabId(page), { timeout: 5_000, message: `Terminal tab ${tabId} did not become active` }) .toBe(tabId) } async function emitBellAndWaitForTitleFlush( page: Page, ptyId: string, markerTitle: string ): Promise { await waitForPtyShellEcho(page, ptyId, 30_000) // Use an OSC title as a byte-stream fence proving the prior BEL traversed the PTY; printf survives stripped PATH. await execInTerminal(page, ptyId, `printf '\\a\\033]0;${markerTitle}\\007'`) await expect .poll(async () => (await getRendererTitleLog(page)).includes(markerTitle), { timeout: 10_000, message: 'Marker title did not land — byte stream may not have been flushed' }) .toBe(true) } async function getUnreadTerminalTabIds(page: Page): Promise { return page.evaluate(() => { const store = window.__store if (!store) { return [] } return Object.keys(store.getState().unreadTerminalTabs) }) } async function focusActiveXterm(page: Page, tabId: string): Promise { await page.evaluate((targetTabId) => { const manager = window.__paneManagers?.get(targetTabId) const pane = manager?.getActivePane?.() const textarea = pane?.container.querySelector('.xterm-helper-textarea') if (!pane || !textarea) { throw new Error(`No active xterm textarea for terminal tab ${targetTabId}`) } pane.terminal.focus() textarea.focus() }, tabId) await expect .poll( () => page.evaluate( () => document.activeElement?.classList.contains('xterm-helper-textarea') ?? false ), { timeout: 5_000, message: 'xterm helper textarea did not receive keyboard focus' } ) .toBe(true) } test.describe('Terminal attention', () => { // Units own BEL parsing; E2E pins background indicator raise and selection clear. test('background terminal attention marks a tab unread and clears on focus', async ({ orcaPage }) => { await waitForSessionReady(orcaPage) await waitForActiveWorktree(orcaPage) await ensureTerminalVisible(orcaPage) await waitForActiveTerminalManager(orcaPage, 30_000) const firstTabId = await getActiveTabId(orcaPage) if (!firstTabId) { throw new Error('Expected an initial terminal tab') } const secondTabId = await createTerminalTab(orcaPage) await waitForActiveTerminalManager(orcaPage, 30_000) // Focus the first tab so the second becomes a background tab; attention // arriving there should raise its indicator. await activateTerminalTab(orcaPage, firstTabId) await orcaPage.evaluate((tabId) => { const store = window.__store if (!store) { throw new Error('window.__store is unavailable') } const state = store.getState() const ownerWorktreeId = Object.entries(state.tabsByWorktree).find(([, tabs]) => tabs.some((tab) => tab.id === tabId) )?.[0] ?? null if (!ownerWorktreeId) { throw new Error(`No owner worktree found for terminal tab ${tabId}`) } const leafId = state.terminalLayoutsByTabId[tabId]?.activeLeafId if (!leafId) { throw new Error(`No active leaf found for terminal tab ${tabId}`) } state.markTerminalBell({ worktreeId: ownerWorktreeId, tabId, paneKey: `${tabId}:${leafId}` }) }, secondTabId) await expect .poll(async () => (await getUnreadTerminalTabIds(orcaPage)).includes(secondTabId), { timeout: 10_000, message: 'Background tab did not become unread after BEL' }) .toBe(true) const secondTabBell = orcaPage .locator( `[data-testid="sortable-tab"][data-tab-id="${secondTabId}"] [data-testid="tab-activity-bell"]` ) .first() await expect(secondTabBell).toBeVisible() // Activating the tab counts as "the user saw it" — the indicator clears. await activateTerminalTab(orcaPage, secondTabId) await expect .poll(async () => (await getUnreadTerminalTabIds(orcaPage)).includes(secondTabId), { timeout: 5_000, message: 'Unread state did not clear when the user focused the tab' }) .toBe(false) await expect(secondTabBell).toBeHidden() }) // Why (on screen means read, #28637): a BEL on the tab the present user is // looking at is already read, so it must leave no unread marker or bell. test('a BEL on the focused tab leaves no unread marker', async ({ orcaPage }) => { await waitForSessionReady(orcaPage) await waitForActiveWorktree(orcaPage) await ensureTerminalVisible(orcaPage) await waitForActiveTerminalManager(orcaPage, 30_000) const activeTabId = await getActiveTabId(orcaPage) if (!activeTabId) { throw new Error('Expected an active terminal tab') } const activePtyId = await waitForActivePanePtyId(orcaPage) await installRendererTitleLog(orcaPage) // Why: presence comes only from trusted input; a bare Shift keydown sends nothing to the shell. await focusActiveXterm(orcaPage, activeTabId) await orcaPage.keyboard.press('Shift') await emitBellAndWaitForTitleFlush( orcaPage, activePtyId, `focused-tab-bell-marker-${Date.now()}` ) await expect .poll(async () => (await getUnreadTerminalTabIds(orcaPage)).includes(activeTabId), { timeout: 5_000, message: 'A BEL on the focused tab left it unread' }) .toBe(false) await expect( orcaPage.locator( `[data-testid="sortable-tab"][data-tab-id="${activeTabId}"] [data-testid="tab-activity-bell"]` ) ).toHaveCount(0) }) // Replay can re-enable mode 1004 from an old TUI snapshot; fresh-shell focus then emits escape bytes and unbound-key BELs. // Require POST_REPLAY_MODE_RESET to prevent post-reset focus reporting. })