Files
orca/tests/e2e/terminal-restart-persistence.spec.ts

208 lines
8.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// #461’s 3-minute renderer scrollback serialization caused multi-pane input stalls; #729 replaced it with daemon history and pruned local session buffers.
// Require daemon-backed scrollback, cursor and pane titles across clean restart.
import type { ElectronApplication } from '@stablyai/playwright-test'
import { test, expect } from './helpers/orca-app'
import {
execInTerminal,
waitForTerminalOutput,
waitForPaneCount,
getTerminalContent,
splitActiveTerminalPane
} from './helpers/terminal'
import { getActiveTabId } from './helpers/store'
import { createRestartSession } from './helpers/orca-restart'
import { PTY_SESSION_ID_SEPARATOR } from '../../src/shared/terminal/pty/lifecycle/pty-session-id-format'
import {
seededRepoPathOrSkip,
bootstrapFirstLaunch,
bootstrapRestoredLaunch,
waitForTerminalActiveLine,
readTerminalActiveLine,
setPaneTitleFromTerminalMenu,
getTabCustomTitle,
expectSavedLayoutToContainTitle
} from './helpers/terminal-restart-persistence'
// Serialize full quit/relaunch cycles to avoid competing Electron cache locks.
test.describe.configure({ mode: 'serial' })
test.describe('Terminal restart persistence', () => {
test('scrollback survives clean quit and relaunch', async (// oxlint-disable-next-line no-empty-pattern -- Playwright's second fixture arg is testInfo; the first must be an object destructure to opt out of the default fixture set.
{}, testInfo) => {
const repoPath = seededRepoPathOrSkip()
const session = createRestartSession(testInfo)
let firstApp: ElectronApplication | null = null
let secondApp: ElectronApplication | null = null
try {
const firstLaunch = await session.launch()
firstApp = firstLaunch.app
const { worktreeId, ptyId } = await bootstrapFirstLaunch(firstLaunch.page, repoPath)
// Require daemon-backed persistence; LocalPtyProvider fallback intentionally prunes buffers and would give the wrong failure signal.
expect(ptyId).toContain(PTY_SESSION_ID_SEPARATOR)
// Use a retry-unique marker plus newline so prompt/OSC residue cannot satisfy snapshot retention.
const marker = `SCROLLBACK_PERSIST_${Date.now()}`
await execInTerminal(firstLaunch.page, ptyId, `echo ${marker}`)
await waitForTerminalOutput(firstLaunch.page, marker)
// Close saves session bindings and disconnects; daemon retains scrollback.
await session.close(firstApp)
firstApp = null
const secondLaunch = await session.launch()
secondApp = secondLaunch.app
await bootstrapRestoredLaunch(secondLaunch.page, worktreeId)
// Poll live xterm after snapshot replay; local scrollback is intentionally absent from store.
await expect
.poll(async () => (await getTerminalContent(secondLaunch.page)).includes(marker), {
timeout: 15_000,
message: 'Restored terminal did not contain the pre-quit scrollback marker'
})
.toBe(true)
} finally {
if (secondApp) {
await session.close(secondApp)
}
if (firstApp) {
await session.close(firstApp)
}
await session.dispose()
}
})
test('daemon snapshot relaunch preserves the cursor on the shell prompt', async (// oxlint-disable-next-line no-empty-pattern -- Playwright's second fixture arg is testInfo; the first must be an object destructure to opt out of the default fixture set.
{}, testInfo) => {
const repoPath = seededRepoPathOrSkip()
const session = createRestartSession(testInfo)
let firstApp: ElectronApplication | null = null
let secondApp: ElectronApplication | null = null
try {
const firstLaunch = await session.launch()
firstApp = firstLaunch.app
const { worktreeId, ptyId } = await bootstrapFirstLaunch(firstLaunch.page, repoPath)
expect(ptyId).toContain(PTY_SESSION_ID_SEPARATOR)
const prompt = `ORCA_RESTART_PROMPT_${Date.now()}_GT `
const marker = `ORCA_CURSOR_RESTART_${Date.now()}`
const promptCommand =
process.platform === 'win32'
? `function global:prompt { '${prompt}' }`
: `export PS1='${prompt}'; PROMPT='${prompt}'`
// PowerShell prompt is a function; keep PS1/PROMPT for Bash/Zsh.
await execInTerminal(firstLaunch.page, ptyId, promptCommand)
await waitForTerminalActiveLine(firstLaunch.page, prompt.trim())
await execInTerminal(firstLaunch.page, ptyId, `echo ${marker}`)
await waitForTerminalOutput(firstLaunch.page, marker)
const beforeActiveLine = await waitForTerminalActiveLine(firstLaunch.page, prompt.trim())
await session.close(firstApp)
firstApp = null
const secondLaunch = await session.launch()
secondApp = secondLaunch.app
await bootstrapRestoredLaunch(secondLaunch.page, worktreeId)
await waitForTerminalOutput(secondLaunch.page, marker, 15_000)
await expect
.poll(
async () => {
const activeLine = await readTerminalActiveLine(secondLaunch.page)
if (!activeLine || activeLine.includes(marker)) {
return false
}
// Require a settled prompt, not the temporary PS1 surviving: reattach may preserve the shell or replay before a new prompt.
return activeLine === beforeActiveLine || /[$#%>]\s*$/.test(activeLine)
},
{
timeout: 10_000,
message: 'Restored terminal cursor did not settle on a shell prompt line'
}
)
.toBe(true)
} finally {
if (secondApp) {
await session.close(secondApp)
}
if (firstApp) {
await session.close(firstApp)
}
await session.dispose()
}
})
test('restored Set Title pane label survives agent title churn', async (// oxlint-disable-next-line no-empty-pattern -- Playwright's second fixture arg is testInfo; the first must be an object destructure to opt out of the default fixture set.
{}, testInfo) => {
const repoPath = seededRepoPathOrSkip()
const session = createRestartSession(testInfo)
let firstApp: ElectronApplication | null = null
let secondApp: ElectronApplication | null = null
try {
const firstLaunch = await session.launch()
firstApp = firstLaunch.app
const { worktreeId } = await bootstrapFirstLaunch(firstLaunch.page, repoPath)
const title = `Restored pane label ${Date.now()}`
const firstTabId = (await getActiveTabId(firstLaunch.page))!
await setPaneTitleFromTerminalMenu(firstLaunch.page, title)
await expect
.poll(() => getTabCustomTitle(firstLaunch.page, worktreeId, firstTabId), {
timeout: 3_000
})
.toBe(null)
await session.close(firstApp)
firstApp = null
const secondLaunch = await session.launch()
secondApp = secondLaunch.app
await bootstrapRestoredLaunch(secondLaunch.page, worktreeId)
const restoredTabId = (await getActiveTabId(secondLaunch.page))!
await expect(secondLaunch.page.locator('.pane-title-text', { hasText: title })).toBeVisible()
await expect
.poll(() => getTabCustomTitle(secondLaunch.page, worktreeId, restoredTabId), {
timeout: 3_000
})
.toBe(null)
await expectSavedLayoutToContainTitle(secondLaunch.page, restoredTabId, title)
const runtimeTitle = '⠋ Codex restored working'
await secondLaunch.page.evaluate(
({ targetTabId, title }) => {
window.__store!.getState().updateTabTitle(targetTabId, title)
},
{ targetTabId: restoredTabId, title: runtimeTitle }
)
await expect(
secondLaunch.page.locator(`[data-testid="sortable-tab"][data-tab-id="${restoredTabId}"]`)
).toHaveAttribute('data-tab-title', runtimeTitle)
await expect(secondLaunch.page.locator('.pane-title-text', { hasText: title })).toBeVisible()
await expect
.poll(() => getTabCustomTitle(secondLaunch.page, worktreeId, restoredTabId), {
timeout: 3_000
})
.toBe(null)
await splitActiveTerminalPane(secondLaunch.page, 'vertical')
await waitForPaneCount(secondLaunch.page, 2)
await expect(secondLaunch.page.locator('.pane-title-text', { hasText: title })).toBeVisible()
} finally {
if (secondApp) {
await session.close(secondApp)
}
if (firstApp) {
await session.close(firstApp)
}
await session.dispose()
}
})
})