Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

33.5 KB
Raw Blame
// Drive DOM keydown→capture→policy→transport→IPC for every terminal chord.
// Intercept main pty:write for exact bytes without OS-specific readline; assert action effects through panes/search/buffer.
// Skip platform-only chords where runtime policy would never emit them.

import { test, expect } from './helpers/orca-app'
import type { ElectronApplication, Page } from '@stablyai/playwright-test'
import { FLOATING_TERMINAL_WORKTREE_ID } from '../../src/shared/constants'
import {
  execInTerminal,
  sendToTerminal,
  countVisibleTerminalPanes,
  waitForActiveTerminalManager,
  waitForTerminalOutput,
  waitForPaneCount,
  getTerminalContent,
  waitForActivePanePtyId,
  focusActiveTerminalInput
} from './helpers/terminal'
import { waitForSessionReady, waitForActiveWorktree, ensureTerminalVisible } from './helpers/store'
import {
  clearTerminalPtyWriteLog as clearPtyWriteLog,
  installTerminalPtyWriteSpy as installMainProcessPtyWriteSpy,
  readTerminalPtyWrites as getPtyWrites
} from './helpers/terminal-pty-write-spy'
import {
  armKittyKeyboardFromPty,
  getKittyKeyboardFlags,
  interruptAndExpectEtx
} from './helpers/terminal-kitty-keyboard'

async function setActivePaneForegroundAgent(
  page: Page,
  agent: 'droid' | 'antigravity' | null
): Promise<string> {
  return page.evaluate((agent) => {
    const state = window.__store?.getState()
    const worktreeId = state?.activeWorktreeId
    const tabId =
      state?.activeTabType === 'terminal'
        ? state.activeTabId
        : worktreeId
          ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null)
          : null
    const manager = tabId ? window.__paneManagers?.get(tabId) : null
    const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
    if (!state || !tabId || !pane) {
      throw new Error('No active terminal pane for foreground-agent setup')
    }
    const paneKey = `${tabId}:${pane.leafId}`
    state.setPaneForegroundAgent(paneKey, {
      agent,
      shellForeground: false,
      // The shortcut only emits CSI-u for a process identity confirmed to
      // belong to this PTY; keep the fixture aligned with that trust gate.
      routingTrusted: agent === 'droid'
    })
    return paneKey
  }, agent)
}

async function focusFloatingTerminal(page: Page): Promise<void> {
  await page
    .locator(
      `[data-floating-terminal-panel][aria-hidden="false"] [data-terminal-tab-id] .xterm-helper-textarea`
    )
    .first()
    .focus()
}

async function seedFloatingTerminalTabSwitchScenario(page: Page): Promise<{
  backgroundFirstTabId: string
  floatingFirstTabId: string
  floatingSecondTabId: string
}> {
  return page.evaluate((floatingWorktreeId) => {
    const store = window.__store
    if (!store) {
      throw new Error('Store unavailable')
    }
    const state = store.getState()
    const backgroundWorktreeId = state.activeWorktreeId
    if (!backgroundWorktreeId) {
      throw new Error('No active background worktree')
    }

    const backgroundFirst =
      state.tabsByWorktree[backgroundWorktreeId]?.find(
        (tab) => tab.id === state.activeTabIdByWorktree[backgroundWorktreeId]
      ) ??
      state.tabsByWorktree[backgroundWorktreeId]?.find((tab) => tab.id === state.activeTabId) ??
      state.createTab(backgroundWorktreeId)
    state.createTab(backgroundWorktreeId)
    state.setActiveTab(backgroundFirst.id)
    state.setActiveTabType('terminal', window.__store?.getState().activeWorktreeId ?? null)

    const floatingFirst = state.createTab(floatingWorktreeId, undefined, undefined, {
      activate: false
    })
    state.activateTab(floatingFirst.id)
    const floatingGroupId =
      state.activeGroupIdByWorktree[floatingWorktreeId] ??
      state.groupsByWorktree[floatingWorktreeId]?.[0]?.id
    const floatingSecond = state.createTab(floatingWorktreeId, floatingGroupId, undefined, {
      activate: false
    })
    state.activateTab(floatingFirst.id)

    return {
      backgroundFirstTabId: backgroundFirst.id,
      floatingFirstTabId: floatingFirst.id,
      floatingSecondTabId: floatingSecond.id
    }
  }, FLOATING_TERMINAL_WORKTREE_ID)
}

