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.

14.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  describeNativeChatActiveTurnLabel,
  describeNativeChatTurnStatus,
  formatNativeChatActiveTurnLabel,
  formatNativeChatDuration,
  formatNativeChatTurnStatusLabel,
  nativeChatElapsedSeconds,
  reduceNativeChatTurnTiming,
  selectNativeChatTurnStatuses,
  type NativeChatTurnTimingByTurn
} from './native-chat-turn-status'

describe('formatNativeChatDuration', () => {
  it.each([
    [0, '0s'],
    [12, '12s'],
    [59, '59s'],
    [60, '1m 0s'],
    [184, '3m 4s'],
    [3600, '1h 0m 0s'],
    [3723, '1h 2m 3s']
  ])('formats %i seconds as %s', (seconds, expected) => {
    expect(formatNativeChatDuration(seconds)).toBe(expected)
  })

  it('floors a fractional count and clamps a negative or non-finite one', () => {
    expect(formatNativeChatDuration(12.9)).toBe('12s')
    expect(formatNativeChatDuration(-5)).toBe('0s')
    expect(formatNativeChatDuration(Number.NaN)).toBe('0s')
  })
})

describe('describeNativeChatTurnStatus', () => {
  it('prefers the settled duration over the running clock', () => {
    expect(describeNativeChatTurnStatus({ workedSeconds: 184, elapsedSeconds: 9 })).toEqual({
      key: 'workedFor',
      duration: '3m 4s'
    })
  })

  it.each([
    ['cancellation', 'interruptedAfter'],
    ['superseded', 'interruptedAfter'],
    ['failure', 'failedAfter'],
    // A crash or restart cut it off: the turn reads finished and the chat's notice row says why.
    ['interruption', 'workedFor'],
    ['success', 'workedFor'],
    ['unconfirmed', 'workedFor'],
    [undefined, 'workedFor']
  ] as const)('heads a settled %s turn with %s', (verdict, key) => {
    expect(describeNativeChatTurnStatus({ workedSeconds: 12, elapsedSeconds: 0, verdict })).toEqual(
      { key, duration: '12s' }
    )
  })

  it('counts from the first second of the turn', () => {
    expect(describeNativeChatTurnStatus({ workedSeconds: null, elapsedSeconds: 0 })).toEqual({
      key: 'workingFor',
      duration: '0s'
    })
  })
})

describe('describeNativeChatActiveTurnLabel', () => {
  it('lets provider activity beat both fallbacks', () => {
    expect(
      describeNativeChatActiveTurnLabel({ activityText: 'Reading src/main.ts', thinking: true })
    ).toEqual({ source: 'activity', text: 'Reading src/main.ts' })
  })

  it('falls back to reasoning when the provider says nothing usable', () => {
    expect(describeNativeChatActiveTurnLabel({ activityText: '   ', thinking: true })).toEqual({
      source: 'status',
      key: 'thinking'
    })
    expect(describeNativeChatActiveTurnLabel({ activityText: null, thinking: true })).toEqual({
      source: 'status',
      key: 'thinking'
    })
  })

  // The clock belongs to the turn bar; repeating it here is two rows saying "Working for".
  it('falls back to plain working, never the clock, when neither applies', () => {
    expect(describeNativeChatActiveTurnLabel({ thinking: false })).toEqual({
      source: 'status',
      key: 'working'
    })
  })

  it("says Stopping over the provider's activity once a person's Stop is ending the turn", () => {
    expect(
      describeNativeChatActiveTurnLabel({
        activityText: 'Running pnpm test',
        thinking: true,
        stopping: true
      })
    ).toEqual({ source: 'status', key: 'stopping' })
  })
})

describe('formatNativeChatActiveTurnLabel', () => {
  it('renders the live tail line in English for platforms without i18n', () => {
    expect(
      formatNativeChatActiveTurnLabel({ activityText: 'Running pnpm test', thinking: false })
    ).toBe('Running pnpm test')
    expect(formatNativeChatActiveTurnLabel({ thinking: true })).toBe('Thinking')
    expect(formatNativeChatActiveTurnLabel({ thinking: false })).toBe('Working…')
    expect(formatNativeChatActiveTurnLabel({ thinking: false, stopping: true })).toBe('Stopping…')
  })
})

