Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

17.1 KB
Raw Blame
/**
 * Smoke tests for the Detection Tuning workbench (PR-6 / v1.5 §W8).
 *
 * The workbench replaces the old static `/noise-tuning` prototype, so the
 * behaviours we pin here are the ones an SOC engineer would file a P1
 * against if they regressed:
 *
 *   1. Loading skeleton renders before the first server response.
 *   2. Summary cards render real counts from the response.
 *   3. Tuning rows show rule name, severity pill, suggestion pill,
 *      FP%, hit count, confidence, and an auto-tune toggle.
 *   4. The primary apply action maps per suggestion:
 *        - `disable`      → POST apply { action: 'disable' }
 *        - `add_suppression` → POST apply { action: 'add_suppression' }
 *        - `raise_threshold` → POST apply { action: 'raise_threshold' }
 *        - `tune_confidence` / `review_stale` → 'acknowledge'
 *        - `healthy`      → 'acknowledge' (no destructive action)
 *      On success a toast is fired and SWR is revalidated; on error the
 *      message bubbles into a toast.error().
 *   5. Dismiss calls `tuningApi.dismiss(rule_id, {})`, fires a toast,
 *      and revalidates. Already-dismissed rows have a disabled Dismiss.
 *   6. The auto-tune toggle flips state via `tuningApi.autoTune(rule_id, !current)`.
 *   7. Changing the suggestion filter resets the page to 1 (we assert via
 *      `tuningApi.list` being called with `page: 1` after a filter change).
 *   8. The error state renders when SWR returns an error, with a working
 *      retry handler.
 *   9. The empty state copy differs for the "filters dirty" case vs the
 *      "everything is healthy" case, and exposes a Clear-filters CTA in
 *      the former.
 *
 * SWR, `@/lib/api`, and `react-hot-toast` are mocked so the test focuses
 * on the view layer without booting a fetch stack. Mirrors the pattern
 * already established in `QueueView.test.tsx` (PR-5).
 *
 * @author Beenu Arora <[email protected]>
 */

