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.5 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  deriveNativeChatStreamingText,
  nativeChatStreamingMessage,
  NATIVE_CHAT_STREAMING_ID
} from './native-chat-streaming'
import type { NativeChatMessage } from '../native-chat-types'

const assistant = (text: string): NativeChatMessage => ({
  id: `a-${text.length}`,
  role: 'assistant',
  blocks: [{ type: 'text', text }],
  timestamp: null,
  source: 'transcript'
})
const user = (text: string): NativeChatMessage => ({
  id: `u-${text.length}`,
  role: 'user',
  blocks: [{ type: 'text', text }],
  timestamp: null,
  source: 'transcript'
})

describe('deriveNativeChatStreamingText', () => {
  it('returns null when not working (stale preview never shows)', () => {
    expect(
      deriveNativeChatStreamingText({ messages: [], previewText: 'Hello there', working: false })
    ).toBeNull()
  })

  it('returns null for empty / whitespace preview', () => {
    expect(
      deriveNativeChatStreamingText({ messages: [], previewText: '', working: true })
    ).toBeNull()
    expect(
      deriveNativeChatStreamingText({ messages: [], previewText: '   ', working: true })
    ).toBeNull()
  })

  it('shows the preview while it leads an empty/user-tailed transcript', () => {
    expect(
      deriveNativeChatStreamingText({
        messages: [user('do the thing')],
        previewText: 'Working on it',
        working: true
      })
    ).toBe('Working on it')
  })

  it('treats an optimistic user echo as the active streaming-turn boundary', () => {
    const optimistic = {
      ...user('new prompt'),
      id: 'pending:send-1',
      timestamp: 20,
      source: 'scrape' as const
    }
    expect(
      deriveNativeChatStreamingText({
        messages: [assistant('A much longer answer from the completed prior turn'), optimistic],
        previewText: 'New reply',
        working: true
      })
    ).toBe('New reply')
  })

  it('drops the preview once the real assistant turn contains it (no duplicate)', () => {
    expect(
      deriveNativeChatStreamingText({
        messages: [assistant('Working on it, here is the full answer.')],
        previewText: 'Working on it',
        working: true
      })
    ).toBeNull()
  })

  it('drops the preview when it is not longer than the last assistant turn (no flicker)', () => {
    expect(
      deriveNativeChatStreamingText({
        messages: [assistant('Same length text')],
        previewText: 'Same length text',
        working: true
      })
    ).toBeNull()
  })

  it('drops a preview flagged as tool output even when it leads the transcript', () => {
    // Status previews may contain tool stdout that never enters assistant blocks; catch-up cannot retire it, so gate tool output explicitly.
    expect(
      deriveNativeChatStreamingText({
        messages: [assistant('Partial')],
        previewText: 'Exit code 1\nimport { Foo } from "./foo"\nexport function bar() {}',
        working: true,
        previewIsToolOutput: true
      })
    ).toBeNull()
  })

  it('still shows a leading preview when it is not tool output', () => {
    expect(
      deriveNativeChatStreamingText({
        messages: [assistant('Partial')],
        previewText: 'Partial answer that is now much longer than before',
        working: true,
        previewIsToolOutput: false
      })
    ).toBe('Partial answer that is now much longer than before')
  })

  it('hides the sentence it streamed while a tool runs, but not a new reply after the tool', () => {
    // A Claude transcript writes the sentence and the tool call it led into as separate rows.
    const sentence = 'Checking the notes file now, then I will compare it with the settings.'
    const toolCall: NativeChatMessage = {
      id: 'f4470ba3',
      role: 'assistant',
      blocks: [{ type: 'tool-call', name: 'Bash', input: { command: 'sleep 12' } }],
      timestamp: null,
      source: 'transcript'
    }
    const messages = [user('Say it, then run sleep 12.'), assistant(sentence), toolCall]
    expect(
      deriveNativeChatStreamingText({ messages, previewText: sentence, working: true })
    ).toBeNull()
    expect(
      deriveNativeChatStreamingText({
        messages,
        previewText: 'The settings look correct.',
        working: true
      })
    ).toBe('The settings look correct.')
  })
})

describe('nativeChatStreamingMessage', () => {
  it('builds a stable-id assistant hook message', () => {
    const m = nativeChatStreamingMessage('hi')
    expect(m.id).toBe(NATIVE_CHAT_STREAMING_ID)
    expect(m.role).toBe('assistant')
    expect(m.source).toBe('hook')
    expect(m.blocks).toEqual([{ type: 'text', text: 'hi' }])
  })
})