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.

6.2 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen, waitFor } from '@solidjs/testing-library';
import { useSettingsNavigation } from '../useSettingsNavigation';

const presentationPolicyIsReadOnlyMock = vi.hoisted(() => vi.fn(() => false));
const sessionPresentationPolicyResolvedMock = vi.hoisted(() => vi.fn(() => true));
const navigateSpy = vi.hoisted(() => vi.fn());

vi.mock('@/stores/sessionPresentationPolicy', () => ({
  presentationPolicyIsReadOnly: () => presentationPolicyIsReadOnlyMock(),
  sessionPresentationPolicyResolved: () => sessionPresentationPolicyResolvedMock(),
}));

function renderHarness(pathname = '/settings', search = '', hash = '') {
  return render(() => {
    const navigation = useSettingsNavigation({
      navigate: navigateSpy,
      location: {
        pathname,
        search,
        hash,
      },
    });

    return (
      <>
        <button type="button" onClick={() => navigation.setActiveTab('infrastructure-systems')}>
          open infrastructure settings
        </button>
        <div data-testid="selected-agent">{navigation.selectedAgent()}</div>
      </>
    );
  });
}

describe('useSettingsNavigation', () => {
  afterEach(() => {
    cleanup();
    navigateSpy.mockReset();
    presentationPolicyIsReadOnlyMock.mockReset();
    sessionPresentationPolicyResolvedMock.mockReset();
    presentationPolicyIsReadOnlyMock.mockReturnValue(false);
    sessionPresentationPolicyResolvedMock.mockReturnValue(true);
  });

  it('lands /settings on reporting inventory when the session is read-only', async () => {
    presentationPolicyIsReadOnlyMock.mockReturnValue(true);
    renderHarness('/settings');

    await waitFor(() => {
      expect(navigateSpy).toHaveBeenCalledWith('/settings/infrastructure', {
        replace: true,
        scroll: false,
      });
    });
  });

  it('preserves Assistant OAuth callback queries while routing /settings to Provider & Models', async () => {
    renderHarness('/settings', '?ai_oauth_error=unsupported');

    await waitFor(() => {
      expect(navigateSpy).toHaveBeenCalledWith(
        '/settings/pulse-intelligence/provider?ai_oauth_error=unsupported',
        {
          replace: true,
          scroll: false,
        },
      );
    });
  });

  it('routes legacy external-agent API hashes to Pulse Intelligence External agents', async () => {
    for (const hash of ['#external-agent-setup', '#pulse-mcp-setup']) {
      cleanup();
      navigateSpy.mockReset();
      renderHarness('/settings/security/api', '', hash);

      await waitFor(() => {
        expect(navigateSpy).toHaveBeenCalledWith(
          '/settings/pulse-intelligence/agents#external-agent-setup',
          {
            replace: true,
            scroll: false,
          },
        );
      });
    }
  });

  it('keeps the setup anchor when retired Assistant settings links land on External agents', async () => {
    cleanup();
    navigateSpy.mockReset();
    renderHarness('/settings/pulse-intelligence/assistant', '', '#external-agent-setup');

    await waitFor(() => {
      expect(navigateSpy).toHaveBeenCalledWith(
        '/settings/pulse-intelligence/agents#external-agent-setup',
        {
          replace: true,
          scroll: false,
        },
      );
    });
  });

  it('keeps the setup anchor for every retired Assistant settings link form', async () => {
    const forms: Array<[string, string]> = [
      ['/settings/pulse-intelligence/assistant/', ''],
      ['/settings', '?tab=assistant'],
      ['/settings', '?tab=system-ai-assistant'],
    ];
    for (const [pathname, search] of forms) {
      cleanup();
      navigateSpy.mockReset();
      renderHarness(pathname, search, '#external-agent-setup');
      await waitFor(() => {
        expect(navigateSpy).toHaveBeenCalledWith(
          '/settings/pulse-intelligence/agents#external-agent-setup',
          {
            replace: true,
            scroll: false,
          },
        );
      });
    }
  });

  it('keeps token creation preset links on API Access', async () => {
    renderHarness(
      '/settings/security/api',
      '?tokenPreset=pulse-intelligence-agent',
      '#api-token-create',
    );

    await waitFor(() => {
      expect(navigateSpy).not.toHaveBeenCalled();
    });
  });

  it('routes setup-oriented infrastructure tab clicks back to systems when the session is read-only', () => {
    presentationPolicyIsReadOnlyMock.mockReturnValue(true);
    renderHarness('/settings/system-general');

    fireEvent.click(screen.getByRole('button', { name: 'open infrastructure settings' }));

    expect(navigateSpy).toHaveBeenCalledWith('/settings/infrastructure', {
      scroll: false,
    });
  });

  it('does not strip infrastructure onboarding queries before presentation policy resolves', async () => {
    presentationPolicyIsReadOnlyMock.mockReturnValue(true);
    sessionPresentationPolicyResolvedMock.mockReturnValue(false);

    renderHarness('/settings/infrastructure', '?add=pick');

    await waitFor(() => {
      expect(navigateSpy).not.toHaveBeenCalledWith('/settings/infrastructure', {
        replace: true,
        scroll: false,
      });
    });
  });

  it('does not derive selected agents from retired infrastructure deep links', async () => {
    renderHarness('/settings/infrastructure/platforms/proxmox/pbs');

    await waitFor(() => {
      expect(screen.getByTestId('selected-agent')).toHaveTextContent('pve');
    });
    expect(navigateSpy).not.toHaveBeenCalled();
  });

  it('does not translate retired infrastructure setup paths', async () => {
    renderHarness('/settings/infrastructure/install');

    await waitFor(() => {
      expect(navigateSpy).not.toHaveBeenCalled();
    });
  });

  it('does not translate retired workloads settings paths', async () => {
    renderHarness('/settings/workloads/docker');

    await waitFor(() => {
      expect(navigateSpy).not.toHaveBeenCalled();
    });
  });

  it('does not translate remaining retired settings aliases', async () => {
    for (const path of [
      '/settings/operations/reporting',
      '/settings/integrations/api',
      '/settings/system-pro',
    ]) {
      cleanup();
      navigateSpy.mockReset();
      renderHarness(path);

      await waitFor(() => {
        expect(navigateSpy).not.toHaveBeenCalled();
      });
    }
  });
});