Skip to content

Commit d0eccc7

Browse files
committed
test(terminal): close mutation-surviving seams in the deferred park latch
Adversarial review mutation testing found four surviving mutants in the latch integration seam (the latch unit tests and both storm repros stayed green through all of them): - evaluateCoverage hardcoded true: an uncoverable deferred tab was silently claimed by watchers (the zero-watcher-darkness direction the coverage gate exists for). New: 'keeps an uncoverable deferred tab out of watcher ownership'. - portal guard dropped from the extracted deferred clause. New: 'never claims a portal-hosted deferred tab for watchers'. - restore policy hardcoded in the hook (and the two new memo deps dropped): the SSH kill switch and paired-capability re-key edges were unproven end-to-end. New: 're-opens the latched verdict on restore-policy changes and settles' — also demonstrates each re-key settles in one evaluation. - prune wiring removed from the hook: a stale FALSE surviving a reveal -> re-defer cycle would strand the re-deferred tab unmounted and unwatched. New: 're-evaluates fresh when a revealed tab is deferred again'. Also resets settings/runtimeStatusByEnvironmentId in beforeEach so the new policy-change case cannot leak state into the runtime-flap case.
1 parent 0a99cc1 commit d0eccc7

1 file changed

Lines changed: 133 additions & 1 deletion

File tree

‎src/renderer/src/components/terminal-pane/terminal-cold-park-reveal-storm.react185.test.tsx‎

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ import { createRoot, type Root } from 'react-dom/client'
3030
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3131
import type { Tab, TabGroup } from '../../../../shared/tab-types'
3232
import type { TerminalTab } from '../../../../shared/terminal-tab-types'
33+
import type { ActivityTerminalPortalTarget } from '../activity/activity-terminal-portal'
3334

3435
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
3536

