Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.9 KB
Raw Blame
import { readInlineScreen } from '../../testing/inlineScreen.js'
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { RegisteredSession } from '../../lib/registry.js'
import { activityRuntimeKey } from '../../lib/runtimeActivity.js'
import type { ActivityFrame } from '../../lib/turnActivity.js'
import { activityOf, createTurnActivity, type TurnActivityDeps } from './activity.js'

const live = { agentId: 'a1', sessionId: 's1', engine: 'claude', active: true } as RegisteredSession
const idle = { agentId: 'a2', sessionId: 's2', engine: 'claude', active: false } as RegisteredSession
const shell = { agentId: 'a3', sessionId: 's3', engine: 'terminal', active: true } as RegisteredSession
const sessions = new Map([[live.sessionId, live], [idle.sessionId, idle], [shell.sessionId, shell]])

function setup(over: Partial<TurnActivityDeps> = {}) {
  const deps: TurnActivityDeps = {
    readScreen: readInlineScreen,
    terminals: { capture: vi.fn(async () => ({ state: 'succeeded' as const, value: 'screen' })) },
    bySession: (sessionId) => sessions.get(sessionId),
    sessionTurnOpen: (sessionId) => sessionId === 's1',
    drain: vi.fn(async () => {}),
    nativeActivity: vi.fn(async () => 'idle' as const),
    ...over,
  }
  return { deps, readers: createTurnActivity(deps) }
}

/** What a reader was constructed with. */
const given = (reader: object) => (reader as unknown as { deps: Record<string, (...args: unknown[]) => unknown> }).deps

describe('turn activity', () => {
  afterEach(() => vi.restoreAllMocks())

  it('checks only an active session, keyed to the runtime that runs it', async () => {
    const { deps, readers } = setup()
    const turn = given(readers.turnActivity)
    expect(turn.runtime('s1')).toEqual({ key: activityRuntimeKey(live), turnOpen: true })
    expect(turn.runtime('s2')).toBeUndefined()
    expect(turn.runtime('nobody')).toBeUndefined()
    await turn.drain('s1')
    expect(deps.drain).toHaveBeenCalledWith('s1')
    const read = vi.spyOn(readers.runtimeActivity, 'read').mockResolvedValue('working')
    expect(await turn.probe('s1')).toBe('working')
    expect(read).toHaveBeenCalledWith(live)
    expect(await turn.probe('s2')).toBe('unknown')
  })

  it('reads Codex\'s activity from its own server (through its worker), and every other runtime from what its pane shows', async () => {
    const { deps, readers } = setup()
    const runtime = given(readers.runtimeActivity)
    expect(await runtime.codex(live)).toBe('idle')
    expect(deps.nativeActivity).toHaveBeenCalledWith(live)
    expect(await runtime.capture(live)).toBe('screen')
    expect(deps.terminals.capture).toHaveBeenCalledWith(live, { mode: 'visible', ansi: true })
    vi.mocked(deps.terminals.capture).mockResolvedValueOnce({ state: 'failed', reason: 'no pane' })
    expect(await runtime.capture(live)).toBeNull()
  })

  it('reports the working state of the live agents asked about, and leaves out those that are not live', () => {
    const frames: Record<string, ActivityFrame | null> = { a1: { state: 'working' } as ActivityFrame, a3: null }
    const byAgent = (id: string) => [live, shell].find((s) => s.agentId === id)
    expect(activityOf(['a1', 'a3', 'gone'], byAgent, (s) => frames[s.agentId] ?? null)).toEqual({ a1: 'working', a3: 'unknown' })
    expect(activityOf(['a3'], byAgent, () => null)).toEqual({ a3: 'unknown' })
    expect(activityOf([], byAgent, () => null)).toEqual({})
  })

  it('shows the app an agent\'s activity, and none for a plain terminal', () => {
    const { readers } = setup()
    const frame = { state: 'working' } as ActivityFrame
    const snapshot = vi.spyOn(readers.turnActivity, 'snapshot').mockReturnValueOnce(frame).mockReturnValueOnce(undefined)
    expect(readers.activityFrame(live)).toBe(frame)
    expect(readers.activityFrame(live)).toBeNull()
    expect(readers.activityFrame(shell)).toBeNull()
    expect(snapshot).toHaveBeenCalledTimes(2)
  })
})