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.

16.2 KB
Raw Blame
import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest'
import type { DiffComment } from '../../../src/shared/diff-comment-types'
import type { RpcClient } from '../transport/rpc-client'
import type { RpcResponse } from '../transport/types'
import type { ReviewScreenState } from './mobile-diff-review-screen-model'
import type { ReviewSheetIntents } from './mobile-diff-review-sheets'
import {
  isMobileNativeChatInputStale,
  markMobileNativeChatInputStale,
  resetMobileNativeChatStaleInputForTests
} from './native-chat/mobile-native-chat-stale-input'
import { useMobileDiffReviewSendActions } from './use-mobile-diff-review-send-actions'

function requestPortRpcClient(sendRequest: RpcClient['sendRequest']): RpcClient {
  return {
    sendRequest,
    subscribe: () => () => {},
    updateTerminalSubscriptionViewport: () => {},
    getState: () => 'connected',
    getReconnectAttempt: () => 0,
    getLastConnectedAt: () => null,
    onStateChange: () => () => {},
    notifyForeground: () => {},
    close: () => {}
  }
}

type SendActions = ReturnType<typeof useMobileDiffReviewSendActions>
type SendActionArgs = Parameters<typeof useMobileDiffReviewSendActions>[0]

vi.mock('../platform/haptics', () => ({ triggerSuccess: vi.fn() }))
// Return the pasteboard’s real success boolean; undefined would exercise only refusal unnoticed.
const clipboardMock = vi.hoisted(() => ({ setStringAsync: vi.fn() }))
vi.mock('expo-clipboard', () => clipboardMock)

function sendResponse(accepted: boolean) {
  return {
    id: 'send',
    ok: true as const,
    result: { send: { accepted } },
    _meta: { runtimeId: 'runtime' }
  }
}

const COMMENT: DiffComment = {
  id: 'comment-1',
  worktreeId: 'wt-1',
  filePath: 'src/a.ts',
  lineNumber: 3,
  body: 'rename this',
  createdAt: 1,
  side: 'modified'
}

const LAUNCH_CAPABILITIES = [
  'agent.launch.v2',
  'agent.launch.replay.v1',
  'agent.launch.replay-required.v1'
]

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

function launchedReply(promptOutcome: 'handed-to-terminal' | 'not-delivered', warning?: string) {
  return rpcReply({
    outcome: { kind: 'terminal', handle: 'term-1' },
    worktreeId: 'wt-1',
    receipt: { mode: 'terminal', preferred: 'terminal', reason: 'user_default', detail: 'd' },
    prompt: { delivery: 'submit', outcome: promptOutcome },
    ...(warning ? { warning } : {})
  })
}

function launchClient(
  promptOutcome: 'handed-to-terminal' | 'not-delivered',
  launchReply: () => Promise<RpcResponse> = async () => launchedReply(promptOutcome)
) {
  const sendRequest = vi.fn(async (method: string, _params?: unknown): 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'])
    }
    if (method === 'agent.launchReplay') {
      return launchReply()
    }
    throw new Error(`Unexpected request: ${method}`)
  })
  return { client: requestPortRpcClient(sendRequest), sendRequest }
}

const READY: Extract<ReviewScreenState, { kind: 'ready' }> = {
  kind: 'ready',
  status: {
    entries: [],
    conflictOperation: 'unknown',
    branch: undefined,
    head: undefined,
    upstreamStatus: undefined
  },
  branchCompare: null,
  comments: [COMMENT],
  reviewState: { version: 1, files: {} }
}

