test(pty-connection): drain async confirm reads between tests (#9260)

The connectPanePty suite intermittently fails in CI on
`clearAgentLaunchConfig` call-count assertions (e.g. expected exactly
once, received 5 with pane keys belonging to other tests).

Root cause is cross-test async leakage: pane foreground-agent trackers
kick off `confirmForegroundProcess` reads whose continuations settle on
the microtask queue, not on fake timers. A test that ends with one still
in flight leaks it into the next test, where it resolves against that
test's fresh store mock and calls clearAgentLaunchConfig with the prior
test's pane key.

Drain pending microtasks in afterEach (before tearing down fake timers,
while the test still owns its store mock) so each test absorbs its own
async fallout. No behavior change; full suite stays green (446/446).

Note: this is a CI-timing flake not reproducible locally in isolation
(ordered runs pass repeatedly, incl. under GC pressure); the fix targets
the confirmed leak mechanism and removes the timing dependence.
This commit is contained in:
Brennan Benson
2026-07-17 19:17:33 -07:00
committed by GitHub
parent d1532956fd
commit bd7205684a
@@ -906,7 +906,16 @@ describe('connectPanePty', () => {
globalThis.cancelAnimationFrame = vi.fn()
})
afterEach(() => {
afterEach(async () => {
// Why: pane foreground-agent trackers run async foreground-confirm reads
// (`confirmForegroundProcess`) whose continuations settle on the microtask
// queue, not on fake timers. A test that ends with one still in flight
// leaks it into the NEXT test, where it resolves against that test's fresh
// store mock and calls e.g. clearAgentLaunchConfig with THIS test's pane
// key — a flaky cross-test call-count failure (seen in CI). Drain pending
// microtasks while this test still owns the store mock, before fake timers
// are torn down, so each test absorbs its own async fallout.
await flushAsyncTicks()
vi.useRealTimers()
if (originalRequestAnimationFrame) {
globalThis.requestAnimationFrame = originalRequestAnimationFrame