async function getActiveFloatingTerminalTabId(page: Page): Promise<string | null> {
  return page.evaluate((floatingWorktreeId) => {
    const state = window.__store?.getState()
    if (!state) {
      return null
    }
    const groupId = state.activeGroupIdByWorktree[floatingWorktreeId]
    const group =
      (groupId
        ? state.groupsByWorktree[floatingWorktreeId]?.find((candidate) => candidate.id === groupId)
        : null) ??
      state.groupsByWorktree[floatingWorktreeId]?.find((candidate) => candidate.activeTabId) ??
      null
    const activeTab = group?.activeTabId
      ? state.unifiedTabsByWorktree[floatingWorktreeId]?.find((tab) => tab.id === group.activeTabId)
      : null
    return activeTab?.contentType === 'terminal' ? activeTab.entityId : null
  }, FLOATING_TERMINAL_WORKTREE_ID)
}

async function getActiveBackgroundTerminalTabId(page: Page): Promise<string | null> {
  return page.evaluate(() => {
    const state = window.__store?.getState()
    const worktreeId = state?.activeWorktreeId
    return worktreeId ? (state.activeTabIdByWorktree[worktreeId] ?? state.activeTabId) : null
  })
}

async function getActiveTerminalViewport(
  page: Page
): Promise<{ viewportY: number; baseY: number }> {
  return page.evaluate(() => {
    const state = window.__store?.getState()
    const worktreeId = state?.activeWorktreeId
    const tabId =
      state?.activeTabType === 'terminal'
        ? state.activeTabId
        : worktreeId
          ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null)
          : null
    const manager = tabId ? window.__paneManagers?.get(tabId) : null
    const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
    const buffer = pane?.terminal.buffer.active
    if (!buffer) {
      throw new Error('No active terminal buffer')
    }
    return {
      viewportY: buffer.viewportY,
      baseY: buffer.baseY
    }
  })
}

async function pressShiftedRussianLayoutKey(page: Page): Promise<{
  keydownDefaultPrevented: boolean
  keypressSent: boolean
  inputSent: boolean
  terminalInputSent: boolean
  keyupSent: boolean
}> {
  return page.evaluate(() => {
    const state = window.__store?.getState()
    const worktreeId = state?.activeWorktreeId
    const tabId =
      state?.activeTabType === 'terminal'
        ? state.activeTabId
        : worktreeId
          ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null)
          : null
    const manager = tabId ? window.__paneManagers?.get(tabId) : null
    const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
    pane?.terminal.focus()
    const textarea = pane?.container.querySelector(
      '.xterm-helper-textarea'
    ) as HTMLTextAreaElement | null
    if (!pane || !textarea) {
      throw new Error('No xterm helper textarea to receive keyboard input')
    }
    textarea.focus()

    const keydown = new KeyboardEvent('keydown', {
      key: 'Ф',
      code: 'KeyA',
      shiftKey: true,
      bubbles: true,
      cancelable: true
    })
    Object.defineProperty(keydown, 'keyCode', { get: () => 65 })
    Object.defineProperty(keydown, 'which', { get: () => 65 })
    textarea.dispatchEvent(keydown)

    if (keydown.defaultPrevented) {
      return {
        keydownDefaultPrevented: true,
        keypressSent: false,
        inputSent: false,
        terminalInputSent: false,
        keyupSent: false
      }
    }

    const keypress = new KeyboardEvent('keypress', {
      key: 'Ф',
      code: 'KeyA',
      shiftKey: true,
      bubbles: true,
      cancelable: true
    })
    Object.defineProperty(keypress, 'keyCode', { get: () => 1060 })
    Object.defineProperty(keypress, 'charCode', { get: () => 1060 })
    Object.defineProperty(keypress, 'which', { get: () => 1060 })
    textarea.dispatchEvent(keypress)

    const makeTextInputEvent = (): InputEvent => {
      const input = new InputEvent('input', {
        data: 'Ф',
        inputType: 'insertText',
        bubbles: true,
        cancelable: false,
        composed: false
      })
      // Populate InputEvent fields explicitly for older Linux Chromium whose synthetic init drops them.
      Object.defineProperties(input, {
        data: { get: () => 'Ф' },
        inputType: { get: () => 'insertText' },
        composed: { get: () => false }
      })
      return input
    }

    // Supply input-event text because Linux untrusted keypress may lack charCode.
    const input = makeTextInputEvent()
    textarea.dispatchEvent(input)

    const keyup = new KeyboardEvent('keyup', {
      key: 'Ф',
      code: 'KeyA',
      shiftKey: true,
      bubbles: true,
      cancelable: true
    })
    Object.defineProperty(keyup, 'keyCode', { get: () => 65 })
    Object.defineProperty(keyup, 'which', { get: () => 65 })
    textarea.dispatchEvent(keyup)

    // Linux CI drops untrusted InputEvent data; use xterm.input for the same PTY route.
    // Keep keydown assertion to catch kitty sequences leaking from shifted layout keys.
    pane.terminal.input('Ф')

    return {
      keydownDefaultPrevented: false,
      keypressSent: true,
      inputSent: true,
      terminalInputSent: true,
      keyupSent: true
    }
  })
}