3637
const harness = vi.hoisted(() => ({
3738
worktreeId: 'repo::/cold-park-reveal-storm',
3839
/** Neutralizes the oscillating input: coverage stops reading watcher state. */
3940
coverageIgnoresWatcherState: false,
41+
/** Models an uncoverable tab (pty-less leaf / no layout): coverage always denies. */
42+
coverageDenied: false,
4043
/** Per-case park policy; {} keeps production 30s timing. */
4144
parkingOverrides: {} as Record<string, number>,
4245
coverageCalls: 0,
@@ -149,6 +152,9 @@ vi.mock('./terminal-parked-tab-watchers', async () => {
149152
// — state that watcher start/dispose and the pane lifecycle mutate.
150153
canWatcherCoverParkedTerminalTab: (_worktreeId: string, tab: { id: string }) => {
151154
harness.coverageCalls += 1
155+
if (harness.coverageDenied) {
156+
return false
157+
}
152158
if (harness.coverageIgnoresWatcherState) {
153159
return true
154160
}
@@ -226,7 +232,10 @@ function unifiedTerminalTab(id: string): Tab {
226232

227233
function renderRevealStormLayer(
228234
root: Root,
229-
options: { activationDeferredMountTabIds: ReadonlySet<string> | null }
235+
options: {
236+
activationDeferredMountTabIds: ReadonlySet<string> | null
237+
activityTerminalPortals?: ActivityTerminalPortalTarget[]
238+
}
230239
): unknown {
231240
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
232241
let thrown: unknown = null
@@ -240,6 +249,9 @@ function renderRevealStormLayer(
240249
isWorktreeActive={true}
241250
coldParkTerminalPanes={false}
242251
activationDeferredMountTabIds={options.activationDeferredMountTabIds}
252+
{...(options.activityTerminalPortals
253+
? { activityTerminalPortals: options.activityTerminalPortals }
254+
: {})}
243255
/>
244256
</StrictMode>
245257
)
@@ -257,6 +269,7 @@ describe('TerminalPaneOverlayLayer activation-deferred reveal storm', () => {
257269

258270
beforeEach(() => {
259271
harness.coverageIgnoresWatcherState = false
272+
harness.coverageDenied = false
260273
harness.parkingOverrides = {}
261274
harness.crumbs.length = 0
262275
harness.coverageCalls = 0
@@ -269,6 +282,8 @@ describe('TerminalPaneOverlayLayer activation-deferred reveal storm', () => {
269282
const tabs = TAB_IDS.map(terminalTab)
270283
const unifiedTabs = TAB_IDS.map(unifiedTerminalTab)
271284
harnessStore.setState({
285+
settings: {},
286+
runtimeStatusByEnvironmentId: new Map(),
272287
activeGroupIdByWorktree: { [harness.worktreeId]: GROUP_ID },
273288
groupsByWorktree: {
274289
[harness.worktreeId]: [
@@ -341,6 +356,123 @@ describe('TerminalPaneOverlayLayer activation-deferred reveal storm', () => {
341356
expect(harness.watcherEntries.size).toBe(0)
342357
})
343358

359+
// Why: the latch must hold the REAL verdict — an uncoverable tab (pty-less
360+
// leaf / no layout) claimed by watchers would silently drop its bytes.
361+
it('keeps an uncoverable deferred tab out of watcher ownership', () => {
362+
harness.coverageDenied = true
363+
root = createRoot(container)
364+
const thrown = renderRevealStormLayer(root, {
365+
activationDeferredMountTabIds: new Set([DEFERRED_TAB_ID])
366+
})
367+
368+
expect(thrown).toBeNull()
369+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(false)
370+
// The unparked tab mounts a pane instead of parking into darkness.
371+
expect(harness.slotMounts).toBeGreaterThan(0)
372+
expect(harness.syncCalls).toBeLessThan(10)
373+
})
374+
375+
// Why: portal-hosted tabs render through Activity — watcher ownership there
376+
// would double-attach the pty byte stream.
377+
it('never claims a portal-hosted deferred tab for watchers', () => {
378+
root = createRoot(container)
379+
const portal = {
380+
slotId: 'slot-1',
381+
requestToken: 'req-1',
382+
target: document.createElement('div'),
383+
worktreeId: harness.worktreeId,
384+
tabId: DEFERRED_TAB_ID,
385+
paneKey: 'pane-1',
386+
active: true
387+
} as ActivityTerminalPortalTarget
388+
const thrown = renderRevealStormLayer(root, {
389+
activationDeferredMountTabIds: new Set([DEFERRED_TAB_ID]),
390+
activityTerminalPortals: [portal]
391+
})
392+
393+
expect(thrown).toBeNull()
394+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(false)
395+
})
396+
397+
// Why: the latch may not outlive deferral — a stale FALSE surviving a
398+
// reveal → re-defer cycle would leave the re-deferred tab unmounted with no
399+
// watcher (zero-watcher darkness), the failure the coverage gate exists for.
400+
it('re-evaluates fresh when a revealed tab is deferred again', () => {
401+
harness.coverageDenied = true
402+
root = createRoot(container)
403+
// Deferred while uncoverable: latched FALSE, no watcher, pane mounts.
404+
let thrown = renderRevealStormLayer(root, {
405+
activationDeferredMountTabIds: new Set([DEFERRED_TAB_ID])
406+
})
407+
expect(thrown).toBeNull()
408+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(false)
409+
410+
// Revealed: leaves the deferred set, which prunes its latch entry.
411+
thrown = renderRevealStormLayer(root, { activationDeferredMountTabIds: null })
412+
expect(thrown).toBeNull()
413+
414+
// Re-deferred after coverage returns (same pty/generation/policy → same
415+
// material key): the verdict must be re-asked, not replayed.
416+
harness.coverageDenied = false
417+
harness.coverageIgnoresWatcherState = true
418+
thrown = renderRevealStormLayer(root, {
419+
activationDeferredMountTabIds: new Set([DEFERRED_TAB_ID])
420+
})
421+
expect(thrown).toBeNull()
422+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(true)
423+
})
424+
425+
// Why: the material key must be wired to the LIVE restore policy — the SSH
426+
// kill switch and paired-runtime capability changes are the documented
427+
// re-open edges, and each re-key must settle in one evaluation, not loop.
428+
it('re-opens the latched verdict on restore-policy changes and settles', () => {
429+
root = createRoot(container)
430+
const thrown = renderRevealStormLayer(root, {
431+
activationDeferredMountTabIds: new Set([DEFERRED_TAB_ID])
432+
})
433+
expect(thrown).toBeNull()
434+
// Latched TRUE at deferral entry: watchers own the tab.
435+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(true)
436+
437+
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
438+
let policyThrow: unknown = null
439+
try {
440+
// SSH parking kill switch: re-keys the latch; the fresh evaluation sees
441+
// the watcher-feedback verdict (false) and releases the tab.
442+
act(() => {
443+
harnessStore.setState({ settings: { terminalSshViewParking: false } })
444+
})
445+
} catch (error) {
446+
policyThrow = error
447+
}
448+
consoleError.mockRestore()
449+
expect(policyThrow).toBeNull()
450+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(false)
451+
452+
const syncCallsBeforePairedChange = harness.syncCalls
453+
const consoleError2 = vi.spyOn(console, 'error').mockImplementation(() => {})
454+
let pairedThrow: unknown = null
455+
try {
456+
// Paired-runtime capability change: re-keys again; coverage now passes
457+
// (no watcher entry) and the tab returns to watcher ownership.
458+
act(() => {
459+
harnessStore.setState({
460+
runtimeStatusByEnvironmentId: new Map([
461+
['env-latch', { status: { capabilities: ['terminal.paired-parking.v1'] } }]
462+
])
463+
})
464+
})
465+
} catch (error) {
466+
pairedThrow = error
467+
}
468+
consoleError2.mockRestore()
469+
expect(pairedThrow).toBeNull()
470+
expect(harness.watcherEntries.has(DEFERRED_TAB_ID)).toBe(true)
471+
// Each re-key settles instead of re-entering the storm.
472+
expect(harness.syncCalls - syncCallsBeforePairedChange).toBeLessThan(10)
473+
expect(harness.slotMounts).toBeLessThan(20)
474+
})
475+
344476
// Field signature from crash 60d84e6d: churn crumbs with trigger=window
345477
// (flips=38 over ~15s ≈ 2.5/s), migrating across web-terminal tab ids —
346478
// paired-runtime capability flapping. At that cadence the burst pin

0 commit comments

Comments
 (0)