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.

13.1 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { getExecutionHostLabel } from './execution-host'
import { MIN_COMPATIBLE_RUNTIME_SERVER_VERSION, RUNTIME_PROTOCOL_VERSION } from './protocol-version'
import { buildExecutionHostRegistry } from './execution-host-registry'

const LOCAL_HOST_LABEL = getExecutionHostLabel('local')

describe('execution host registry', () => {
  it('returns only the local host for local-only state', () => {
    expect(
      buildExecutionHostRegistry({
        repos: [{ connectionId: null }],
        settings: { activeRuntimeEnvironmentId: null }
      })
    ).toEqual([
      {
        id: 'local',
        kind: 'local',
        label: LOCAL_HOST_LABEL,
        detail: 'This computer',
        health: 'local'
      }
    ])
  })

  it('includes saved and repo-derived SSH hosts with connection health', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [{ connectionId: 'repo-ssh' }],
      settings: { activeRuntimeEnvironmentId: null },
      sshTargetLabels: new Map([['saved-ssh', 'Saved SSH']]),
      sshConnectionStates: new Map([
        [
          'repo-ssh',
          {
            targetId: 'repo-ssh',
            status: 'connected',
            error: null,
            reconnectAttempt: 0
          }
        ],
        [
          'saved-ssh',
          {
            targetId: 'saved-ssh',
            status: 'auth-failed',
            error: 'Permission denied',
            reconnectAttempt: 1
          }
        ]
      ])
    })

    expect(hosts).toMatchObject([
      { id: 'local', health: 'local' },
      { id: 'ssh:saved-ssh', label: 'Saved SSH', health: 'error', connectionStatus: 'auth-failed' },
      { id: 'ssh:repo-ssh', label: 'repo-ssh', health: 'available', connectionStatus: 'connected' }
    ])
  })

  it('excludes repository references from configured-only registries', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [
        { connectionId: 'removed-ssh' },
        { connectionId: null, executionHostId: 'runtime:removed-runtime' }
      ],
      settings: { activeRuntimeEnvironmentId: 'removed-focused-runtime' },
      hostSource: 'configured-only',
      sshTargetLabels: new Map([['saved-ssh', 'Saved SSH']]),
      runtimeEnvironments: [{ id: 'saved-runtime', name: 'Saved Runtime' }]
    })

    expect(hosts.map((host) => host.id)).toEqual([
      'local',
      'runtime:saved-runtime',
      'ssh:saved-ssh'
    ])
  })

  it('keeps repository references in the default reference-inclusive registry', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [
        { connectionId: 'removed-ssh' },
        { connectionId: null, executionHostId: 'runtime:removed-runtime' }
      ],
      settings: { activeRuntimeEnvironmentId: null }
    })

    expect(hosts.map((host) => host.id)).toEqual([
      'local',
      'runtime:removed-runtime',
      'ssh:removed-ssh'
    ])
  })

  it('hides machine-provider SSH targets the same way', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [{ connectionId: 'machine-0123', executionHostId: 'ssh:machine-0123' }],
      settings: { activeRuntimeEnvironmentId: null },
      sshTargetLabels: new Map([['machine-0123', 'omarchy-dev']])
    })
    expect(hosts.some((h) => h.id.includes('machine-0123'))).toBe(false)
  })

  it('hides runtime-owned (ephemeral VM) SSH targets from repo-derived hosts', () => {
    const hosts = buildExecutionHostRegistry({
      // A VM-backed repo carries the hidden runtime-owned target on both fields.
      repos: [
        {
          connectionId: 'runtime-ssh-orca-instance-1',
          executionHostId: 'ssh:runtime-ssh-orca-instance-1'
        },
        { connectionId: 'repo-ssh' }
      ],
      settings: { activeRuntimeEnvironmentId: null },
      // Stale labels must not reveal hidden runtime targets.
      sshTargetLabels: new Map([['runtime-ssh-orca-instance-1', 'Hidden VM']])
    })

    expect(hosts.some((h) => h.id.includes('runtime-ssh-orca-instance-1'))).toBe(false)
    expect(hosts.some((h) => h.id === 'ssh:repo-ssh')).toBe(true)
  })

  it("does not add a client SSH host from a paired server's nested SSH target", () => {
    const hosts = buildExecutionHostRegistry({
      // The server's own target id; this client has no such SSH host.
      repos: [{ connectionId: 'server-ssh', executionHostId: 'runtime:env-1' }],
      settings: { activeRuntimeEnvironmentId: null },
      sshConnectionStates: new Map([
        [
          'server-ssh',
          { targetId: 'server-ssh', status: 'connected', error: null, reconnectAttempt: 0 }
        ]
      ])
    })

    expect(hosts.map((h) => h.id)).toEqual(['local', 'runtime:env-1'])
  })

  it('adds saved runtime environments and preserves compatibility state per host', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [],
      settings: { activeRuntimeEnvironmentId: 'old-server' },
      runtimeEnvironments: [{ id: 'builder', name: 'Linux Builder' }],
      runtimeStatusByEnvironmentId: new Map([
        [
          'builder',
          {
            checkedAt: 0,
            appVersion: '1.8.0',
            status: {
              runtimeId: 'runtime-builder',
              rendererGraphEpoch: 1,
              graphStatus: 'ready',
              authoritativeWindowId: 1,
              liveTabCount: 0,
              liveLeafCount: 0,
              runtimeProtocolVersion: RUNTIME_PROTOCOL_VERSION,
              minCompatibleRuntimeClientVersion: 1,
              capabilities: ['terminal.binary-stream.v1'],
              hostPlatform: 'linux'
            }
          }
        ],
        [
          'old-server',
          {
            checkedAt: 0,
            appVersion: '1.6.0',
            status: {
              runtimeId: 'runtime-old',
              rendererGraphEpoch: 1,
              graphStatus: 'ready',
              authoritativeWindowId: 1,
              liveTabCount: 0,
              liveLeafCount: 0,
              runtimeProtocolVersion: MIN_COMPATIBLE_RUNTIME_SERVER_VERSION - 1,
              minCompatibleRuntimeClientVersion: 1,
              capabilities: []
            }
          }
        ]
      ])
    })

    expect(hosts).toMatchObject([
      { id: 'local', health: 'local' },
      {
        id: 'runtime:builder',
        label: 'Linux Builder',
        health: 'available',
        appVersion: '1.8.0',
        protocolVersion: RUNTIME_PROTOCOL_VERSION,
        capabilities: ['terminal.binary-stream.v1'],
        platform: 'linux',
        compatibility: { kind: 'ok' }
      },
      {
        id: 'runtime:old-server',
        label: 'old-server',
        health: 'blocked',
        appVersion: '1.6.0',
        compatibility: { kind: 'blocked', reason: 'server-too-old' }
      }
    ])
  })

  it('uses shared-control diagnostics to show reconnecting runtime host health', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [],
      settings: { activeRuntimeEnvironmentId: null },
      runtimeEnvironments: [{ id: 'dev-box', name: 'Dev Box' }],
      runtimeStatusByEnvironmentId: new Map([
        [
          'dev-box',
          {
            checkedAt: 0,
            status: {
              runtimeId: 'runtime-dev',
              rendererGraphEpoch: 1,
              graphStatus: 'ready',
              authoritativeWindowId: 1,
              liveTabCount: 0,
              liveLeafCount: 0,
              remoteControl: {
                state: 'reconnecting',
                pendingRequestCount: 0,
                subscriptionCount: 2,
                reconnectAttempt: 1,
                lastConnectedAt: 123,
                lastClose: { code: 1006, reason: '' },
                lastError: 'Remote Orca runtime closed the connection.'
              }
            }
          }
        ]
      ])
    })

    expect(hosts).toMatchObject([
      { id: 'local', health: 'local' },
      {
        id: 'runtime:dev-box',
        label: 'Dev Box',
        health: 'connecting',
        remoteControlState: { state: 'reconnecting', subscriptionCount: 2 }
      }
    ])
  })

  it('treats ready shared-control diagnostics without a status payload as available', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [],
      settings: { activeRuntimeEnvironmentId: null },
      runtimeEnvironments: [{ id: 'dev-box', name: 'Dev Box' }],
      runtimeStatusByEnvironmentId: new Map([
        [
          'dev-box',
          {
            checkedAt: 0,
            status: null,
            remoteControl: {
              state: 'ready',
              pendingRequestCount: 0,
              subscriptionCount: 1,
              reconnectAttempt: 0,
              lastConnectedAt: 123,
              lastClose: null,
              lastError: null
            }
          }
        ]
      ])
    })

    expect(hosts).toMatchObject([
      { id: 'local', health: 'local' },
      {
        id: 'runtime:dev-box',
        label: 'Dev Box',
        health: 'available',
        remoteControlState: { state: 'ready' }
      }
    ])
  })

  it('preserves runtime environment source on runtime hosts', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [],
      settings: { activeRuntimeEnvironmentId: null },
      runtimeEnvironments: [{ id: 'vm-runtime', name: 'VM Runtime', source: 'ephemeral-vm' }],
      runtimeStatusByEnvironmentId: new Map([
        [
          'vm-runtime',
          {
            checkedAt: 0,
            status: {
              runtimeId: 'runtime-vm',
              rendererGraphEpoch: 1,
              graphStatus: 'ready',
              authoritativeWindowId: 1,
              liveTabCount: 0,
              liveLeafCount: 0,
              runtimeProtocolVersion: RUNTIME_PROTOCOL_VERSION,
              minCompatibleRuntimeClientVersion: 1,
              capabilities: ['project-host-setup.v1']
            }
          }
        ]
      ])
    })

    expect(hosts).toContainEqual(
      expect.objectContaining({
        id: 'runtime:vm-runtime',
        kind: 'runtime',
        source: 'ephemeral-vm'
      })
    )
  })

  it('applies per-host display-label overrides to derived labels', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [{ connectionId: 'repo-ssh' }],
      settings: { activeRuntimeEnvironmentId: null },
      sshTargetLabels: new Map([['repo-ssh', 'Derived SSH']]),
      hostLabelOverrides: new Map([
        ['ssh:repo-ssh', 'Renamed Box'],
        ['local', 'My Laptop']
      ])
    })

    expect(hosts).toMatchObject([
      { id: 'local', label: 'My Laptop' },
      { id: 'ssh:repo-ssh', label: 'Renamed Box' }
    ])
  })

  it('keeps derived labels for hosts without an override', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [{ connectionId: 'repo-ssh' }],
      settings: { activeRuntimeEnvironmentId: null },
      sshTargetLabels: new Map([['repo-ssh', 'Derived SSH']]),
      hostLabelOverrides: new Map([['ssh:other', 'Unrelated']])
    })

    expect(hosts).toMatchObject([
      { id: 'local', label: LOCAL_HOST_LABEL },
      { id: 'ssh:repo-ssh', label: 'Derived SSH' }
    ])
  })

  it('keeps runtime hosts checking before their first status result', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [{ connectionId: null, executionHostId: 'runtime:env-2' }],
      settings: { activeRuntimeEnvironmentId: null }
    })

    expect(hosts).toMatchObject([
      { id: 'local', health: 'local' },
      { id: 'runtime:env-2', kind: 'runtime', label: 'env-2', health: 'connecting' }
    ])
  })

  it('includes runtime hosts from hydrated status even when they are not focused', () => {
    const hosts = buildExecutionHostRegistry({
      repos: [],
      settings: { activeRuntimeEnvironmentId: null },
      runtimeStatusByEnvironmentId: new Map([
        [
          'gpu',
          {
            checkedAt: 0,
            appVersion: '1.8.0',
            status: {
              runtimeId: 'runtime-gpu',
              rendererGraphEpoch: 1,
              graphStatus: 'ready',
              authoritativeWindowId: 1,
              liveTabCount: 0,
              liveLeafCount: 0,
              runtimeProtocolVersion: RUNTIME_PROTOCOL_VERSION,
              minCompatibleRuntimeClientVersion: 1,
              capabilities: ['project-host-setup.v1'],
              hostPlatform: 'linux'
            }
          }
        ]
      ])
    })

    expect(hosts).toMatchObject([
      { id: 'local', health: 'local' },
      {
        id: 'runtime:gpu',
        kind: 'runtime',
        label: 'gpu',
        health: 'available',
        capabilities: ['project-host-setup.v1'],
        platform: 'linux'
      }
    ])
  })
})

it('keeps an initial unknown-transport verification connecting', () => {
  const hosts = buildExecutionHostRegistry({
    repos: [],
    settings: null,
    runtimeEnvironments: [{ id: 'host', name: 'Host' }],
    runtimeStatusByEnvironmentId: new Map([
      [
        'host',
        {
          checkedAt: 0,
          status: null,
          snapshot: {
            environmentId: 'host',
            pairingRevision: 1,
            sequence: 1,
            checkedAt: 0,
            status: null,
            verification: 'checking',
            transport: 'unknown'
          }
        }
      ]
    ])
  })
  expect(hosts.find((host) => host.id === 'runtime:host')?.health).toBe('connecting')
})