describe('formatNativeChatTurnStatusLabel', () => {
  it('renders the turn bar in English for platforms without i18n', () => {
    expect(formatNativeChatTurnStatusLabel({ workedSeconds: null, elapsedSeconds: 12 })).toBe(
      'Working for 12s'
    )
    expect(formatNativeChatTurnStatusLabel({ workedSeconds: 184, elapsedSeconds: 0 })).toBe(
      'Worked for 3m 4s'
    )
  })

  // The phone's turn bar renders through this function, so it reads the same as desktop.
  it('reads a crash-cut turn as worked and a Stop as interrupted in English', () => {
    expect(
      formatNativeChatTurnStatusLabel({
        workedSeconds: 222,
        elapsedSeconds: 0,
        verdict: 'interruption'
      })
    ).toBe('Worked for 3m 42s')
    expect(
      formatNativeChatTurnStatusLabel({
        workedSeconds: 222,
        elapsedSeconds: 0,
        verdict: 'cancellation'
      })
    ).toBe('Interrupted after 3m 42s')
  })
})

describe('reduceNativeChatTurnTiming', () => {
  const validTurnKeys = new Set(['u1'])

  it('stamps a start when a turn begins working', () => {
    const next = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'u1', validTurnKeys, isWorking: true, now: 1_000 }
    )
    expect(next).toEqual({ u1: { startedAt: 1_000, workedSeconds: null } })
  })

  it('keeps the original start across later working ticks', () => {
    const first = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'u1', validTurnKeys, isWorking: true, now: 1_000 }
    )
    const second = reduceNativeChatTurnTiming(first, {
      activeTurnKey: 'u1',
      validTurnKeys,
      isWorking: true,
      now: 9_000
    })
    expect(second).toBe(first)
  })

  it('prefers an authoritative host start over the local stamp', () => {
    const next = reduceNativeChatTurnTiming(
      {},
      {
        activeTurnKey: 'u1',
        validTurnKeys,
        isWorking: true,
        workingStartedAt: 500,
        now: 1_000
      }
    )
    expect(next.u1?.startedAt).toBe(500)
  })

  it('does not move a live turn later before its request origin arrives', () => {
    const optimistic = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'u1', validTurnKeys, isWorking: true, now: 1_000 }
    )
    const turnStarted = reduceNativeChatTurnTiming(optimistic, {
      activeTurnKey: 'u1',
      validTurnKeys,
      isWorking: true,
      workingStartedAt: 8_000,
      now: 8_000
    })
    const exactOrigin = reduceNativeChatTurnTiming(turnStarted, {
      activeTurnKey: 'u1',
      validTurnKeys,
      isWorking: true,
      workingStartedAt: 900,
      now: 8_100
    })

    expect(turnStarted).toBe(optimistic)
    expect(exactOrigin.u1).toEqual({ startedAt: 900, workedSeconds: null })
  })

  it('settles the turn to whole elapsed seconds when work stops', () => {
    const working = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'u1', validTurnKeys, isWorking: true, now: 1_000 }
    )
    const settled = reduceNativeChatTurnTiming(working, {
      activeTurnKey: 'u1',
      validTurnKeys,
      isWorking: false,
      now: 13_400
    })
    expect(settled.u1).toEqual({ startedAt: 1_000, workedSeconds: 12 })
  })

  it('never re-settles an already settled turn', () => {
    const settled: NativeChatTurnTimingByTurn = { u1: { startedAt: 1_000, workedSeconds: 12 } }
    expect(
      reduceNativeChatTurnTiming(settled, {
        activeTurnKey: 'u1',
        validTurnKeys,
        isWorking: false,
        now: 99_000
      })
    ).toBe(settled)
  })

  it('does not invent a settled turn that never started', () => {
    expect(
      reduceNativeChatTurnTiming(
        {},
        { activeTurnKey: 'u1', validTurnKeys, isWorking: false, now: 1_000 }
      )
    ).toEqual({})
  })

  it('carries the elapsed start across an optimistic echo becoming a transcript row', () => {
    // Mobile pending-N ids become transcript ids mid-turn; carry timing so the running clock cannot restart.
    const working = reduceNativeChatTurnTiming(
      {},
      {
        activeTurnKey: 'pending-1',
        validTurnKeys: new Set<string>(),
        isWorking: true,
        now: 1_000
      }
    )
    expect(working['pending-1']?.startedAt).toBe(1_000)
    const swapped = reduceNativeChatTurnTiming(working, {
      activeTurnKey: 'u9',
      previousActiveTurnKey: 'pending-1',
      validTurnKeys: new Set(['u9']),
      isWorking: true,
      now: 9_000
    })
    expect(swapped.u9).toEqual({ startedAt: 1_000, workedSeconds: null })
    expect(swapped['pending-1']).toBeUndefined()
  })

  it('keeps a settled turn visible when the echo is replaced after it finished', () => {
    // Re-key settled timing too or late echo replacement removes the Worked for row.
    const settled: NativeChatTurnTimingByTurn = {
      'pending-1': { startedAt: 1_000, workedSeconds: 12 }
    }
    const swapped = reduceNativeChatTurnTiming(settled, {
      activeTurnKey: 'u9',
      previousActiveTurnKey: 'pending-1',
      validTurnKeys: new Set(['u9']),
      isWorking: false,
      now: 20_000
    })
    expect(swapped.u9).toEqual({ startedAt: 1_000, workedSeconds: 12 })
    expect(swapped['pending-1']).toBeUndefined()
  })

  it('settles a re-keyed in-flight turn from its original start', () => {
    const working = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'pending-1', validTurnKeys: new Set<string>(), isWorking: true, now: 1_000 }
    )
    const settled = reduceNativeChatTurnTiming(working, {
      activeTurnKey: 'u9',
      previousActiveTurnKey: 'pending-1',
      validTurnKeys: new Set(['u9']),
      isWorking: false,
      now: 13_400
    })
    expect(settled.u9).toEqual({ startedAt: 1_000, workedSeconds: 12 })
  })

  it('does not carry the start into a genuinely new turn', () => {
    // The previous turn is still in the transcript, so this is the user sending
    // again — that turn starts its own clock.
    const working = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'u1', validTurnKeys: new Set(['u1']), isWorking: true, now: 1_000 }
    )
    const next = reduceNativeChatTurnTiming(working, {
      activeTurnKey: 'u2',
      previousActiveTurnKey: 'u1',
      validTurnKeys: new Set(['u1', 'u2']),
      isWorking: true,
      now: 9_000
    })
    expect(next.u2?.startedAt).toBe(9_000)
  })

  it('does not carry a start from a turn that had already settled', () => {
    const settled: NativeChatTurnTimingByTurn = {
      'pending-1': { startedAt: 1_000, workedSeconds: 5 }
    }
    const next = reduceNativeChatTurnTiming(settled, {
      activeTurnKey: 'u9',
      previousActiveTurnKey: 'pending-1',
      validTurnKeys: new Set(['u9']),
      isWorking: true,
      now: 9_000
    })
    expect(next.u9?.startedAt).toBe(9_000)
  })

  it('drops timings for turns that left the transcript, keeping the active one', () => {
    const current: NativeChatTurnTimingByTurn = {
      gone: { startedAt: 1, workedSeconds: 2 },
      u1: { startedAt: 1_000, workedSeconds: 12 }
    }
    const next = reduceNativeChatTurnTiming(current, {
      activeTurnKey: 'u1',
      validTurnKeys,
      isWorking: false,
      now: 2_000
    })
    expect(Object.keys(next)).toEqual(['u1'])
  })
})

