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.

13.5 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import {
  ALERT_INCIDENT_TIMELINE_DIALOG_TITLE,
  ALERT_RESOURCE_INCIDENT_EMPTY_STATE,
  ALERT_RESOURCE_INCIDENT_LOADING_STATE,
  ALERT_RESOURCE_INCIDENT_NOTE_PLACEHOLDER,
  ALERT_RESOURCE_INCIDENT_NO_EVENTS_FILTERED_STATE,
  ALERT_RESOURCE_INCIDENT_PANEL_TITLE,
  ALERT_RESOURCE_INCIDENT_REFRESHING_LABEL,
  ALERT_RESOURCE_INCIDENT_REFRESH_LABEL,
  ALERT_RESOURCE_INCIDENT_SAVE_NOTE_LABEL,
  ALERT_RESOURCE_INCIDENT_SAVING_NOTE_LABEL,
  ALERT_HISTORY_MOVED_STATUS,
  getAlertHistoryStatusPresentation,
  getAlertIncidentLevelBadgeClass,
  getAlertResolutionDetail,
  getAlertIncidentTimelineDialogTitle,
  getAlertHistoryInvestigationDialogTitle,
  getAlertResourceIncidentAcknowledgedByLabel,
  getAlertResourceIncidentCountLabel,
  getAlertResourceIncidentEmptyState,
  getAlertResourceIncidentFilteredEventsEmptyState,
  getAlertResourceIncidentLoadingState,
  getAlertResourceIncidentNotePlaceholder,
  getAlertResourceIncidentPanelTitle,
  getAlertResourceIncidentRecentEventsSummary,
  getAlertResourceIncidentRefreshLabel,
  getAlertResourceIncidentSaveNoteLabel,
  getAlertResourceIncidentToggleLabel,
  getAlertIncidentStatusPresentation,
  getAlertIncidentEventFilterActionButtonClass,
  getAlertIncidentEventFilterChipClass,
  getAlertIncidentEventFilterContainerClass,
  getAlertIncidentEventFilterLabelClass,
  getAlertIncidentAcknowledgedBadgeClass,
  getAlertIncidentNoteSaveButtonClass,
  getAlertIncidentNoteTextareaClass,
  getAlertIncidentTimelineEventCardClass,
  getAlertIncidentTimelineMetaRowClass,
  getAlertIncidentTimelineHeadingClass,
  getAlertIncidentTimelineDetailClass,
  getAlertIncidentTimelineCommandClass,
  getAlertIncidentTimelineOutputClass,
  getAlertResourceIncidentActivityChipClass,
  getAlertResourceIncidentActivitySummaryClass,
  getAlertResourceIncidentCardClass,
  getAlertResourceIncidentSummaryRowClass,
  getAlertResourceIncidentToggleButtonClass,
  getAlertResourceIncidentTruncatedEventsLabel,
  normalizeAlertIncidentStatus,
  getAlertHistoryResourceActionLabel,
  getAlertHistoryTimelineActionLabel,
} from '@/utils/alertIncidentPresentation';

