Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

16.7 KB
Raw Blame
/**
 * Unit tests for the boot-check orchestrator.
 *
 * Uses the injectable transport so no real Tauri IPC or HTTP calls are made.
 */
import { describe, expect, it, vi } from 'vitest';

import { type BootCheckResult, type BootCheckTransport, runBootCheck } from './index';

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/** Build a minimal transport stub for tests. */
function makeTransport(overrides?: Partial<BootCheckTransport>): BootCheckTransport {
  return { callRpc: vi.fn(), invokeCmd: vi.fn().mockResolvedValue(undefined), ...overrides };
}

/**
 * Build a callRpc mock that answers specific methods.
 *
 * `responses` maps method-name → resolved value (or Error to reject with).
 */
function rpcResponder(responses: Record<string, unknown>): BootCheckTransport['callRpc'] {
  return vi.fn(async (method: string) => {
    if (method in responses) {
      const val = responses[method];
      if (val instanceof Error) throw val;
      return val;
    }
    throw new Error(`Unexpected RPC call: ${method}`);
  }) as BootCheckTransport['callRpc'];
}

// ---------------------------------------------------------------------------
// Local mode tests
// ---------------------------------------------------------------------------

describe('runBootCheck — local mode', () => {
  it('returns match when ping succeeds, no daemon, versions match', async () => {
    const appVersion = (await import('../../utils/config')).APP_VERSION;

    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: false },
        'openhuman.update_version': { result: { version: appVersion } },
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result).toEqual({ kind: 'match' });
  });

  it('returns daemonDetected when service_status shows installed=true', async () => {
    const appVersion = (await import('../../utils/config')).APP_VERSION;

    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: true, running: false },
        'openhuman.update_version': { result: { version: appVersion } },
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result).toEqual({ kind: 'daemonDetected' });
  });

  it('returns daemonDetected when service_status shows running=true', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: true },
        'openhuman.update_version': { result: { version: 'x' } },
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result).toEqual({ kind: 'daemonDetected' });
  });

  it('returns outdatedLocal when core version differs from app version', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: false },
        'openhuman.update_version': { result: { version: '0.0.0-different' } },
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result).toEqual({ kind: 'outdatedLocal' });
  });

  it('returns noVersionMethod when update_version returns -32601', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: false },
        'openhuman.update_version': new Error('JSON-RPC error -32601 Method not found'),
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result).toEqual({ kind: 'noVersionMethod' });
  });

  it('returns noVersionMethod on "method not found" text variant', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: false },
        'openhuman.update_version': new Error('method not found'),
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result).toEqual({ kind: 'noVersionMethod' });
  });

  it('returns unreachable when start_core_process invoke fails', async () => {
    const transport = makeTransport({
      invokeCmd: vi.fn().mockRejectedValue(new Error('process launch failed')),
      callRpc: vi.fn(),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('unreachable');
  });

  it('returns unreachable when ping never succeeds', async () => {
    // Provide a fast-cycling callRpc that always fails ping
    const callRpc = vi.fn().mockRejectedValue(new Error('ECONNREFUSED'));
    const transport = makeTransport({ callRpc });

    // Override setTimeout to avoid real waiting — tick forward immediately
    vi.useFakeTimers();
    const promise = runBootCheck({ kind: 'local' }, transport);
    // Drain all pending micro-tasks + setTimeout callbacks
    await vi.runAllTimersAsync();
    const result = await promise;
    vi.useRealTimers();

    expect(result.kind).toBe('unreachable');
    // start_core_process succeeded (invokeCmd resolves) so portConflict must NOT be set —
    // the timeout alone is not evidence of a port conflict.
    if (result.kind === 'unreachable') {
      expect(result.portConflict).toBeFalsy();
    }
  });
});

// ---------------------------------------------------------------------------
// Cloud mode tests
// ---------------------------------------------------------------------------

