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.

22.9 KB
Raw Blame
import { renderHook, waitFor } from '@solidjs/testing-library';
import { createSignal } from 'solid-js';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { AlertsAPI } from '@/api/alerts';
import { NotificationsAPI, type NotificationTestResult } from '@/api/notifications';
import { notificationStore } from '@/stores/notifications';
import { showErrorWithDetail } from '@/utils/toast';

import { useAlertDestinationsTabState } from '../useAlertDestinationsTabState';
import type { UIAppriseConfig, UIEmailConfig } from '../types';

vi.mock('@/api/notifications', () => ({
  NotificationsAPI: {
    createWebhook: vi.fn(),
    deleteWebhook: vi.fn(),
    getDeliveryLog: vi.fn(),
    getHealth: vi.fn(),
    getWebhooks: vi.fn(),
    dismissTerminalFailures: vi.fn(),
    retryTerminalFailures: vi.fn(),
    testNotification: vi.fn(),
    testWebhook: vi.fn(),
    updateWebhook: vi.fn(),
  },
}));

vi.mock('@/api/alerts', () => ({
  AlertsAPI: {
    getEvents: vi.fn(),
  },
}));

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

vi.mock('@/utils/logger', () => ({
  logger: {
    error: vi.fn(),
  },
}));

vi.mock('@/utils/toast', () => ({
  showErrorWithDetail: vi.fn(),
}));

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,
});