describe('useMobileDiffReviewSendActions', () => {
  let renderer: ReactTestRenderer | null = null
  let actions: SendActions | null = null
  let mountedClient: RpcClient | null = null
  let setActionError: Mock<SendActionArgs['setActionError']>
  let sheets: {
    openSheet: Mock<ReviewSheetIntents['openSheet']>
    closeSheet: Mock<ReviewSheetIntents['closeSheet']>
    updateSendSheet: Mock<ReviewSheetIntents['updateSendSheet']>
  }
  let saveCommentsAndReviewState: Mock<SendActionArgs['saveCommentsAndReviewState']>
  let screenState: ReviewScreenState = READY

  beforeEach(() => {
    screenState = READY
    clipboardMock.setStringAsync.mockReset().mockResolvedValue(true)
    resetMobileNativeChatStaleInputForTests()
    setActionError = vi.fn()
    sheets = { openSheet: vi.fn(), closeSheet: vi.fn(), updateSendSheet: vi.fn() }
    saveCommentsAndReviewState = vi.fn().mockResolvedValue(undefined)
  })

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

  function Harness(): null {
    actions = useMobileDiffReviewSendActions({
      client: mountedClient,
      connState: 'connected',
      hostCapabilities: LAUNCH_CAPABILITIES,
      worktreeId: 'wt-1',
      screenState,
      setActionError,
      sheets,
      saveCommentsAndReviewState
    })
    return null
  }

  async function mount(client: RpcClient): Promise<void> {
    mountedClient = client
    await act(async () => {
      renderer = create(createElement(Harness))
    })
  }

  async function mountWithoutClient(): Promise<void> {
    mountedClient = null
    await act(async () => {
      renderer = create(createElement(Harness))
    })
  }

  it('copies the notes through the platform seam and says so', async () => {
    await mountWithoutClient()
    await act(async () => {
      await actions?.copyNotes()
    })
    expect(clipboardMock.setStringAsync).toHaveBeenCalledExactlyOnceWith(
      'File: src/a.ts\nLine: 3\nUser comment: "rename this"'
    )
    expect(setActionError).toHaveBeenLastCalledWith('Review notes copied')
  })

  it('reports a refused copy instead of claiming it copied', async () => {
    // Web clipboard refusal rejects a fire-and-forget caller, so feedback must settle without an unhandled rejection.
    clipboardMock.setStringAsync.mockResolvedValue(false)
    await mountWithoutClient()
    await act(async () => {
      await actions?.copyNotes()
    })
    expect(setActionError).toHaveBeenLastCalledWith('the clipboard did not accept this text')
  })

  it('heals a marked terminal BEFORE submitting the notes', async () => {
    const sendRequest = vi.fn().mockResolvedValue(sendResponse(true))
    await mount({ sendRequest } as unknown as RpcClient)
    markMobileNativeChatInputStale('terminal-1')

    await act(async () => {
      await actions?.sendPromptToTerminal('terminal-1', [COMMENT])
    })

    expect(sendRequest).toHaveBeenCalledTimes(2)
    // Clear before the Enter-carrying notes write so an orphaned paste cannot join the prompt.
    expect(sendRequest.mock.calls[0]?.[1]).toMatchObject({
      terminal: 'terminal-1',
      text: '\x15',
      enter: false
    })
    expect(sendRequest.mock.calls[1]?.[1]).toMatchObject({ terminal: 'terminal-1', enter: true })
    expect(String(sendRequest.mock.calls[1]?.[1]?.text)).toContain('rename this')
    expect(isMobileNativeChatInputStale('terminal-1')).toBe(false)
    expect(setActionError).toHaveBeenCalledWith('Review notes sent')
  })

  it('does not submit when the heal reports the line is not safe', async () => {
    const sendRequest = vi.fn().mockResolvedValue(sendResponse(false))
    await mount({ sendRequest } as unknown as RpcClient)
    markMobileNativeChatInputStale('terminal-1')

    let error: unknown
    await act(async () => {
      error = await actions?.sendPromptToTerminal('terminal-1', [COMMENT]).catch((err) => err)
    })

    expect(error).toBeInstanceOf(Error)
    expect((error as Error).message).toBe('Failed to send notes')
    expect(sendRequest).toHaveBeenCalledTimes(1)
    expect(sendRequest.mock.calls[0]?.[1]).toMatchObject({ text: '\x15', enter: false })
    expect(saveCommentsAndReviewState).not.toHaveBeenCalled()
    expect(setActionError).not.toHaveBeenCalled()
    expect(sheets.closeSheet).not.toHaveBeenCalled()
    expect(isMobileNativeChatInputStale('terminal-1')).toBe(true)
  })

  it('keeps the marker and skips the notes when the clear throws', async () => {
    const sendRequest = vi.fn().mockRejectedValue(new Error('offline'))
    await mount({ sendRequest } as unknown as RpcClient)
    markMobileNativeChatInputStale('terminal-1')

    let error: unknown
    await act(async () => {
      error = await actions?.sendPromptToTerminal('terminal-1', [COMMENT]).catch((err) => err)
    })

    expect((error as Error).message).toBe('Failed to send notes')
    expect(sendRequest).toHaveBeenCalledTimes(1)
    expect(saveCommentsAndReviewState).not.toHaveBeenCalled()
    expect(isMobileNativeChatInputStale('terminal-1')).toBe(true)
  })

  it('sends an unmarked terminal with no extra RPC', async () => {
    const sendRequest = vi.fn().mockResolvedValue(sendResponse(true))
    await mount({ sendRequest } as unknown as RpcClient)

    await act(async () => {
      await actions?.sendPromptToTerminal('terminal-1', [COMMENT])
    })

    expect(sendRequest).toHaveBeenCalledTimes(1)
    expect(sendRequest.mock.calls[0]?.[0]).toBe('terminal.send')
    expect(sendRequest.mock.calls[0]?.[1]).toMatchObject({ terminal: 'terminal-1', enter: true })
    expect(saveCommentsAndReviewState).toHaveBeenCalledTimes(1)
    expect(setActionError).toHaveBeenCalledWith('Review notes sent')
    expect(sheets.closeSheet).toHaveBeenCalledWith('send')
  })

  it('only heals the terminal that was marked', async () => {
    const sendRequest = vi.fn().mockResolvedValue(sendResponse(true))
    await mount({ sendRequest } as unknown as RpcClient)
    markMobileNativeChatInputStale('terminal-other')

    await act(async () => {
      await actions?.sendPromptToTerminal('terminal-1', [COMMENT])
    })

    expect(sendRequest).toHaveBeenCalledTimes(1)
    expect(isMobileNativeChatInputStale('terminal-other')).toBe(true)
  })

  it('still reports a rejected terminal.send after a successful heal', async () => {
    const sendRequest = vi
      .fn()
      .mockResolvedValueOnce(sendResponse(true))
      .mockResolvedValueOnce(sendResponse(false))
    await mount({ sendRequest } as unknown as RpcClient)
    markMobileNativeChatInputStale('terminal-1')

    let error: unknown
    await act(async () => {
      error = await actions?.sendPromptToTerminal('terminal-1', [COMMENT]).catch((err) => err)
    })

    expect((error as Error).message).toBe('Terminal input is locked')
    expect(saveCommentsAndReviewState).not.toHaveBeenCalled()
  })

  it('reports a failed terminal.send response', async () => {
    const sendRequest = vi
      .fn()
      .mockResolvedValue({ id: 'send', ok: false, error: { message: 'pane gone' } })
    await mount({ sendRequest } as unknown as RpcClient)

    let error: unknown
    await act(async () => {
      error = await actions?.sendPromptToTerminal('terminal-1', [COMMENT]).catch((err) => err)
    })

    expect((error as Error).message).toBe('pane gone')
    expect(saveCommentsAndReviewState).not.toHaveBeenCalled()
  })

  it('starts a new agent with the notes through the host and marks them sent', async () => {
    const { client, sendRequest } = launchClient('handed-to-terminal')
    await mount(client)
    await act(async () => {
      await actions?.createTerminalAndSend([COMMENT])
    })
    const launch = sendRequest.mock.calls.find(([method]) => method === 'agent.launchReplay')
    expect(launch?.[1]).toMatchObject({
      agent: 'codex',
      target: { kind: 'existing', worktree: 'id:wt-1' },
      prompt: {
        delivery: 'submit',
        // Desktop sends the formatted notes alone.
        text: ['File: src/a.ts', 'Line: 3', 'User comment: "rename this"'].join('\n')
      },
      launchSource: 'notes_send'
    })
    expect(sendRequest.mock.calls.some(([method]) => method === 'terminal.send')).toBe(false)
    expect(saveCommentsAndReviewState).toHaveBeenCalledOnce()
    expect(setActionError).toHaveBeenLastCalledWith('Review notes sent')
  })

  it('keeps saying the notes were sent when the host adds a warning', async () => {
    await mount(
      launchClient('handed-to-terminal', async () =>
        launchedReply('handed-to-terminal', 'the requested arguments were ignored.')
      ).client
    )
    await act(async () => {
      await actions?.createTerminalAndSend([COMMENT])
    })
    expect(saveCommentsAndReviewState).toHaveBeenCalledOnce()
    expect(setActionError).toHaveBeenLastCalledWith(
      'Review notes sent. the requested arguments were ignored.'
    )
  })

  it('says it is waiting for the desktop instead of rejecting when there is no connection', async () => {
    await mountWithoutClient()
    await act(async () => {
      await expect(actions?.createTerminalAndSend([COMMENT])).resolves.toBeUndefined()
    })
    expect(sheets.closeSheet).toHaveBeenCalledWith('send')
    expect(setActionError).toHaveBeenLastCalledWith('Waiting for desktop...')
  })

  it('keeps the notes unsent when the agent started without them', async () => {
    await mount(launchClient('not-delivered').client)
    await act(async () => {
      await actions?.createTerminalAndSend([COMMENT])
    })
    expect(saveCommentsAndReviewState).not.toHaveBeenCalled()
    expect(setActionError).toHaveBeenLastCalledWith(
      "The agent started, but the notes weren't sent. Use Copy Notes to paste them."
    )
  })

  it('shows a launch that did not start on the review screen instead of rejecting', async () => {
    await mount(
      launchClient('handed-to-terminal', async () => ({
        id: 'rpc',
        ok: false,
        error: { code: 'selector_not_found', message: 'Workspace not found' },
        _meta: { runtimeId: 'r' }
      })).client
    )
    await act(async () => {
      await actions?.createTerminalAndSend([COMMENT])
    })
    expect(sheets.closeSheet).toHaveBeenCalledWith('send')
    expect(setActionError).toHaveBeenLastCalledWith('Workspace not found')
    expect(saveCommentsAndReviewState).not.toHaveBeenCalled()
  })

  it('starts one agent for a double tap and keeps notes written while it starts', async () => {
    let answerLaunch: (reply: RpcResponse) => void = () => {}
    const { client, sendRequest } = launchClient(
      'handed-to-terminal',
      () =>
        new Promise<RpcResponse>((resolve) => {
          answerLaunch = resolve
        })
    )
    await mount(client)
    let first: Promise<void> | undefined
    await act(async () => {
      first = actions?.createTerminalAndSend([COMMENT])
      await actions?.createTerminalAndSend([COMMENT])
    })
    expect(setActionError).toHaveBeenLastCalledWith('Starting an agent...')
    const written: DiffComment = { ...COMMENT, id: 'comment-2', body: 'written meanwhile' }
    screenState = { ...READY, comments: [COMMENT, written] }
    await act(async () => {
      renderer?.update(createElement(Harness))
    })
    await act(async () => {
      answerLaunch(launchedReply('handed-to-terminal'))
      await first
    })
    expect(
      sendRequest.mock.calls.filter(([method]) => method === 'agent.launchReplay')
    ).toHaveLength(1)
    expect(saveCommentsAndReviewState).toHaveBeenCalledWith(
      [
        expect.objectContaining({ id: 'comment-1', sentAt: expect.any(Number) }),
        expect.not.objectContaining({ sentAt: expect.anything() })
      ],
      READY.reviewState
    )
    expect(saveCommentsAndReviewState.mock.calls[0]?.[0]?.[1]?.id).toBe('comment-2')
    expect(setActionError).toHaveBeenLastCalledWith('Review notes sent')
  })

  // The save rolls back to the screen it was created with, so a stale one would drop newer notes.
  it('marks the notes sent through the save of the latest render, not the one from the tap', async () => {
    let answerLaunch: (reply: RpcResponse) => void = () => {}
    const { client } = launchClient(
      'handed-to-terminal',
      () =>
        new Promise<RpcResponse>((resolve) => {
          answerLaunch = resolve
        })
    )
    await mount(client)
    let first: Promise<void> | undefined
    await act(async () => {
      first = actions?.createTerminalAndSend([COMMENT])
    })
    const tapTimeSave = saveCommentsAndReviewState
    saveCommentsAndReviewState = vi.fn().mockResolvedValue(undefined)
    await act(async () => {
      renderer?.update(createElement(Harness))
    })
    await act(async () => {
      answerLaunch(launchedReply('handed-to-terminal'))
      await first
    })
    expect(tapTimeSave).not.toHaveBeenCalled()
    expect(saveCommentsAndReviewState).toHaveBeenCalledOnce()
  })
})