async function pressAndExpectWrite(
  page: Page,
  app: ElectronApplication,
  chord: string,
  expectedData: string,
  repetitions = 1
): Promise<void> {
  await clearPtyWriteLog(app)
  await focusActiveTerminalInput(page)
  for (let index = 0; index < repetitions; index++) {
    await page.keyboard.press(chord)
  }

  // Match exact bytes: single control codes can occur inside unrelated prompt/paste writes.
  await expect
    .poll(async () => (await getPtyWrites(app)).filter((write) => write === expectedData).length, {
      timeout: 5_000,
      message: `Expected chord "${chord}" to write ${JSON.stringify(expectedData)}`
    })
    .toBeGreaterThanOrEqual(repetitions)
}

const isMac = process.platform === 'darwin'
const mod = isMac ? 'Meta' : 'Control'

// Mac splits use Cmd+D/Cmd+Shift+D; Linux/Windows reserve Ctrl+D for EOF (#586) and use Ctrl+Shift+D/Alt+Shift+D.
const splitVerticalChord = isMac ? `${mod}+d` : `${mod}+Shift+d`
const splitHorizontalChord = isMac ? `${mod}+Shift+d` : 'Alt+Shift+d'

// Poll confirmation/count because fresh split may transiently report a running child.
async function closeActivePaneAndSettle(page: Page, expectedCount: number): Promise<void> {
  // Focus PaneManager’s active textarea, not the first of multiple split panes.
  await focusActiveTerminalInput(page)
  await page.keyboard.press(`${mod}+w`)
  // The "Stop running command?" confirm surfaces a "Stop and Close" action when
  // the pane still reports a running child.
  const confirmButton = page.getByRole('button', { name: /Stop and Close/i })
  await expect
    .poll(
      async () => {
        if (await confirmButton.isVisible().catch(() => false)) {
          // Surface click errors directly instead of hiding actionability/strictness failure behind a count timeout.
          await confirmButton.click().catch((err) => {
            console.warn('closeActivePaneAndSettle: confirm click failed', err)
          })
        }
        return countVisibleTerminalPanes(page)
      },
      {
        timeout: 10_000,
        message: `Expected ${expectedCount} visible terminal panes after close`
      }
    )
    .toBe(expectedCount)
}

