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.

15.3 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor } from '@solidjs/testing-library';
import { createSignal } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { NotificationsAPI, type NotificationHealth } from '@/api/notifications';
import { notificationStore } from '@/stores/notifications';
import { DEFAULT_LOCALE, setActiveLocale } from '@/i18n';
import { DestinationsTab } from '../tabs/DestinationsTab';
import type { UIAppriseConfig, UIEmailConfig } from '../types';

vi.mock('@/api/notifications', () => ({
  NotificationsAPI: {
    getHealth: vi.fn(),
    getDeliveryLog: vi.fn(),
    retryTerminalFailures: vi.fn(),
    dismissTerminalFailures: vi.fn(),
  },
}));
vi.mock('@/api/alerts', () => ({ AlertsAPI: { getEvents: vi.fn().mockResolvedValue([]) } }));
vi.mock('@/stores/notifications', () => ({
  notificationStore: { success: vi.fn(), error: vi.fn(), warning: vi.fn() },
}));
vi.mock('@/utils/logger', () => ({ logger: { error: vi.fn() } }));
vi.mock('@/stores/license', () => ({ hasFeature: () => false }));
vi.mock('@/stores/licenseCommercial', () => ({ getUpgradeActionDestination: () => null }));
vi.mock('@/stores/sessionPresentationPolicy', () => ({
  presentationPolicyHidesUpgradePrompts: () => true,
}));
vi.mock('@/stores/alertsActivation', () => ({
  useAlertsActivation: () => ({ config: () => null }),
}));
// Keep the actual tab, delivery hooks, health/log/feedback cards and SMTP editor.
// Unrelated destination transports and their independent network calls are excluded.
vi.mock('../AlertAppriseDestinationsSection', () => ({
  AlertAppriseDestinationsSection: () => null,
}));
vi.mock('../AlertWebhookDestinationsSection', () => ({
  AlertWebhookDestinationsSection: () => null,
}));
vi.mock('../AlertDeadManDestinationSection', () => ({
  AlertDeadManDestinationSection: () => null,
}));
vi.mock('../AlertPushDestinationsSection', () => ({ AlertPushDestinationsSection: () => null }));

const buildEmailConfig = (): UIEmailConfig => ({
  enabled: true,
  from: '[email protected]',
  password: '',
  port: 587,
  provider: 'smtp',
  rateLimit: 60,
  server: 'smtp.example.com',
  startTLS: true,
  tls: true,
  to: ['[email protected]'],
  username: '[email protected]',
});

const buildAppriseConfig = (): UIAppriseConfig => ({
  apiKey: '',
  apiKeyHeader: 'X-API-KEY',
  cliPath: '/usr/local/bin/apprise',
  configKey: '',
  enabled: true,
  hasApiKey: false,
  mode: 'cli',
  serverUrl: '',
  skipTlsVerify: false,
  targetsText: 'mailto://[email protected]',
  timeoutSeconds: 20,
});

function degradedHealth(): NotificationHealth {
  return {
    overallHealthy: false,
    queue: {
      pending: 0,
      sending: 0,
      sent: 12,
      failed: 0,
      deadLetter: 85,
      healthy: false,
      status: 'degraded',
      attentionRequired: 85,
      reasonCodes: ['dead_letter_retained'],
      completedRetentionDays: 7,
      deadLetterRetentionDays: 30,
      countsAreRetentionBounded: true,
      retryAttemptsAffectHealth: false,
      terminalFailuresAffectHealth: true,
      failureClasses7d: {
        authentication: 0,
        rate_limited: 0,
        connectivity: 1,
        tls: 0,
        configuration: 0,
        rejected: 0,
        server_error: 0,
        unknown: 0,
      },
      failureClassesAvailable: true,
      failureClassWindowDays: 7,
    },
  };
}

