Skip to content

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

4.6 KB
Raw Blame
import { act, renderHook, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { memorySourcesList } from '../../../services/api/memoryApi';
import { isTauri } from '../../../utils/tauriCommands/common';
import { openhumanCronList } from '../../../utils/tauriCommands/cron';
import { useBackgroundActivity, useMemorySyncActive } from './useBackgroundActivity';

vi.mock('../../../utils/tauriCommands/common', () => ({ isTauri: vi.fn(() => true) }));
vi.mock('../../../utils/tauriCommands/cron', () => ({ openhumanCronList: vi.fn() }));
vi.mock('../../../services/api/memoryApi', () => ({ memorySourcesList: vi.fn() }));

const mockCron = vi.mocked(openhumanCronList);
const mockSources = vi.mocked(memorySourcesList);
const mockIsTauri = vi.mocked(isTauri);

function fixtures() {
  mockCron.mockResolvedValue({
    result: [
      {
        id: 'j1',
        expression: '',
        schedule: { kind: 'cron', expr: '0 9 * * *' },
        command: '',
        job_type: 'agent',
        session_target: 'isolated',
        enabled: true,
        delivery: { mode: 'silent', best_effort: true },
        delete_after_run: false,
        created_at: '2024-01-01T00:00:00Z',
        next_run: '2999-01-01T00:00:00Z',
      },
    ],
    logs: [],
  });
  mockSources.mockResolvedValue({
    sources: [
      { id: 's1', kind: 'folder', target: '/notes', label: 'Inbox', status: 'syncing', items: 3 },
      {
        id: 's2',
        kind: 'rss',
        target: 'https://example.com/feed',
        label: '',
        status: 'idle',
        items: 0,
      },
    ],
  });
}

describe('useBackgroundActivity', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockIsTauri.mockReturnValue(true);
    fixtures();
  });
  afterEach(() => vi.restoreAllMocks());

  it('does not fetch while closed', () => {
    renderHook(() => useBackgroundActivity(false));
    expect(mockCron).not.toHaveBeenCalled();
  });

  it('loads cron and memory once open', async () => {
    const { result } = renderHook(() => useBackgroundActivity(true));
    await waitFor(() => expect(result.current.cronJobs).toHaveLength(1));
    expect(result.current.memory).toMatchObject({
      ingesting: true,
      currentTitle: 'Inbox',
      queueDepth: 1,
    });
    expect(result.current.memory.providers).toEqual([
      { provider: 'Inbox', freshness: 'active' },
      { provider: 'https://example.com/feed', freshness: 'idle' },
    ]);
  });

  it('shows no memory activity when memory is off', async () => {
    mockSources.mockRejectedValue(new Error('MEMORY_OFF'));
    const { result } = renderHook(() => useBackgroundActivity(true));
    await waitFor(() => expect(result.current.cronJobs).toHaveLength(1));
    expect(result.current.memory).toEqual({
      ingesting: false,
      currentTitle: undefined,
      queueDepth: 0,
      providers: [],
    });
  });

  it('polls core RPC outside Tauri too (browser attached to a core)', async () => {
    mockIsTauri.mockReturnValue(false);
    const { result } = renderHook(() => useBackgroundActivity(true));
    await waitFor(() => expect(result.current.loading).toBe(false));
    expect(mockCron).toHaveBeenCalled();
  });

  it('tolerates a failing source without dropping the others', async () => {
    mockCron.mockRejectedValue(new Error('boom'));
    const { result } = renderHook(() => useBackgroundActivity(true));
    await waitFor(() => expect(result.current.memory.ingesting).toBe(true));
    expect(result.current.cronJobs).toHaveLength(0);
    expect(result.current.memory.ingesting).toBe(true);
  });
});

describe('useMemorySyncActive', () => {
  function dispatch(detail: Record<string, unknown>) {
    act(() => {
      window.dispatchEvent(new CustomEvent('openhuman:memory-sync-stage', { detail }));
    });
  }

  it('lights up on a non-terminal stage and clears on a terminal one', () => {
    const { result } = renderHook(() => useMemorySyncActive());
    expect(result.current).toBe(false);

    dispatch({ stage: 'ingesting', source_id: 'src-1' });
    expect(result.current).toBe(true);

    dispatch({ stage: 'completed', source_id: 'src-1' });
    expect(result.current).toBe(false);
  });

  it('stays active until every source settles', () => {
    const { result } = renderHook(() => useMemorySyncActive());
    dispatch({ stage: 'fetching', source_id: 'a' });
    dispatch({ stage: 'fetching', source_id: 'b' });
    expect(result.current).toBe(true);

    dispatch({ stage: 'failed', source_id: 'a' });
    expect(result.current).toBe(true); // b still syncing

    dispatch({ stage: 'completed', source_id: 'b' });
    expect(result.current).toBe(false);
  });
});