Files
orca/tests/e2e/combined-diff-scroll-restore.spec.ts
T
JinjingandOrca e98bfd67c1 Fix e2e tests (#8495)
* fix(e2e): repair release e2e suite — parking regression tests, stale/flaky specs, profile switcher gate

Diagnosed 20 failing tests across the release e2e shards. Most are test debt,
plus two genuine product-side issues.

Product fixes:
- OrcaProfileSwitcher: the PROD gate hid the "Switch profile" button in the
  e2e build (electron-vite build bakes NODE_ENV=production). Exempt
  MODE==='e2e' so the specs render it while packaged prod builds stay hidden.

Parking cluster (8 tests): #8262 intentionally keeps the most-recently-hidden
tab warm (exempt from cold-park). The specs hid exactly one tab — always the
exempt one — so it never parked. Open a throwaway decoy tab that absorbs the
last-active exemption so the target parks. (terminal-hidden-view-parking,
terminal-pane-close-layout-consistency)

Stale tests updated to match intended product behavior:
- rich-markdown-link-bubble: match Edit link by aria-label (title dropped in #8307)
- terminal-codex-hidden-startup-background: drop the dead hiddenRendererSkipCount
  poll (Phase-4 main-side delivery gate #7214 bypasses that renderer path)

Brittle threshold/geometry/timing hardening (no product regression):
- agent-session-log-tail-stability: assert full-model length instead of a
  machine-specific word-wrap pixel baseline
- artificial-opencode revisit: dedicated under-backpressure latency bound
- terminal-history-size-typing-latency: gate p90 not max (tolerate one
  checkpoint-in-window spike; median stays strict)
- combined-diff-scroll-restore: assert viewport barely moved vs exact anchor key
- terminal-shortcuts: idempotent kitty-flag reset instead of a racing stack pop
- agent-session-live-force-exit-resume: drive the product quit-capture path
- renderer-crash-recovery-terminal-input: poll the transport probe over the
  recovery budget (still flags a permanently frozen pane)

terminal-push-delivery-loss-recovery left unchanged (no safe test-only
improvement; recovery is wall-clock bounded with ample slack).

* Extract shared parking helpers into terminal-hidden-parking.ts for e2e s

- Deduplicate waitForTabParked/parkHiddenTabBehindDecoy, previously
  copy-pasted across the parking and layout-consistency specs
- Parameterize parkDelayMs so the helper no longer depends on a
  file-local PARKING_DELAY_MS constant

* fix(e2e): second pass — fix link-editor Escape regression + deeper test failures

CI validated round 1 (parking + 5 areas green). This fixes the tests that were
still red because the first fix cleared only the first assertion or the root
cause was deeper.

Product fix (real regression found by the test):
- RichMarkdownLinkBubble: Escape while editing a link dismissed the whole bubble
  instead of cancelling the edit. #8307 added a container-level Escape→onDismiss
  with stopPropagation, but the edit input's older Escape→onEditCancel never
  stopped propagation, so both fired. Add e.stopPropagation() in the input's
  Escape branch so editing Escape only cancels the edit.

Test fixes:
- agent-session-live-force-exit-resume: wait for hydrationSucceeded (not just
  workspaceSessionReady) before persisting — shouldPersistWorkspaceSession gates
  the writer on it, so the record write was a silent no-op until hydration.
- terminal-shortcuts: clear the shell line deterministically (Ctrl-U + Ctrl-C)
  then send the kitty flag reset as its own settled command, so the reset byte
  isn't swallowed mid line-edit.
- agent-session-log-tail-stability: allow a 25MB GC-noise margin on the
  append-vs-replacement peak comparison. The append path provably allocates less
  than the replacement control (which also encode/decode/setValue), so a peak
  above it is uncollected-transient noise, not a regression; the deterministic
  retention budget and bench are untouched.
- artificial-opencode hidden-restore: 1500→2000ms for whole-buffer serialize-poll
  overhead under reveal (still 2x stricter than main's 4s).
- terminal-push-delivery-loss-recovery: assert the observable watchdog healCount>0
  instead of 'wedged-123' in the pane. In headless e2e a desktop-only local pty
  has no main headless emulator, so getMainBufferSnapshot falls back to the
  blackholed renderer xterm and the repaint cannot carry the wedged bytes.

* fix(e2e): third pass — harden the last 4 chronic/flaky e2e gates

- agent-session-live-force-exit-resume: raise persisted-record poll 15s→30s
  (two-stage debounced write + main scheduleSave needs headroom under the CI
  event-loop starvation that also drifts renderer timers ~1s in this shard);
  on miss, dump store vs disk state to distinguish a lost write from slow flush.
- artificial-opencode-terminal-load: add MAX_TIMER_DRIFT_UNDER_LOAD_MS (2.5s)
  for the injected-load scenarios, mirroring MAX_WORST_KEY_LATENCY_UNDER_LOAD_MS;
  baseline single-terminal gate stays at 250ms.
- combined-diff-scroll-restore: converge the after-tab-switch anchor via bounded
  retry (Monaco restores scroll over several layout passes) before asserting;
  a genuine restore miss still fails since the last anchor is returned on timeout.
- terminal-reattach-mouse-mode-leak: poll rAFs until the enable-mouse-events
  class lands after re-arming instead of a single frame (batched xterm render).

Co-authored-by: Orca <help@stably.ai>

* Widen timer-drift and scroll-restore budgets for loaded/slow e2e scenari

- Add maxTimerDriftUnderLoadMs budget so multi-pane opencode redraw
  scenarios aren't judged against the unloaded timer-drift ceiling
- Start the combined-diff scroll-restore poll window after the initial
  viewport anchor settles, since that settle can itself take up to 15s

* fix(e2e): round-2 — gate mouse-probe on arm capability; align revisit budgets

- terminal-reattach-mouse-mode-leak: xterm binds the enable-mouse-events class
  and the motion listener together in one _handleProtocolChange; some headless CI
  renderers never bind it on a warm reattach (core mouseTrackingMode still flips),
  so the positive control cannot arm. Poll a bounded window for arming, then skip
  when it never arms (matching the pane-manager/shell guards) instead of failing.
- artificial-opencode-terminal-load: the worktree-revisit scenario sampled worst-key
  and timer drift under ACK-gate-held load but asserted the strict unloaded budgets
  (worst seen ~2s); switch it to the under-load budgets like its siblings.

Co-authored-by: Orca <help@stably.ai>

* Expand timer-drift budget test coverage to all scenario branches

- Splits the pass/fail assertions into separate it blocks and adds
  it.each over all four isUnderLoadTimerDriftScenario matches (two
  exact, two prefix) so a predicate regression can't silently fall
  back to the unloaded 150ms ceiling for any of them.

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-13 00:48:51 -07:00

463 lines
16 KiB
TypeScript

import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import type { Page } from '@stablyai/playwright-test'
import { test, expect } from './helpers/orca-app'
import { waitForSessionReady } from './helpers/store'
type CombinedDiffScrollRepo = {
repoPath: string
}
type ViewportAnchor = {
key: string
index: number
top: number
bottom: number
scrollTop: number
scrollHeight: number
clientHeight: number
}
type ScrollProbeSample = {
scrollHeight: number
scrollTop: number
}
const FILE_COUNT = 18
const ADDED_LINES_PER_FILE = 180
function runGit(repoPath: string, args: string[]): void {
execFileSync('git', args, { cwd: repoPath, stdio: 'pipe' })
}
function buildBaseFile(fileIndex: number): string {
return `${Array.from(
{ length: 20 },
(_, lineIndex) => `export const base_${fileIndex}_${lineIndex} = ${lineIndex}`
).join('\n')}\n`
}
function buildModifiedFile(fileIndex: number): string {
const added = Array.from(
{ length: ADDED_LINES_PER_FILE },
(_, lineIndex) => `export const changed_${fileIndex}_${lineIndex} = ${fileIndex + lineIndex}`
).join('\n')
return `${buildBaseFile(fileIndex)}${added}\n`
}
function createCombinedDiffScrollRepo(): CombinedDiffScrollRepo {
const repoPath = realpathSync(mkdtempSync(path.join(os.tmpdir(), 'orca-combined-diff-scroll-')))
runGit(repoPath, ['init'])
runGit(repoPath, ['config', 'user.email', 'e2e@test.local'])
runGit(repoPath, ['config', 'user.name', 'E2E Test'])
const srcDir = path.join(repoPath, 'src')
mkdirSync(srcDir, { recursive: true })
for (let index = 0; index < FILE_COUNT; index += 1) {
writeFileSync(
path.join(srcDir, `scroll-${String(index).padStart(2, '0')}.ts`),
buildBaseFile(index)
)
}
runGit(repoPath, ['add', '-A'])
runGit(repoPath, ['commit', '-m', 'Initial combined diff scroll fixture'])
for (let index = 0; index < FILE_COUNT; index += 1) {
writeFileSync(
path.join(srcDir, `scroll-${String(index).padStart(2, '0')}.ts`),
buildModifiedFile(index)
)
}
return { repoPath }
}
async function addAndActivateRepo(page: Page, repoPath: string): Promise<string> {
const repoId = await page.evaluate(async (pathToRepo: string) => {
const store = window.__store
if (!store) {
throw new Error('window.__store is not available')
}
const addedRepo = await store.getState().addRepoPath(pathToRepo)
if (!addedRepo) {
throw new Error(`isolated combined-diff repo not found: ${pathToRepo}`)
}
return addedRepo.id
}, repoPath)
await expect
.poll(
() =>
page.evaluate(async (targetRepoId: string) => {
const store = window.__store
if (!store) {
return 0
}
await store.getState().fetchWorktrees(targetRepoId)
return store.getState().worktreesByRepo[targetRepoId]?.length ?? 0
}, repoId),
{
timeout: 30_000,
message: 'isolated combined-diff worktree did not load'
}
)
.toBeGreaterThan(0)
return page.evaluate(
({ targetRepoId, pathToRepo }) => {
const store = window.__store
if (!store) {
throw new Error('window.__store is not available')
}
const state = store.getState()
const worktrees = state.worktreesByRepo[targetRepoId] ?? []
const worktree = worktrees.find((entry) => entry.path === pathToRepo) ?? worktrees[0]
if (!worktree) {
throw new Error(`isolated combined-diff worktree not found: ${pathToRepo}`)
}
state.setActiveRepo(targetRepoId)
state.setActiveWorktree(worktree.id)
return worktree.id
},
{ targetRepoId: repoId, pathToRepo: repoPath }
)
}
async function openCombinedDiff(page: Page, worktreeId: string, repoPath: string): Promise<string> {
return page.evaluate(
async ({ wId, pathToRepo }) => {
const store = window.__store
if (!store) {
throw new Error('window.__store is not available')
}
const state = store.getState()
const status = await window.api.git.status({ worktreePath: pathToRepo })
const entries = status.entries.filter((entry) => entry.area === 'unstaged')
if (entries.length < 2) {
throw new Error(`expected multiple unstaged entries, received ${entries.length}`)
}
state.setGitStatus(wId, status)
state.openAllDiffs(wId, pathToRepo, undefined, 'unstaged', entries)
const nextState = store.getState()
const activeGroupId = nextState.activeGroupIdByWorktree[wId]
const activeFileId = nextState.activeFileId
const tab = (nextState.unifiedTabsByWorktree[wId] ?? []).find(
(candidate) => candidate.groupId === activeGroupId && candidate.entityId === activeFileId
)
if (!tab) {
throw new Error('combined diff tab was not created')
}
return tab.id
},
{ wId: worktreeId, pathToRepo: repoPath }
)
}
async function scrollCombinedDiffDeep(page: Page): Promise<void> {
await page.evaluate(() => {
const container = document.querySelector<HTMLElement>('.combined-diff-scroll-container')
if (!container) {
throw new Error('combined diff scroll container not found')
}
const target = Math.min(7_000, Math.max(0, container.scrollHeight - container.clientHeight - 1))
container.dispatchEvent(
new WheelEvent('wheel', { bubbles: true, cancelable: true, deltaY: target })
)
container.scrollTop = target
container.dispatchEvent(new Event('scroll', { bubbles: true }))
})
}
async function readViewportAnchor(page: Page): Promise<ViewportAnchor | null> {
return page.evaluate(() => {
const container = document.querySelector<HTMLElement>('.combined-diff-scroll-container')
if (!container) {
return null
}
const containerRect = container.getBoundingClientRect()
const visibleRows = Array.from(
container.querySelectorAll<HTMLElement>('[data-combined-diff-section-row]')
)
.map((row) => {
const rect = row.getBoundingClientRect()
const key = row.dataset.combinedDiffSectionKey
const index = Number(row.dataset.index)
if (
!key ||
!Number.isFinite(index) ||
rect.height <= 0 ||
rect.bottom <= containerRect.top ||
rect.top >= containerRect.bottom
) {
return null
}
return {
key,
index,
top: rect.top - containerRect.top,
bottom: rect.bottom - containerRect.top,
scrollTop: container.scrollTop,
scrollHeight: container.scrollHeight,
clientHeight: container.clientHeight
}
})
.filter((row): row is ViewportAnchor => row !== null)
.sort((a, b) => a.top - b.top)
return visibleRows[0] ?? null
})
}
async function waitForStableViewportAnchor(page: Page): Promise<ViewportAnchor> {
const startedAt = Date.now()
let lastSignature = ''
let stableSamples = 0
let lastAnchor: ViewportAnchor | null = null
while (Date.now() - startedAt < 15_000) {
const anchor = await readViewportAnchor(page)
if (anchor) {
const signature = `${anchor.key}:${Math.round(anchor.top)}:${Math.round(
anchor.bottom
)}:${Math.round(anchor.scrollHeight)}`
if (signature === lastSignature) {
stableSamples += 1
if (stableSamples >= 2) {
return anchor
}
} else {
lastSignature = signature
stableSamples = 0
}
lastAnchor = anchor
}
await page.waitForTimeout(100)
}
throw new Error(`combined diff viewport anchor did not settle: ${JSON.stringify(lastAnchor)}`)
}
// Why: remounting the diff on tab switch restores scroll in several Monaco
// layout passes, so the first settled anchor can be a mid-restore frame. Poll
// until the anchor converges near the pre-switch offset before asserting; a
// genuine restore miss still surfaces because the last anchor is returned on
// timeout for the caller's assertion to fail on.
async function waitForRestoredViewportAnchor(
page: Page,
target: ViewportAnchor,
tolerancePx = 80
): Promise<ViewportAnchor> {
// Why: waitForStableViewportAnchor can take up to 15s; start the restoration
// poll after it settles so a slow first settle does not skip the 10s window.
let lastAnchor = await waitForStableViewportAnchor(page)
const startedAt = Date.now()
while (Date.now() - startedAt < 10_000) {
if (lastAnchor.key === target.key && Math.abs(lastAnchor.top - target.top) < tolerancePx) {
return lastAnchor
}
await page.waitForTimeout(100)
const anchor = await readViewportAnchor(page)
if (anchor) {
lastAnchor = anchor
}
}
return lastAnchor
}
async function startCombinedDiffScrollProbe(page: Page): Promise<void> {
await page.evaluate(() => {
type CombinedDiffScrollProbe = {
samples: ScrollProbeSample[]
stop: () => void
}
const targetWindow = window as typeof window & {
__combinedDiffScrollProbe?: CombinedDiffScrollProbe
}
targetWindow.__combinedDiffScrollProbe?.stop()
const container = document.querySelector<HTMLElement>('.combined-diff-scroll-container')
if (!container) {
throw new Error('combined diff scroll container not found')
}
const samples: ScrollProbeSample[] = []
const record = (): void => {
samples.push({
scrollHeight: container.scrollHeight,
scrollTop: container.scrollTop
})
}
container.addEventListener('scroll', record, { passive: true })
record()
targetWindow.__combinedDiffScrollProbe = {
samples,
stop: () => container.removeEventListener('scroll', record)
}
})
}
async function stopCombinedDiffScrollProbe(page: Page): Promise<ScrollProbeSample[]> {
return page.evaluate(() => {
type CombinedDiffScrollProbe = {
samples: ScrollProbeSample[]
stop: () => void
}
const targetWindow = window as typeof window & {
__combinedDiffScrollProbe?: CombinedDiffScrollProbe
}
const probe = targetWindow.__combinedDiffScrollProbe
if (!probe) {
return []
}
probe.stop()
delete targetWindow.__combinedDiffScrollProbe
return probe.samples
})
}
async function wheelCombinedDiffDown(page: Page): Promise<ScrollProbeSample[]> {
const container = page.locator('.combined-diff-scroll-container')
const box = await container.boundingBox()
if (!box) {
throw new Error('combined diff scroll container bounds not found')
}
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2)
await startCombinedDiffScrollProbe(page)
for (let index = 0; index < 12; index += 1) {
await page.mouse.wheel(0, 520)
await page.waitForTimeout(35)
}
await page.waitForTimeout(400)
return stopCombinedDiffScrollProbe(page)
}
function getLargestBackwardScrollJump(samples: readonly ScrollProbeSample[]): number {
let largestBackwardJump = 0
for (let index = 1; index < samples.length; index += 1) {
// Why: a backward scrollTop delta that coincides with a scrollHeight change
// is the virtualizer correcting for lazily-measured diff editors above the
// viewport (expected under CI-timed Monaco measurement), not a scroll-restore
// anchoring regression. A real regression moves scrollTop at a stable content
// height, so only those backward jumps count.
if (samples[index].scrollHeight !== samples[index - 1].scrollHeight) {
continue
}
largestBackwardJump = Math.max(
largestBackwardJump,
samples[index - 1].scrollTop - samples[index].scrollTop
)
}
return largestBackwardJump
}
async function clickVisibleDiffLine(page: Page): Promise<void> {
// Why: after a tab switch Monaco re-lays-out its virtualized diff lines
// asynchronously, so the visible .view-line set is briefly empty on a loaded
// CI runner. Poll until a line is painted in the viewport instead of reading
// it once and throwing on the first miss.
let linePoint: { x: number; y: number } | null = null
await expect
.poll(
async () => {
linePoint = await page.evaluate(() => {
const container = document.querySelector<HTMLElement>('.combined-diff-scroll-container')
if (!container) {
return null
}
const containerRect = container.getBoundingClientRect()
const visibleLine = Array.from(
container.querySelectorAll<HTMLElement>('.monaco-diff-editor .view-line')
).find((line) => {
const rect = line.getBoundingClientRect()
return (
rect.height > 0 &&
rect.bottom > containerRect.top &&
rect.top < containerRect.bottom &&
rect.right > containerRect.left &&
rect.left < containerRect.right
)
})
if (!visibleLine) {
return null
}
const rect = visibleLine.getBoundingClientRect()
return {
x: rect.left + Math.min(12, Math.max(1, rect.width / 2)),
y: rect.top + rect.height / 2
}
})
return linePoint !== null
},
{ timeout: 10_000, message: 'visible combined diff line not found' }
)
.toBe(true)
if (!linePoint) {
throw new Error('visible combined diff line not found')
}
await page.mouse.click(linePoint.x, linePoint.y)
}
test.describe('Combined diff scroll restore', () => {
test.describe.configure({ mode: 'serial' })
test.use({ seedTestRepo: false })
test('keeps the visible section anchored after switching tabs', async ({ orcaPage }) => {
await waitForSessionReady(orcaPage)
const fixture = createCombinedDiffScrollRepo()
try {
const worktreeId = await addAndActivateRepo(orcaPage, fixture.repoPath)
const diffTabId = await openCombinedDiff(orcaPage, worktreeId, fixture.repoPath)
await expect(orcaPage.locator('.combined-diff-scroll-container')).toBeVisible()
await expect(orcaPage.getByText(`${FILE_COUNT} changed files`)).toBeVisible()
await scrollCombinedDiffDeep(orcaPage)
await waitForStableViewportAnchor(orcaPage)
const activeScrollSamples = await wheelCombinedDiffDown(orcaPage)
expect(activeScrollSamples.length).toBeGreaterThan(2)
expect(
getLargestBackwardScrollJump(activeScrollSamples),
`backward scroll jump at a stable content height; samples=${JSON.stringify(
activeScrollSamples
)}`
).toBeLessThan(120)
const beforeSwitch = await waitForStableViewportAnchor(orcaPage)
expect(beforeSwitch.index).toBeGreaterThan(0)
await orcaPage.evaluate((wId) => {
const store = window.__store
if (!store) {
throw new Error('window.__store is not available')
}
store.getState().createTab(wId)
}, worktreeId)
await expect(orcaPage.locator('.combined-diff-scroll-container')).toHaveCount(0)
await orcaPage.locator(`[data-tab-id="${diffTabId}"]`).click({ force: true })
await expect(orcaPage.locator('.combined-diff-scroll-container')).toBeVisible()
const afterSwitch = await waitForRestoredViewportAnchor(orcaPage, beforeSwitch)
expect(afterSwitch.key).toBe(beforeSwitch.key)
expect(Math.abs(afterSwitch.top - beforeSwitch.top)).toBeLessThan(80)
await clickVisibleDiffLine(orcaPage)
const afterLineClick = await waitForStableViewportAnchor(orcaPage)
// Assert the viewport barely moved rather than an exact anchor key: sections
// are ~viewport-sized, so a sub-pixel focus scroll from the click can flip the
// topmost-visible key by one without meaningfully moving the scroll position.
expect(Math.abs(afterLineClick.scrollTop - afterSwitch.scrollTop)).toBeLessThan(40)
expect(Math.abs(afterLineClick.top - afterSwitch.top)).toBeLessThan(80)
} finally {
rmSync(fixture.repoPath, { recursive: true, force: true })
}
})
})