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.

20.6 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { DEFAULT_LOCALE, setActiveLocale } from '@/i18n';
import {
  getAlertHistoryFocusNotice,
  ALERT_BUCKET_EMPTY_LABEL,
  ALERT_HISTORY_ALL_TIME_FILTER_LABEL,
  ALERT_HISTORY_EMPTY_DESCRIPTION,
  ALERT_HISTORY_EMPTY_STATE,
  ALERT_HISTORY_LOADING_STATE,
  ALERT_HISTORY_SEARCH_PLACEHOLDER,
  ALERT_OVERVIEW_ACKNOWLEDGED_LABEL,
  ALERT_OVERVIEW_LAST_24_HOURS_LABEL,
  ALERT_OVERVIEW_WORKLOAD_OVERRIDES_LABEL,
  ALERTS_EMPTY_STATE,
  ALERTS_PAGE_DEFAULT_TITLE,
  ALERTS_PAGE_DEFAULT_DESCRIPTION,
  ALERTS_PAGE_DESTINATIONS_DESCRIPTION,
  ALERTS_PAGE_DESTINATIONS_TITLE,
  ALERTS_PAGE_HISTORY_DESCRIPTION,
  ALERTS_PAGE_HISTORY_TITLE,
  ALERTS_PAGE_OVERVIEW_DESCRIPTION,
  ALERTS_PAGE_OVERVIEW_TITLE,
  ALERTS_PAGE_SCHEDULE_DESCRIPTION,
  ALERTS_PAGE_SCHEDULE_TITLE,
  ALERTS_PAGE_THRESHOLDS_DESCRIPTION,
  ALERTS_PAGE_THRESHOLDS_TITLE,
  ALERT_TIMELINE_EMPTY_STATE,
  ALERT_TIMELINE_FAILURE_STATE,
  ALERT_TIMELINE_FILTER_EMPTY_STATE,
  ALERT_TIMELINE_LOADING_STATE,
  ALERT_TIMELINE_RETRY_LABEL,
  ALERT_TIMELINE_UNAVAILABLE_STATE,
  ALERTS_THRESHOLD_HINT,
  getAlertOverviewAcknowledgedBadgeClass,
  getAlertOverviewAcknowledgedBadgeLabel,
  getAlertOverviewAcknowledgedNotification,
  getAlertOverviewAcknowledgedToggleLabel,
  getAlertOverviewActiveSectionTitle,
  getAlertOverviewBulkAcknowledgeFailureNotification,
  getAlertOverviewBulkAcknowledgeGenericFailureNotification,
  getAlertOverviewBulkAcknowledgeLabel,
  getAlertOverviewBulkAcknowledgedNotification,
  getAlertOverviewCardPresentation,
  formatAlertOverviewStartedAgo,
  getAlertOverviewEmptyState,
  getAlertOverviewGroupAcknowledgeLabel,
  getAlertOverviewGroupDisclosureLabel,
  getAlertOverviewHydrationState,
  getAlertOverviewNodeLabel,
  getAlertOverviewPausedState,
  getAlertOverviewPrimaryActionClass,
  getAlertOverviewPrimaryActionLabel,
  getAlertOverviewRestoredNotification,
  getAlertOverviewResumeLabel,
  getAlertOverviewSecondaryActionClass,
  getAlertOverviewSnoozeDescription,
  getAlertOverviewSnoozeFailureNotification,
  getAlertOverviewSnoozeLabel,
  getAlertOverviewSnoozeOptionLabel,
  getAlertOverviewSnoozedUntilLabel,
  getAlertOverviewSnoozedNotification,
  getAlertOverviewResumedNotification,
  getAlertOverviewStartedAtLabel,
  getAlertOverviewStartedAtClass,
  getAlertOverviewStatsLabels,
  getAlertOverviewTimelineActionLabel,
  getAlertHistoryEmptyState,
  getAlertHistoryLoadingState,
  getAlertHistoryLoadFailure,
  getAlertHistorySearchPlaceholder,
  getAlertBucketCountLabel,
  getAlertFilteredEmptyState,
  getAlertTimelineAcknowledgedLabel,
  getAlertTimelineClosedAtLabel,
  getAlertTimelineEmptyState,
  getAlertTimelineEventTypeLabel,
  getAlertTimelineFailureState,
  getAlertTimelineFilterEmptyState,
  getAlertTimelineFilterLabel,
  getAlertTimelineHeading,
  getAlertTimelineLoadingState,
  getAlertTimelineNoteLabel,
  getAlertTimelineNotePlaceholder,
  getAlertTimelineOpenedAtLabel,
  getAlertTimelineQuickFilterLabel,
  getAlertTimelineSaveNoteLabel,
  getAlertTimelineUnavailableState,
  getAlertListEmptyState,
  getAlertsPageHeaderMeta,
} from '@/utils/alertOverviewPresentation';