describe('runBootCheck — cloud mode', () => {
  it('returns match when cloud core version matches', async () => {
    const appVersion = (await import('../../utils/config')).APP_VERSION;

    const transport = makeTransport({
      callRpc: rpcResponder({ 'openhuman.update_version': { result: { version: appVersion } } }),
    });

    const result = await runBootCheck(
      { kind: 'cloud', url: 'https://core.example.com/rpc' },
      transport
    );
    expect(result).toEqual({ kind: 'match' });
  });

  it('returns outdatedCloud when version differs', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({ 'openhuman.update_version': { result: { version: '0.0.0-old' } } }),
    });

    const result = await runBootCheck(
      { kind: 'cloud', url: 'https://core.example.com/rpc' },
      transport
    );
    expect(result).toEqual({ kind: 'outdatedCloud' });
  });

  it('returns noVersionMethod when cloud core returns -32601', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({ 'openhuman.update_version': new Error('-32601 Method not found') }),
    });

    const result = await runBootCheck(
      { kind: 'cloud', url: 'https://core.example.com/rpc' },
      transport
    );
    expect(result).toEqual({ kind: 'noVersionMethod' });
  });

  it('returns unreachable on network failure', async () => {
    const transport = makeTransport({
      callRpc: vi.fn().mockRejectedValue(new Error('Network unreachable')),
    });

    const result = await runBootCheck(
      { kind: 'cloud', url: 'https://unreachable.example.com/rpc' },
      transport
    );
    expect(result.kind).toBe('unreachable');
  });
});

// ---------------------------------------------------------------------------
// Unset mode guard
// ---------------------------------------------------------------------------

describe('runBootCheck — unset mode', () => {
  it('returns unreachable when called with unset mode', async () => {
    const transport = makeTransport();
    const result: BootCheckResult = await runBootCheck({ kind: 'unset' }, transport);
    expect(result.kind).toBe('unreachable');
  });
});

// ---------------------------------------------------------------------------
// Port conflict auto-recovery tests
// ---------------------------------------------------------------------------

describe('runBootCheck — port conflict auto-recovery', () => {
  it('auto-recovery succeeds: start fails, recovery succeeds, second start succeeds', async () => {
    const appVersion = (await import('../../utils/config')).APP_VERSION;

    let startCallCount = 0;
    const transport: BootCheckTransport = {
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: false },
        'openhuman.update_version': { result: { version: appVersion } },
      }),
      invokeCmd: vi.fn(async (cmd: string) => {
        if (cmd === 'start_core_process') {
          startCallCount += 1;
          if (startCallCount === 1) throw new Error('port in use');
          return undefined;
        }
        return undefined;
      }) as BootCheckTransport['invokeCmd'],
      recoverPortConflict: vi
        .fn()
        .mockResolvedValue({ success: true, message: 'recovered', new_port: 7789 }),
    };

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('match');
    expect(transport.recoverPortConflict).toHaveBeenCalled();
  });

  it('returns unreachable with portConflict=true when both start and recovery fail', async () => {
    const transport: BootCheckTransport = {
      callRpc: vi.fn(),
      invokeCmd: vi.fn().mockRejectedValue(new Error('port in use')),
      recoverPortConflict: vi
        .fn()
        .mockResolvedValue({ success: false, message: 'port still busy', new_port: undefined }),
    };

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('unreachable');
    if (result.kind === 'unreachable') {
      expect(result.portConflict).toBe(true);
    }
  });

  it('threads the foreign owner through when recovery identifies one', async () => {
    const transport: BootCheckTransport = {
      callRpc: vi.fn(),
      invokeCmd: vi.fn().mockRejectedValue(new Error('port in use')),
      recoverPortConflict: vi
        .fn()
        .mockResolvedValue({
          success: false,
          message: 'port still busy',
          foreign_owner: { pid: 4242, name: 'Skype.exe' },
        }),
    };

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('unreachable');
    if (result.kind === 'unreachable') {
      expect(result.portConflict).toBe(true);
      expect(result.foreignOwner).toEqual({ pid: 4242, name: 'Skype.exe' });
    }
  });

  it('clears RPC URL cache and retries waitForCore on timeout', async () => {
    const appVersion = (await import('../../utils/config')).APP_VERSION;

    let pingCallCount = 0;
    const transport: BootCheckTransport = {
      invokeCmd: vi.fn().mockResolvedValue(undefined),
      callRpc: vi.fn(async (method: string) => {
        if (method === 'core.ping') {
          pingCallCount += 1;
          // waitForCore(10_000) makes ~12 attempts with 200→1000ms exponential backoff.
          // Fail exactly those 12 so the initial call times out; ping 13 succeeds so
          // the cache-clear retry waitForCore(5_000) returns true on its first attempt.
          if (pingCallCount <= 12) throw new Error('timeout');
          return {};
        }
        if (method === 'openhuman.service_status') return { installed: false, running: false };
        if (method === 'openhuman.update_version') return { result: { version: appVersion } };
        throw new Error(`Unexpected RPC: ${method}`);
      }) as BootCheckTransport['callRpc'],
    };

    vi.useFakeTimers();
    const promise = runBootCheck({ kind: 'local' }, transport);
    await vi.runAllTimersAsync();
    const result = await promise;
    vi.useRealTimers();

    // Initial waitForCore timed out → cache cleared → second waitForCore succeeded.
    expect(result.kind).toBe('match');
  });
});

