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.

3.2 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { pairNativeChatToolResults } from './native-chat-tool-pairing'
import type {
  NativeChatBlock,
  NativeChatToolCallBlock,
  NativeChatToolResultBlock
} from '../../native-chat-types'

const call = (name: string): NativeChatToolCallBlock => ({ type: 'tool-call', name, input: {} })
const result = (output: string): NativeChatToolResultBlock => ({ type: 'tool-result', output })

describe('pairNativeChatToolResults', () => {
  it('gives each call the result that answered it', () => {
    const [a, ra, b, rb] = [call('read'), result('one'), call('shell'), result('two')]
    const { resultByCall, pairedResults } = pairNativeChatToolResults([a, ra, b, rb])

    expect(resultByCall.get(a)).toBe(ra)
    expect(resultByCall.get(b)).toBe(rb)
    expect(pairedResults.size).toBe(2)
  })

  it('answers the oldest unanswered call when two are interleaved', () => {
    const [outer, inner, first, second] = [call('a'), call('b'), result('inner'), result('outer')]
    const { resultByCall } = pairNativeChatToolResults([outer, inner, first, second])

    expect(resultByCall.get(outer)).toBe(first)
    expect(resultByCall.get(inner)).toBe(second)
  })

  it('leaves a still-running call without a result', () => {
    const [a, b, only] = [call('a'), call('b'), result('one')]
    const { resultByCall } = pairNativeChatToolResults([a, b, only])

    expect(resultByCall.get(a)).toBe(only)
    expect(resultByCall.has(b)).toBe(false)
  })

  it('leaves a result with no call to answer unpaired, so it still draws its own row', () => {
    const orphan = result('one')
    const { resultByCall, pairedResults } = pairNativeChatToolResults([orphan])

    expect(resultByCall.size).toBe(0)
    expect(pairedResults.has(orphan)).toBe(false)
  })

  it('gives a result that names its call to that call, past one that finished with no output', () => {
    const named = (name: string, callId: string): NativeChatToolCallBlock => ({
      ...call(name),
      callId
    })
    const [spawn, wait, shell] = [named('spawn', 's'), named('wait', 'w'), named('shell', 'x')]
    const [waited, ran]: NativeChatToolResultBlock[] = [
      { ...result('CHILD_REPLY'), callId: 'w' },
      { ...result('CHILD_DONE'), callId: 'x' }
    ]
    const { resultByCall } = pairNativeChatToolResults([spawn, wait, waited, shell, ran])

    expect(resultByCall.has(spawn)).toBe(false)
    expect(resultByCall.get(wait)).toBe(waited)
    expect(resultByCall.get(shell)).toBe(ran)
  })

  it('leaves a result that names a call not in the run unpaired, rather than give it to another', () => {
    const waiting: NativeChatToolCallBlock = { ...call('spawn'), callId: 's' }
    const stray: NativeChatToolResultBlock = { ...result('elsewhere'), callId: 'missing' }
    const { resultByCall, pairedResults } = pairNativeChatToolResults([waiting, stray])

    expect(resultByCall.has(waiting)).toBe(false)
    expect(pairedResults.has(stray)).toBe(false)
  })

  it('ignores blocks that are neither a call nor a result', () => {
    const text: NativeChatBlock = { type: 'text', text: 'hi' }
    const [a, ra] = [call('a'), result('r')]
    const { resultByCall } = pairNativeChatToolResults([text, a, ra])

    expect(resultByCall.get(a)).toBe(ra)
  })
})