Skip to content

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

7.6 KB
Raw Blame
import { isTauri } from '@tauri-apps/api/core';
import { afterEach, beforeEach, describe, expect, type Mock, test, vi } from 'vitest';

import { callCoreRpc } from '../../services/coreRpcClient';

vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn(), isTauri: vi.fn() }));
vi.mock('../../services/coreRpcClient', () => ({ callCoreRpc: vi.fn() }));

describe('tauriCommands/config', () => {
  const mockIsTauri = isTauri as Mock;
  const mockCallCoreRpc = callCoreRpc as Mock;
  let openhumanGetAutonomySettings: typeof import('./config').openhumanGetAutonomySettings;
  let openhumanUpdateAutonomySettings: typeof import('./config').openhumanUpdateAutonomySettings;
  let openhumanUpdateLocalAiSettings: typeof import('./config').openhumanUpdateLocalAiSettings;

  beforeEach(async () => {
    vi.clearAllMocks();
    mockIsTauri.mockReturnValue(true);
    const actual = await vi.importActual<typeof import('./config')>('./config');
    openhumanGetAutonomySettings = actual.openhumanGetAutonomySettings;
    openhumanUpdateAutonomySettings = actual.openhumanUpdateAutonomySettings;
    openhumanUpdateLocalAiSettings = actual.openhumanUpdateLocalAiSettings;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  describe('openhumanUpdateLocalAiSettings', () => {
    test('calls core RPC when not running in Tauri', async () => {
      mockIsTauri.mockReturnValue(false);
      mockCallCoreRpc.mockResolvedValue({ result: {}, logs: [] });
      await openhumanUpdateLocalAiSettings({ runtime_enabled: true });
      expect(mockCallCoreRpc).toHaveBeenCalledTimes(1);
    });

    test('forwards the patch to openhuman.inference_update_local_settings', async () => {
      mockCallCoreRpc.mockResolvedValue({
        result: { config: {}, workspace_dir: '/tmp', config_path: '/tmp/cfg.toml' },
        logs: [],
      });
      const patch = {
        runtime_enabled: true,
        opt_in_confirmed: true,
        provider: 'lm_studio',
        base_url: 'http://localhost:1234/v1',
        model_id: 'local-model',
        chat_model_id: 'local-model',
        usage_embeddings: true,
      };
      await openhumanUpdateLocalAiSettings(patch);
      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.inference_update_local_settings',
        params: patch,
      });
    });
  });

  describe('openhumanUpdateAutonomySettings', () => {
    test('calls core RPC when not running in Tauri', async () => {
      mockIsTauri.mockReturnValue(false);
      mockCallCoreRpc.mockResolvedValue({ result: {}, logs: [] });
      await openhumanUpdateAutonomySettings({ max_actions_per_hour: 100 });
      expect(mockCallCoreRpc).toHaveBeenCalledTimes(1);
    });

    test('forwards the patch to openhuman.config_update_autonomy_settings', async () => {
      mockCallCoreRpc.mockResolvedValue({
        result: { config: {}, workspace_dir: '/tmp', config_path: '/tmp/cfg.toml' },
        logs: [],
      });
      await openhumanUpdateAutonomySettings({ max_actions_per_hour: 100 });
      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.config_update_autonomy_settings',
        params: { max_actions_per_hour: 100 },
      });
    });
  });

  describe('openhumanGetAutonomySettings', () => {
    test('calls core RPC when not running in Tauri', async () => {
      mockIsTauri.mockReturnValue(false);
      mockCallCoreRpc.mockResolvedValue({ result: {}, logs: [] });
      await openhumanGetAutonomySettings();
      expect(mockCallCoreRpc).toHaveBeenCalledTimes(1);
    });

    test('reads via openhuman.config_get_autonomy_settings', async () => {
      mockCallCoreRpc.mockResolvedValue({ result: { max_actions_per_hour: 250 }, logs: [] });
      const out = await openhumanGetAutonomySettings();
      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.config_get_autonomy_settings',
      });
      expect(out.result.max_actions_per_hour).toBe(250);
    });
  });

  describe('openhumanUpdateComposioTriggerSettings', () => {
    let openhumanUpdateComposioTriggerSettings: typeof import('./config').openhumanUpdateComposioTriggerSettings;

    beforeEach(async () => {
      const actual = await vi.importActual<typeof import('./config')>('./config');
      openhumanUpdateComposioTriggerSettings = actual.openhumanUpdateComposioTriggerSettings;
    });

    test('calls core RPC when not running in Tauri', async () => {
      mockIsTauri.mockReturnValue(false);
      mockCallCoreRpc.mockResolvedValue({ result: {}, logs: [] });
      await openhumanUpdateComposioTriggerSettings({ triage_disabled: true });
      expect(mockCallCoreRpc).toHaveBeenCalledTimes(1);
    });

    test('forwards the patch to openhuman.config_update_composio_trigger_settings', async () => {
      mockCallCoreRpc.mockResolvedValue({
        result: { config: {}, workspace_dir: '/tmp', config_path: '/tmp/cfg.toml' },
        logs: [],
      });
      const patch = { triage_disabled: true, triage_disabled_toolkits: ['gmail', 'slack'] };
      await openhumanUpdateComposioTriggerSettings(patch);
      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.config_update_composio_trigger_settings',
        params: patch,
      });
    });

    test('returns no-op on unknown method from stale core (#1597)', async () => {
      mockCallCoreRpc.mockRejectedValue(
        new Error('unknown method: openhuman.config_update_composio_trigger_settings')
      );
      const out = await openhumanUpdateComposioTriggerSettings({ triage_disabled: true });
      expect(out).toEqual({ result: { config: {}, workspace_dir: '', config_path: '' }, logs: [] });
    });

    test('rethrows non-unknown-method errors', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('network timeout'));
      await expect(
        openhumanUpdateComposioTriggerSettings({ triage_disabled: true })
      ).rejects.toThrow('network timeout');
    });
  });

  describe('openhumanGetComposioTriggerSettings', () => {
    let openhumanGetComposioTriggerSettings: typeof import('./config').openhumanGetComposioTriggerSettings;

    beforeEach(async () => {
      const actual = await vi.importActual<typeof import('./config')>('./config');
      openhumanGetComposioTriggerSettings = actual.openhumanGetComposioTriggerSettings;
    });

    test('calls core RPC when not running in Tauri', async () => {
      mockIsTauri.mockReturnValue(false);
      mockCallCoreRpc.mockResolvedValue({ result: {}, logs: [] });
      await openhumanGetComposioTriggerSettings();
      expect(mockCallCoreRpc).toHaveBeenCalledTimes(1);
    });

    test('reads via openhuman.config_get_composio_trigger_settings', async () => {
      mockCallCoreRpc.mockResolvedValue({
        result: { triage_disabled: false, triage_disabled_toolkits: ['slack'] },
        logs: [],
      });
      const out = await openhumanGetComposioTriggerSettings();
      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.config_get_composio_trigger_settings',
      });
      expect(out.result.triage_disabled).toBe(false);
      expect(out.result.triage_disabled_toolkits).toEqual(['slack']);
    });

    test('returns defaults on unknown method from stale core (#1597)', async () => {
      mockCallCoreRpc.mockRejectedValue(
        new Error('unknown method: openhuman.config_get_composio_trigger_settings')
      );
      const out = await openhumanGetComposioTriggerSettings();
      expect(out.result.triage_disabled).toBe(false);
      expect(out.result.triage_disabled_toolkits).toEqual([]);
    });

    test('rethrows non-unknown-method errors', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('network timeout'));
      await expect(openhumanGetComposioTriggerSettings()).rejects.toThrow('network timeout');
    });
  });
});