Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

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

const getAllProviders = vi.fn();
const isProviderAvailable = vi.fn(() => true);

vi.mock('./providers.js', () => ({ getAllProviders }));
vi.mock('./providerStatus.js', () => ({ isProviderAvailable }));

const {
  eligiblePublicReviewProviders,
  publicReviewPostureForTask,
  resolvePublicReviewProvider,
} = await import('./publicReviewProviderSelection.js');

const CODEX = { id: 'codex-cli', type: 'cli', command: 'codex' };
const GROK = { id: 'grok-cli', type: 'cli', command: 'grok' };
const LOCAL_CLAUDE = { id: 'claude-ollama', type: 'cli', command: 'claude', ollamaBacked: true };
const OPENCODE = { id: 'opencode', type: 'cli', command: 'opencode' };
const OLLAMA_API = { id: 'ollama', type: 'api' };

const seed = (providers, activeProvider = null) => {
  getAllProviders.mockResolvedValue({ providers, activeProvider });
};

describe('publicReviewPostureForTask', () => {
  it('maps a stage execution profile to its posture and ignores ordinary tasks', () => {
    expect(publicReviewPostureForTask({ metadata: { executionProfile: 'public-review-gate' } })).toBe('no-tool');
    expect(publicReviewPostureForTask({ metadata: { executionProfile: 'public-review-actions' } })).toBe('sandboxed-actions');
    expect(publicReviewPostureForTask({ metadata: {} })).toBeNull();
    expect(publicReviewPostureForTask(undefined)).toBeNull();
  });
});

describe('eligiblePublicReviewProviders', () => {
  beforeEach(() => vi.clearAllMocks());

  // The regression this uniquely catches: an install without codex/antigravity
  // must still surface its own providers rather than an empty list.
  it('returns this install’s own eligible providers, not a fixed vendor list', async () => {
    seed([GROK, OPENCODE, OLLAMA_API]);
    expect((await eligiblePublicReviewProviders('no-tool')).map((p) => p.id)).toEqual(['grok-cli']);
    // The actions stage is open to every binary provider — opencode has no
    // sandbox recipe but runs headless in the disposable worktree; the api
    // provider has no binary at all.
    expect((await eligiblePublicReviewProviders('sandboxed-actions')).map((p) => p.id)).toEqual(['grok-cli']);
  });

  it('excludes providers the user has switched off', async () => {
    seed([{ ...CODEX, enabled: false }, LOCAL_CLAUDE]);
    expect((await eligiblePublicReviewProviders('no-tool')).map((p) => p.id)).toEqual(['claude-ollama']);
  });

  it('keeps a momentarily-unavailable provider selectable', async () => {
    isProviderAvailable.mockReturnValue(false);
    seed([GROK]);
    expect((await eligiblePublicReviewProviders('no-tool')).map((p) => p.id)).toEqual(['grok-cli']);
  });
});

describe('resolvePublicReviewProvider', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    isProviderAvailable.mockReturnValue(true);
  });

  it('honors an eligible stage pin over the active provider', async () => {
    seed([CODEX, GROK], { id: 'codex-cli' });
    await expect(resolvePublicReviewProvider({ posture: 'no-tool', pinnedProviderId: 'grok-cli' }))
      .resolves.toMatchObject({ ok: true, pinHonored: true, provider: { id: 'grok-cli' } });
  });

  it('blocks an ineligible pin instead of spending on another provider', async () => {
    seed([OPENCODE, GROK], { id: 'opencode' });
    const resolved = await resolvePublicReviewProvider({ posture: 'no-tool', pinnedProviderId: 'opencode' });
    expect(resolved).toMatchObject({ ok: false, code: 'public-review-provider-pin-unavailable' });
  });

  it('blocks a worktree-only actions pin instead of choosing a subscription provider', async () => {
    seed([OPENCODE, CODEX], { id: 'codex-cli' });
    const resolved = await resolvePublicReviewProvider({ posture: 'sandboxed-actions', pinnedProviderId: 'opencode' });
    expect(resolved).toMatchObject({ ok: false, code: 'public-review-provider-pin-unavailable' });
  });

  it.each([{ ...LOCAL_CLAUDE, enabled: false }, { ...LOCAL_CLAUDE, id: 'renamed' }])('blocks a disabled or missing local pin', async local => {
    seed([local, GROK], { id: 'grok-cli' });
    expect(await resolvePublicReviewProvider({ posture: 'no-tool', pinnedProviderId: LOCAL_CLAUDE.id }))
      .toMatchObject({ ok: false, code: 'public-review-provider-pin-unavailable' });
  });

  it('blocks an unavailable local pin even when a subscription provider is healthy', async () => {
    seed([LOCAL_CLAUDE, GROK], { id: 'grok-cli' });
    isProviderAvailable.mockImplementation(id => id === 'grok-cli');
    expect(await resolvePublicReviewProvider({ posture: 'no-tool', pinnedProviderId: LOCAL_CLAUDE.id }))
      .toMatchObject({ ok: false, code: 'public-review-provider-unavailable' });
  });

  it('prefers an available provider over an unavailable earlier one', async () => {
    isProviderAvailable.mockImplementation((id) => id === 'grok-cli');
    seed([CODEX, GROK]);
    await expect(resolvePublicReviewProvider({ posture: 'no-tool' }))
      .resolves.toMatchObject({ provider: { id: 'grok-cli' } });
  });

  it('still resolves when every eligible provider is momentarily unavailable', async () => {
    isProviderAvailable.mockReturnValue(false);
    seed([GROK]);
    await expect(resolvePublicReviewProvider({ posture: 'no-tool' }))
      .resolves.toMatchObject({ ok: true, provider: { id: 'grok-cli' } });
  });

  it('fails closed with an actionable reason when nothing on this install qualifies', async () => {
    seed([OPENCODE, OLLAMA_API]);
    const gate = await resolvePublicReviewProvider({ posture: 'no-tool' });
    expect(gate.ok).toBe(false);
    expect(gate.code).toBe('public-review-no-eligible-provider');
    expect(gate.error).toMatch(/no-tool/);

    seed([OLLAMA_API, { ...OPENCODE, enabled: false }]);
    const actions = await resolvePublicReviewProvider({ posture: 'sandboxed-actions' });
    expect(actions.ok).toBe(false);
    expect(actions.code).toBe('public-review-no-eligible-provider');
    expect(actions.error).toMatch(/sandboxed-actions/);
  });
});