Skip to content

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

15.0 KB
Raw Blame
import { fireEvent, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { renderWithProviders } from '../../../../test/test-utils';
import {
  type AgentSettings,
  type AutonomySettings,
  isTauri,
  openhumanGetAgentSettings,
  openhumanGetAutonomySettings,
  openhumanUpdateAgentSettings,
  openhumanUpdateAutonomySettings,
} from '../../../../utils/tauriCommands';
import AgentAccessPanel from '../AgentAccessPanel';

// ──────────────────────────────────────────────────────────────────────────────
// Note: Tier-selection and action-dir editing tests live in
// PermissionsPanel.test.tsx (those controls moved to the layman panel).
// This file covers the ADVANCED surface: workspace confinement, action
// timeout, granted folders, always-allowed tools, and the approval-history
// link.
// ──────────────────────────────────────────────────────────────────────────────

const autonomy = (overrides: Partial<AutonomySettings> = {}): AutonomySettings => ({
  level: 'supervised',
  workspace_only: false,
  allowed_commands: [],
  forbidden_paths: [],
  trusted_roots: [],
  allow_tool_install: true,
  max_actions_per_hour: 0,
  auto_approve: [],
  auto_approve_all: false,
  ...overrides,
});

const agentSettings = (overrides: Partial<AgentSettings> = {}): AgentSettings => ({
  agent_timeout_secs: 120,
  effective_timeout_secs: 120,
  env_override: false,
  min_timeout_secs: 1,
  max_timeout_secs: 3600,
  tool_dispatcher: 'auto',
  tool_dispatcher_env_override: false,
  ...overrides,
});

vi.mock('../../hooks/useSettingsNavigation', () => ({
  useSettingsNavigation: () => ({
    navigateBack: vi.fn(),
    navigateToSettings: vi.fn(),
    breadcrumbs: [],
  }),
}));

vi.mock('../../../../utils/tauriCommands', async () => {
  const actual = await vi.importActual<typeof import('../../../../utils/tauriCommands')>(
    '../../../../utils/tauriCommands'
  );
  return {
    ...actual,
    isTauri: vi.fn(() => true),
    openhumanGetAutonomySettings: vi.fn(),
    openhumanUpdateAutonomySettings: vi.fn(),
    openhumanGetAgentSettings: vi.fn(),
    openhumanUpdateAgentSettings: vi.fn(),
    // The "Files folder" row (#5505) reads the agent paths on mount.
    openhumanGetAgentPaths: vi.fn(async () => ({
      result: {
        action_dir: '/home/u/OpenHuman/projects',
        workspace_dir: '/home/u/.openhuman/users/u/workspace',
        projects_dir: '/home/u/OpenHuman/projects',
        action_dir_source: 'default',
        files_dir: '/home/u/OpenHuman/projects/Files',
        default_files_dir: '/home/u/OpenHuman/projects/Files',
        files_dir_source: 'default',
      },
      logs: [],
    })),
    openhumanUpdateAgentPaths: vi.fn(),
  };
});

const mockGet = vi.mocked(openhumanGetAutonomySettings);
const mockUpdate = vi.mocked(openhumanUpdateAutonomySettings);
const mockGetAgent = vi.mocked(openhumanGetAgentSettings);
const mockUpdateAgent = vi.mocked(openhumanUpdateAgentSettings);
describe('AgentAccessPanel (advanced)', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(isTauri).mockReturnValue(true);
    mockGet.mockResolvedValue({ result: autonomy(), logs: [] });
    mockUpdate.mockResolvedValue({ result: {} as never, logs: [] });
    mockGetAgent.mockResolvedValue({ result: agentSettings(), logs: [] });
    mockUpdateAgent.mockResolvedValue({ result: {} as never, logs: [] });
  });

  it('loads settings on mount and renders the advanced controls', async () => {
    renderWithProviders(<AgentAccessPanel />);
    await waitFor(() => expect(mockGet).toHaveBeenCalledTimes(1));
    // The tier radio buttons no longer live in this panel.
    expect(screen.queryByText('Read-only')).not.toBeInTheDocument();
    expect(screen.queryByText('Ask before edit')).not.toBeInTheDocument();
    expect(screen.queryByText('Full access')).not.toBeInTheDocument();
    // The advanced controls are present.
    expect(await screen.findByText('Confine to workspace')).toBeInTheDocument();
    expect(screen.getByText('Granted folders')).toBeInTheDocument();
    expect(screen.getByText('Always-allowed tools')).toBeInTheDocument();
    expect(screen.getByText('Files folder')).toBeInTheDocument();
  });

  it('toggling "confine to workspace" persists workspace_only', async () => {
    renderWithProviders(<AgentAccessPanel />);
    await screen.findByText('Confine to workspace');
    // Controls are now role="switch" (SettingsSwitch) instead of native checkboxes.
    fireEvent.click(screen.getByRole('switch', { name: /confine to workspace/i }));
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenCalledWith(expect.objectContaining({ workspace_only: true }))
    );
  });

  it('adding then removing a granted folder persists the updated list', async () => {
    renderWithProviders(<AgentAccessPanel />);
    await screen.findByText('Granted folders');

    fireEvent.change(screen.getByLabelText('Absolute folder path'), {
      target: { value: '/tmp/proj' },
    });
    fireEvent.click(screen.getByText('Add'));
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenCalledWith(
        expect.objectContaining({ trusted_roots: [{ path: '/tmp/proj', access: 'read' }] })
      )
    );

    fireEvent.click(await screen.findByText('Remove'));
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenLastCalledWith(expect.objectContaining({ trusted_roots: [] }))
    );
  });

  it('renders the loaded tier and pre-existing granted folders (loaded but not shown)', async () => {
    mockGet.mockResolvedValue({
      result: autonomy({
        level: 'readonly',
        workspace_only: true,
        trusted_roots: [{ path: '/home/u/notes', access: 'readwrite' }],
      }),
      logs: [],
    });
    renderWithProviders(<AgentAccessPanel />);
    // Folder path is visible.
    expect(await screen.findByText('/home/u/notes')).toBeInTheDocument();
    // workspace_only switch has aria-checked="true".
    expect(screen.getByRole('switch', { name: /confine to workspace/i })).toHaveAttribute(
      'aria-checked',
      'true'
    );
    // But the tier radio UI is NOT here (lives in PermissionsPanel).
    expect(screen.queryByText('Read-only')).not.toBeInTheDocument();
  });

  it('shows the empty "always-allow" state when no tools are allow-listed', async () => {
    renderWithProviders(<AgentAccessPanel />);
    expect(await screen.findByText('Always-allowed tools')).toBeInTheDocument();
    expect(screen.getByText('No always-allowed tools yet.')).toBeInTheDocument();
  });

  it('lists always-allowed tools and removing one persists the trimmed list', async () => {
    mockGet.mockResolvedValue({ result: autonomy({ auto_approve: ['shell', 'curl'] }), logs: [] });
    renderWithProviders(<AgentAccessPanel />);

    // The allowlist renders each tool name.
    expect(await screen.findByText('shell')).toBeInTheDocument();
    expect(screen.getByText('curl')).toBeInTheDocument();

    // trusted_roots is empty, so the only Remove buttons belong to the
    // allowlist. Removing the first entry persists the trimmed list.
    fireEvent.click(screen.getAllByText('Remove')[0]);
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenLastCalledWith(
        expect.objectContaining({ auto_approve: ['curl'] })
      )
    );
  });

  it('surfaces a load error without crashing', async () => {
    mockGet.mockRejectedValue(new Error('boom'));
    renderWithProviders(<AgentAccessPanel />);
    expect(await screen.findByText('boom')).toBeInTheDocument();
  });

  it('loads settings over core RPC off-Tauri without a desktop-only notice', async () => {
    vi.mocked(isTauri).mockReturnValue(false);
    renderWithProviders(<AgentAccessPanel />);
    await waitFor(() => expect(mockGet).toHaveBeenCalled());
    expect(mockGetAgent).toHaveBeenCalled();
    expect(
      screen.queryByText('Access settings are only available in the desktop app.')
    ).not.toBeInTheDocument();
  });

  it('loads the configured action timeout into the input', async () => {
    mockGetAgent.mockResolvedValue({
      result: agentSettings({ agent_timeout_secs: 300 }),
      logs: [],
    });
    renderWithProviders(<AgentAccessPanel />);
    const input = (await screen.findByLabelText('Action timeout')) as HTMLInputElement;
    expect(input.value).toBe('300');
  });

  it('persists a changed action timeout on blur-sm', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const input = await screen.findByLabelText('Action timeout');
    fireEvent.change(input, { target: { value: '300' } });
    fireEvent.blur(input);
    await waitFor(() => expect(mockUpdateAgent).toHaveBeenCalledWith({ agent_timeout_secs: 300 }));
  });

  it('rejects an out-of-range timeout without calling the RPC', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const input = await screen.findByLabelText('Action timeout');
    fireEvent.change(input, { target: { value: '99999' } });
    fireEvent.blur(input);
    expect(await screen.findByText(/within the allowed range/i)).toBeInTheDocument();
    expect(mockUpdateAgent).not.toHaveBeenCalled();
  });

  it('does not re-persist when the timeout is unchanged', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const input = await screen.findByLabelText('Action timeout');
    fireEvent.blur(input); // value still the loaded 120
    await waitFor(() => expect(mockGetAgent).toHaveBeenCalled());
    expect(mockUpdateAgent).not.toHaveBeenCalled();
  });

  it('defaults the tool call format to JSON (auto)', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const select = (await screen.findByLabelText('Tool call format')) as HTMLSelectElement;
    await waitFor(() => expect(mockGetAgent).toHaveBeenCalled());
    expect(select.value).toBe('auto');
  });

  it('persists a changed tool call format', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const select = await screen.findByLabelText('Tool call format');
    fireEvent.change(select, { target: { value: 'python' } });
    await waitFor(() =>
      expect(mockUpdateAgent).toHaveBeenCalledWith({ tool_dispatcher: 'python' })
    );
  });

  it('reverts the tool call format when saving fails', async () => {
    mockUpdateAgent.mockRejectedValueOnce(new Error('nope'));
    renderWithProviders(<AgentAccessPanel />);
    const select = (await screen.findByLabelText('Tool call format')) as HTMLSelectElement;
    fireEvent.change(select, { target: { value: 'xml' } });
    expect(await screen.findByText('nope')).toBeInTheDocument();
    expect(select.value).toBe('auto');
  });

  it('locks the tool call format when the env override is active', async () => {
    mockGetAgent.mockResolvedValue({
      result: agentSettings({ tool_dispatcher: 'native', tool_dispatcher_env_override: true }),
      logs: [],
    });
    renderWithProviders(<AgentAccessPanel />);
    const select = (await screen.findByLabelText('Tool call format')) as HTMLSelectElement;
    await waitFor(() => expect(select.disabled).toBe(true));
    expect(select.value).toBe('native');
    expect(screen.getByText(/OPENHUMAN_TOOL_DISPATCHER/)).toBeInTheDocument();
  });

  it('disables the timeout input and warns when an env override is active', async () => {
    mockGetAgent.mockResolvedValue({ result: agentSettings({ env_override: true }), logs: [] });
    renderWithProviders(<AgentAccessPanel />);
    const input = (await screen.findByLabelText('Action timeout')) as HTMLInputElement;
    expect(input.disabled).toBe(true);
    expect(screen.getByText(/OPENHUMAN_TOOL_TIMEOUT_SECS/)).toBeInTheDocument();
  });

  it('approval history link button is present and has the correct data-testid', async () => {
    renderWithProviders(<AgentAccessPanel />);
    await screen.findByText('Approval history');
    expect(screen.getByTestId('agent-access-approval-history-link')).toBeInTheDocument();
  });

  // ── auto-approve-all bypass (security-sensitive) ────────────────────────

  it('renders the auto-approve-all toggle OFF by default', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const sw = await screen.findByRole('switch', { name: /auto-approve all actions/i });
    expect(sw).toHaveAttribute('aria-checked', 'false');
  });

  it('toggling auto-approve-all ON persists auto_approve_all: true', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const sw = await screen.findByRole('switch', { name: /auto-approve all actions/i });
    fireEvent.click(sw);
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenCalledWith(expect.objectContaining({ auto_approve_all: true }))
    );
  });

  it('shows the warning description regardless of toggle state', async () => {
    renderWithProviders(<AgentAccessPanel />);
    const sw = await screen.findByRole('switch', { name: /auto-approve all actions/i });

    // Off state: warning is already visible.
    expect(sw).toHaveAttribute('aria-checked', 'false');
    expect(screen.getByTestId('auto-approve-all-warning')).toBeInTheDocument();
    expect(screen.getByTestId('auto-approve-all-warning')).toHaveTextContent(
      /credential and system directories/i
    );

    // On state: toggling it on keeps the same warning mounted, still visible.
    fireEvent.click(sw);
    await waitFor(() => expect(sw).toHaveAttribute('aria-checked', 'true'));
    expect(screen.getByTestId('auto-approve-all-warning')).toBeInTheDocument();
  });

  it('reverts the auto-approve-all toggle when the save fails', async () => {
    mockUpdate.mockRejectedValueOnce(new Error('boom'));
    renderWithProviders(<AgentAccessPanel />);
    const sw = await screen.findByRole('switch', { name: /auto-approve all actions/i });
    fireEvent.click(sw);
    // The RPC must actually be attempted (and fail)…
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenCalledWith(expect.objectContaining({ auto_approve_all: true }))
    );
    // …then the optimistic flip reverts to off, so the switch never shows a
    // falsely-safe "off" or falsely-enabled "on" state the server disagrees with.
    await waitFor(() => expect(sw).toHaveAttribute('aria-checked', 'false'));
  });

  it('does not resend auto_approve_all when an unrelated field is saved', async () => {
    // auto_approve_all starts true; toggling an unrelated switch (workspace
    // confinement) must omit auto_approve_all from the patch entirely so a
    // stale/loaded panel value can never clobber it back down.
    mockGet.mockResolvedValue({ result: autonomy({ auto_approve_all: true }), logs: [] });
    renderWithProviders(<AgentAccessPanel />);
    fireEvent.click(await screen.findByRole('switch', { name: /confine to workspace/i }));
    await waitFor(() =>
      expect(mockUpdate).toHaveBeenCalledWith(expect.objectContaining({ workspace_only: true }))
    );
    const [[payload]] = mockUpdate.mock.calls;
    expect(payload).not.toHaveProperty('auto_approve_all');
  });
});