describe('DestinationsTab recovery while editing SMTP', () => {
  it('withdraws saved destination details and all editors during a failed read, then recovers explicitly', async () => {
    const [emailConfig, setEmailConfig] = createSignal(buildEmailConfig());
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const [error, setError] = createSignal<string | null>(null);
    const [loading, setLoading] = createSignal(false);
    const retry = vi.fn();
    render(() => (
      <DestinationsTab
        emailConfig={emailConfig}
        setEmailConfig={setEmailConfig}
        appriseConfig={appriseConfig}
        setAppriseConfig={setAppriseConfig}
        setHasUnsavedChanges={vi.fn()}
        configLoadError={error}
        isRetrying={() => false}
        isLoadingDestinations={loading}
        onRetryLoad={retry}
        webhooks={() => []}
        deadManPingUrl={() => ''}
        setDeadManPingUrl={vi.fn()}
        pushMinimumSeverity={() => 'all'}
        setPushMinimumSeverity={vi.fn()}
      />
    ));
    await screen.findByRole('textbox', { name: 'SMTP server' });
    setLoading(true);
    expect(screen.queryByRole('textbox', { name: 'SMTP server' })).not.toBeInTheDocument();
    setError('Unable to load notification settings.');
    setLoading(false);
    expect(screen.queryByRole('textbox', { name: 'SMTP server' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Test/ })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: /^Retry$/ }));
    expect(retry).toHaveBeenCalledOnce();
    expect(screen.getByText(/Destination details and controls stay hidden/)).toBeInTheDocument();
    setEmailConfig({ ...emailConfig(), server: 'smtp.current.example.test' });
    setError(null);
    expect(await screen.findByRole('textbox', { name: 'SMTP server' })).toHaveValue(
      'smtp.current.example.test',
    );
  });

  beforeEach(() => {
    vi.resetAllMocks();
    setActiveLocale(DEFAULT_LOCALE);
    vi.spyOn(window, 'confirm').mockReturnValue(true);
    vi.mocked(NotificationsAPI.getHealth).mockResolvedValue(degradedHealth());
    vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({
      entries: [
        {
          notificationId: 'retained-attempt',
          type: 'email',
          destinationId: 'email:fixture',
          outcome: 'failed',
          success: false,
          alertIds: ['retained-alert'],
          alertCount: 1,
          attempts: 3,
          timestamp: '2026-09-07T12:00:00Z',
          errorMessage: 'SMTP fixture rejected',
        },
      ],
      windowDays: 7,
      completedRetentionDays: 7,
      deadLetterRetentionDays: 30,
    });
  });
  afterEach(() => {
    cleanup();
    vi.restoreAllMocks();
  });

  it('collapses switched-off email to one line and reveals settings without enabling it', async () => {
    const [emailConfig, setEmailConfig] = createSignal({ ...buildEmailConfig(), enabled: false });
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const dirty = vi.fn();
    render(() => (
      <DestinationsTab
        emailConfig={emailConfig}
        setEmailConfig={setEmailConfig}
        appriseConfig={appriseConfig}
        setAppriseConfig={setAppriseConfig}
        setHasUnsavedChanges={dirty}
        configLoadError={() => null}
        isRetrying={() => false}
        isLoadingDestinations={() => false}
        onRetryLoad={vi.fn()}
        webhooks={() => []}
        deadManPingUrl={() => ''}
        setDeadManPingUrl={vi.fn()}
        pushMinimumSeverity={() => 'all'}
        setPushMinimumSeverity={vi.fn()}
      />
    ));

    await screen.findByText('Off. Turn it on to send alerts by email through your SMTP server.');
    expect(screen.queryByRole('textbox', { name: 'SMTP server' })).toBeNull();

    const reveal = screen.getByRole('button', { name: 'Show settings' });
    reveal.focus();
    fireEvent.click(reveal);

    expect(screen.getByRole('textbox', { name: 'SMTP server' })).toBeInTheDocument();
    // The button unmounts, so focus lands on the first revealed control.
    expect(document.activeElement).not.toBe(document.body);
    expect(document.activeElement?.closest('[data-settings-panel]')).not.toBeNull();
    expect(emailConfig().enabled).toBe(false);
    expect(dirty).not.toHaveBeenCalled();
  });

  it.each([
    { health: 'healthy', logFirst: false },
    { health: 'degraded', logFirst: true },
  ] as const)(
    'places delivery activity $health: before destinations only when delivery is degraded',
    async ({ health, logFirst }) => {
      if (health === 'healthy') {
        const healthy = degradedHealth();
        healthy.overallHealthy = true;
        healthy.queue.status = 'healthy';
        healthy.queue.healthy = true;
        vi.mocked(NotificationsAPI.getHealth).mockResolvedValue(healthy);
      }
      const [emailConfig, setEmailConfig] = createSignal(buildEmailConfig());
      const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
      render(() => (
        <DestinationsTab
          emailConfig={emailConfig}
          setEmailConfig={setEmailConfig}
          appriseConfig={appriseConfig}
          setAppriseConfig={setAppriseConfig}
          setHasUnsavedChanges={vi.fn()}
          configLoadError={() => null}
          isRetrying={() => false}
          isLoadingDestinations={() => false}
          onRetryLoad={vi.fn()}
          webhooks={() => []}
          deadManPingUrl={() => ''}
          setDeadManPingUrl={vi.fn()}
          pushMinimumSeverity={() => 'all'}
          setPushMinimumSeverity={vi.fn()}
        />
      ));

      const smtp = await screen.findByRole('textbox', { name: 'SMTP server' });
      if (logFirst) await screen.findByRole('button', { name: 'Retry retained deliveries' });
      const activity = await screen.findByText('SMTP fixture rejected');
      const activityFollowsSmtp = Boolean(
        smtp.compareDocumentPosition(activity) & Node.DOCUMENT_POSITION_FOLLOWING,
      );
      expect(activityFollowsSmtp).toBe(!logFirst);
      expect(screen.getAllByText('SMTP fixture rejected')).toHaveLength(1);
    },
  );

  it.each(
    (
      [
        ['retryTerminalFailures', 'Retry retained deliveries', 'retry'],
        ['dismissTerminalFailures', 'Dismiss retained failures', 'dismiss'],
      ] as const
    ).flatMap(([action, label, verb]) =>
      (['available', 'unavailable'] as const).map((refresh) => ({ action, label, verb, refresh })),
    ),
  )(
    'preserves unfinished edits through $action with $refresh refresh',
    async ({ action, label, verb, refresh }) => {
      const [emailConfig, setEmailConfig] = createSignal(buildEmailConfig());
      const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
      const dirty = vi.fn();
      render(() => (
        <DestinationsTab
          emailConfig={emailConfig}
          setEmailConfig={setEmailConfig}
          appriseConfig={appriseConfig}
          setAppriseConfig={setAppriseConfig}
          setHasUnsavedChanges={dirty}
          configLoadError={() => null}
          isRetrying={() => false}
          isLoadingDestinations={() => false}
          onRetryLoad={vi.fn()}
          webhooks={() => []}
          deadManPingUrl={() => ''}
          setDeadManPingUrl={vi.fn()}
          pushMinimumSeverity={() => 'all'}
          setPushMinimumSeverity={vi.fn()}
        />
      ));
      await screen.findByRole('button', { name: label });
      await screen.findByText('SMTP fixture rejected');
      const editor = screen.getByRole('textbox', { name: 'SMTP server' });
      fireEvent.input(editor, { target: { value: 'unfinished.smtp.example' } });
      expect(dirty).toHaveBeenCalledWith(true);
      dirty.mockClear();
      const status = screen.getByRole('status');

      let reject!: (error: Error) => void;
      vi.mocked(NotificationsAPI[action]).mockReturnValueOnce(
        new Promise((_, fail) => {
          reject = fail;
        }),
      );
      fireEvent.click(screen.getByRole('button', { name: label }));
      editor.focus();
      expect(screen.getByRole('button', { name: label })).toBeDisabled();
      reject(new Error('fixture rejection'));
      await waitFor(() => expect(status).toHaveTextContent('Unable to ' + verb));
      expect(editor).toHaveFocus();
      expect(editor).toHaveValue('unfinished.smtp.example');
      expect(emailConfig().server).toBe('unfinished.smtp.example');
      expect(dirty).not.toHaveBeenCalledWith(false);
      expect(screen.getByText('SMTP fixture rejected')).toBeInTheDocument();
      expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(1);

      // Cancelling after a failure must not erase that evidence or start work.
      vi.mocked(window.confirm).mockReturnValueOnce(false);
      fireEvent.click(screen.getByRole('button', { name: label }));
      expect(NotificationsAPI[action]).toHaveBeenCalledTimes(1);
      expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(1);
      expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(1);
      expect(screen.getByRole('button', { name: label })).toBeEnabled();
      expect(screen.getByRole('status')).toBe(status);
      expect(status).toHaveTextContent('Unable to ' + verb);
      expect(editor).toHaveFocus();
      expect(editor).toHaveValue('unfinished.smtp.example');
      expect(dirty).not.toHaveBeenCalledWith(false);
      expect(screen.getByText('SMTP fixture rejected')).toBeInTheDocument();

      let accept!: (result: { success: boolean; affected: number }) => void;
      vi.mocked(NotificationsAPI[action]).mockReturnValueOnce(
        new Promise((resolve) => {
          accept = resolve;
        }),
      );
      const healthy = degradedHealth();
      healthy.queue = {
        ...healthy.queue,
        status: 'healthy',
        healthy: true,
        attentionRequired: 0,
        failed: 0,
        deadLetter: 0,
      };
      if (refresh === 'unavailable') {
        vi.mocked(NotificationsAPI.getHealth).mockRejectedValueOnce(
          new Error('health unavailable'),
        );
        vi.mocked(NotificationsAPI.getDeliveryLog).mockRejectedValueOnce(
          new Error('history unavailable'),
        );
      } else {
        vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce(healthy);
      }
      vi.mocked(notificationStore.error).mockClear();
      fireEvent.click(screen.getByRole('button', { name: label }));
      editor.focus();
      accept({ success: true, affected: 85 });
      if (refresh === 'available') {
        await waitFor(() => expect(screen.queryByRole('button', { name: label })).toBeNull());
      } else {
        await screen.findByText('Notification delivery status is unavailable');
        await waitFor(() => expect(screen.getByRole('button', { name: label })).toBeEnabled());
      }
      await waitFor(() => expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(2));
      expect(screen.getByRole('textbox', { name: 'SMTP server' })).toBe(editor);
      expect(editor).toHaveFocus();
      expect(editor).toHaveValue('unfinished.smtp.example');
      expect(emailConfig().server).toBe('unfinished.smtp.example');
      expect(dirty).not.toHaveBeenCalledWith(false);
      expect(screen.getByRole('status')).toBe(status);
      expect(status).toBeEmptyDOMElement();
      if (refresh === 'unavailable') {
        expect(
          await screen.findByText('Notification delivery status is unavailable'),
        ).toBeInTheDocument();
        expect(
          await screen.findByText(
            'Pulse could not read the delivery log. Refresh to check delivery attempts.',
          ),
        ).toBeInTheDocument();
        expect(screen.queryByText('SMTP fixture rejected')).not.toBeInTheDocument();
      } else {
        expect(screen.getByText('SMTP fixture rejected')).toBeInTheDocument();
      }
      // A failed read must not relabel an accepted mutation as rejected.
      expect(notificationStore.success).toHaveBeenCalledTimes(1);
      expect(notificationStore.error).not.toHaveBeenCalled();
      expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(2);
      expect(NotificationsAPI[action]).toHaveBeenCalledTimes(2);
    },
  );
});