// ---------------------------------------------------------------------------
// Edge-case branches surfaced by the diff-coverage gate
// ---------------------------------------------------------------------------

describe('runBootCheck — error and edge branches', () => {
  it('treats service_status throw as "no daemon" and continues', async () => {
    const appVersion = (await import('../../utils/config')).APP_VERSION;

    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': new Error('rpc transport blew up'),
        'openhuman.update_version': { result: { version: appVersion } },
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('match');
  });

  it('treats empty version as outdatedLocal', async () => {
    const transport = makeTransport({
      callRpc: rpcResponder({
        'core.ping': {},
        'openhuman.service_status': { installed: false, running: false },
        'openhuman.update_version': { result: { version: '' } },
      }),
    });

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('outdatedLocal');
  });

  it('returns unreachable when start_core_process Tauri command fails', async () => {
    const transport: BootCheckTransport = {
      callRpc: vi.fn(),
      invokeCmd: vi.fn().mockRejectedValue(new Error('tauri command not registered')),
    };

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('unreachable');
    if (result.kind === 'unreachable') {
      expect(result.reason).toContain('Failed to start local core');
    }
  });

  it('returns unreachable when local version check throws repeatedly', async () => {
    let pingCalls = 0;
    const transport: BootCheckTransport = {
      callRpc: vi.fn(async (method: string) => {
        if (method === 'core.ping') {
          pingCalls += 1;
          if (pingCalls === 1) return {};
          throw new Error('subsequent failure');
        }
        if (method === 'openhuman.service_status') {
          return { installed: false, running: false };
        }
        if (method === 'openhuman.update_version') {
          // Generic transport error (no -32601), should map to 'unreachable'.
          throw new Error('connection reset');
        }
        throw new Error(`Unexpected RPC: ${method}`);
      }) as BootCheckTransport['callRpc'],
      invokeCmd: vi.fn().mockResolvedValue(undefined),
    };

    const result = await runBootCheck({ kind: 'local' }, transport);
    expect(result.kind).toBe('unreachable');
  });

  it('refuses to persist an invalid cloud URL', async () => {
    const transport = makeTransport();
    const result = await runBootCheck({ kind: 'cloud', url: 'not a url' }, transport);
    expect(result.kind).toBe('unreachable');
    if (result.kind === 'unreachable') {
      expect(result.reason).toContain('valid URL');
    }
    expect(transport.callRpc).not.toHaveBeenCalled();
  });
});

// ---------------------------------------------------------------------------
// Gateway mode tests
// ---------------------------------------------------------------------------

describe('runBootCheck — gateway mode', () => {
  it('re-activates the chosen gateway before reporting a match', async () => {
    // A provisioned gateway lives only as long as the process holding its
    // tunnel, so a relaunch starts with nothing held open and the shell
    // answering from the embedded core. Skipping this would leave the user
    // silently on the wrong core with everything appearing to work.
    const invokeCmd = vi.fn().mockResolvedValue(undefined);
    const transport = makeTransport({ invokeCmd });

    const result: BootCheckResult = await runBootCheck(
      { kind: 'gateway', gatewayId: 'builder' },
      transport
    );

    expect(invokeCmd).toHaveBeenCalledWith('gateway_activate', { id: 'builder' });
    expect(result).toEqual({ kind: 'match' });
  });

  it('reports the failure instead of falling back to the local core', async () => {
    // The two hold different data. Quietly swapping one for the other is how a
    // user ends up wondering where their conversations went.
    const invokeCmd = vi.fn().mockRejectedValue(new Error('could not reach the box'));
    const transport = makeTransport({ invokeCmd });

    const result: BootCheckResult = await runBootCheck(
      { kind: 'gateway', gatewayId: 'builder' },
      transport
    );

    expect(result).toEqual({ kind: 'unreachable', reason: 'could not reach the box' });
  });

  it('does not run the version check against a gateway core', async () => {
    // A gateway's core is whatever image or binary the user pointed at, so
    // "older than this UI" is a choice they made, not a broken install.
    const callRpc = vi.fn();
    const transport = makeTransport({ callRpc });

    await runBootCheck({ kind: 'gateway', gatewayId: 'builder' }, transport);

    expect(callRpc).not.toHaveBeenCalled();
  });
});