// Serialize shared layout/buffer/expand state and the singleton main pty:write spy to prevent interleaved chord effects.
test.describe.configure({ mode: 'serial' })
test.describe('Terminal Shortcuts', () => {
  test.beforeEach(async ({ orcaPage }) => {
    await waitForSessionReady(orcaPage)
    await waitForActiveWorktree(orcaPage)
    await ensureTerminalVisible(orcaPage)
    const hasPaneManager = await waitForActiveTerminalManager(orcaPage, 30_000)
      .then(() => true)
      .catch(() => false)
    test.skip(
      !hasPaneManager,
      'Electron automation in this environment never mounts the live TerminalPane manager.'
    )
    await waitForPaneCount(orcaPage, 1, 30_000)
  })

  test('Shift+Enter follows the pane Kitty keyboard state', async ({ orcaPage, electronApp }) => {
    await installMainProcessPtyWriteSpy(electronApp)
    const ptyId = await waitForActivePanePtyId(orcaPage)

    await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b\r')
    if (process.platform === 'win32') {
      return
    }

    // Drive the production PTY-output mirror that shortcut policy reads, not renderer-local flags.
    // Disarm before exit because host grounding clears flags left armed.
    await execInTerminal(orcaPage, ptyId, "printf '\\033[>1u'; read -r _; printf '\\033[<u'")
    await expect.poll(() => getKittyKeyboardFlags(orcaPage)).toBe(1)
    await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b[13;2u')

    await sendToTerminal(orcaPage, ptyId, '\r')
    await expect.poll(() => getKittyKeyboardFlags(orcaPage)).toBe(0)
    await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b\r')
  })

  test('Droid gets CSI-u Shift+Enter on Windows without changing Antigravity', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(process.platform !== 'win32', 'Windows ConPTY encoding contract')
    await installMainProcessPtyWriteSpy(electronApp)
    await waitForActivePanePtyId(orcaPage)
    const paneKey = await setActivePaneForegroundAgent(orcaPage, 'droid')
    try {
      await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b[13;2u', 2)
      await setActivePaneForegroundAgent(orcaPage, 'antigravity')
      await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b\r')
    } finally {
      await orcaPage.evaluate(
        (key) => window.__store?.getState().clearPaneForegroundAgent(key),
        paneKey
      )
    }
  })

  test('Windows forwards genuine Ctrl+Alt text chords to the PTY', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(process.platform !== 'win32', 'Windows xterm AltGr classification regression')
    await installMainProcessPtyWriteSpy(electronApp)
    await waitForActivePanePtyId(orcaPage)

    await pressAndExpectWrite(orcaPage, electronApp, 'Control+Alt+u', '\x1b\x15')
    await pressAndExpectWrite(orcaPage, electronApp, 'Control+Alt+2', '\x1b2')
    await pressAndExpectWrite(orcaPage, electronApp, 'Control+Alt+;', '\x1b;')
  })

  test('Ctrl+Enter protects local ConPTY shells without breaking trusted TUI chords', async ({
    orcaPage,
    electronApp
  }) => {
    await installMainProcessPtyWriteSpy(electronApp)
    await waitForActivePanePtyId(orcaPage)

    if (process.platform === 'win32') {
      await pressAndExpectWrite(orcaPage, electronApp, 'Control+Enter', '\r')
      const paneKey = await setActivePaneForegroundAgent(orcaPage, 'droid')
      try {
        // Droid queries CSI-u without activating live flags; trusted process evidence preserves cue/queue.
        await pressAndExpectWrite(orcaPage, electronApp, 'Control+Enter', '\x1b[13;5u')
      } finally {
        await orcaPage.evaluate(
          (key) => window.__store?.getState().clearPaneForegroundAgent(key),
          paneKey
        )
      }
      return
    }

    // Preserve the established query-only Droid/Grok contract outside local ConPTY.
    await pressAndExpectWrite(orcaPage, electronApp, 'Control+Enter', '\x1b[13;5u')
  })

  test('plain Ctrl+C sends ETX and leaves a surviving app its kitty flags', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(process.platform === 'win32', 'POSIX trap and printf fixture')
    await installMainProcessPtyWriteSpy(electronApp)
    const ptyId = await waitForActivePanePtyId(orcaPage)
    // SIGINT during shell startup kills the shell, so interrupt only a ready prompt.
    await execInTerminal(orcaPage, ptyId, 'echo "CTRL_C_""READY"')
    await waitForTerminalOutput(orcaPage, 'CTRL_C_READY')
    // An app that survives SIGINT, like an agent TUI, and disarms its own flags on exit.
    await armKittyKeyboardFromPty(
      orcaPage,
      ptyId,
      `bash -c 'trap "" INT; printf "\\033[=31u"; read -r _; printf "\\033[=0u"'`
    )

    await interruptAndExpectEtx(orcaPage, electronApp)

    // Require xterm and policy mirror to retain encoding after unproven app death (Shift+Enter stays CSI-u).
    expect(await getKittyKeyboardFlags(orcaPage)).toBe(31)
    await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b[13;2u')

    await sendToTerminal(orcaPage, ptyId, '\r')
    await expect.poll(async () => await getKittyKeyboardFlags(orcaPage)).toBe(0)
    await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b\r')
  })

  test('the host grounds kitty flags an app killed by Ctrl+C left armed', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(process.platform === 'win32', 'POSIX printf fixture')
    await installMainProcessPtyWriteSpy(electronApp)
    const ptyId = await waitForActivePanePtyId(orcaPage)
    await execInTerminal(orcaPage, ptyId, 'echo "CTRL_C_""READY"')
    await waitForTerminalOutput(orcaPage, 'CTRL_C_READY')
    // `cat` dies on SIGINT with the flags still armed, like a crashed TUI.
    await armKittyKeyboardFromPty(orcaPage, ptyId, `printf '\\033[=31u'; cat`)

    await interruptAndExpectEtx(orcaPage, electronApp)

    await expect
      .poll(async () => await getKittyKeyboardFlags(orcaPage), {
        timeout: 5_000,
        message: 'the host did not ground the dead app kitty flags at command end'
      })
      .toBe(0)
    await clearPtyWriteLog(electronApp)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.type('x')
    await expect
      .poll(async () => (await getPtyWrites(electronApp)).some((write) => write === 'x'), {
        timeout: 5_000,
        message: 'Post-interrupt keyboard input stayed in Kitty CSI-u mode'
      })
      .toBe(true)
    expect((await getPtyWrites(electronApp)).join('')).not.toContain('\x1b[')
    await orcaPage.keyboard.press('Backspace')
  })

  test('unselected Cmd+C reaches a Kitty app, including after Ctrl+C', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(process.platform !== 'darwin', 'macOS copy binding')
    await installMainProcessPtyWriteSpy(electronApp)
    const ptyId = await waitForActivePanePtyId(orcaPage)
    await execInTerminal(orcaPage, ptyId, 'echo "CMD_C_""READY"')
    await waitForTerminalOutput(orcaPage, 'CMD_C_READY')
    const select = (text: string | null): Promise<string> =>
      orcaPage.evaluate(async (selectedText) => {
        const state = window.__store!.getState()
        const pane = window.__paneManagers!.get(state.activeTabId!)!.getActivePane()!
        if (selectedText === null) {
          pane.terminal.clearSelection()
          return ''
        }
        await new Promise<void>((resolve) =>
          pane.terminal.write(`\r\n${selectedText}\r\n`, resolve)
        )
        const buffer = pane.terminal.buffer.active
        pane.terminal.select(0, buffer.baseY + buffer.cursorY - 1, selectedText.length)
        return pane.terminal.getSelection()
      }, text)
    // Presses Cmd+C, then a marker chord whose write proves the Cmd+C bytes had their turn.
    const pressCmdC = async (marker: string, markerWrite: string): Promise<string[]> => {
      await clearPtyWriteLog(electronApp)
      await focusActiveTerminalInput(orcaPage)
      await orcaPage.keyboard.press('Meta+c')
      await orcaPage.keyboard.press(marker)
      await expect.poll(async () => await getPtyWrites(electronApp)).toContain(markerWrite)
      const writes = await getPtyWrites(electronApp)
      return writes.slice(0, writes.indexOf(markerWrite))
    }
    const kittyCmdC = ['\x1b[99;9;99u', '\x1b[99;9:3u']
    const originalClipboard = await electronApp.evaluate(({ clipboard }) => clipboard.readText())
    try {
      await select(null)
      expect(await pressCmdC('x', 'x')).toEqual([])
      await orcaPage.keyboard.press('Backspace')

      // An app that survives SIGINT, like an agent TUI, and disarms its own flags on exit.
      await armKittyKeyboardFromPty(
        orcaPage,
        ptyId,
        `bash -c 'trap "" INT; printf "\\033[=31u"; read -r _; printf "\\033[=0u"'`
      )
      await select(null)
      expect(await pressCmdC('Shift+Enter', '\x1b[13;2u')).toEqual(kittyCmdC)

      const selectedText = await select('Terminal copy selection')
      expect(selectedText).toBe('Terminal copy selection')
      await electronApp.evaluate(({ clipboard }) => clipboard.writeText(''))
      expect(await pressCmdC('Shift+Enter', '\x1b[13;2u')).toEqual([])
      await expect
        .poll(() => electronApp.evaluate(({ clipboard }) => clipboard.readText()))
        .toBe(selectedText)

      await interruptAndExpectEtx(orcaPage, electronApp)
      expect(await getKittyKeyboardFlags(orcaPage)).toBe(31)
      await select(null)
      expect(await pressCmdC('Shift+Enter', '\x1b[13;2u')).toEqual(kittyCmdC)

      await sendToTerminal(orcaPage, ptyId, '\r')
      await expect.poll(async () => await getKittyKeyboardFlags(orcaPage)).toBe(0)
      expect(await pressCmdC('x', 'x')).toEqual([])
      await orcaPage.keyboard.press('Backspace')
    } finally {
      await electronApp.evaluate(
        ({ clipboard }, text) => clipboard.writeText(text),
        originalClipboard
      )
    }
  })

  test('@headful Codex-like background output stays visible without disabling WebGL in auto mode', async ({
    orcaPage
  }) => {
    const hasPane = await orcaPage.evaluate(() => {
      const state = window.__store?.getState()
      const worktreeId = state?.activeWorktreeId
      const tabId =
        state?.activeTabType === 'terminal'
          ? state.activeTabId
          : worktreeId
            ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null)
            : null
      const manager = tabId ? window.__paneManagers?.get(tabId) : null
      manager?.setTerminalGpuAcceleration('auto')
      const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
      return Boolean(pane)
    })
    test.skip(!hasPane, 'No active terminal pane for renderer validation')
    const webglActive = await orcaPage
      .waitForFunction(
        () => {
          const state = window.__store?.getState()
          const worktreeId = state?.activeWorktreeId
          const tabId =
            state?.activeTabType === 'terminal'
              ? state.activeTabId
              : worktreeId
                ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null)
                : null
          const manager = tabId ? window.__paneManagers?.get(tabId) : null
          const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
          return Boolean(pane?.webglAddon)
        },
        null,
        { timeout: 5_000 }
      )
      .then(() => true)
      .catch(() => false)
    test.skip(!webglActive, 'WebGL was not active in this headful environment')

    const ptyId = await waitForActivePanePtyId(orcaPage)
    const marker = `CODEX_BG_${Date.now()}`
    await execInTerminal(orcaPage, ptyId, `printf '\\033[48;2;52;52;52m  ${marker}  \\033[0m\\n'`)
    await waitForTerminalOutput(orcaPage, marker)

    await expect
      .poll(
        () =>
          orcaPage.evaluate((expectedMarker) => {
            const state = window.__store?.getState()
            const worktreeId = state?.activeWorktreeId
            const tabId =
              state?.activeTabType === 'terminal'
                ? state.activeTabId
                : worktreeId
                  ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null)
                  : null
            const manager = tabId ? window.__paneManagers?.get(tabId) : null
            const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
            const activeBuffer = pane?.terminal.buffer.active
            const terminalText = activeBuffer
              ?.getLine(activeBuffer.baseY + activeBuffer.cursorY)
              ?.translateToString(true)
              .trim()
            const visibleText = pane?.container.textContent ?? ''
            return {
              markerVisible:
                visibleText.includes(expectedMarker) || terminalText === expectedMarker,
              hasComplexScriptOutput: pane?.hasComplexScriptOutput === true,
              hasWebgl: Boolean(pane?.webglAddon)
            }
          }, marker),
        {
          timeout: 5_000,
          message: 'Background SGR output did not stay visible on the auto renderer'
        }
      )
      .toEqual({
        markerVisible: true,
        hasComplexScriptOutput: false,
        hasWebgl: true
      })
  })

  test('floating terminal owns tab switch shortcuts while focused', async ({ orcaPage }) => {
    const scenario = await seedFloatingTerminalTabSwitchScenario(orcaPage)
    await orcaPage.evaluate(async () => {
      const state = window.__store?.getState()
      if (state?.settings?.floatingTerminalEnabled !== true) {
        await state?.updateSettings({ floatingTerminalEnabled: true })
      }
      if (!document.querySelector('[data-floating-terminal-panel][aria-hidden="false"]')) {
        window.dispatchEvent(new CustomEvent('orca-toggle-floating-terminal'))
      }
    })
    await expect(
      orcaPage.locator('[data-floating-terminal-panel][aria-hidden="false"]')
    ).toBeVisible()
    await focusFloatingTerminal(orcaPage)

    await orcaPage.keyboard.press(`${mod}+Shift+BracketRight`)
    await expect
      .poll(() => getActiveFloatingTerminalTabId(orcaPage), {
        timeout: 5_000,
        message: 'floating terminal did not switch to the next tab'
      })
      .toBe(scenario.floatingSecondTabId)
    await expect
      .poll(() => getActiveBackgroundTerminalTabId(orcaPage), {
        timeout: 1_000,
        message: 'background terminal tab changed while floating terminal was focused'
      })
      .toBe(scenario.backgroundFirstTabId)

    await focusFloatingTerminal(orcaPage)
    await orcaPage.keyboard.press(`${mod}+Shift+BracketLeft`)
    await expect
      .poll(() => getActiveFloatingTerminalTabId(orcaPage), {
        timeout: 5_000,
        message: 'floating terminal did not switch back to the previous tab'
      })
      .toBe(scenario.floatingFirstTabId)
    await expect(getActiveBackgroundTerminalTabId(orcaPage)).resolves.toBe(
      scenario.backgroundFirstTabId
    )
  })

  test('all terminal chords reach the PTY or fire their action', async ({
    orcaPage,
    electronApp
  }) => {
    await installMainProcessPtyWriteSpy(electronApp)

    const ptyId = await waitForActivePanePtyId(orcaPage)
    const marker = `SHORTCUT_TEST_${Date.now()}`
    await execInTerminal(orcaPage, ptyId, `echo ${marker}`)
    await waitForTerminalOutput(orcaPage, marker)

    // Alt+←/→ → readline backward-word / forward-word (\eb / \ef).
    await pressAndExpectWrite(orcaPage, electronApp, 'Alt+ArrowLeft', '\x1bb')
    await pressAndExpectWrite(orcaPage, electronApp, 'Alt+ArrowRight', '\x1bf')

    // Ctrl+←/→ on non-mac → readline backward-word / forward-word (\eb / \ef).
    // macOS reserves Ctrl+Arrow; Windows ConPTY leaves it to PSReadLine.
    if (!isMac && process.platform !== 'win32') {
      await pressAndExpectWrite(orcaPage, electronApp, 'Control+ArrowLeft', '\x1bb')
      await pressAndExpectWrite(orcaPage, electronApp, 'Control+ArrowRight', '\x1bf')
    }

    // Alt+Backspace → Esc+DEL (readline backward-kill-word).
    await pressAndExpectWrite(orcaPage, electronApp, 'Alt+Backspace', '\x1b\x7f')

    // Ctrl+Backspace → \x17 (unix-word-rubout).
    await pressAndExpectWrite(orcaPage, electronApp, 'Control+Backspace', '\x17')

    // The shell has not enabled KKP, so Shift+Enter must not leak CSI-u text.
    await pressAndExpectWrite(orcaPage, electronApp, 'Shift+Enter', '\x1b\r')

    if (isMac) {
      // Cmd+←/→ → Ctrl+A / Ctrl+E (beginning/end of line).
      await pressAndExpectWrite(orcaPage, electronApp, 'Meta+ArrowLeft', '\x01')
      await pressAndExpectWrite(orcaPage, electronApp, 'Meta+ArrowRight', '\x05')

      // Cmd+Backspace → Ctrl+U (kill line). Cmd+Delete → Ctrl+K (kill to EOL).
      await pressAndExpectWrite(orcaPage, electronApp, 'Meta+Backspace', '\x15')
      await pressAndExpectWrite(orcaPage, electronApp, 'Meta+Delete', '\x0b')
    }

    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(`${mod}+k`)
    await expect
      .poll(async () => (await getTerminalContent(orcaPage)).includes(marker), {
        timeout: 5_000,
        message: 'Cmd+K did not clear the terminal buffer'
      })
      .toBe(false)

    const panesBeforeSplit = await countVisibleTerminalPanes(orcaPage)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(splitVerticalChord)
    await waitForPaneCount(orcaPage, panesBeforeSplit + 1)
    // Require the split PTY binding before close to avoid racing unfinished creation.
    await waitForActivePanePtyId(orcaPage)

    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(`${mod}+BracketRight`)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(`${mod}+BracketLeft`)
    expect(await countVisibleTerminalPanes(orcaPage)).toBe(panesBeforeSplit + 1)

    // Test expand while split remains open because it requires >1 pane.
    const readExpanded = async (): Promise<boolean> =>
      orcaPage.evaluate(() => {
        const state = window.__store?.getState()
        const tabId = state?.activeTabId
        if (!state || !tabId) {
          return false
        }
        return state.expandedPaneByTabId[tabId] === true
      })
    expect(await readExpanded()).toBe(false)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(`${mod}+Shift+Enter`)
    await expect
      .poll(readExpanded, { timeout: 3_000, message: 'Cmd+Shift+Enter did not expand pane' })
      .toBe(true)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(`${mod}+Shift+Enter`)
    await expect
      .poll(readExpanded, { timeout: 3_000, message: 'Cmd+Shift+Enter did not collapse pane' })
      .toBe(false)

    await closeActivePaneAndSettle(orcaPage, panesBeforeSplit)

    const panesBeforeHSplit = await countVisibleTerminalPanes(orcaPage)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(splitHorizontalChord)
    await waitForPaneCount(orcaPage, panesBeforeHSplit + 1)
    await waitForActivePanePtyId(orcaPage)
    await closeActivePaneAndSettle(orcaPage, panesBeforeHSplit)

    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press(`${mod}+f`)
    const searchInput = orcaPage.locator('[data-terminal-search-root] input').first()
    // Wait for search-input focus: React’s Escape handler otherwise loses an early key to xterm.
    await expect(searchInput).toBeFocused({ timeout: 3_000 })
    await orcaPage.keyboard.press('Escape')
    await expect(orcaPage.locator('[data-terminal-search-root]').first()).toBeHidden({
      timeout: 3_000
    })
  })

  test('Cmd+Up/Down scrolls terminal viewport without writing to the PTY on macOS', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(!isMac, 'Cmd+Up/Down terminal scroll navigation is macOS-only')

    await installMainProcessPtyWriteSpy(electronApp)

    const ptyId = await waitForActivePanePtyId(orcaPage)
    const marker = `CMD_ARROW_SCROLL_${Date.now()}`
    await execInTerminal(orcaPage, ptyId, `for i in {1..120}; do echo ${marker}_$i; done`)
    await waitForTerminalOutput(orcaPage, `${marker}_120`)

    await expect
      .poll(
        async () => {
          const viewport = await getActiveTerminalViewport(orcaPage)
          return viewport.baseY > 0 && viewport.viewportY === viewport.baseY
        },
        {
          timeout: 5_000,
          message: 'terminal did not settle at the bottom with scrollback for Cmd+Up/Down repro'
        }
      )
      .toBe(true)

    await clearPtyWriteLog(electronApp)
    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press('Meta+ArrowUp')
    await expect
      .poll(async () => getActiveTerminalViewport(orcaPage), {
        timeout: 5_000,
        message: 'Cmd+Up did not scroll the terminal viewport to the top'
      })
      .toMatchObject({ viewportY: 0 })
    expect(await getPtyWrites(electronApp)).toEqual([])

    await focusActiveTerminalInput(orcaPage)
    await orcaPage.keyboard.press('Meta+ArrowDown')
    await expect
      .poll(
        async () => {
          const viewport = await getActiveTerminalViewport(orcaPage)
          return viewport.viewportY === viewport.baseY
        },
        {
          timeout: 5_000,
          message: 'Cmd+Down did not scroll the terminal viewport to the bottom'
        }
      )
      .toBe(true)
    expect(await getPtyWrites(electronApp)).toEqual([])
  })

  test('Shift with Russian layout text reaches the PTY as Cyrillic under kitty keyboard reporting', async ({
    orcaPage,
    electronApp
  }) => {
    test.skip(process.platform === 'win32', 'ConPTY panes withhold the kitty protocol')
    await installMainProcessPtyWriteSpy(electronApp)
    // Probe real PTY binding before input so onData cannot race disconnected sendInput; then clear spy writes.
    const ptyId = await waitForActivePanePtyId(orcaPage)
    await armKittyKeyboardFromPty(orcaPage, ptyId, `printf '\\033[=31u'; cat`)
    await clearPtyWriteLog(electronApp)

    const dispatch = await pressShiftedRussianLayoutKey(orcaPage)

    expect(dispatch).toEqual({
      keydownDefaultPrevented: false,
      keypressSent: true,
      inputSent: true,
      terminalInputSent: true,
      keyupSent: true
    })
    await expect
      .poll(async () => (await getPtyWrites(electronApp)).some((write) => write.includes('Ф')), {
        timeout: 5_000,
        message: 'Shift+Russian layout text did not reach the PTY as Cyrillic'
      })
      .toBe(true)
    const writes = await getPtyWrites(electronApp)
    const joinedWrites = writes.join('')
    expect(joinedWrites).not.toContain('\x1b[97:1060;2;1060u')
    expect(joinedWrites).not.toContain('\x1b[97:1060;2:3u')
    await sendToTerminal(orcaPage, ptyId, '\x03')
  })
})