Files
orca/src/shared/agent-process-presence-probe.test.ts
T
Neil e65a1dd553 Keep idle Claude sessions selected during nested Codex work (#29386)
* Preserve idle foreground Claude session ownership through nested hooks

* Discard obsolete hook probes and cancel queued restarts
2026-10-15 22:36:36 -07:00

62 lines
2.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
probeAgentProcessPresence,
type AgentProcessObservation
} from './agent-process-presence-probe'
import type { AgentProcessIdentity } from './agent-process-presence'
const identity: AgentProcessIdentity = { pid: 4242, platform: 'linux', startTime: 'boot:100' }
describe('single-process presence evidence', () => {
it('reports suspension only after matching the process identity', async () => {
const observe = vi.fn()
const read = vi.fn(async (): Promise<AgentProcessObservation> => ({
verdict: 'live',
startTime: identity.startTime,
zombie: false,
stopped: true
}))
expect(await probeAgentProcessPresence(identity, read, 'linux', observe)).toBe('unverifiable')
expect(observe).toHaveBeenCalledExactlyOnceWith(true)
observe.mockClear()
read.mockResolvedValue({ verdict: 'unverifiable' })
expect(await probeAgentProcessPresence(identity, read, 'linux', observe)).toBe('unverifiable')
expect(observe).not.toHaveBeenCalled()
read.mockResolvedValue({
verdict: 'live',
startTime: 'another-birth',
zombie: false,
stopped: true
})
expect(await probeAgentProcessPresence(identity, read, 'linux', observe)).toBe('exited')
expect(observe).not.toHaveBeenCalled()
})
it.each<[AgentProcessObservation, string]>([
[{ verdict: 'live', startTime: 'boot:100', zombie: false }, 'live'],
[{ verdict: 'live', startTime: 'boot:101', zombie: false }, 'exited'],
[{ verdict: 'live', startTime: 'boot:100', zombie: true }, 'exited'],
[{ verdict: 'live', startTime: 'boot:100', zombie: false, stopped: true }, 'unverifiable'],
[{ verdict: 'exited' }, 'exited'],
[{ verdict: 'unverifiable' }, 'unverifiable']
])('judges %j as %s', async (observation, verdict) => {
const read = vi.fn(async () => observation)
expect(await probeAgentProcessPresence(identity, read, 'linux')).toBe(verdict)
expect(read).toHaveBeenCalledExactlyOnceWith(4242)
})
it('keeps a timeout or permission error unverifiable', async () => {
const read = vi.fn(async () => {
throw new Error('EPERM')
})
expect(await probeAgentProcessPresence(identity, read, 'linux')).toBe('unverifiable')
})
it('does not inspect a missing identity or a different execution platform', async () => {
const read = vi.fn()
expect(await probeAgentProcessPresence(undefined, read, 'linux')).toBe('unverifiable')
expect(await probeAgentProcessPresence(identity, read, 'win32')).toBe('unverifiable')
expect(read).not.toHaveBeenCalled()
})
})