describe('alert overview grouping presentation', () => {
  afterEach(() => setActiveLocale(DEFAULT_LOCALE));

  it('distinguishes explicit linked signals from same-resource related alerts', () => {
    expect(
      getAlertOverviewGroupDisclosureLabel({ count: 1, correlated: true, expanded: false }),
    ).toBe('+1 linked signal');
    expect(
      getAlertOverviewGroupDisclosureLabel({ count: 2, correlated: true, expanded: false }),
    ).toBe('+2 linked signals');
    expect(
      getAlertOverviewGroupDisclosureLabel({ count: 1, correlated: false, expanded: false }),
    ).toBe('+1 related');
    expect(
      getAlertOverviewGroupDisclosureLabel({ count: 1, correlated: true, expanded: true }),
    ).toBe('Hide');
    expect(getAlertOverviewGroupAcknowledgeLabel(3)).toBe('Acknowledge all (3)');
  });
});

describe('alertOverviewPresentation', () => {
  beforeEach(() => {
    setActiveLocale(DEFAULT_LOCALE);
  });

  afterEach(() => {
    setActiveLocale(DEFAULT_LOCALE);
  });

  it('returns canonical alert overview empty-state copy', () => {
    expect(ALERTS_EMPTY_STATE).toBe('No active alerts');
    expect(ALERTS_THRESHOLD_HINT).toBe('Alerts will appear here when thresholds are exceeded');
    expect(getAlertListEmptyState(true)).toBe('No active alerts');
    expect(getAlertListEmptyState(false)).toBe('No unacknowledged alerts');
    expect(getAlertOverviewEmptyState()).toEqual({
      title: 'No active alerts',
      description: 'Alerts will appear here when thresholds are exceeded',
    });
    expect(getAlertOverviewPausedState()).toEqual({
      title: 'Alerting is paused',
      description: 'Toggle alerts on to resume monitoring and unlock configuration tabs',
    });
    expect(getAlertOverviewHydrationState('pending')).toEqual({
      title: 'Checking active alerts',
      description: 'Pulse is confirming the current incident state before reporting an all-clear.',
      retryLabel: 'Retry',
    });
    expect(getAlertOverviewHydrationState('unavailable')).toEqual({
      title: 'Active alert status is temporarily unavailable',
      description:
        'Pulse could not confirm the current incident state. This page will not report an all-clear until alert data is available.',
      retryLabel: 'Retry',
    });
  });

  it('returns canonical alert overview stat labels', () => {
    expect(ALERT_OVERVIEW_ACKNOWLEDGED_LABEL).toBe('Acknowledged');
    expect(ALERT_OVERVIEW_LAST_24_HOURS_LABEL).toBe('Triggered (24h)');
    expect(ALERT_OVERVIEW_WORKLOAD_OVERRIDES_LABEL).toBe('Workload Overrides');
    expect(getAlertOverviewStatsLabels()).toEqual({
      last24Hours: 'Triggered (24h)',
      acknowledged: 'Acknowledged',
      workloadOverrides: 'Workload Overrides',
    });
  });

  it('presents snooze as a bounded delivery hold while monitoring continues', () => {
    expect(getAlertOverviewSnoozeLabel()).toBe('Snooze');
    expect(getAlertOverviewResumeLabel()).toBe('Resume');
    expect(getAlertOverviewSnoozeDescription()).toBe(
      'Pause notifications and escalation for this incident. Monitoring continues and resolution is still detected.',
    );
    expect(getAlertOverviewSnoozeOptionLabel('oneHour')).toBe('For 1 hour');
    expect(getAlertOverviewSnoozeOptionLabel('tomorrowMorning')).toBe('Until tomorrow at 9:00');
    expect(getAlertOverviewSnoozedUntilLabel('27/08/2026, 17:00')).toBe(
      'Snoozed until 27/08/2026, 17:00',
    );
    expect(getAlertOverviewSnoozedNotification('27/08/2026, 17:00')).toBe(
      'Alert snoozed until 27/08/2026, 17:00',
    );
    expect(getAlertOverviewResumedNotification()).toBe('Alert notifications resumed');
    expect(getAlertOverviewSnoozeFailureNotification(false)).toBe('Failed to snooze alert');
    expect(getAlertOverviewSnoozeFailureNotification(true)).toBe(
      'Failed to resume alert notifications',
    );
  });

  it('returns canonical alert history search and empty-state copy', () => {
    expect(ALERT_HISTORY_SEARCH_PLACEHOLDER).toBe('Search alerts...');
    expect(getAlertHistorySearchPlaceholder()).toBe('Search alerts...');
    expect(ALERT_HISTORY_ALL_TIME_FILTER_LABEL).toBe('All time');
    expect(ALERT_HISTORY_EMPTY_STATE).toBe('No alerts found');
    expect(ALERT_HISTORY_EMPTY_DESCRIPTION).toBe('Try adjusting your filters or check back later');
    expect(getAlertHistoryEmptyState()).toEqual({
      title: 'No alerts found',
      description: 'Try adjusting your filters or check back later',
    });
    expect(getAlertFilteredEmptyState('TrueNAS alerts', 'severity')).toEqual({
      title: 'No TrueNAS alerts match current filters',
      description: 'Adjust the search or severity filter to see more TrueNAS alerts.',
    });
    expect(ALERT_HISTORY_LOADING_STATE).toBe('Loading alert history...');
    expect(getAlertHistoryLoadingState()).toEqual({
      text: 'Loading alert history...',
    });
  });

  it('returns canonical alerts page header metadata', () => {
    expect(ALERTS_PAGE_DEFAULT_TITLE).toBe('Alerts');
    expect(ALERTS_PAGE_OVERVIEW_TITLE).toBe('Alerts Overview');
    expect(ALERTS_PAGE_THRESHOLDS_TITLE).toBe('Alert Thresholds');
    expect(ALERTS_PAGE_DESTINATIONS_TITLE).toBe('Notifications');
    expect(ALERTS_PAGE_SCHEDULE_TITLE).toBe('Delivery Schedule');
    expect(ALERTS_PAGE_HISTORY_TITLE).toBe('Alert History');
    expect(ALERTS_PAGE_DEFAULT_DESCRIPTION).toBe(
      'Review active incidents, inspect alert history, and manage thresholds, notifications, and schedules.',
    );
    expect(ALERTS_PAGE_OVERVIEW_DESCRIPTION).toBe(
      'Review active incidents and confirm current alert coverage across monitored resources.',
    );
    expect(ALERTS_PAGE_THRESHOLDS_DESCRIPTION).toBe(
      'Tune thresholds and scoped overrides for infrastructure, workloads, and integrations.',
    );
    expect(ALERTS_PAGE_DESTINATIONS_DESCRIPTION).toBe(
      'Route alert notifications to email, Apprise, webhook, and mobile push destinations.',
    );
    expect(ALERTS_PAGE_SCHEDULE_DESCRIPTION).toBe(
      'Define quiet hours, grouping, cooldowns, recovery, and escalation behavior for alert delivery.',
    );
    expect(ALERTS_PAGE_HISTORY_DESCRIPTION).toBe(
      'Search prior alerts, review incident timelines, and inspect alert frequency over time.',
    );
    expect(getAlertsPageHeaderMeta()).toEqual({
      overview: {
        title: 'Alerts Overview',
        description:
          'Review active incidents and confirm current alert coverage across monitored resources.',
      },
      thresholds: {
        title: 'Alert Thresholds',
        description:
          'Tune thresholds and scoped overrides for infrastructure, workloads, and integrations.',
      },
      destinations: {
        title: 'Notifications',
        description:
          'Route alert notifications to email, Apprise, webhook, and mobile push destinations.',
      },
      schedule: {
        title: 'Delivery Schedule',
        description:
          'Define quiet hours, grouping, cooldowns, recovery, and escalation behavior for alert delivery.',
      },
      history: {
        title: 'Alert History',
        description:
          'Search prior alerts, review incident timelines, and inspect alert frequency over time.',
      },
      default: {
        title: 'Alerts',
        description:
          'Review active incidents, inspect alert history, and manage thresholds, notifications, and schedules.',
      },
    });
  });

  it('formats canonical alert bucket count labels', () => {
    expect(ALERT_BUCKET_EMPTY_LABEL).toBe('No alerts');
    expect(getAlertBucketCountLabel(0)).toBe('No alerts');
    expect(getAlertBucketCountLabel(1)).toBe('1 alert');
    expect(getAlertBucketCountLabel(3)).toBe('3 alerts');
  });

  it('returns canonical incident timeline state copy', () => {
    expect(ALERT_TIMELINE_LOADING_STATE).toBe('Loading timeline...');
    expect(ALERT_TIMELINE_FILTER_EMPTY_STATE).toBe(
      'No timeline events match the selected filters.',
    );
    expect(ALERT_TIMELINE_EMPTY_STATE).toBe('No timeline events yet.');
    expect(ALERT_TIMELINE_UNAVAILABLE_STATE).toBe('No incident timeline available.');
    expect(ALERT_TIMELINE_FAILURE_STATE).toBe('Failed to load timeline.');
    expect(ALERT_TIMELINE_RETRY_LABEL).toBe('Retry');
    expect(getAlertTimelineLoadingState()).toEqual({ text: ALERT_TIMELINE_LOADING_STATE });
    expect(getAlertTimelineFilterEmptyState()).toEqual({
      text: ALERT_TIMELINE_FILTER_EMPTY_STATE,
    });
    expect(getAlertTimelineEmptyState()).toEqual({ text: ALERT_TIMELINE_EMPTY_STATE });
    expect(getAlertTimelineUnavailableState()).toEqual({
      text: ALERT_TIMELINE_UNAVAILABLE_STATE,
    });
    expect(getAlertTimelineFailureState()).toEqual({
      text: ALERT_TIMELINE_FAILURE_STATE,
      actionLabel: ALERT_TIMELINE_RETRY_LABEL,
    });
    expect(getAlertTimelineHeading()).toBe('Incident');
    expect(getAlertTimelineAcknowledgedLabel()).toBe('acknowledged');
    expect(getAlertTimelineOpenedAtLabel('1/1/2026')).toBe('opened 1/1/2026');
    expect(getAlertTimelineClosedAtLabel('1/2/2026')).toBe('closed 1/2/2026');
    expect(getAlertTimelineNoteLabel()).toBe('Incident note');
    expect(getAlertTimelineNotePlaceholder()).toBe('Add a note for this incident...');
    expect(getAlertTimelineSaveNoteLabel(false)).toBe('Save Note');
    expect(getAlertTimelineSaveNoteLabel(true)).toBe('Saving...');
    expect(getAlertTimelineFilterLabel('panel')).toBe('Filter events:');
    expect(getAlertTimelineFilterLabel('compact')).toBe('Filters');
    expect(getAlertTimelineQuickFilterLabel('all')).toBe('All');
    expect(getAlertTimelineQuickFilterLabel('none')).toBe('None');
    expect(getAlertTimelineEventTypeLabel('command')).toBe('Cmd');
    expect(getAlertTimelineEventTypeLabel('unknown_event')).toBe('unknown_event');
  });

  it('returns canonical active alert card presentation', () => {
    const resourceClassName = 'text-sm font-semibold truncate text-base-content';
    expect(getAlertOverviewCardPresentation('critical', false, false)).toEqual({
      cardClassName:
        'border border-l-4 rounded-md px-3 py-2.5 transition-all border-border border-l-red-500 dark:border-l-red-500 bg-surface',
      iconClassName: 'mr-2.5 mt-0.5 shrink-0 transition-all text-red-600 dark:text-red-400',
      resourceClassName,
    });
    expect(getAlertOverviewCardPresentation('warning', true, true)).toEqual({
      cardClassName:
        'border border-l-4 rounded-md px-3 py-2.5 transition-all opacity-50 border-border border-l-border bg-surface-alt',
      iconClassName: 'mr-2.5 mt-0.5 shrink-0 transition-all text-green-600 dark:text-green-400',
      resourceClassName,
    });
    expect(getAlertOverviewCardPresentation('info', false, false)).toEqual({
      cardClassName:
        'border border-l-4 rounded-md px-3 py-2.5 transition-all border-border border-l-blue-500 dark:border-l-blue-400 bg-surface',
      iconClassName: 'mr-2.5 mt-0.5 shrink-0 transition-all text-blue-600 dark:text-blue-400',
      resourceClassName,
    });
    expect(getAlertOverviewCardPresentation('notice', false, false)).toEqual({
      cardClassName:
        'border border-l-4 rounded-md px-3 py-2.5 transition-all border-border border-l-yellow-500 dark:border-l-yellow-400 bg-surface',
      iconClassName: 'mr-2.5 mt-0.5 shrink-0 transition-all text-yellow-600 dark:text-yellow-400',
      resourceClassName,
    });
    expect(getAlertOverviewAcknowledgedBadgeClass()).toBe(
      'px-2 py-0.5 text-xs bg-yellow-200 dark:bg-yellow-800 text-yellow-800 dark:text-yellow-200 rounded-sm',
    );
    expect(getAlertOverviewStartedAtClass()).toBe('text-xs text-muted');
    expect(getAlertOverviewPrimaryActionClass(true)).toBe(
      'px-3 py-1.5 text-xs font-medium border rounded-md transition-all disabled:opacity-50 disabled:cursor-not-allowed text-base-content border-border hover:bg-surface-hover',
    );
    expect(getAlertOverviewPrimaryActionClass(false)).toBe(
      'px-3 py-1.5 text-xs font-medium border rounded-md transition-all disabled:opacity-50 disabled:cursor-not-allowed text-yellow-700 dark:text-yellow-300 border-yellow-300 dark:border-yellow-700 hover:bg-yellow-50 dark:hover:bg-yellow-900/25',
    );
    expect(getAlertOverviewSecondaryActionClass()).toBe(
      'px-3 py-1.5 text-xs font-medium border rounded-md transition-all bg-surface text-base-content border-border hover:bg-surface-hover',
    );
  });

  it('keeps the Started run on the footer baseline (#2119)', () => {
    // The alert-card footer is an items-center flex row that already carries the
    // top margin. A vertical margin on this child shifted the Started run below
    // the adjacent delivery-status span, which the reporter underlined.
    const startedClass = getAlertOverviewStartedAtClass();
    expect(startedClass.split(/\s+/)).not.toContain('mt-1');
    expect(startedClass).toContain('text-xs');
    expect(startedClass).toContain('text-muted');
  });

  it('localizes alert overview helper copy through the active locale', () => {
    setActiveLocale('es');

    expect(getAlertsPageHeaderMeta().overview.title).toBe('Resumen de alertas');
    expect(getAlertOverviewActiveSectionTitle()).toBe('Alertas activas');
    expect(getAlertOverviewStatsLabels()).toEqual({
      last24Hours: 'Activadas (24h)',
      acknowledged: 'Reconocidas',
      workloadOverrides: 'Excepciones de cargas',
    });
    expect(getAlertOverviewEmptyState()).toEqual({
      title: 'No hay alertas activas',
      description: 'Las alertas apareceran aqui cuando se superen los umbrales',
    });
    expect(getAlertOverviewPausedState().title).toBe('Las alertas estan pausadas');
    expect(getAlertListEmptyState(false)).toBe('No hay alertas sin reconocer');
    expect(getAlertOverviewAcknowledgedToggleLabel(true)).toBe('Ocultar reconocidas');
    expect(getAlertOverviewBulkAcknowledgeLabel(2, false)).toBe('Reconocer todas (2)');
    expect(getAlertOverviewBulkAcknowledgeLabel(2, true)).toBe('Reconociendo...');
    expect(getAlertOverviewAcknowledgedBadgeLabel()).toBe('Reconocida');
    expect(getAlertOverviewNodeLabel('pve-01')).toBe('en pve-01');
    expect(getAlertOverviewStartedAtLabel('1/1/2026')).toBe('Inicio: 1/1/2026');
    expect(getAlertOverviewPrimaryActionLabel({ acknowledged: false, processing: false })).toBe(
      'Reconocer',
    );
    expect(getAlertOverviewPrimaryActionLabel({ acknowledged: true, processing: false })).toBe(
      'Quitar reconocimiento',
    );
    expect(getAlertOverviewPrimaryActionLabel({ acknowledged: false, processing: true })).toBe(
      'Procesando...',
    );
    expect(getAlertOverviewTimelineActionLabel(false)).toBe('Linea de tiempo');
    expect(getAlertOverviewAcknowledgedNotification()).toBe('Alerta reconocida');
    expect(getAlertOverviewRestoredNotification()).toBe('Alerta restaurada');
    expect(getAlertOverviewBulkAcknowledgedNotification(2)).toBe('2 alertas reconocidas.');
    expect(getAlertOverviewBulkAcknowledgeFailureNotification(1)).toBe(
      'No se pudo reconocer 1 alerta.',
    );
    expect(getAlertOverviewBulkAcknowledgeGenericFailureNotification()).toBe(
      'No se pudieron reconocer las alertas',
    );
    expect(getAlertTimelineFailureState()).toEqual({
      text: 'No se pudo cargar la linea de tiempo.',
      actionLabel: 'Reintentar',
    });
    expect(getAlertTimelineFilterLabel('panel')).toBe('Filtrar eventos:');
    expect(getAlertTimelineQuickFilterLabel('none')).toBe('Ninguna');
    expect(getAlertTimelineEventTypeLabel('command')).toBe('Comando');

    setActiveLocale('de');

    expect(getAlertOverviewActiveSectionTitle()).toBe('Aktive Warnmeldungen');
    expect(getAlertTimelineHeading()).toBe('Vorfall');
    expect(getAlertTimelineEventTypeLabel('alert_resolved')).toBe('Behoben');
  });

  it('describes a failed History read as incomplete evidence with a read-only retry', () => {
    expect(getAlertHistoryLoadFailure()).toEqual({
      title: 'Could not load alert history',
      description:
        'Any entries shown may be incomplete or out of date. Retry to refresh saved history.',
      retryLabel: 'Retry history',
      retryingLabel: 'Retrying…',
    });
    expect(getAlertHistoryLoadFailure().title).not.toBe(getAlertHistoryEmptyState().title);
  });

  it('says how long ago an alert started in the active locale and moves with the clock', () => {
    const start = '2026-10-04T12:00:00Z';
    const at = (minutes: number) => Date.parse(start) + minutes * 60_000;
    expect(formatAlertOverviewStartedAgo(start, at(0))).toBe('this minute');
    expect(formatAlertOverviewStartedAgo(start, at(10))).toBe('10 min. ago');
    expect(formatAlertOverviewStartedAgo(start, at(11))).toBe('11 min. ago');
    expect(formatAlertOverviewStartedAgo(start, at(180))).toBe('3 hr. ago');
    expect(formatAlertOverviewStartedAgo(start, at(3 * 24 * 60))).toBe('3 days ago');
    expect(formatAlertOverviewStartedAgo('not-a-date', at(5))).toBe('');
    setActiveLocale('de');
    expect(formatAlertOverviewStartedAgo(start, at(10))).toBe('vor 10 Min.');
    setActiveLocale(DEFAULT_LOCALE);
  });
});

describe('getAlertHistoryFocusNotice', () => {
  it('names the linked alert and says plainly when history no longer holds it', () => {
    expect(getAlertHistoryFocusNotice({ missing: false }).text).toBe(
      'Showing one alert, opened from a link.',
    );
    expect(
      getAlertHistoryFocusNotice({ alertTitle: 'CPU usage', resourceName: 'db-01', missing: false })
        .text,
    ).toBe('Showing CPU usage on db-01, opened from a link.');
    expect(getAlertHistoryFocusNotice({ missing: false, liveAlertsUnavailable: true }).text).toBe(
      'Pulse could not load the alerts firing now, so this alert may still be active.',
    );
    expect(getAlertHistoryFocusNotice({ alertTitle: 'CPU usage', missing: true })).toEqual({
      text: "That alert is no longer in Pulse's alert history.",
      clearLabel: 'Show all alerts',
    });
  });
});