Skip to content

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

7.5 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import reducer, { resetCoreMode, setCoreMode } from './coreModeSlice';

describe('coreModeSlice', () => {
  it('initialises to unset', () => {
    const state = reducer(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({ kind: 'unset' });
  });

  it('sets local mode', () => {
    const state = reducer(undefined, setCoreMode({ kind: 'local' }));
    expect(state.mode).toEqual({ kind: 'local' });
  });

  it('sets cloud mode with url', () => {
    const state = reducer(
      undefined,
      setCoreMode({ kind: 'cloud', url: 'https://core.example.com/rpc' })
    );
    expect(state.mode).toEqual({ kind: 'cloud', url: 'https://core.example.com/rpc' });
  });

  it('sets cloud mode with url + token', () => {
    const state = reducer(
      undefined,
      setCoreMode({ kind: 'cloud', url: 'https://core.example.com/rpc', token: 'tok-1234' })
    );
    expect(state.mode).toEqual({
      kind: 'cloud',
      url: 'https://core.example.com/rpc',
      token: 'tok-1234',
    });
  });

  it('resets to unset', () => {
    const withLocal = reducer(undefined, setCoreMode({ kind: 'local' }));
    const reset = reducer(withLocal, resetCoreMode());
    expect(reset.mode).toEqual({ kind: 'unset' });
  });

  it('overwrites previous mode on setCoreMode', () => {
    const withCloud = reducer(
      undefined,
      setCoreMode({ kind: 'cloud', url: 'https://old.example.com' })
    );
    const withLocal = reducer(withCloud, setCoreMode({ kind: 'local' }));
    expect(withLocal.mode).toEqual({ kind: 'local' });
  });

  it('slice name is coreMode', () => {
    // Structural assertion: the key used by redux-persist must match the
    // persist config key declared in store/index.ts.
    expect(setCoreMode.type).toMatch(/^coreMode\//);
  });
});

describe('coreModeSlice — sync-localStorage-derived initial state', () => {
  // The slice's initialState comes from `deriveInitialMode()` which reads
  // `localStorage` at module load. We re-import per test to exercise each
  // branch of that derivation.
  async function freshImport() {
    vi.resetModules();
    return import('./coreModeSlice');
  }

  it('uses local mode when the E2E default core mode config is local', async () => {
    localStorage.clear();
    vi.resetModules();
    vi.doMock('../utils/config', () => ({
      CORE_RPC_URL: 'http://127.0.0.1:7788/rpc',
      E2E_DEFAULT_CORE_MODE: 'local',
    }));
    try {
      const mod = await import('./coreModeSlice');
      const state = mod.default(undefined, { type: '@@INIT' });
      expect(state.mode).toEqual({ kind: 'local' });
    } finally {
      vi.doUnmock('../utils/config');
      vi.resetModules();
    }
  });

  it('hydrates to local when openhuman_core_mode=local', async () => {
    localStorage.clear();
    localStorage.setItem('openhuman_core_mode', 'local');
    const mod = await freshImport();
    const state = mod.default(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({ kind: 'local' });
  });

  it('hydrates to cloud with url + token when all three keys are present', async () => {
    localStorage.clear();
    localStorage.setItem('openhuman_core_mode', 'cloud');
    localStorage.setItem('openhuman_core_rpc_url', 'https://core.example.com/rpc');
    localStorage.setItem('openhuman_core_rpc_token', 'tok-abc');
    const mod = await freshImport();
    const state = mod.default(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({
      kind: 'cloud',
      url: 'https://core.example.com/rpc',
      token: 'tok-abc',
    });
  });

  it('normalizes restored cloud base URLs to the /rpc endpoint', async () => {
    localStorage.clear();
    localStorage.setItem('openhuman_core_mode', 'cloud');
    localStorage.setItem('openhuman_core_rpc_url', 'https://example.trycloudflare.com/');
    localStorage.setItem('openhuman_core_rpc_token', 'tok-abc');
    const mod = await freshImport();
    const state = mod.default(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({
      kind: 'cloud',
      url: 'https://example.trycloudflare.com/rpc',
      token: 'tok-abc',
    });
  });

  it('falls back to unset when cloud marker exists but URL or token is missing', async () => {
    localStorage.clear();
    localStorage.setItem('openhuman_core_mode', 'cloud');
    localStorage.setItem('openhuman_core_rpc_url', 'https://core.example.com/rpc');
    // Token deliberately missing.
    const mod = await freshImport();
    const state = mod.default(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({ kind: 'unset' });
  });

  it('returns unset when no marker is stored', async () => {
    localStorage.clear();
    const mod = await freshImport();
    const state = mod.default(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({ kind: 'unset' });
  });

  it('keeps the synchronous local marker when redux-persist rehydrates stale unset state', async () => {
    localStorage.clear();
    localStorage.setItem('openhuman_core_mode', 'local');

    const mod = await freshImport();
    const { persistReducer } =
      await vi.importActual<typeof import('redux-persist')>('redux-persist');
    const persistedReducer = persistReducer(
      {
        key: 'coreMode',
        storage: { getItem: vi.fn(), setItem: vi.fn(), removeItem: vi.fn() },
        whitelist: ['mode'],
      },
      mod.default
    );

    const next = persistedReducer(
      { mode: { kind: 'local' }, _persist: { version: -1, rehydrated: false } },
      {
        type: 'persist/REHYDRATE',
        key: 'coreMode',
        payload: { mode: { kind: 'unset' } },
      } as Parameters<typeof persistedReducer>[1]
    );

    expect(next.mode).toEqual({ kind: 'local' });
  });
});

describe('coreModeSlice — gateway mode', () => {
  async function freshImportWith(entries: Record<string, string>) {
    localStorage.clear();
    for (const [k, v] of Object.entries(entries)) localStorage.setItem(k, v);
    vi.resetModules();
    return import('./coreModeSlice');
  }

  it('recovers the chosen gateway synchronously on reload', async () => {
    // redux-persist flushes asynchronously, so a reload can beat it. Without
    // the synchronous marker the app would fall back to the picker after every
    // restart even though the user had chosen a gateway.
    const mod = await freshImportWith({
      openhuman_core_mode: 'gateway',
      openhuman_core_gateway_id: 'builder',
    });

    const state = mod.default(undefined, { type: '@@INIT' });
    expect(state.mode).toEqual({ kind: 'gateway', gatewayId: 'builder' });
  });

  it('falls through to unset when the id is missing', async () => {
    // There is nothing to activate, so asking again beats failing later.
    const mod = await freshImportWith({ openhuman_core_mode: 'gateway' });

    expect(mod.default(undefined, { type: '@@INIT' }).mode).toEqual({ kind: 'unset' });
  });

  it('stores only an id, never a spec or a credential', async () => {
    const mod = await freshImportWith({
      openhuman_core_mode: 'gateway',
      openhuman_core_gateway_id: 'builder',
    });
    const state = mod.default(
      undefined,
      mod.setCoreMode({ kind: 'gateway', gatewayId: 'builder' })
    );

    expect(Object.keys(state.mode)).toEqual(['kind', 'gatewayId']);
  });

  it('switching away from a gateway replaces the mode outright', async () => {
    const mod = await freshImportWith({});
    const asGateway = mod.default(undefined, mod.setCoreMode({ kind: 'gateway', gatewayId: 'b' }));

    expect(mod.default(asGateway, mod.setCoreMode({ kind: 'local' })).mode).toEqual({
      kind: 'local',
    });
  });
});