describe('selectNativeChatTurnStatuses', () => {
  it('keeps the selected live start monotonic until the exact request origin arrives', () => {
    const optimistic = reduceNativeChatTurnTiming(
      {},
      { activeTurnKey: 'u1', validTurnKeys: new Set(['u1']), isWorking: true, now: 1_000 }
    )
    const turnStarted = reduceNativeChatTurnTiming(optimistic, {
      activeTurnKey: 'u1',
      validTurnKeys: new Set(['u1']),
      isWorking: true,
      workingStartedAt: 8_000,
      now: 8_000
    })
    const beforeEcho = selectNativeChatTurnStatuses(turnStarted, {
      activeTurnKey: 'u1',
      isWorking: true,
      workingStartedAt: 8_000,
      thinking: false
    })
    const exactOrigin = reduceNativeChatTurnTiming(turnStarted, {
      activeTurnKey: 'u1',
      validTurnKeys: new Set(['u1']),
      isWorking: true,
      workingStartedAt: 900,
      now: 8_100
    })
    const afterEcho = selectNativeChatTurnStatuses(exactOrigin, {
      activeTurnKey: 'u1',
      isWorking: true,
      workingStartedAt: 900,
      thinking: false
    })

    expect(beforeEcho.active?.startedAt).toBe(1_000)
    expect(afterEcho.active?.startedAt).toBe(900)
  })

  it('carries the reasoning verdict it is given onto the working turn', () => {
    const { active } = selectNativeChatTurnStatuses(
      { u1: { startedAt: 1_000, workedSeconds: null } },
      { activeTurnKey: 'u1', isWorking: true, thinking: true }
    )
    expect(active).toEqual({ startedAt: 1_000, thinking: true, workedSeconds: null })
  })

  it('reports a working turn that is not reasoning as counting', () => {
    const { active } = selectNativeChatTurnStatuses(
      { u1: { startedAt: 1_000, workedSeconds: null } },
      { activeTurnKey: 'u1', isWorking: true, thinking: false }
    )
    expect(active?.thinking).toBe(false)
  })

  it('exposes settled turns and resolves the active one from them when idle', () => {
    const { active, completedByTurn } = selectNativeChatTurnStatuses(
      { u1: { startedAt: 1_000, workedSeconds: 12 } },
      { activeTurnKey: 'u1', isWorking: false, thinking: false }
    )
    expect(completedByTurn.u1).toEqual({ startedAt: 1_000, thinking: false, workedSeconds: 12 })
    expect(active).toEqual(completedByTurn.u1)
  })

  it('omits an in-flight turn from the completed map', () => {
    const { completedByTurn } = selectNativeChatTurnStatuses(
      { u1: { startedAt: 1_000, workedSeconds: null } },
      { activeTurnKey: 'u1', isWorking: true, thinking: false }
    )
    expect(completedByTurn).toEqual({})
  })

  it('keeps the just-ended turn live until its local duration is stamped', () => {
    const running = { u1: { startedAt: 1_000, workedSeconds: null } }
    const { active } = selectNativeChatTurnStatuses(running, {
      activeTurnKey: 'u1',
      isWorking: false,
      thinking: true
    })
    expect(active).toEqual({ startedAt: 1_000, thinking: false, workedSeconds: null })
    // The host saying it never saw the end still wins.
    const unverifiable = selectNativeChatTurnStatuses(running, {
      activeTurnKey: 'u1',
      isWorking: false,
      thinking: false,
      settledByTurn: new Map([['u1', null]])
    })
    expect(unverifiable.active).toBeNull()
  })
})

describe('nativeChatElapsedSeconds', () => {
  it('falls back to the mount epoch before the turn start lands', () => {
    expect(nativeChatElapsedSeconds(null, 1_000, 5_400)).toBe(4)
    expect(nativeChatElapsedSeconds(2_000, 1_000, 5_400)).toBe(3)
  })

  it('never counts backwards', () => {
    expect(nativeChatElapsedSeconds(9_000, 1_000, 5_000)).toBe(0)
  })
})