describe('alertIncidentPresentation', () => {
  it('normalizes alert incident status canonically', () => {
    expect(normalizeAlertIncidentStatus('open', true)).toBe('acknowledged');
    expect(normalizeAlertIncidentStatus('open', false)).toBe('open');
    expect(normalizeAlertIncidentStatus('resolved', false)).toBe('resolved');
    expect(normalizeAlertIncidentStatus('closed', false)).toBe('resolved');
    expect(normalizeAlertIncidentStatus(undefined, false)).toBe('unknown');
  });

  it('returns canonical incident status presentation classes', () => {
    expect(getAlertIncidentStatusPresentation('open', true)).toEqual({
      label: 'acknowledged',
      className:
        'px-2 py-0.5 rounded-sm bg-emerald-100 dark:bg-emerald-900/25 text-emerald-700 dark:text-emerald-300',
    });

    expect(getAlertIncidentStatusPresentation('open', false)).toEqual({
      label: 'open',
      className:
        'px-2 py-0.5 rounded-sm bg-red-100 dark:bg-red-900/25 text-red-700 dark:text-red-300',
    });

    expect(getAlertIncidentStatusPresentation('resolved', false)).toEqual({
      label: 'resolved',
      className: 'px-2 py-0.5 rounded-sm bg-surface-hover text-base-content',
    });
  });

  it('returns canonical incident level badge classes', () => {
    expect(getAlertIncidentLevelBadgeClass('critical')).toContain('bg-red-100');
    expect(getAlertIncidentLevelBadgeClass('warning')).toContain('bg-amber-100');
    expect(getAlertIncidentLevelBadgeClass('info')).toContain('bg-blue-100');
    expect(getAlertIncidentLevelBadgeClass(undefined)).toContain('bg-amber-100');
  });

  it('returns canonical alert history status presentation', () => {
    expect(getAlertHistoryStatusPresentation('active')).toEqual({
      label: 'active',
      className:
        'text-xs px-2 py-0.5 rounded-sm bg-red-100 dark:bg-red-900/25 text-red-700 dark:text-red-300 font-medium',
      rowClassName: 'bg-red-50 dark:bg-red-900/25',
    });

    expect(getAlertHistoryStatusPresentation('acknowledged')).toEqual({
      label: 'acknowledged',
      className:
        'text-xs px-2 py-0.5 rounded-sm bg-yellow-100 dark:bg-yellow-900/25 text-yellow-700 dark:text-yellow-300',
      rowClassName: '',
    });

    expect(getAlertHistoryStatusPresentation('resolved')).toEqual({
      label: 'resolved',
      className: 'text-xs px-2 py-0.5 rounded-sm bg-surface-hover text-base-content',
      rowClassName: '',
    });

    expect(getAlertHistoryStatusPresentation(ALERT_HISTORY_MOVED_STATUS)).toEqual({
      label: 'moved to agent',
      className:
        'text-xs px-2 py-0.5 rounded-sm bg-blue-100 dark:bg-blue-900/25 text-blue-700 dark:text-blue-300',
      rowClassName: '',
    });
  });

  // The history table row uses an 11px badge so the Status track needs 108px
  // for "moved to agent" instead of 119px; the cards keep the 12px badge.
  it('gives a table row the compact status badge without changing the card badge', () => {
    expect(getAlertHistoryStatusPresentation('acknowledged', { compact: true })).toEqual({
      label: 'acknowledged',
      className:
        'text-[11px] px-1.5 py-0.5 rounded-sm bg-yellow-100 dark:bg-yellow-900/25 text-yellow-700 dark:text-yellow-300',
      rowClassName: '',
    });
    expect(
      getAlertHistoryStatusPresentation(ALERT_HISTORY_MOVED_STATUS, { compact: true }).className,
    ).toContain('text-[11px] px-1.5');
    expect(getAlertHistoryStatusPresentation('resolved').className).toContain('text-xs px-2');
  });

  it('says a handed-over alert did not recover, and leaves recoveries alone', () => {
    expect(getAlertResolutionDetail(undefined)).toBeUndefined();
    expect(
      getAlertResolutionDetail({ reason: 'moved_to_agent', successorName: 'pve1 (Host Agent)' }),
    ).toBe(
      'Moved to pve1 (Host Agent). This is not a recovery: check the agent for the current reading.',
    );
    expect(getAlertResolutionDetail({ reason: 'moved_to_agent' })).toBe(
      'Moved to its Pulse agent. This is not a recovery: check the agent for the current reading.',
    );
    // A future reason the UI has no copy for still shows the engine's account.
    expect(
      getAlertResolutionDetail({ reason: 'replaced', summary: 'Alert replaced by a new check.' }),
    ).toBe('Alert replaced by a new check.');
  });

  it('returns canonical resource incident loading and empty-state copy', () => {
    expect(ALERT_INCIDENT_TIMELINE_DIALOG_TITLE).toBe('Incident timeline');
    expect(ALERT_RESOURCE_INCIDENT_PANEL_TITLE).toBe('Resource incidents');
    expect(ALERT_RESOURCE_INCIDENT_LOADING_STATE).toBe('Loading incidents...');
    expect(ALERT_RESOURCE_INCIDENT_EMPTY_STATE).toBe(
      'No incidents recorded for this resource yet.',
    );
    expect(ALERT_RESOURCE_INCIDENT_REFRESH_LABEL).toBe('Refresh');
    expect(ALERT_RESOURCE_INCIDENT_REFRESHING_LABEL).toBe('Refreshing...');
    expect(ALERT_RESOURCE_INCIDENT_NO_EVENTS_FILTERED_STATE).toBe(
      'No events match the selected filters.',
    );
    expect(ALERT_RESOURCE_INCIDENT_NOTE_PLACEHOLDER).toBe('Add a note for this incident...');
    expect(ALERT_RESOURCE_INCIDENT_SAVE_NOTE_LABEL).toBe('Save Note');
    expect(ALERT_RESOURCE_INCIDENT_SAVING_NOTE_LABEL).toBe('Saving...');
    expect(getAlertResourceIncidentPanelTitle()).toBe('Resource incidents');
    expect(getAlertIncidentTimelineDialogTitle()).toBe('Incident timeline');
    expect(getAlertHistoryInvestigationDialogTitle('resource')).toBe('Resource incidents');
    expect(getAlertHistoryInvestigationDialogTitle('timeline')).toBe('Incident timeline');
    expect(getAlertResourceIncidentLoadingState()).toEqual({
      text: 'Loading incidents...',
    });
    expect(getAlertResourceIncidentEmptyState()).toEqual({
      text: 'No incidents recorded for this resource yet.',
    });
    expect(getAlertResourceIncidentCountLabel(1)).toBe('1 incident');
    expect(getAlertResourceIncidentCountLabel(3)).toBe('3 incidents');
    expect(getAlertResourceIncidentRefreshLabel(false)).toBe('Refresh');
    expect(getAlertResourceIncidentRefreshLabel(true)).toBe('Refreshing...');
    expect(getAlertResourceIncidentNotePlaceholder()).toBe('Add a note for this incident...');
    expect(getAlertResourceIncidentSaveNoteLabel(false)).toBe('Save Note');
    expect(getAlertResourceIncidentSaveNoteLabel(true)).toBe('Saving...');
    expect(getAlertResourceIncidentAcknowledgedByLabel('alice')).toBe('Acknowledged by alice');
    expect(getAlertResourceIncidentToggleLabel(false, '2/8')).toBe('Events (2/8)');
    expect(getAlertResourceIncidentToggleLabel(true, '2/8')).toBe('Hide events');
    expect(getAlertResourceIncidentFilteredEventsEmptyState()).toEqual({
      text: 'No events match the selected filters.',
    });
    expect(getAlertResourceIncidentRecentEventsSummary(6)).toBe('Showing last 6 events');
  });

  it('returns canonical incident event filter presentation classes', () => {
    expect(getAlertIncidentEventFilterContainerClass('compact')).toBe(
      'flex flex-wrap items-center gap-2 text-[10px] text-muted',
    );
    expect(getAlertIncidentEventFilterContainerClass('panel')).toBe(
      'flex flex-wrap items-center gap-1.5 rounded-sm border border-border p-2',
    );
    expect(getAlertIncidentEventFilterLabelClass('compact')).toBe(
      'uppercase tracking-wide text-[9px] text-muted',
    );
    expect(getAlertIncidentEventFilterLabelClass('panel')).toBe(
      'mr-1 text-xs font-medium text-muted',
    );
    expect(getAlertIncidentEventFilterActionButtonClass()).toBe(
      'px-2 py-0.5 rounded-sm border border-border text-muted hover:bg-surface-hover',
    );
    expect(getAlertIncidentEventFilterChipClass(true, 'compact')).toBe(
      'px-2 py-0.5 rounded-sm border text-[10px] transition-colors border-blue-300 bg-blue-100 text-blue-700 dark:border-blue-800 dark:bg-blue-900/25 dark:text-blue-300',
    );
    expect(getAlertIncidentEventFilterChipClass(false, 'compact')).toBe(
      'px-2 py-0.5 rounded-sm border text-[10px] transition-colors border-border text-slate-500',
    );
    expect(getAlertIncidentEventFilterChipClass(false, 'panel')).toBe(
      'px-2 py-0.5 rounded-sm border text-[10px] transition-colors font-medium border-border text-muted hover:bg-surface-alt',
    );
  });

  it('returns canonical incident timeline presentation classes', () => {
    expect(getAlertIncidentAcknowledgedBadgeClass()).toBe(
      'px-2 py-0.5 rounded-sm bg-emerald-100 text-emerald-700 dark:bg-emerald-900/25 dark:text-emerald-300',
    );
    expect(getAlertIncidentTimelineEventCardClass('alt')).toBe(
      'rounded-sm border border-border bg-surface-alt p-2 whitespace-normal wrap-break-word',
    );
    expect(getAlertIncidentTimelineEventCardClass('surface')).toBe(
      'rounded-sm border border-border bg-surface p-2 whitespace-normal wrap-break-word',
    );
    expect(getAlertIncidentNoteTextareaClass()).toBe(
      'w-full rounded-sm border border-border bg-surface p-2 text-xs text-base-content',
    );
    expect(getAlertIncidentNoteSaveButtonClass()).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 disabled:opacity-50 disabled:cursor-not-allowed',
    );
    expect(getAlertIncidentTimelineMetaRowClass()).toBe(
      'flex flex-wrap items-center gap-2 text-xs text-muted',
    );
    expect(getAlertIncidentTimelineHeadingClass()).toBe('font-medium text-base-content');
    expect(getAlertIncidentTimelineDetailClass()).toBe(
      'mt-1 text-xs text-base-content whitespace-pre-wrap',
    );
    expect(getAlertIncidentTimelineCommandClass()).toBe('mt-1 font-mono text-xs text-base-content');
    expect(getAlertIncidentTimelineOutputClass()).toBe('mt-1 text-xs text-muted');
  });

  it('returns canonical resource incident card presentation classes', () => {
    expect(getAlertResourceIncidentCardClass()).toBe(
      'rounded-sm border border-border bg-surface p-3',
    );
    expect(getAlertResourceIncidentSummaryRowClass()).toBe(
      'mt-2 flex flex-wrap items-center justify-between gap-2 text-xs text-muted',
    );
    expect(getAlertResourceIncidentActivitySummaryClass()).toBe(
      'flex flex-wrap items-center gap-1.5',
    );
    expect(getAlertResourceIncidentActivityChipClass()).toBe(
      'rounded-sm bg-surface-alt px-2 py-0.5 text-[10px] font-medium text-base-content',
    );
    expect(getAlertResourceIncidentToggleButtonClass()).toBe(
      'px-2 py-1 text-[10px] border rounded-md border-border text-muted hover:bg-surface-hover',
    );
    expect(getAlertResourceIncidentTruncatedEventsLabel(6)).toBe('Showing last 6 events');
    expect(getAlertResourceIncidentTruncatedEventsLabel(6, 6)).toBe('Showing 6 events');
    expect(getAlertResourceIncidentTruncatedEventsLabel(6, 9)).toBe('Showing last 6 of 9 events');
  });
});

describe('alert history row action names', () => {
  it('names the alert, resource and start time, led by the visible label', () => {
    const base = { alertTitle: 'CPU usage', resourceName: 'db-01', startedAt: '15 Oct, 11:44' };
    expect(getAlertHistoryTimelineActionLabel({ ...base, open: false })).toBe(
      'Timeline for CPU usage on db-01, 15 Oct, 11:44',
    );
    expect(getAlertHistoryTimelineActionLabel({ ...base, open: true })).toBe(
      'Hide timeline for CPU usage on db-01, 15 Oct, 11:44',
    );
    expect(getAlertHistoryTimelineActionLabel({ ...base, resourceName: '', open: false })).toBe(
      'Timeline for CPU usage, 15 Oct, 11:44',
    );
    expect(getAlertHistoryResourceActionLabel('db-01')).toBe('Resource incidents for db-01');
    expect(getAlertHistoryResourceActionLabel(' ')).toBe('Resource incidents for this resource');
  });
});