Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

4.2 KB
Raw Blame
import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { RpcClient } from '../transport/rpc-client'
import type { RpcResponse } from '../transport/types'
import type { MobileCommitFailureRecovery } from './mobile-commit-failure-recovery'

vi.mock('../platform/haptics', () => ({ triggerError: vi.fn(), triggerSuccess: vi.fn() }))
vi.mock('../components/host-protocol-gates-context', () => ({
  useHostProtocolGates: () => ({
    hostCapabilities: [
      'agent.launch.v2',
      'agent.launch.replay.v1',
      'agent.launch.replay-required.v1'
    ],
    statusPending: false,
    statusReadable: true
  })
}))

import { useMobileCommitFailureRecovery } from './use-mobile-commit-failure-recovery'
import { stubRpcClient } from '../transport/rpc-client-test-stub'

type Recovery = ReturnType<typeof useMobileCommitFailureRecovery>

function rpcReply(result: unknown): RpcResponse {
  return { id: 'rpc', ok: true, result, _meta: { runtimeId: 'r' } }
}

const LAUNCHED_WITH_WARNING = rpcReply({
  outcome: { kind: 'terminal', handle: 'term-1' },
  worktreeId: 'wt-1',
  receipt: { mode: 'terminal', preferred: 'terminal', reason: 'user_default', detail: 'd' },
  prompt: { delivery: 'submit', outcome: 'handed-to-terminal' },
  warning: 'the requested arguments were ignored.'
})

function hostClient(launchReply: () => Promise<RpcResponse>) {
  const sendRequest = vi.fn(async (method: string): Promise<RpcResponse> => {
    if (method === 'repo.list') {
      return rpcReply({ repos: [{ id: 'wt-1' }] })
    }
    if (method === 'settings.get') {
      return rpcReply({ settings: { defaultTuiAgent: 'codex' } })
    }
    if (method === 'preflight.detectAgents') {
      return rpcReply(['codex'])
    }
    return launchReply()
  })
  return { client: stubRpcClient({ sendRequest }), sendRequest }
}

const FAILURE: MobileCommitFailureRecovery = {
  error: 'pre-commit hook failed',
  commitMessage: 'wip',
  stagedEntries: []
}

describe('useMobileCommitFailureRecovery', () => {
  let renderer: ReactTestRenderer | null = null
  let recovery: Recovery | null = null

  afterEach(() => {
    act(() => renderer?.unmount())
    renderer = null
    recovery = null
  })

  function mount(client: RpcClient, failure: MobileCommitFailureRecovery | null = FAILURE) {
    function Harness(props: { failure: MobileCommitFailureRecovery | null }): null {
      recovery = useMobileCommitFailureRecovery({
        client,
        connState: 'connected',
        worktreeId: 'wt-1',
        workspaceLabel: 'feature-login',
        failure: props.failure
      })
      return null
    }
    act(() => {
      renderer = create(createElement(Harness, { failure }))
    })
    return (next: MobileCommitFailureRecovery | null) =>
      act(() => renderer?.update(createElement(Harness, { failure: next })))
  }

  it('reports the host warning on a launch that went ahead as a warning, not an error', async () => {
    const rerender = mount(hostClient(async () => LAUNCHED_WITH_WARNING).client)
    let succeeded: boolean | undefined
    await act(async () => {
      succeeded = await recovery?.launch()
    })
    expect(succeeded).toBe(true)
    expect(recovery?.launchError).toBeNull()
    expect(recovery?.launchWarning).toBe('the requested arguments were ignored.')
    expect(recovery?.launchSuccess).toBe('Agent started in feature-login')
    rerender({ ...FAILURE, error: 'another hook failed' })
    expect(recovery?.launchWarning).toBeNull()
    expect(recovery?.launchSuccess).toBeNull()
  })

  it('starts one agent for two taps before the first one re-renders', async () => {
    let release: () => void = () => {}
    const answered = new Promise<void>((resolve) => (release = resolve))
    const { client, sendRequest } = hostClient(async () => {
      await answered
      return LAUNCHED_WITH_WARNING
    })
    mount(client)
    const launch = recovery?.launch
    let taps: boolean[] = []
    await act(async () => {
      const both = Promise.all([launch?.(), launch?.()])
      release()
      taps = (await both).map(Boolean)
    })
    expect(taps).toEqual([true, false])
    expect(
      sendRequest.mock.calls.filter(([method]) => method === 'agent.launchReplay')
    ).toHaveLength(1)
  })
})