Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

21.9 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor } from '@solidjs/testing-library';
import { createSignal, Suspense } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { InvestigationRecord } from '@/api/ai';
import type { Investigation } from '@/api/patrol';
import { resetCreateNonSuspendingQueryCacheForTest } from '@/hooks/createNonSuspendingQuery';
import {
  getInvestigationOutcomeBadgeTone,
  getInvestigationOutcomeLabel,
  getInvestigationStatusBadgeTone,
} from '@/utils/aiFindingPresentation';
import InvestigationSection from '../InvestigationSection';

const getInvestigationMock = vi.hoisted(() => vi.fn<() => Promise<Investigation>>());
const reinvestigateFindingMock = vi.hoisted(() => vi.fn());
const loadFindingsMock = vi.hoisted(() => vi.fn());

vi.mock('@/api/patrol', () => ({
  getInvestigation: getInvestigationMock,
  reinvestigateFinding: reinvestigateFindingMock,
  formatTimestamp: (value: string) => value,
}));

vi.mock('@/stores/notifications', () => ({
  notificationStore: {
    success: vi.fn(),
    error: vi.fn(),
  },
}));

vi.mock('@/stores/aiIntelligence', () => ({
  aiIntelligenceStore: {
    loadFindings: loadFindingsMock,
  },
}));

vi.mock('../InvestigationMessages', () => ({
  InvestigationMessages: () => <div>Mock investigation messages</div>,
}));

