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.

13.0 KB
Raw Blame
import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { MobileNativeChatSendOutcome } from '../mobile-native-chat-send'

const sendWithOutcome = vi.fn()
const clearInputWrite = vi.fn()
const typeCommandWithOutcome = vi.fn()
vi.mock('../mobile-native-chat-send', () => ({
  sendMobileNativeChatMessageWithOutcome: (...args: unknown[]) => sendWithOutcome(...args),
  typeMobileNativeChatCommandWithOutcome: (...args: unknown[]) => typeCommandWithOutcome(...args),
  clearMobileNativeChatInput: (...args: unknown[]) => clearInputWrite(...args),
  openMobileNativeChatSendBudget: () => Date.now() + 15_000,
  MOBILE_NATIVE_CHAT_SEND_TIMEOUT_MS: 15_000,
  MOBILE_NATIVE_CHAT_MIN_WRITE_TIMEOUT_MS: 2_000
}))
vi.mock('../mobile-native-chat-stale-input', () => ({
  healMobileNativeChatStaleInput: () => Promise.resolve(true)
}))

import { useMobileNativeChatMessageSend } from './use-mobile-native-chat-message-send'
import { resetMobileNativeChatTerminalWritesForTests } from '../mobile-native-chat-terminal-write-lock'
import { buildAgentTuiClearInputForText } from '../../../../../src/shared/agent-tui-input-clear'

type Send = ReturnType<typeof useMobileNativeChatMessageSend>

const DRAFT = 'Linked Linear issue: ABC-123\nhttps://linear.app/x/issue/ABC-123'

