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.

5.0 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { buildExecutionHostRegistry } from './execution-host-registry'
import { RUNTIME_PROTOCOL_VERSION } from './protocol-version'
import type { RemoteRuntimeSharedConnectionDiagnostics } from './remote-runtime/control/remote-runtime-shared-control-types'
import {
  runtimeEnvironmentStatusFromSnapshot,
  type RuntimeHostStatusSnapshot
} from './runtime-host-status'
import { RuntimeHostStatusOwner } from './runtime-host-status-owner'
import type { RuntimeStatus } from './runtime-types'

const STATUS: RuntimeStatus = {
  runtimeId: 'runtime-1',
  rendererGraphEpoch: 1,
  graphStatus: 'ready',
  authoritativeWindowId: 1,
  liveTabCount: 0,
  liveLeafCount: 0,
  runtimeProtocolVersion: RUNTIME_PROTOCOL_VERSION,
  minCompatibleRuntimeClientVersion: 1
}

function diagnostics(
  state: RemoteRuntimeSharedConnectionDiagnostics['state']
): RemoteRuntimeSharedConnectionDiagnostics {
  return {
    state,
    pendingRequestCount: 0,
    subscriptionCount: 0,
    reconnectAttempt: 0,
    lastConnectedAt: null,
    lastClose: null,
    lastError: null
  }
}

function healthFor(snapshot: RuntimeHostStatusSnapshot): string | undefined {
  return hostFor(snapshot)?.health
}

function hostFor(snapshot: RuntimeHostStatusSnapshot) {
  return buildExecutionHostRegistry({
    repos: [],
    settings: null,
    runtimeEnvironments: [{ id: 'env-1', name: 'Server' }],
    runtimeStatusByEnvironmentId: new Map([
      ['env-1', runtimeEnvironmentStatusFromSnapshot(snapshot)]
    ])
  }).find((host) => host.id === 'runtime:env-1')
}

function snapshot(patch: Partial<RuntimeHostStatusSnapshot>): RuntimeHostStatusSnapshot {
  return {
    environmentId: 'env-1',
    pairingRevision: 1,
    sequence: 1,
    checkedAt: 1,
    status: STATUS,
    verification: 'verified',
    transport: 'connecting',
    remoteControl: diagnostics('awaiting_authenticated'),
    ...patch
  }
}

function ownerSnapshots(): {
  owner: RuntimeHostStatusOwner
  latest: () => RuntimeHostStatusSnapshot
} {
  const owner = new RuntimeHostStatusOwner({
    environmentId: 'env-1',
    pairingRevision: 1,
    request: () => new Promise(() => {}),
    verified: () => true,
    publish: () => {}
  })
  return { owner, latest: () => owner.read() }
}

describe('runtime host health while shared control connects (#10704)', () => {
  it('is available when the status probe answered and shared control is still connecting', () => {
    expect(healthFor(snapshot({}))).toBe('available')
    expect(
      healthFor(snapshot({ transport: 'unknown', remoteControl: diagnostics('awaiting_ready') }))
    ).toBe('available')
  })

  it('stays connecting when only a status retained from the previous socket is held', () => {
    expect(healthFor(snapshot({ verification: 'unavailable' }))).toBe('connecting')
    expect(healthFor(snapshot({ verification: 'checking' }))).toBe('connecting')
    expect(healthFor(snapshot({ verification: 'checking', transport: 'ready' }))).toBe('connecting')
  })

  it('stays connecting when shared control lost its socket', () => {
    expect(
      healthFor(snapshot({ transport: 'disconnected', remoteControl: diagnostics('reconnecting') }))
    ).toBe('connecting')
  })

  it('keeps a version-blocked server blocked while shared control connects', () => {
    expect(
      healthFor(snapshot({ status: { ...STATUS, runtimeProtocolVersion: 0, protocolVersion: 0 } }))
    ).toBe('blocked')
  })

  it('follows the status owner: fresh answer is available, a reconnect withdraws it', () => {
    const { owner, latest } = ownerSnapshots()
    owner.acceptVerified({ id: 'status.get', ok: true, result: STATUS, _meta: { runtimeId: 'r' } })
    owner.connectionChanged('connecting', diagnostics('awaiting_authenticated'))
    expect(healthFor(latest())).toBe('available')

    owner.connectionChanged('ready', diagnostics('ready'))
    expect(healthFor(latest())).toBe('available')

    owner.connectionChanged('disconnected', diagnostics('reconnecting'))
    owner.connectionChanged('connecting', diagnostics('awaiting_authenticated'))
    expect(latest().status).toEqual(STATUS)
    expect(healthFor(latest())).toBe('connecting')
    owner.dispose()
  })

  it('carries why a blocked host refused this client', () => {
    expect(
      hostFor(snapshot({ verification: 'blocked', blockedReason: 'access-revoked' }))
    ).toMatchObject({ health: 'blocked', blockedReason: 'access-revoked' })
    expect(
      hostFor(snapshot({ verification: 'blocked', blockedReason: 'incompatible' }))
    ).toMatchObject({ health: 'blocked', blockedReason: 'incompatible' })
    expect(
      hostFor(
        snapshot({
          status: { ...STATUS, runtimeProtocolVersion: 0, protocolVersion: 0 }
        })
      )
    ).toMatchObject({ health: 'blocked', blockedReason: 'incompatible' })
    expect(hostFor(snapshot({}))).not.toHaveProperty('blockedReason')
    const retired = hostFor(
      snapshot({ verification: 'blocked', retired: true, transport: 'disconnected' })
    )
    expect(retired).toMatchObject({ health: 'disconnected' })
    expect(retired).not.toHaveProperty('blockedReason')
  })
})