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.1 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { RemoteRuntimeClientError } from '../../shared/remote-runtime/remote-runtime-client-error'
import type { PairedRuntimeConnection } from '../../shared/paired-runtime-connection'
import { orchestrationMutationRecoveryError } from '../orchestration-mutation-recovery'

const { sendRemoteRuntimeRequest, sendRemoteRuntimeRequestWithStatusPreflight } = vi.hoisted(
  () => ({
    sendRemoteRuntimeRequest: vi.fn(),
    sendRemoteRuntimeRequestWithStatusPreflight: vi.fn()
  })
)

vi.mock('../../shared/remote-runtime/remote-runtime-client', () => ({
  sendRemoteRuntimeRequest,
  sendRemoteRuntimeRequestWithStatusPreflight
}))

import {
  sendWebSocketRequest,
  sendWebSocketRequestWithStatusPreflight
} from './websocket-transport'

const pairing = {
  endpoint: 'ws://127.0.0.1:1',
  hostPublicKeyB64: Buffer.from(new Uint8Array(32).fill(1)).toString('base64'),
  credential: { kind: 'token', deviceToken: 'token' }
} satisfies PairedRuntimeConnection

describe('CLI WebSocket transport error identity', () => {
  it('preserves recovery data from one-shot transport errors', async () => {
    sendRemoteRuntimeRequest.mockRejectedValueOnce(
      new RemoteRuntimeClientError('runtime_timeout', 'timed out', {
        data: {
          orchestrationRequestId: 'request_1',
          dispatchId: 'dispatch_1',
          originalCommand: ['orca', 'orchestration', 'worker-start', '--task', 'task_1']
        }
      })
    )

    try {
      await sendWebSocketRequest(pairing, 'orchestration.workerStart', {}, 100)
      throw new Error('expected transport failure')
    } catch (error) {
      expect(error).toMatchObject({
        code: 'runtime_timeout',
        data: {
          orchestrationRequestId: 'request_1',
          dispatchId: 'dispatch_1',
          originalCommand: ['orca', 'orchestration', 'worker-start', '--task', 'task_1']
        }
      })
      expect(orchestrationMutationRecoveryError(error)).toMatchObject({
        data: {
          recovery: {
            dispatchId: 'dispatch_1',
            queryCommand: [
              'orca',
              'orchestration',
              'worker-show',
              '--dispatch',
              'dispatch_1',
              '--json'
            ],
            retryCommand: [
              'orca',
              'orchestration',
              'worker-start',
              '--task',
              'task_1',
              '--retry-request',
              'request_1'
            ]
          }
        }
      })
    }
  })

  it('preserves recovery data through status-preflight transport errors', async () => {
    sendRemoteRuntimeRequestWithStatusPreflight.mockRejectedValueOnce(
      new RemoteRuntimeClientError('runtime_timeout', 'timed out', {
        data: { orchestrationRequestId: 'request_2' }
      })
    )

    await expect(
      sendWebSocketRequestWithStatusPreflight(
        pairing,
        'orchestration.workerStart',
        {},
        100,
        () => {}
      )
    ).rejects.toMatchObject({
      code: 'runtime_timeout',
      data: { orchestrationRequestId: 'request_2' }
    })
  })
})