describe('useAlertDestinationsTabState', () => {
  it.each(['email', 'apprise'] as const)(
    'does not announce a late %s Test after the editor is withdrawn',
    async (kind) => {
      vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
        queue: { status: 'healthy' },
      } as never);
      vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({ entries: [] } as never);
      vi.mocked(AlertsAPI.getEvents).mockResolvedValue([]);
      const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
      let accept!: (value: NotificationTestResult) => void;
      vi.mocked(NotificationsAPI.testNotification).mockReturnValueOnce(
        new Promise<NotificationTestResult>((resolve) => {
          accept = resolve;
        }),
      );
      const { result, cleanup } = renderHook(() =>
        useAlertDestinationsTabState({
          appriseConfig,
          setAppriseConfig,
          emailConfig: () => buildEmailConfig(),
          configLoadError: () => null,
          isRetrying: () => false,
          isLoadingDestinations: () => false,
          onRetryLoad: vi.fn(),
          webhooks: () => [],
        }),
      );
      const pending = kind === 'email' ? result.testEmailConfig() : result.testApprise();
      expect(NotificationsAPI.testNotification).toHaveBeenCalledOnce();
      cleanup();
      vi.mocked(notificationStore.success).mockClear();
      vi.mocked(notificationStore.warning).mockClear();
      vi.mocked(showErrorWithDetail).mockClear();
      accept({ status: 'success', message: 'Test notification sent', deliveryPaused: false });
      await pending;
      expect(notificationStore.success).not.toHaveBeenCalled();
      expect(notificationStore.warning).not.toHaveBeenCalled();
      expect(showErrorWithDetail).not.toHaveBeenCalled();
      await result.testEmailConfig();
      await result.testApprise();
      expect(NotificationsAPI.testNotification).toHaveBeenCalledOnce();
    },
  );

  describe.each(['retryTerminalFailures', 'dismissTerminalFailures'] as const)(
    '%s view replacement',
    (action) => {
      it.each(['accepted', 'rejected'] as const)(
        'does not attribute the retired %s result or refresh to the new Notifications tab',
        async (outcome) => {
          const confirm = vi.spyOn(window, 'confirm').mockReturnValue(true);
          vi.mocked(NotificationsAPI.getWebhooks).mockResolvedValue([]);
          vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({ entries: [] } as never);
          vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
            queue: { status: 'degraded', attentionRequired: 2 },
          } as never);
          const mountTab = () => {
            const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
            return renderHook(() =>
              useAlertDestinationsTabState({
                appriseConfig,
                setAppriseConfig,
                configLoadError: () => null,
                emailConfig: () => buildEmailConfig(),
                isLoadingDestinations: () => false,
                isRetrying: () => false,
                onRetryLoad: vi.fn(),
              }),
            );
          };
          const retired = mountTab();
          let current: ReturnType<typeof mountTab> | undefined;
          try {
            await waitFor(() => expect(retired.result.deliveryNeedsAttention()).toBe(true));
            let accept!: (
              value: Awaited<ReturnType<(typeof NotificationsAPI)[typeof action]>>,
            ) => void;
            let reject!: (error: Error) => void;
            vi.mocked(NotificationsAPI[action]).mockReturnValueOnce(
              new Promise((yes, no) => {
                accept = yes;
                reject = no;
              }),
            );
            const pending = retired.result[action]();
            retired.cleanup();
            current = mountTab();
            await waitFor(() => expect(current!.result.deliveryNeedsAttention()).toBe(true));
            if (outcome === 'accepted') accept({ success: true, affected: 2 });
            else reject(new Error('retired action failed'));
            await pending;
            expect(NotificationsAPI[action]).toHaveBeenCalledOnce();
            expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(2);
            expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(2);
            expect(AlertsAPI.getEvents).toHaveBeenCalledTimes(2);
            expect(notificationStore.success).not.toHaveBeenCalled();
            expect(notificationStore.error).not.toHaveBeenCalled();
            expect(current.result.queueActionFeedback()).toBeNull();
            expect(current.result.deliveryNeedsAttention()).toBe(true);
            expect(current.result.retryingTerminalFailures()).toBe(false);
            expect(current.result.dismissingTerminalFailures()).toBe(false);
            await retired.result[action]();
            await retired.result.loadDeliveryHealth();
            expect(confirm).toHaveBeenCalledOnce();
            expect(NotificationsAPI[action]).toHaveBeenCalledOnce();
            expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(2);

            // The replacement owns its own deliberate action and readback.
            vi.mocked(NotificationsAPI[action]).mockResolvedValueOnce({
              success: true,
              affected: 2,
            });
            vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce({
              queue: { status: 'healthy', attentionRequired: 0 },
            } as never);
            await current.result[action]();
            expect(notificationStore.success).toHaveBeenCalledOnce();
            expect(notificationStore.error).not.toHaveBeenCalled();
            expect(NotificationsAPI[action]).toHaveBeenCalledTimes(2);
            expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(3);
            expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(3);
            expect(AlertsAPI.getEvents).toHaveBeenCalledTimes(3);
            expect(current.result.deliveryNeedsAttention()).toBe(false);
          } finally {
            retired.cleanup();
            current?.cleanup();
            confirm.mockRestore();
          }
        },
      );
    },
  );

  it('preserves degraded configuration Retry health when the older mount request finishes', async () => {
    let finishMount!: (health: Awaited<ReturnType<typeof NotificationsAPI.getHealth>>) => void;
    const healthy = { queue: { status: 'healthy', attentionRequired: 0 } } as Awaited<
      ReturnType<typeof NotificationsAPI.getHealth>
    >;
    vi.mocked(NotificationsAPI.getWebhooks).mockResolvedValue([]);
    vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({ entries: [] } as never);
    vi.mocked(NotificationsAPI.getHealth)
      .mockReturnValueOnce(
        new Promise((resolve) => {
          finishMount = resolve;
        }),
      )
      .mockResolvedValueOnce({ ...healthy, queue: { ...healthy.queue, status: 'degraded' } });
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const { result } = renderHook(() =>
      useAlertDestinationsTabState({
        appriseConfig,
        setAppriseConfig,
        configLoadError: () => null,
        emailConfig: () => buildEmailConfig(),
        isLoadingDestinations: () => false,
        isRetrying: () => false,
        onRetryLoad: vi.fn(),
      }),
    );
    result.handleRetry();
    await waitFor(() => expect(result.deliveryNeedsAttention()).toBe(true));
    finishMount(healthy);
    await Promise.resolve();
    expect(result.deliveryHealth()?.queue.status).toBe('degraded');
    expect(result.deliveryNeedsAttention()).toBe(true);
  });

  it('preserves Retry log evidence when the registered mount read fails late', async () => {
    let rejectMount!: (error: Error) => void;
    const current = {
      entries: [],
      windowDays: 30,
      completedRetentionDays: 7,
      deadLetterRetentionDays: 30,
    };
    vi.mocked(NotificationsAPI.getWebhooks).mockResolvedValue([]);
    vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
      queue: { status: 'healthy' },
    } as never);
    vi.mocked(NotificationsAPI.getDeliveryLog)
      .mockReturnValueOnce(
        new Promise((_resolve, reject) => {
          rejectMount = reject;
        }),
      )
      .mockResolvedValueOnce(current);
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const onRetryLoad = vi.fn();
    const { result, cleanup } = renderHook(() =>
      useAlertDestinationsTabState({
        appriseConfig,
        setAppriseConfig,
        configLoadError: () => 'configuration unavailable',
        emailConfig: () => buildEmailConfig(),
        isLoadingDestinations: () => false,
        isRetrying: () => false,
        onRetryLoad,
      }),
    );
    try {
      await waitFor(() => expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(1));
      result.handleRetry();
      await waitFor(() => expect(result.deliveryLog()).toEqual(current));
      expect(onRetryLoad).toHaveBeenCalledTimes(1);
      rejectMount(new Error('stale mount failure'));
      await Promise.resolve();
      expect(result.deliveryLog()).toEqual(current);
      expect(result.deliveryLogUnavailable()).toBe(false);
      expect(result.heldEventsUnavailable()).toBe(false);
      expect(result.refreshingHeldEvents()).toBe(false);
      vi.mocked(AlertsAPI.getEvents).mockRejectedValueOnce(new Error('held events denied'));
      vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValueOnce(current);
      await result.loadDeliveryLog();
      expect(result.heldEvents()).toEqual([]);
      expect(result.heldEventsUnavailable()).toBe(true);
      expect(result.refreshingHeldEvents()).toBe(false);
      expect(result.deliveryLogUnavailable()).toBe(false);
      expect(result.deliveryLog()).toEqual(current);

      expect(result.refreshingDeliveryLog()).toBe(false);
    } finally {
      cleanup();
    }
  });

  beforeEach(() => {
    vi.mocked(AlertsAPI.getEvents).mockReset();
    vi.mocked(AlertsAPI.getEvents).mockResolvedValue([]);
    vi.mocked(NotificationsAPI.createWebhook).mockReset();
    vi.mocked(NotificationsAPI.deleteWebhook).mockReset();
    vi.mocked(NotificationsAPI.getDeliveryLog).mockReset();
    vi.mocked(NotificationsAPI.getHealth).mockReset();
    vi.mocked(NotificationsAPI.getWebhooks).mockReset();
    vi.mocked(NotificationsAPI.dismissTerminalFailures).mockReset();
    vi.mocked(NotificationsAPI.retryTerminalFailures).mockReset();
    vi.mocked(NotificationsAPI.testNotification).mockReset();
    vi.mocked(NotificationsAPI.testWebhook).mockReset();
    vi.mocked(NotificationsAPI.updateWebhook).mockReset();
    vi.mocked(notificationStore.error).mockReset();
    vi.mocked(notificationStore.success).mockReset();
    vi.mocked(notificationStore.warning).mockReset();
    vi.mocked(showErrorWithDetail).mockReset();
  });

  it('owns webhook runtime and destination test actions separately from config load/save state', async () => {
    const [emailConfig] = createSignal(buildEmailConfig());
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const [configLoadError] = createSignal<string | null>(null);
    const [isRetrying] = createSignal(false);
    const [isLoadingDestinations] = createSignal(false);
    const onRetryLoad = vi.fn();

    vi.mocked(NotificationsAPI.getWebhooks).mockResolvedValue([
      {
        enabled: true,
        headers: {},
        id: 'hook-1',
        method: 'POST',
        name: 'Ops',
        url: 'https://hooks.example.test/ops',
      },
    ] as never);
    vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
      overallHealthy: true,
      queue: {
        pending: 0,
        sending: 0,
        sent: 0,
        failed: 0,
        deadLetter: 0,
        healthy: true,
        status: 'healthy',
        attentionRequired: 0,
        reasonCodes: [],
        completedRetentionDays: 7,
        deadLetterRetentionDays: 30,
        countsAreRetentionBounded: true,
        retryAttemptsAffectHealth: false,
        terminalFailuresAffectHealth: true,
        failureClasses7d: {
          authentication: 0,
          rate_limited: 0,
          connectivity: 0,
          tls: 0,
          configuration: 0,
          rejected: 0,
          server_error: 0,
          unknown: 0,
        },
        failureClassesAvailable: true,
        failureClassWindowDays: 7,
      },
    });
    vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({
      entries: [
        {
          notificationId: 'email-1',
          type: 'email',
          outcome: 'sent',
          alertIds: ['disk-critical-1'],
          alertCount: 1,
          attempts: 1,
          success: true,
          timestamp: '2026-08-20T12:00:00Z',
        },
      ],
      windowDays: 30,
      completedRetentionDays: 7,
      deadLetterRetentionDays: 30,
    });
    vi.mocked(AlertsAPI.getEvents).mockResolvedValue([
      {
        id: 41,
        occurredAt: '2026-08-20T12:01:00Z',
        type: 'notification_suppressed',
        alertId: 'disk-critical-2',
        resourceName: 'backup-pool',
        alertType: 'usage',
        reason: 'notifications_inactive',
        message: 'Notification suppressed: alert delivery is not turned on.',
      },
    ]);
    vi.mocked(NotificationsAPI.testNotification).mockResolvedValue({ status: 'success' } as never);
    vi.mocked(NotificationsAPI.testWebhook).mockResolvedValue({ success: true } as never);

    const { result } = renderHook(() =>
      useAlertDestinationsTabState({
        appriseConfig,
        configLoadError,
        emailConfig,
        isLoadingDestinations,
        isRetrying,
        onRetryLoad,
        setAppriseConfig,
      }),
    );

    await waitFor(() => expect(NotificationsAPI.getWebhooks).toHaveBeenCalledTimes(1));
    await waitFor(() => expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(1));
    await waitFor(() => expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(1));
    await waitFor(() => expect(AlertsAPI.getEvents).toHaveBeenCalledTimes(1));
    expect(AlertsAPI.getEvents).toHaveBeenCalledWith(
      expect.objectContaining({
        types: ['notification_suppressed', 'notification_deferred'],
        limit: 100,
      }),
      { expectedOrgID: 'default', retry: false },
    );
    expect(result.deliveryHealth()?.queue.status).toBe('healthy');
    expect(result.deliveryLog()?.entries).toHaveLength(1);
    expect(result.heldEvents()).toEqual([
      expect.objectContaining({ reason: 'notifications_inactive', resourceName: 'backup-pool' }),
    ]);
    expect(result.deliveryLogUnavailable()).toBe(false);
    expect(result.webhooks()).toEqual([
      expect.objectContaining({ id: 'hook-1', service: 'generic' }),
    ]);

    await result.testEmailConfig();
    expect(NotificationsAPI.testNotification).toHaveBeenCalledWith(
      expect.objectContaining({ type: 'email' }),
      { expectedOrgID: 'default', retry: false },
    );

    await result.testApprise();
    expect(NotificationsAPI.testNotification).toHaveBeenCalledWith(
      expect.objectContaining({
        type: 'apprise',
        config: expect.objectContaining({
          mode: 'cli',
          targets: ['mailto://[email protected]'],
        }),
      }),
      { expectedOrgID: 'default', retry: false },
    );

    expect(result.webhooks()).toEqual([
      expect.objectContaining({ id: 'hook-1', service: 'generic' }),
    ]);

    result.updateApprise({ mode: 'http', serverUrl: 'https://apprise.internal' });
    expect(result.appriseState()).toEqual(
      expect.objectContaining({ mode: 'http', serverUrl: 'https://apprise.internal' }),
    );

    result.handleRetry();
    expect(onRetryLoad).toHaveBeenCalledTimes(1);
    await waitFor(() => expect(NotificationsAPI.getWebhooks).toHaveBeenCalledTimes(2));
    await waitFor(() => expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(2));
    await waitFor(() => expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(2));
    await waitFor(() => expect(AlertsAPI.getEvents).toHaveBeenCalledTimes(2));
    expect(notificationStore.success).toHaveBeenCalledTimes(2);
    expect(notificationStore.warning).not.toHaveBeenCalled();
    expect(showErrorWithDetail).not.toHaveBeenCalled();
  });

  it('warns instead of celebrating when a test send reports delivery is paused', async () => {
    const [emailConfig] = createSignal(buildEmailConfig());
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const [configLoadError] = createSignal<string | null>(null);
    const [isRetrying] = createSignal(false);
    const [isLoadingDestinations] = createSignal(false);

    vi.mocked(NotificationsAPI.getWebhooks).mockResolvedValue([]);
    vi.mocked(NotificationsAPI.getHealth).mockRejectedValue(new Error('offline'));
    vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({
      entries: [],
      windowDays: 30,
      completedRetentionDays: 7,
      deadLetterRetentionDays: 30,
    });
    // The backend reports the test went out while the activation gate keeps
    // real alerts suppressed; plain success here is the postmortem trap.
    vi.mocked(NotificationsAPI.testNotification).mockResolvedValue({
      status: 'success',
      deliveryPaused: true,
    } as never);

    const { result } = renderHook(() =>
      useAlertDestinationsTabState({
        appriseConfig,
        configLoadError,
        emailConfig,
        isLoadingDestinations,
        isRetrying,
        onRetryLoad: vi.fn(),
        setAppriseConfig,
      }),
    );

    // A Test is withheld while the independently loaded webhook snapshot is pending.
    await result.testEmailConfig();
    expect(NotificationsAPI.testNotification).not.toHaveBeenCalled();
    await waitFor(() => expect(result.isLoading()).toBe(false));
    expect(result.hasLoadError()).toBeFalsy();
    await result.testEmailConfig();
    expect(NotificationsAPI.testNotification).toHaveBeenCalledOnce();
    expect(notificationStore.warning).toHaveBeenCalledWith(
      expect.stringContaining('delivery is paused'),
    );
    expect(notificationStore.success).not.toHaveBeenCalled();

    await result.testApprise();
    expect(notificationStore.warning).toHaveBeenCalledTimes(2);
    expect(notificationStore.success).not.toHaveBeenCalled();
  });

  it('confirms and resolves retained terminal deliveries without deleting delivery history', async () => {
    const [emailConfig] = createSignal(buildEmailConfig());
    const [appriseConfig, setAppriseConfig] = createSignal(buildAppriseConfig());
    const [configLoadError] = createSignal<string | null>(null);
    const [isRetrying] = createSignal(false);
    const [isLoadingDestinations] = createSignal(false);
    const confirmSpy = vi.spyOn(globalThis, 'confirm').mockReturnValue(true);

    vi.mocked(NotificationsAPI.getWebhooks).mockResolvedValue([]);
    vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
      overallHealthy: false,
      queue: {
        attentionRequired: 3,
        completedRetentionDays: 7,
        countsAreRetentionBounded: true,
        deadLetter: 2,
        deadLetterRetentionDays: 30,
        failed: 1,
        failureClasses7d: {
          authentication: 3,
          configuration: 0,
          connectivity: 0,
          rate_limited: 0,
          rejected: 0,
          server_error: 0,
          tls: 0,
          unknown: 0,
        },
        failureClassesAvailable: true,
        failureClassWindowDays: 7,
        healthy: false,
        pending: 0,
        reasonCodes: ['retained_failed_deliveries', 'retained_dead_letter_deliveries'],
        retryAttemptsAffectHealth: false,
        sending: 0,
        sent: 0,
        status: 'degraded',
        terminalFailuresAffectHealth: true,
      },
    });
    vi.mocked(NotificationsAPI.getDeliveryLog).mockResolvedValue({
      entries: [],
      windowDays: 30,
      completedRetentionDays: 7,
      deadLetterRetentionDays: 30,
    });
    vi.mocked(NotificationsAPI.retryTerminalFailures).mockResolvedValue({
      affected: 3,
      success: true,
    });
    vi.mocked(NotificationsAPI.dismissTerminalFailures).mockResolvedValue({
      affected: 3,
      success: true,
    });

    const { result } = renderHook(() =>
      useAlertDestinationsTabState({
        appriseConfig,
        configLoadError,
        emailConfig,
        isLoadingDestinations,
        isRetrying,
        onRetryLoad: vi.fn(),
        setAppriseConfig,
      }),
    );

    await waitFor(() => expect(result.deliveryNeedsAttention()).toBe(true));
    await result.retryTerminalFailures();
    expect(confirmSpy).toHaveBeenCalledWith(expect.stringContaining('Retry 3 retained deliveries'));
    expect(NotificationsAPI.retryTerminalFailures).toHaveBeenCalledTimes(1);
    expect(notificationStore.success).toHaveBeenCalledWith(
      '3 retained deliveries queued for retry.',
    );

    await result.dismissTerminalFailures();
    expect(confirmSpy).toHaveBeenCalledWith(expect.stringContaining('Dismiss 3 retained failures'));
    expect(NotificationsAPI.dismissTerminalFailures).toHaveBeenCalledTimes(1);
    expect(notificationStore.success).toHaveBeenCalledWith('3 retained failures dismissed.');
    expect(NotificationsAPI.getDeliveryLog).toHaveBeenCalledTimes(3);

    confirmSpy.mockRestore();
  });
});