describe('InvestigationSection', () => {
  beforeEach(() => {
    resetCreateNonSuspendingQueryCacheForTest();
    getInvestigationMock.mockReset();
    reinvestigateFindingMock.mockReset();
    loadFindingsMock.mockReset();
  });

  afterEach(() => {
    cleanup();
  });

  it('does not offer another run before the first read says what the run is doing', async () => {
    let finishRead: (value: Investigation) => void = () => {};
    getInvestigationMock.mockReturnValueOnce(
      new Promise<Investigation>((resolve) => {
        finishRead = resolve;
      }),
    );

    render(() => <InvestigationSection findingId="finding-pending" investigationStatus="failed" />);

    expect(await screen.findByText('Loading investigation...')).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();

    finishRead({
      id: 'inv-pending',
      finding_id: 'finding-pending',
      session_id: 'session-pending',
      status: 'failed',
      outcome: 'fix_queued',
      started_at: '2026-10-23T10:00:00.000Z',
      turn_count: 6,
    });
    expect(await screen.findByText(getInvestigationOutcomeLabel('fix_queued'))).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
  });

  it('keeps the last answer when a later read fails', async () => {
    getInvestigationMock.mockResolvedValueOnce({
      id: 'inv-kept',
      finding_id: 'finding-kept',
      session_id: 'session-kept',
      status: 'completed',
      outcome: 'unclassified',
      started_at: '2026-10-23T10:00:00.000Z',
      turn_count: 3,
      summary: 'The service restarted twice.',
    } satisfies Investigation);
    getInvestigationMock.mockRejectedValueOnce(new Error('Pulse is restarting'));
    reinvestigateFindingMock.mockResolvedValue({ success: true });

    render(() => <InvestigationSection findingId="finding-kept" investigationStatus="completed" />);
    fireEvent.click(await screen.findByRole('button', { name: 'Re-investigate' }));
    await waitFor(() => expect(getInvestigationMock).toHaveBeenCalledTimes(2));
    // Let the failed read settle.
    await new Promise((resolve) => setTimeout(resolve, 0));

    expect(screen.getByText('The service restarted twice.')).toBeInTheDocument();
    expect(screen.getByRole('alert', { name: 'Investigation read failed' })).toHaveTextContent(
      'Showing previously loaded investigation evidence. It may be out of date.',
    );
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
    expect(screen.queryByText(/No investigation yet/)).not.toBeInTheDocument();
  });

  it('retries an unavailable cold read without offering or starting another investigation', async () => {
    getInvestigationMock.mockRejectedValueOnce(
      Object.assign(new Error('private path'), { status: 503 }),
    );
    let finishRead: (value: Investigation) => void = () => {};
    getInvestigationMock.mockReturnValueOnce(
      new Promise<Investigation>((resolve) => {
        finishRead = resolve;
      }),
    );
    render(() => <InvestigationSection findingId="unavailable" />);

    const alert = await screen.findByRole('alert', { name: 'Investigation read failed' });
    expect(alert).toHaveTextContent('This does not mean no investigation exists.');
    expect(alert).not.toHaveTextContent('private path');
    expect(screen.queryByText(/No investigation yet/)).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Retry history' }));
    expect(screen.getByRole('button', { name: 'Retrying history...' })).toBeDisabled();
    expect(alert).toBeInTheDocument();
    finishRead({
      id: 'recovered',
      finding_id: 'unavailable',
      session_id: 'session-recovered',
      status: 'failed',
      outcome: 'fix_queued',
      started_at: '2026-10-29T08:00:00Z',
      turn_count: 1,
      summary: 'A fix is already waiting.',
    });
    expect(await screen.findByText('A fix is already waiting.')).toBeInTheDocument();
    expect(screen.queryByRole('alert')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
    expect(reinvestigateFindingMock).not.toHaveBeenCalled();
  });

  it('keeps a genuine 404 distinct from an unread investigation', async () => {
    getInvestigationMock.mockRejectedValueOnce(
      Object.assign(new Error('not found'), { status: 404 }),
    );
    render(() => <InvestigationSection findingId="absent" />);
    expect(await screen.findByText(/No investigation yet/)).toBeInTheDocument();
    expect(screen.queryByRole('alert')).not.toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Re-investigate' })).toBeEnabled();
  });

  it('never shows one finding as the history of a different unavailable finding', async () => {
    getInvestigationMock.mockResolvedValueOnce({
      id: 'first',
      finding_id: 'first',
      session_id: 'first',
      status: 'completed',
      started_at: '2026-10-29T08:00:00Z',
      turn_count: 1,
      summary: 'Evidence for the first finding.',
    });
    getInvestigationMock.mockRejectedValueOnce(
      Object.assign(new Error('unavailable'), { status: 503 }),
    );
    const [findingId, setFindingId] = createSignal('first');
    render(() => <InvestigationSection findingId={findingId()} />);
    expect(await screen.findByText('Evidence for the first finding.')).toBeInTheDocument();
    setFindingId('second');
    await screen.findByRole('alert', { name: 'Investigation read failed' });
    expect(screen.queryByText('Evidence for the first finding.')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
  });

  it.each([401, 403])(
    'withdraws retained history after HTTP %s without exposing the error payload',
    async (status) => {
      vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
      try {
        getInvestigationMock.mockResolvedValueOnce({
          id: 'private',
          finding_id: 'private',
          session_id: 'private',
          status: 'completed',
          outcome: 'unclassified',
          started_at: '2026-10-29T08:00:00Z',
          turn_count: 1,
          summary: 'Previously authorised evidence.',
        });
        getInvestigationMock.mockRejectedValue(
          Object.assign(new Error('secret error payload'), { status }),
        );
        render(() => <InvestigationSection findingId="private" />);
        expect(await screen.findByText('Previously authorised evidence.')).toBeInTheDocument();
        vi.advanceTimersByTime(30_000);
        const alert = await screen.findByRole('alert', { name: 'Investigation read failed' });
        expect(alert).toHaveTextContent(status === 401 ? 'Sign in again' : 'Access denied');
        expect(alert).not.toHaveTextContent('secret error payload');
        expect(screen.queryByText('Previously authorised evidence.')).not.toBeInTheDocument();
        expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
      } finally {
        vi.useRealTimers();
      }
    },
  );

  it('reads again on its own after a failed first read', async () => {
    // Findings refreshes no longer remount the review, so it retries itself.
    vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
    try {
      getInvestigationMock.mockRejectedValueOnce(new Error('Pulse is restarting'));
      getInvestigationMock.mockResolvedValueOnce({
        id: 'inv-retry',
        finding_id: 'finding-retry',
        session_id: 'session-retry',
        status: 'completed',
        outcome: 'unclassified',
        started_at: '2026-10-23T10:00:00.000Z',
        turn_count: 3,
        summary: 'Read on the second try.',
      } satisfies Investigation);

      render(() => (
        <InvestigationSection findingId="finding-retry" investigationStatus="completed" />
      ));
      await waitFor(() => expect(getInvestigationMock).toHaveBeenCalledTimes(1));
      expect(screen.queryByText('Read on the second try.')).not.toBeInTheDocument();

      vi.advanceTimersByTime(30_000);
      expect(await screen.findByText('Read on the second try.')).toBeInTheDocument();
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps the investigation on screen, and the route in place, while it is read again', async () => {
    getInvestigationMock.mockResolvedValueOnce({
      id: 'inv-reread',
      finding_id: 'finding-reread',
      session_id: 'session-reread',
      status: 'completed',
      outcome: 'unclassified',
      started_at: '2026-10-23T10:00:00.000Z',
      turn_count: 3,
      summary: 'Memory pressure explains the restarts.',
    } satisfies Investigation);
    let finishRead: (value: Investigation) => void = () => {};
    getInvestigationMock.mockReturnValueOnce(
      new Promise<Investigation>((resolve) => {
        finishRead = resolve;
      }),
    );
    reinvestigateFindingMock.mockResolvedValue({ success: true });

    render(() => (
      <Suspense fallback={<div>Loading view...</div>}>
        <InvestigationSection findingId="finding-reread" investigationStatus="completed" />
      </Suspense>
    ));

    expect(screen.queryByText('Loading view...')).not.toBeInTheDocument();
    expect(await screen.findByText('Memory pressure explains the restarts.')).toBeInTheDocument();

    fireEvent.click(screen.getByRole('button', { name: 'Re-investigate' }));
    await waitFor(() => expect(getInvestigationMock).toHaveBeenCalledTimes(2));

    // The second read is still in flight.
    expect(screen.queryByText('Loading view...')).not.toBeInTheDocument();
    expect(screen.queryByText('Loading investigation...')).not.toBeInTheDocument();
    expect(screen.getByText('Memory pressure explains the restarts.')).toBeInTheDocument();

    finishRead({
      id: 'inv-reread-2',
      finding_id: 'finding-reread',
      session_id: 'session-reread-2',
      status: 'running',
      started_at: '2026-10-23T10:05:00.000Z',
      turn_count: 0,
    });
    expect(await screen.findByText('Running')).toBeInTheDocument();
    expect(screen.queryByText('Loading view...')).not.toBeInTheDocument();
  });

  it('renders investigation status badges from the shared finding presentation contract', async () => {
    getInvestigationMock.mockResolvedValue({
      id: 'inv-1',
      finding_id: 'finding-1',
      session_id: 'session-1',
      status: 'running',
      started_at: '2026-03-08T10:00:00.000Z',
      turn_count: 2,
      summary: 'Investigation is active.',
      tools_used: ['ssh'],
    } satisfies Investigation);

    render(() => <InvestigationSection findingId="finding-1" investigationStatus="running" />);

    const statusBadge = await screen.findByText('Running');
    expect(getInvestigationStatusBadgeTone('running')).toBe('info');
    expect(statusBadge.className).toContain('inline-flex');
    expect(statusBadge.className).toContain('border-blue-200');
  });

  it('renders investigation outcome badges from the shared finding presentation contract', async () => {
    getInvestigationMock.mockResolvedValue({
      id: 'inv-2',
      finding_id: 'finding-2',
      session_id: 'session-2',
      status: 'completed',
      outcome: 'needs_attention',
      started_at: '2026-03-08T10:00:00.000Z',
      turn_count: 2,
      summary: 'Manual review needed.',
      tools_used: ['ssh'],
    } satisfies Investigation);

    render(() => <InvestigationSection findingId="finding-2" investigationStatus="completed" />);

    const outcomeBadge = await screen.findByText(getInvestigationOutcomeLabel('needs_attention'));
    expect(getInvestigationOutcomeBadgeTone('needs_attention')).toBe('warning');
    expect(outcomeBadge.className).toContain('inline-flex');
    expect(outcomeBadge.className).toContain('border-amber-200');
  });

  it('offers another run after a run that ended without a clear conclusion', async () => {
    getInvestigationMock.mockResolvedValue({
      id: 'inv-unclassified',
      finding_id: 'finding-unclassified',
      session_id: 'session-unclassified',
      status: 'completed',
      outcome: 'unclassified',
      started_at: '2026-10-22T10:00:00.000Z',
      turn_count: 6,
      summary: 'The service restarted twice; the logs do not say why.',
    } satisfies Investigation);

    render(() => (
      <InvestigationSection findingId="finding-unclassified" investigationStatus="completed" />
    ));

    const outcomeBadge = await screen.findByText('No clear conclusion');
    expect(outcomeBadge.className).toContain('border-amber-200');
    expect(screen.getByText('The service restarted twice; the logs do not say why.')).toBeVisible();
    expect(screen.getByRole('button', { name: 'Re-investigate' })).toBeInTheDocument();
  });

  it('presents a run cut short by a restart as information, not as an error', async () => {
    const note = 'Interrupted: Pulse restarted before this investigation finished.';
    getInvestigationMock.mockResolvedValue({
      id: 'inv-interrupted',
      finding_id: 'finding-interrupted',
      session_id: 'session-interrupted',
      status: 'failed',
      outcome: 'interrupted',
      started_at: '2026-10-22T10:00:00.000Z',
      turn_count: 2,
      error: note,
    } satisfies Investigation);

    render(() => (
      <InvestigationSection
        findingId="finding-interrupted"
        investigationStatus="failed"
        investigationRecord={{
          id: 'record-interrupted',
          finding_id: 'finding-interrupted',
          subject: { resource_id: 'vm-102' },
          trigger: { title: 'Service keeps restarting', detected_at: '2026-10-22T10:00:00Z' },
          status: 'failed',
          outcome: 'interrupted',
          evidence: [],
          verification: [],
          rollback: [],
          tools_used: [],
          started_at: '2026-10-22T10:00:00Z',
          error: note,
        }}
      />
    ));

    // Wait for the session, whose badge heads the section above the record's.
    await screen.findByRole('button', { name: 'Show investigation thread' });
    const badges = screen.getAllByText('Interrupted — will retry');
    expect(badges).toHaveLength(2);
    for (const badge of badges) expect(badge.className).toContain('border-border');
    // The record's failed run status would contradict "will retry".
    expect(screen.queryByText('Failed')).not.toBeInTheDocument();
    // The record and the session carry the same note, so it shows once.
    const noteBox = screen.getByText(note);
    expect(noteBox.className).toContain('text-muted');
    expect(noteBox.className).not.toContain('bg-red-50');
    expect(screen.getByRole('button', { name: 'Re-investigate' })).toBeInTheDocument();
  });

  it('does not offer another run while a planned fix is queued', async () => {
    getInvestigationMock.mockResolvedValue({
      id: 'inv-queued',
      finding_id: 'finding-queued',
      session_id: 'session-queued',
      status: 'failed',
      outcome: 'fix_queued',
      started_at: '2026-10-23T10:00:00.000Z',
      turn_count: 6,
      error: 'The provider stopped responding.',
    } satisfies Investigation);

    render(() => <InvestigationSection findingId="finding-queued" investigationStatus="failed" />);

    // The run failed after planning a fix; the fix is still waiting for approval.
    await screen.findByText(getInvestigationOutcomeLabel('fix_queued'));
    expect(screen.queryByRole('button', { name: 'Re-investigate' })).not.toBeInTheDocument();
    // The run's note is information beside the queued fix, not an alarm.
    const noteBox = screen.getByText('The provider stopped responding.');
    expect(noteBox.className).toContain('text-muted');
    expect(noteBox.className).not.toContain('bg-red-50');
  });

  it('keeps a failed run error in the error style', async () => {
    getInvestigationMock.mockResolvedValue({
      id: 'inv-failed',
      finding_id: 'finding-failed',
      session_id: 'session-failed',
      status: 'failed',
      outcome: 'timed_out',
      started_at: '2026-10-22T10:00:00.000Z',
      turn_count: 12,
      error: 'The provider stopped responding.',
    } satisfies Investigation);

    render(() => <InvestigationSection findingId="finding-failed" investigationStatus="failed" />);

    expect((await screen.findByText('The provider stopped responding.')).className).toContain(
      'bg-red-50',
    );
  });

  it('renders the durable Patrol investigation record through the inline investigation surface', async () => {
    getInvestigationMock.mockResolvedValue(undefined as unknown as Investigation);

    const record: InvestigationRecord = {
      id: 'record-1',
      finding_id: 'finding-1',
      subject: { resource_id: 'vm-100', resource_name: 'web', resource_type: 'vm' },
      trigger: {
        title: 'High CPU usage',
        detected_at: '2026-05-06T12:00:00Z',
      },
      status: 'completed',
      outcome: 'fix_queued',
      confidence: 'high',
      evidence: [{ kind: 'metrics', summary: 'CPU stayed above 95% for 10 minutes' }],
      conclusion: 'Backup job saturated CPU.',
      recommended_action: 'Approve a controlled restart after the backup completes.',
      proposed_fix: {
        id: 'fix-1',
        description: 'Restart the workload service',
        commands: ['systemctl restart workload.service'],
        risk_level: 'medium',
        destructive: false,
        target_host: 'pve-1',
      },
      verification: ['CPU returned below 50%'],
      rollback: [],
      tools_used: ['metrics.history'],
      started_at: '2026-05-06T12:00:00Z',
    };

    render(() => <InvestigationSection findingId="finding-1" investigationRecord={record} />);

    expect(await screen.findByText('Patrol record')).toBeInTheDocument();
    expect(screen.getByText('Fix queued')).toBeInTheDocument();
    expect(screen.queryByText('Completed')).not.toBeInTheDocument();
    expect(screen.getByText('Backup job saturated CPU.')).toBeInTheDocument();
    expect(screen.getByText('CPU stayed above 95% for 10 minutes')).toBeInTheDocument();
    expect(screen.getByText('1 command recorded for approval context')).toBeInTheDocument();
    expect(screen.queryByText(/No investigation data available/)).not.toBeInTheDocument();
    expect(screen.queryByText('systemctl restart workload.service')).not.toBeInTheDocument();
  });

  it('shows the record run status until the run has an outcome', async () => {
    getInvestigationMock.mockResolvedValue(undefined as unknown as Investigation);

    render(() => (
      <InvestigationSection
        findingId="finding-running"
        investigationRecord={{
          id: 'record-running',
          finding_id: 'finding-running',
          subject: { resource_id: 'vm-103' },
          trigger: { title: 'Service keeps restarting', detected_at: '2026-10-22T10:00:00Z' },
          status: 'running',
          started_at: '2026-10-22T10:00:00Z',
          evidence: [],
          verification: [],
          rollback: [],
          tools_used: [],
        }}
      />
    ));

    expect(await screen.findByText('Patrol record')).toBeInTheDocument();
    expect(screen.getByText('Running')).toBeInTheDocument();
  });

  it.each([false, true])(
    'preserves readable evidence and distinct summaries (different: %s)',
    async (different) => {
      const conclusion =
        '### Root cause\n\nThe cause is **unknown**.\n\n- The health check failed.\n- Logs are unavailable.';
      getInvestigationMock.mockResolvedValue({
        id: 'inv-markdown',
        finding_id: 'finding-markdown',
        session_id: 'session-markdown',
        status: 'completed',
        started_at: '2026-09-06T17:00:00Z',
        turn_count: 2,
        summary: different
          ? '### Follow-up\n\nAdditional evidence remains unavailable.'
          : conclusion,
      } satisfies Investigation);
      render(() => (
        <InvestigationSection
          findingId="finding-markdown"
          investigationRecord={{
            id: 'record-markdown',
            finding_id: 'finding-markdown',
            subject: { resource_id: 'container-1' },
            trigger: { title: 'Health check failed', detected_at: '2026-09-06T17:00:00Z' },
            status: 'completed',
            conclusion,
            started_at: '2026-09-06T17:00:00Z',
            evidence: [],
            verification: [],
            rollback: [],
            tools_used: [],
          }}
        />
      ));
      await screen.findByRole('button', { name: 'Show investigation thread' });
      expect(screen.getAllByRole('heading', { name: 'Root cause' })).toHaveLength(1);
      expect(screen.getByText('unknown').tagName).toBe('STRONG');
      expect(screen.getAllByRole('listitem')).toHaveLength(2);
      expect(screen.queryByRole('heading', { name: 'Follow-up' }) !== null).toBe(different);
    },
  );
});