describe('useMobileNativeChatMessageSend', () => {
  let renderer: ReactTestRenderer | null = null
  let api: Send | null = null
  const acceptSend = vi.fn()
  const captureSendOrigin = vi.fn(() => ({ draftKey: 'k', pendingKey: 'p' }) as never)
  const clearDraftForSend = vi.fn()
  const restoreRejectedDraft = vi.fn()
  const holdUnconfirmedSend = vi.fn()
  const onCommandSend = vi.fn()
  const commandSendRef = { current: onCommandSend }
  const agentRef = { current: null as string | null }
  let onSendError = vi.fn()

  const mount = (
    readSeededLaunchDraftSeed: () => { text: string; createdAt: number | null } | null,
    agent: string | null = 'claude'
  ): void => {
    agentRef.current = agent
    function Probe(): null {
      api = useMobileNativeChatMessageSend({
        client: { sendRequest: vi.fn() } as never,
        enabled: true,
        handleRef: { current: 'term' },
        deviceTokenRef: { current: 'device' },
        agentRef,
        commandSendRef,
        captureSendOrigin,
        readSeededLaunchDraftSeed,
        clearDraftForSend,
        restoreRejectedDraft,
        acceptSend,
        holdUnconfirmedSend,
        onSendError
      })
      return null
    }
    act(() => {
      renderer = create(createElement(Probe))
    })
  }

  const sentArgs = (): {
    text?: string
    resolvedLaunchDraft?: { text: string; createdAt: number }
  } =>
    sendWithOutcome.mock.calls[0]![0] as {
      text?: string
      resolvedLaunchDraft?: { text: string; createdAt: number }
    }
  const clearArgs = (): { clearInput?: string } =>
    (clearInputWrite.mock.calls[0]?.[0] ?? {}) as { clearInput?: string }

  beforeEach(() => {
    sendWithOutcome.mockReset()
    sendWithOutcome.mockResolvedValue('accepted')
    clearInputWrite.mockReset()
    clearInputWrite.mockResolvedValue(true)
    typeCommandWithOutcome.mockReset()
    typeCommandWithOutcome.mockResolvedValue('accepted')
    acceptSend.mockReset()
    captureSendOrigin.mockClear()
    clearDraftForSend.mockReset()
    restoreRejectedDraft.mockReset()
    holdUnconfirmedSend.mockReset()
    onCommandSend.mockReset()
    commandSendRef.current = onCommandSend
    onSendError = vi.fn()
    resetMobileNativeChatTerminalWritesForTests()
  })
  afterEach(() => {
    act(() => {
      renderer?.unmount()
    })
    renderer = null
    api = null
  })

  it('issues the burst as its OWN write, before the body', async () => {
    mount(() => ({ text: DRAFT, createdAt: 1 }))
    await act(async () => {
      await api!.send('hello')
    })
    expect(clearInputWrite).toHaveBeenCalledTimes(1)
    expect(sendWithOutcome).toHaveBeenCalledTimes(1)
    expect(clearInputWrite.mock.invocationCallOrder[0]).toBeLessThan(
      sendWithOutcome.mock.invocationCallOrder[0]!
    )
  })

  it('aborts without sending the body when the clear is rejected', async () => {
    clearInputWrite.mockResolvedValue(false)
    mount(() => ({ text: DRAFT, createdAt: 1 }))
    let result: boolean | undefined
    await act(async () => {
      result = await api!.send('hello')
    })
    expect(result).toBe(false)
    expect(sendWithOutcome).not.toHaveBeenCalled()
  })

  it('keeps the body write free of the dedicated terminal control', async () => {
    // Bundled Ctrl+U can arrive as literal body text; keep controls in a separate write.
    mount(() => ({ text: DRAFT, createdAt: 1 }))
    await act(async () => {
      await api!.send('hello')
    })
    expect(sentArgs()).not.toHaveProperty('clearInputFirst')
    expect(sentArgs().resolvedLaunchDraft).toEqual({ text: DRAFT, createdAt: 1 })
  })

  it('writes a single Ctrl+U separately when no launch draft is parked', async () => {
    mount(() => null)
    await act(async () => {
      await api!.send('hello')
    })
    expect(clearArgs().clearInput).toBe('\x15')
    expect(sentArgs()).not.toHaveProperty('clearInputFirst')
    expect(sentArgs().resolvedLaunchDraft).toBeUndefined()
  })

  it('writes the ordinary clear before the body', async () => {
    mount(() => null)
    await act(async () => {
      await api!.send('hello')
    })
    expect(clearInputWrite).toHaveBeenCalledTimes(1)
    expect(clearInputWrite.mock.invocationCallOrder[0]).toBeLessThan(
      sendWithOutcome.mock.invocationCallOrder[0]!
    )
  })

  it('reads the draft at send time, so a retired seed stops widening the clear', async () => {
    let parked: { text: string; createdAt: number } | null = { text: DRAFT, createdAt: 1 }
    mount(() => parked)
    await act(async () => {
      await api!.send('first')
    })
    parked = null
    await act(async () => {
      await api!.send('second')
    })
    expect(clearInputWrite).toHaveBeenCalledTimes(2)
    expect(clearInputWrite.mock.calls[0]![0]).toMatchObject({
      clearInput: buildAgentTuiClearInputForText(DRAFT)
    })
    expect(clearInputWrite.mock.calls[1]![0]).toMatchObject({ clearInput: '\x15' })
  })

  it('does not clear an image send after the image was pasted', async () => {
    mount(() => ({ text: DRAFT, createdAt: 1 }))
    await act(async () => {
      await api!.send('caption', ['file:///a.png'])
    })
    expect(clearInputWrite).not.toHaveBeenCalled()
    expect(sentArgs()).not.toHaveProperty('clearInputFirst')
    expect(sentArgs().resolvedLaunchDraft).toEqual({ text: DRAFT, createdAt: 1 })
  })

  // TUI commands never echo as user turns; optimistic bubbles would remain queued forever.
  it('never creates an optimistic echo for a catalog command send', async () => {
    mount(() => null)
    await act(async () => {
      await api!.send('/clear')
    })
    expect(acceptSend).not.toHaveBeenCalled()
    expect(onCommandSend).toHaveBeenCalledWith('/clear')
  })

  it.each(['claude', 'openclaude'] as const)(
    'keeps %s composer slash sends pasted',
    async (agent) => {
      // Session-option tracking recognizes controls missing from autocomplete without claiming a generic command.
      mount(() => null, agent)
      await act(async () => {
        await api!.send('/model sonnet')
      })
      expect(acceptSend).not.toHaveBeenCalled()
      expect(onCommandSend).toHaveBeenCalledWith('/model sonnet')
      expect(sendWithOutcome).toHaveBeenCalledOnce()
      expect(typeCommandWithOutcome).not.toHaveBeenCalled()
    }
  )

  it('types Codex composer slash sends instead of pasting them', async () => {
    mount(() => null, 'codex')
    await act(async () => {
      await api!.send('/model')
    })
    expect(acceptSend).not.toHaveBeenCalled()
    expect(onCommandSend).toHaveBeenCalledWith('/model')
    expect(typeCommandWithOutcome).toHaveBeenCalledWith(
      expect.objectContaining({ command: '/model', terminal: 'term' })
    )
    expect(sendWithOutcome).not.toHaveBeenCalled()
  })

  it('keeps Codex skill sends pasted', async () => {
    mount(() => null, 'codex')
    await act(async () => {
      await api!.send('$ref-oss')
    })
    expect(acceptSend).not.toHaveBeenCalled()
    expect(onCommandSend).toHaveBeenCalledWith('$ref-oss')
    expect(sendWithOutcome).toHaveBeenCalledWith(
      expect.objectContaining({ text: '$ref-oss', terminal: 'term' })
    )
    expect(typeCommandWithOutcome).not.toHaveBeenCalled()
  })

  it('holds only chat sends for transcript confirmation on a lost ack', async () => {
    sendWithOutcome.mockResolvedValue('unknown')
    mount(() => null)
    await act(async () => {
      await api!.send('/clear')
    })
    expect(holdUnconfirmedSend).not.toHaveBeenCalled()
    await act(async () => {
      await api!.send('hello')
    })
    expect(holdUnconfirmedSend).toHaveBeenCalledTimes(1)
  })

  it.each(['claude', 'openclaude'] as const)(
    'keeps %s session-option commands pasted',
    async (agent) => {
      mount(() => ({ text: DRAFT, createdAt: 1 }), agent)
      let outcome: string | undefined
      await act(async () => {
        outcome = await api!.dispatchCommand('/model sonnet', { delivery: 'type' })
      })
      expect(outcome).toBe('accepted')
      expect(acceptSend).not.toHaveBeenCalled()
      expect(onCommandSend).not.toHaveBeenCalled()
      expect(sentArgs().resolvedLaunchDraft).toBeUndefined()
    }
  )

  it('routes typed picker commands around the pasted composer-text send', async () => {
    mount(() => null, 'codex')
    let outcome: string | undefined
    await act(async () => {
      outcome = await api!.dispatchCommand('/model', { delivery: 'type' })
    })
    expect(outcome).toBe('accepted')
    expect(typeCommandWithOutcome).toHaveBeenCalledWith(
      expect.objectContaining({ command: '/model', terminal: 'term' })
    )
    expect(sendWithOutcome).not.toHaveBeenCalled()
  })

  it('types Codex session-option commands without caller delivery metadata', async () => {
    mount(() => null, 'codex')
    await act(async () => {
      await api!.dispatchCommand('/model')
    })

    expect(typeCommandWithOutcome).toHaveBeenCalledWith(
      expect.objectContaining({ command: '/model', terminal: 'term' })
    )
    expect(sendWithOutcome).not.toHaveBeenCalled()
  })

  it('binds classification to the agent that started the send', async () => {
    let resolveSend!: (outcome: MobileNativeChatSendOutcome) => void
    sendWithOutcome.mockReturnValue(
      new Promise<MobileNativeChatSendOutcome>((resolve) => {
        resolveSend = resolve
      })
    )
    mount(() => null, 'claude')
    let sending!: Promise<boolean>
    act(() => {
      sending = api!.send('$skill')
    })
    agentRef.current = 'codex'
    await act(async () => {
      resolveSend('accepted')
      await sending
    })
    expect(acceptSend).toHaveBeenCalledTimes(1)
  })

  it('records a command against the tab that started the send', async () => {
    let resolveSend!: (outcome: MobileNativeChatSendOutcome) => void
    sendWithOutcome.mockReturnValue(
      new Promise<MobileNativeChatSendOutcome>((resolve) => {
        resolveSend = resolve
      })
    )
    const originalRecorder = vi.fn()
    const nextRecorder = vi.fn()
    commandSendRef.current = originalRecorder
    mount(() => null)
    commandSendRef.current = originalRecorder
    let sending!: Promise<boolean>
    act(() => {
      sending = api!.send('/clear')
    })
    commandSendRef.current = nextRecorder
    await act(async () => {
      resolveSend('accepted')
      await sending
    })
    expect(originalRecorder).toHaveBeenCalledWith('/clear')
    expect(nextRecorder).not.toHaveBeenCalled()
  })

  // Trim wire whitespace so it cannot glue the next rapid send to the agent input line.
  it('trims trailing whitespace without changing leading prompt content', async () => {
    mount(() => null)
    await act(async () => {
      await api!.send('  run the tests \n')
    })
    expect(sentArgs().text).toBe('  run the tests')
    expect(captureSendOrigin).toHaveBeenCalledWith('  run the tests', undefined)
    expect(acceptSend.mock.calls[0]![1]).toBe('  run the tests')
  })

  // Trim only wire text; rejection must restore the exact draft, including blank lines.
  it('restores the untrimmed draft when the send is rejected', async () => {
    const draft = 'first line\n\nsecond line\n\n'
    sendWithOutcome.mockResolvedValue('rejected')
    mount(() => null)

    await act(async () => {
      await api!.send(draft)
    })

    expect(sentArgs().text).toBe('\x1b[200~first line\r\rsecond line\x1b[201~')
    expect(restoreRejectedDraft.mock.calls[0]![1]).toBe(draft)
    expect(clearDraftForSend.mock.calls[0]![1]).toBe(draft)
  })

  it('restores the untrimmed draft when the launch-draft pre-clear is rejected', async () => {
    const draft = 'ship it   '
    clearInputWrite.mockResolvedValue(false)
    mount(() => ({ text: DRAFT, createdAt: 1 }))

    await act(async () => {
      await api!.send(draft)
    })

    expect(sendWithOutcome).not.toHaveBeenCalled()
    expect(restoreRejectedDraft.mock.calls[0]![1]).toBe(draft)
  })
})