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

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

vi.mock('../../services/api', () => ({ getSettings: vi.fn(), updateSettings: vi.fn() }));
vi.mock('../../hooks/useProviderModels', () => ({ default: () => ({
  providers: [
    { id: 'local-api', name: 'Local example', type: 'api', endpoint: 'http://127.0.0.1:11434/v1', models: ['example-model'] },
    { id: 'cloud-api', name: 'Cloud example', type: 'api', endpoint: 'https://example.com/v1', models: ['example-model'], defaultModel: 'example-model' },
  ], loading: false,
}) }));
vi.mock('../ui/Toast', () => ({ default: { success: vi.fn() } }));

import { getSettings, updateSettings } from '../../services/api';
import UntrustedContentPolicyPanel from './UntrustedContentPolicyPanel';

beforeEach(() => {
  vi.clearAllMocks();
  getSettings.mockResolvedValue({ untrustedContent: {
    defaults: { classifierMode: 'required', minBenignScore: 0.95 },
    sources: { 'github-pr': { maxInputChars: 50000 } },
  } });
  updateSettings.mockResolvedValue({});
});

describe('content safety policy configuration', () => {
  it('saves a private source override while preserving other policies and excluding cloud providers', async () => {
    render(<UntrustedContentPolicyPanel />);
    fireEvent.change(await screen.findByLabelText('Source'), { target: { value: 'email' } });
    const provider = screen.getByLabelText('Analysis API provider');
    expect(within(provider).queryByRole('option', { name: 'Cloud example' })).not.toBeInTheDocument();
    fireEvent.change(provider, { target: { value: 'local-api' } });
    fireEvent.change(screen.getByLabelText('Classifier requirement'), { target: { value: 'optional' } });
    expect(screen.getByText(/Those checks miss attacks/)).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Save content policies' }));
    await waitFor(() => expect(updateSettings).toHaveBeenCalledWith({ untrustedContent: {
      defaults: { classifierMode: 'required', minBenignScore: 0.95 },
      sources: {
        'github-pr': { maxInputChars: 50000 },
        email: { providerId: 'local-api', model: null, classifierMode: 'optional' },
      },
    } }));
    await waitFor(() => expect(screen.getByRole('button', { name: 'Save content policies' })).toBeDisabled());
  });

  it('shows what Automatic resolves to, skipping an eligible provider without a default model', async () => {
    render(<UntrustedContentPolicyPanel />);
    fireEvent.change(await screen.findByLabelText('Source'), { target: { value: 'github-pr' } });
    expect(screen.getByText(/Resolves to Cloud example with model example-model/)).toBeInTheDocument();
    fireEvent.change(screen.getByLabelText('Source'), { target: { value: 'email' } });
    expect(screen.getByRole('status')).toHaveTextContent('No eligible API provider has a default model');
    fireEvent.change(screen.getByLabelText('Analysis API provider'), { target: { value: 'local-api' } });
    expect(screen.getByRole('status')).toHaveTextContent('The pinned provider has no default model');
  });

  it('requires loaded settings before editing and supports a failed-read retry', async () => {
    getSettings.mockRejectedValueOnce(new Error('unavailable'));
    render(<UntrustedContentPolicyPanel />);
    expect(await screen.findByRole('alert')).toHaveTextContent('Could not load');
    expect(screen.queryByRole('button', { name: 'Save content policies' })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
    expect(await screen.findByLabelText('Source')).toBeInTheDocument();
    expect(updateSettings).not.toHaveBeenCalled();
  });

  it('shows message-family defaults for private channels and resets a source back to that inheritance', async () => {
    getSettings.mockResolvedValueOnce({ untrustedContent: {
      defaults: { providerId: 'cloud-api', model: 'cloud-model', minBenignScore: 0.9 },
      sources: {
        messages: { providerId: 'local-api', classifierMode: 'required', minBenignScore: 0.98 },
        signal: { classifierMode: 'optional' },
      },
    } });
    render(<UntrustedContentPolicyPanel />);
    fireEvent.change(await screen.findByLabelText('Source'), { target: { value: 'signal' } });
    expect(screen.getByLabelText('Analysis API provider')).toHaveValue('local-api');
    expect(screen.getByLabelText('Minimum benign score')).toHaveValue(0.98);
    fireEvent.click(screen.getByRole('button', { name: 'Use message defaults for this source' }));
    expect(screen.getByLabelText('Classifier requirement')).toHaveValue('');
    expect(screen.getByRole('option', { name: 'Inherit message defaults (required)' })).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Save content policies' }));
    await waitFor(() => expect(updateSettings).toHaveBeenCalledWith({ untrustedContent: {
      defaults: { providerId: 'cloud-api', model: 'cloud-model', minBenignScore: 0.9 },
      sources: { messages: { providerId: 'local-api', classifierMode: 'required', minBenignScore: 0.98 } },
    } }));
  });

  it('saves a per-source scorer mode and margin, and warns about the skip posture', async () => {
    render(<UntrustedContentPolicyPanel />);
    fireEvent.change(await screen.findByLabelText('Source'), { target: { value: 'github-issue' } });
    fireEvent.change(screen.getByLabelText('Local scorer'), { target: { value: 'prefer' } });
    expect(screen.queryByText(/Skipped items are reported/)).not.toBeInTheDocument();
    fireEvent.change(screen.getByLabelText('Local scorer'), { target: { value: 'only' } });
    // The skip posture is the one an operator can misread as "no action needed".
    expect(screen.getByText(/Skipped items are reported/)).toBeInTheDocument();
    fireEvent.change(screen.getByLabelText('Minimum decision margin'), { target: { value: '0.4' } });
    fireEvent.click(screen.getByRole('button', { name: 'Save content policies' }));
    await waitFor(() => expect(updateSettings).toHaveBeenCalledWith({ untrustedContent: {
      defaults: { classifierMode: 'required', minBenignScore: 0.95 },
      sources: {
        'github-pr': { maxInputChars: 50000 },
        'github-issue': { jevMode: 'only', jevMinMargin: 0.4 },
      },
    } }));
  });

  it('lists Moltbook as a public source and still offers a cloud API provider', async () => {
    render(<UntrustedContentPolicyPanel />);
    const source = await screen.findByLabelText('Source');
    expect(within(source).getByRole('option', { name: 'Moltbook' })).toBeInTheDocument();
    fireEvent.change(source, { target: { value: 'moltbook' } });
    expect(within(screen.getByLabelText('Analysis API provider')).getByRole('option', { name: 'Cloud example' })).toBeEnabled();
    expect(screen.getByText(/Cloud APIs may receive public sources/)).toBeInTheDocument();
  });

  it('drops a cleared scorer field instead of pinning an empty value', async () => {
    getSettings.mockResolvedValueOnce({ untrustedContent: {
      defaults: { jevMode: 'prefer' },
      sources: { email: { jevMode: 'only', jevMinMargin: 0.7 } },
    } });
    render(<UntrustedContentPolicyPanel />);
    fireEvent.change(await screen.findByLabelText('Source'), { target: { value: 'email' } });
    fireEvent.change(screen.getByLabelText('Minimum decision margin'), { target: { value: '' } });
    fireEvent.change(screen.getByLabelText('Local scorer'), { target: { value: '' } });
    fireEvent.click(screen.getByRole('button', { name: 'Save content policies' }));
    // Re-inheriting means the KEY is gone: a persisted null would be layered on
    // top of the default it is supposed to defer to.
    await waitFor(() => expect(updateSettings).toHaveBeenCalledWith({ untrustedContent: {
      defaults: { jevMode: 'prefer' },
      sources: { email: {} },
    } }));
  });
});