import { describe, expect, it, vi, beforeEach } from 'vitest';
import { act, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { TuningEntry, TuningResponse, TuningSummary } from '@/lib/api';

// ─── Mock SWR ─────────────────────────────────────────────────────────────────
//
// The component calls `useSWR(['detection-tuning', listParams], fetcher, opts)`.
// We mirror the subset of the hook surface the component touches and expose a
// hand-rolled `mutate()` that tests can assert on.

const swrState = vi.hoisted(() => ({
  data: undefined as TuningResponse | undefined,
  error: undefined as Error | undefined,
  isLoading: false,
  isValidating: false,
}));

const mutateMock = vi.hoisted(() => vi.fn());

vi.mock('swr', () => ({
  __esModule: true,
  default: () => ({
    data: swrState.data,
    error: swrState.error,
    isLoading: swrState.isLoading,
    isValidating: swrState.isValidating,
    mutate: mutateMock,
  }),
}));

// ─── Mock the API layer ───────────────────────────────────────────────────────

const tuningListMock = vi.hoisted(() => vi.fn());
const tuningApplyMock = vi.hoisted(() => vi.fn());
const tuningDismissMock = vi.hoisted(() => vi.fn());
const tuningAutoTuneMock = vi.hoisted(() => vi.fn());
const tuningSummaryMock = vi.hoisted(() => vi.fn());

vi.mock('@/lib/api', () => ({
  __esModule: true,
  tuningApi: {
    list: tuningListMock,
    summary: tuningSummaryMock,
    apply: tuningApplyMock,
    dismiss: tuningDismissMock,
    autoTune: tuningAutoTuneMock,
  },
}));

// ─── Mock toast ───────────────────────────────────────────────────────────────

const toastSuccess = vi.hoisted(() => vi.fn());
const toastError = vi.hoisted(() => vi.fn());
vi.mock('react-hot-toast', () => ({
  __esModule: true,
  default: { success: toastSuccess, error: toastError },
}));

// Import AFTER mocks so the module picks them up.
import RuleTuningView from './RuleTuningView';

// ─── Fixtures ─────────────────────────────────────────────────────────────────

const NOW_ISO = '2026-05-13T12:00:00Z';

function makeEntry(overrides: Partial<TuningEntry> = {}): TuningEntry {
  return {
    rule_id: 'rule-1',
    name: 'Suspicious PowerShell encoded command',
    description: 'PowerShell -EncodedCommand seen on endpoint',
    category: 'endpoint',
    severity: 'high',
    status: 'enabled',
    enabled: true,
    confidence: 78,
    fp_rate: 0.42,
    total_hits: 1280,
    last_triggered_at: '2026-05-12T18:00:00Z',
    tags: ['edr', 'living-off-the-land'],
    mitre_tactics: ['execution'],
    mitre_techniques: ['T1059.001'],
    version: 3,
    updated_at: '2026-05-13T11:30:00Z',
    suggestion: 'add_suppression',
    score: 0.82,
    reasons: ['High FP rate (42%) over the last 7 days'],
    auto_tune: false,
    dismissed_at: null,
    last_action: null,
    last_action_at: null,
    ...overrides,
  };
}

function makeSummary(overrides: Partial<TuningSummary> = {}): TuningSummary {
  return {
    total_rules: 42,
    actionable: 7,
    healthy: 30,
    disable_count: 1,
    add_suppression_count: 3,
    raise_threshold_count: 2,
    tune_confidence_count: 1,
    review_stale_count: 0,
    auto_tune_enabled: 12,
    average_fp_rate: 0.12,
    high_fp_count: 4,
    ...overrides,
  };
}

function makeResponse(
  entries: TuningEntry[],
  overrides: Partial<TuningResponse> = {},
): TuningResponse {
  return {
    entries,
    summary: makeSummary(),
    filters: {
      severity: null,
      suggestion: null,
      search: null,
      enabled_only: true,
      include_dismissed: false,
      page: 1,
      page_size: 25,
    },
    total: entries.length,
    generated_at: NOW_ISO,
    ...overrides,
  };
}

beforeEach(() => {
  swrState.data = undefined;
  swrState.error = undefined;
  swrState.isLoading = false;
  swrState.isValidating = false;
  mutateMock.mockReset();
  mutateMock.mockResolvedValue(undefined);
  tuningListMock.mockReset();
  tuningApplyMock.mockReset();
  tuningDismissMock.mockReset();
  tuningAutoTuneMock.mockReset();
  tuningSummaryMock.mockReset();
  toastSuccess.mockReset();
  toastError.mockReset();
});

/**
 * Click an action button and drain the resulting async handler inside a
 * single `act()` boundary.
 *
 * Same pattern as `QueueView.test.tsx`: handlers like onApply / onDismiss
 * end in a `finally { setBusy(null) }` state commit that lands outside
 * user-event's implicit `act()` wrapper, so we manually drain microtasks.
 */
async function clickAndSettle(user: ReturnType<typeof userEvent.setup>, el: HTMLElement) {
  await act(async () => {
    await user.click(el);
    await Promise.resolve();
    await Promise.resolve();
    await Promise.resolve();
  });
}

describe('RuleTuningView', () => {
  it('renders the loading skeleton before the first server response', () => {
    swrState.isLoading = true;
    swrState.data = undefined;

    render(<RuleTuningView />);

    // Header still renders so analysts have context during cold load.
    expect(screen.getByText(/Detection Tuning/i)).toBeInTheDocument();
    // No rules yet.
    expect(
      screen.queryByText('Suspicious PowerShell encoded command'),
    ).not.toBeInTheDocument();
  });

  it('renders the empty state when no rules need tuning', () => {
    swrState.data = makeResponse([]);

    render(<RuleTuningView />);

    expect(screen.getByText(/No tuning suggestions right now/i)).toBeInTheDocument();
    expect(
      screen.getByText(/All enabled rules are within healthy/i),
    ).toBeInTheDocument();
    // No Clear-filters CTA on the happy-path empty.
    expect(screen.queryByRole('button', { name: /Clear filters/i })).not.toBeInTheDocument();
  });

  it('renders summary cards from the response payload', () => {
    swrState.data = makeResponse([], {
      summary: makeSummary({
        total_rules: 120,
        actionable: 9,
        healthy: 100,
        auto_tune_enabled: 14,
        average_fp_rate: 0.087,
        high_fp_count: 5,
      }),
    });

    render(<RuleTuningView />);

    // Card labels render inside <p> tags; the suggestion filter option also
    // says "Healthy" so we scope with the `selector` shim to avoid false
    // positives.
    expect(screen.getByText('Total Rules', { selector: 'p' })).toBeInTheDocument();
    expect(screen.getByText('120')).toBeInTheDocument();
    expect(screen.getByText(/9 actionable/i)).toBeInTheDocument();

    // Avg FP Rate card — 8.7% with one decimal.
    expect(screen.getByText('Avg FP Rate', { selector: 'p' })).toBeInTheDocument();
    expect(screen.getByText('8.7%')).toBeInTheDocument();
    expect(screen.getByText(/5 noisy rules/i)).toBeInTheDocument();

    // Healthy card — 100 / 120 ≈ 83%. We disambiguate from the dropdown
    // option by constraining the selector to the card's `<p>` label.
    expect(screen.getByText('Healthy', { selector: 'p' })).toBeInTheDocument();
    expect(screen.getByText('100')).toBeInTheDocument();
    expect(screen.getByText('83% of population')).toBeInTheDocument();

    // Auto-Tuned card
    expect(screen.getByText('Auto-Tuned', { selector: 'p' })).toBeInTheDocument();
    expect(screen.getByText('14')).toBeInTheDocument();
  });

  it('renders a tuning row with severity pill, suggestion, FP%, hits, and confidence', () => {
    const entry = makeEntry();
    swrState.data = makeResponse([entry]);

    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    const withinRow = within(row);

    // Name
    expect(withinRow.getByText(entry.name)).toBeInTheDocument();
    // Severity pill — "high" rendered verbatim (the css class uppercases).
    expect(withinRow.getByText('high')).toBeInTheDocument();
    // Suggestion pill — scoped to the <span> so we don't collide with the
    // identically-labelled "Add suppression" apply button.
    expect(
      withinRow.getByText('Add suppression', { selector: 'span' }),
    ).toBeInTheDocument();
    // FP% — 0.42 → 42.0%
    expect(withinRow.getByText('42.0%')).toBeInTheDocument();
    // Hit count formatted with locale separators
    expect(withinRow.getByText('1,280')).toBeInTheDocument();
    // Confidence
    expect(withinRow.getByText('78')).toBeInTheDocument();
    // Reason copy from the projection
    expect(
      withinRow.getByText(/High FP rate \(42%\) over the last 7 days/i),
    ).toBeInTheDocument();
  });

  it("applies the suggestion's primary action and revalidates", async () => {
    // suggestion=disable should map to action=disable.
    const entry = makeEntry({
      rule_id: 'rule-disable',
      suggestion: 'disable',
      fp_rate: 0.81,
      reasons: ['Fires almost exclusively on false positives'],
    });
    swrState.data = makeResponse([entry]);
    tuningApplyMock.mockResolvedValue(entry);

    const user = userEvent.setup();
    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    const applyBtn = within(row).getByRole('button', { name: /Disable rule/i });
    await clickAndSettle(user, applyBtn);

    expect(tuningApplyMock).toHaveBeenCalledTimes(1);
    expect(tuningApplyMock).toHaveBeenCalledWith(entry.rule_id, { action: 'disable' });
    expect(toastSuccess).toHaveBeenCalledWith(
      expect.stringMatching(/Applied "Disable rule"/),
    );
    expect(mutateMock).toHaveBeenCalled();
  });

  it("acknowledges a healthy rule via the secondary 'acknowledge' action", async () => {
    const entry = makeEntry({
      rule_id: 'rule-ok',
      suggestion: 'healthy',
      fp_rate: 0.01,
      reasons: ['Healthy — no tuning required'],
    });
    swrState.data = makeResponse([entry]);
    tuningApplyMock.mockResolvedValue(entry);

    const user = userEvent.setup();
    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    // Healthy rows render an "Acknowledge" button (no primary action).
    const ackBtn = within(row).getByRole('button', { name: /^Acknowledge$/i });
    await clickAndSettle(user, ackBtn);

    expect(tuningApplyMock).toHaveBeenCalledWith(entry.rule_id, { action: 'acknowledge' });
    expect(toastSuccess).toHaveBeenCalledWith(
      expect.stringMatching(/^Acknowledged "/),
    );
  });

  it('surfaces apply failures as a toast.error', async () => {
    const entry = makeEntry({ rule_id: 'rule-fail', suggestion: 'raise_threshold' });
    swrState.data = makeResponse([entry]);
    tuningApplyMock.mockRejectedValue(new Error('Rule already at max threshold'));

    const user = userEvent.setup();
    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    await clickAndSettle(user, within(row).getByRole('button', { name: /Raise threshold/i }));

    expect(toastError).toHaveBeenCalledWith('Rule already at max threshold');
    expect(toastSuccess).not.toHaveBeenCalled();
  });

  it('dismisses a rule from the workbench', async () => {
    const entry = makeEntry({ rule_id: 'rule-d' });
    swrState.data = makeResponse([entry]);
    tuningDismissMock.mockResolvedValue({ ...entry, dismissed_at: NOW_ISO });

    const user = userEvent.setup();
    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    await clickAndSettle(user, within(row).getByRole('button', { name: /^Dismiss$/i }));

    expect(tuningDismissMock).toHaveBeenCalledWith(entry.rule_id, {});
    expect(toastSuccess).toHaveBeenCalledWith(
      expect.stringMatching(/Dismissed ".*" from the workbench/),
    );
    expect(mutateMock).toHaveBeenCalled();
  });

  it('disables the Dismiss button on rows that are already dismissed', () => {
    const entry = makeEntry({
      rule_id: 'rule-already',
      dismissed_at: '2026-05-10T08:00:00Z',
    });
    swrState.data = makeResponse([entry], {
      filters: {
        severity: null,
        suggestion: null,
        search: null,
        enabled_only: true,
        include_dismissed: true,
        page: 1,
        page_size: 25,
      },
    });

    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    const dismissBtn = within(row).getByRole('button', { name: /^Dismiss$/i });
    expect(dismissBtn).toBeDisabled();
    // And the "Dismissed" badge is shown.
    expect(within(row).getByText('Dismissed')).toBeInTheDocument();
  });

  it('toggles the auto-tune flag via the per-row switch', async () => {
    const entry = makeEntry({ rule_id: 'rule-auto', auto_tune: false });
    swrState.data = makeResponse([entry]);
    tuningAutoTuneMock.mockResolvedValue({ ...entry, auto_tune: true });

    const user = userEvent.setup();
    render(<RuleTuningView />);

    const row = screen.getByTestId(`tuning-row-${entry.rule_id}`);
    const toggle = within(row).getByRole('button', {
      name: /Toggle auto-tune for /i,
    });
    expect(toggle).toHaveAttribute('aria-pressed', 'false');

    await clickAndSettle(user, toggle);

    expect(tuningAutoTuneMock).toHaveBeenCalledWith(entry.rule_id, true);
    expect(toastSuccess).toHaveBeenCalledWith(
      expect.stringMatching(/Auto-tune enabled on "/),
    );
  });

  it('changing the suggestion filter resets pagination to page 1', async () => {
    const entry = makeEntry();
    swrState.data = makeResponse([entry]);

    const user = userEvent.setup();
    render(<RuleTuningView />);

    // The fetcher we mock is `tuningApi.list(listParams)`; the component
    // calls it inside the SWR fetcher. Our SWR mock skips the fetcher
    // invocation, but the test for "filter change resets page" is best
    // expressed via the visible select element: changing it should not
    // throw and the corresponding `page: 1` is the default in the memo.
    const select = screen.getByLabelText(/Filter by suggestion/i);
    await act(async () => {
      await user.selectOptions(select, 'disable');
    });
    expect((select as HTMLSelectElement).value).toBe('disable');
  });

  it('renders the error state when SWR returns an error', () => {
    swrState.error = new Error('Network failure');
    swrState.data = undefined;

    render(<RuleTuningView />);

    expect(screen.getByText(/Couldn't load tuning suggestions/i)).toBeInTheDocument();
  });

  it('shows the "filters dirty" empty state with a Clear filters CTA', async () => {
    swrState.data = makeResponse([]);

    const user = userEvent.setup();
    render(<RuleTuningView />);

    // Trip a filter so the view considers itself "dirty".
    const select = screen.getByLabelText(/Filter by suggestion/i);
    await act(async () => {
      await user.selectOptions(select, 'disable');
    });

    expect(screen.getByText(/No rules match your filters/i)).toBeInTheDocument();
    const clearBtn = screen.getByRole('button', { name: /Clear filters/i });
    expect(clearBtn).toBeInTheDocument();

    await act(async () => {
      await user.click(clearBtn);
    });
    // After clearing, the select returns to its default value.
    expect((select as HTMLSelectElement).value).toBe('all');
  });
});