import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { setImmediate as nextNativeTurn } from 'node:timers/promises' const { spawnMock, resolveForegroundMock } = vi.hoisted(() => ({ spawnMock: vi.fn(), resolveForegroundMock: vi.fn() })) vi.mock('node-pty', () => ({ spawn: spawnMock })) vi.mock('../../src/main/pty/shell-startup/pwsh', () => ({ isPwshAvailable: vi.fn(() => false) })) vi.mock('../../src/main/providers/windows-powershell-executable', () => ({ resolveWindowsPowerShellExecutablePath: () => 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', resolveWindowsPowerShellSpawnChain: () => [ 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' ], getWindowsCmdPath: () => 'C:\\Windows\\System32\\cmd.exe' })) vi.mock('../../src/main/providers/agent-foreground-process', () => ({ resolveAgentForegroundProcessWithAvailability: async (...args: unknown[]) => { const value = await resolveForegroundMock(...args) return value && typeof value === 'object' && 'available' in value ? value : { available: true, processName: value } } })) import { createPtySubprocess } from '../../src/main/daemon/pty-subprocess/pty-subprocess' import { createPtyProcessEventEmitter } from '../../src/main/daemon/pty-subprocess/testing/pty-subprocess-test-harness' import { createPaneForegroundAgentTracker } from '../../src/renderer/src/components/terminal-pane/pty-connection/agent-identity/foreground-process/pane-foreground-agent-tracker' import type { PaneForegroundAgentEntry } from '../../src/renderer/src/store/slices/agent-status/pane-foreground-agent' function mockWindowsPty() { return { pid: 12345, process: 'powershell.exe', write: vi.fn(), resize: vi.fn(), kill: vi.fn(), ...createPtyProcessEventEmitter() } } describe('daemon foreground confirmation composes with pane tracking', () => { let platform: PropertyDescriptor | undefined beforeEach(() => { vi.useFakeTimers() spawnMock.mockReset() resolveForegroundMock.mockReset() platform = Object.getOwnPropertyDescriptor(process, 'platform') Object.defineProperty(process, 'platform', { value: 'win32' }) }) afterEach(() => { vi.useRealTimers() if (platform) { Object.defineProperty(process, 'platform', platform) } }) async function createComposedTracker(publish: (entry: PaneForegroundAgentEntry) => void) { const proc = mockWindowsPty() spawnMock.mockReturnValue(proc) const pending = createPtySubprocess({ sessionId: 'test', cols: 80, rows: 24 }) await vi.waitFor(() => expect(spawnMock).toHaveBeenCalledOnce()) await nextNativeTurn() proc._simulateData('\x1b[c') const handle = await pending expect(publish).not.toHaveBeenCalled() const tracker = createPaneForegroundAgentTracker({ getPtyId: () => 'pty-1', isTrackablePtyId: () => true, readForegroundProcess: () => handle.confirmForegroundProcess!(), publish, hasKnownAgentIdentity: () => true }) return { handle, tracker } } it('keeps a restored Droid through a scan resolving after the old cache window', async () => { let resolveFresh!: (value: string) => void resolveForegroundMock.mockReturnValue( new Promise((resolve) => { resolveFresh = resolve }) ) const publish = vi.fn() const { tracker } = await createComposedTracker(publish) tracker.onVisiblePtyBound(true) await vi.advanceTimersByTimeAsync(350) await vi.advanceTimersByTimeAsync(2_000) expect(publish).not.toHaveBeenCalled() resolveFresh('droid') await vi.advanceTimersByTimeAsync(0) expect(publish).toHaveBeenCalledExactlyOnceWith({ agent: 'droid', agentEvidence: 'process-read', routingTrusted: true, shellForeground: false }) expect(resolveForegroundMock).toHaveBeenCalledTimes(1) }) it('clears launch identity after one fresh scan confirms a real shell exit', async () => { resolveForegroundMock.mockResolvedValue('powershell.exe') const publish = vi.fn() const { tracker } = await createComposedTracker(publish) tracker.onCommandFinished() await vi.advanceTimersByTimeAsync(350) expect(publish).toHaveBeenCalledExactlyOnceWith({ agent: null, shellForeground: true }) expect(resolveForegroundMock).toHaveBeenCalledTimes(1) }) it('fails closed without claiming shell when every fresh scan is unavailable', async () => { resolveForegroundMock.mockResolvedValue({ available: false, processName: 'powershell.exe' }) const publish = vi.fn() const { tracker } = await createComposedTracker(publish) tracker.onCommandFinished() await vi.advanceTimersByTimeAsync(350 + 1_200 + 6_000) expect(publish).toHaveBeenCalledExactlyOnceWith({ agent: null, shellForeground: false }) expect(resolveForegroundMock).toHaveBeenCalledTimes(3) }) })