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.

16.2 KB
Raw Blame
/**
 * Tests for status utility functions
 *
 * These tests verify the status indicator logic for various resource types.
 */
import { describe, expect, it } from 'vitest';
import {
  formatStatusLabel,
  getCanonicalStatusLabel,
  isNodeOnline,
  isGuestRunning,
  getNodeStatusIndicator,
  getGuestHealthIndicator,
  getGuestPowerIndicator,
  getAgentStatusIndicator,
  getDockerHostStatusIndicator,
  getDockerContainerStatusIndicator,
  getDockerServiceStatusIndicator,
  getReplicationJobStatusIndicator,
  isConnectedHealthStatus,
  getSimpleStatusIndicator,
  getStatusIndicatorBadgeToneClasses,
  OFFLINE_HEALTH_STATUSES,
  DEGRADED_HEALTH_STATUSES,
  STOPPED_CONTAINER_STATES,
  ERROR_CONTAINER_STATES,
} from '@/utils/status';

describe('isNodeOnline', () => {
  it('returns false for null/undefined', () => {
    expect(isNodeOnline(null)).toBe(false);
    expect(isNodeOnline(undefined)).toBe(false);
  });

  it('returns true for online node with uptime', () => {
    expect(isNodeOnline({ status: 'online', uptime: 1000 })).toBe(true);
  });

  it('returns false for offline status', () => {
    expect(isNodeOnline({ status: 'offline', uptime: 1000 })).toBe(false);
  });

  it('returns false for zero uptime', () => {
    expect(isNodeOnline({ status: 'online', uptime: 0 })).toBe(false);
  });

  it('returns false for negative uptime', () => {
    expect(isNodeOnline({ status: 'online', uptime: -1 })).toBe(false);
  });

  it('returns false for error connection health', () => {
    expect(isNodeOnline({ status: 'online', uptime: 1000, connectionHealth: 'error' })).toBe(false);
    expect(isNodeOnline({ status: 'online', uptime: 1000, connectionHealth: 'offline' })).toBe(
      false,
    );
  });
});

describe('status label helpers', () => {
  it('formats arbitrary status labels predictably', () => {
    expect(formatStatusLabel('online')).toBe('Online');
    expect(formatStatusLabel('')).toBe('Unknown');
    expect(formatStatusLabel(undefined, 'N/A')).toBe('N/A');
  });

  it('returns canonical labels for known statuses and formatted fallbacks for unknown ones', () => {
    expect(getCanonicalStatusLabel('offline')).toBe('Offline');
    expect(getCanonicalStatusLabel('running')).toBe('Running');
    expect(getCanonicalStatusLabel('custom_state')).toBe('custom_state');
  });

  it('returns a generic indicator for simple online/offline/warning states', () => {
    expect(getSimpleStatusIndicator('online')).toEqual({ variant: 'success', label: 'Online' });
    expect(getSimpleStatusIndicator('running')).toEqual({ variant: 'success', label: 'Running' });
    expect(getSimpleStatusIndicator('warning')).toEqual({ variant: 'warning', label: 'Warning' });
    expect(getSimpleStatusIndicator('offline')).toEqual({ variant: 'danger', label: 'Offline' });
    expect(getSimpleStatusIndicator('healthy')).toEqual({ variant: 'success', label: 'Healthy' });
    expect(getSimpleStatusIndicator('')).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('exports badge tone classes for indicator variants', () => {
    expect(getStatusIndicatorBadgeToneClasses('success')).toContain('green');
    expect(getStatusIndicatorBadgeToneClasses('warning')).toContain('amber');
    expect(getStatusIndicatorBadgeToneClasses('danger')).toContain('red');
    expect(getStatusIndicatorBadgeToneClasses('muted')).toContain('bg-surface-alt');
  });

  it('exports a shared connected-status predicate for live monitoring state', () => {
    expect(isConnectedHealthStatus('online')).toBe(true);
    expect(isConnectedHealthStatus('running')).toBe(true);
    expect(isConnectedHealthStatus('healthy')).toBe(true);
    expect(isConnectedHealthStatus('offline')).toBe(false);
    expect(isConnectedHealthStatus(undefined)).toBe(false);
  });
});

describe('isGuestRunning', () => {
  it('returns false for null/undefined', () => {
    expect(isGuestRunning(null)).toBe(false);
    expect(isGuestRunning(undefined)).toBe(false);
  });

  it('returns true for running guest with online parent', () => {
    expect(isGuestRunning({ status: 'running' }, true)).toBe(true);
  });

  it('returns false for running guest with offline parent', () => {
    expect(isGuestRunning({ status: 'running' }, false)).toBe(false);
  });

  it('returns false for stopped guest', () => {
    expect(isGuestRunning({ status: 'stopped' }, true)).toBe(false);
  });
});

describe('getNodeStatusIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getNodeStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
    expect(getNodeStatusIndicator(undefined)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('returns success for online node', () => {
    const result = getNodeStatusIndicator({ status: 'online', uptime: 1000 });
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Online');
  });

  it('returns danger for offline node', () => {
    const result = getNodeStatusIndicator({ status: 'offline', uptime: 0 });
    expect(result.variant).toBe('danger');
  });

  it('returns warning for degraded node', () => {
    const result = getNodeStatusIndicator({ status: 'degraded', uptime: 1000 });
    expect(result.variant).toBe('warning');
  });
});

describe('getGuestPowerIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getGuestPowerIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('returns success for running guest', () => {
    const result = getGuestPowerIndicator({ status: 'running' }, true);
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Running');
  });

  it('returns danger for stopped guest', () => {
    const result = getGuestPowerIndicator({ status: 'stopped' }, true);
    expect(result.variant).toBe('danger');
    expect(result.label).toBe('Stopped');
  });

  it('returns danger when parent node is offline', () => {
    const result = getGuestPowerIndicator({ status: 'running' }, false);
    expect(result.variant).toBe('danger');
    expect(result.label).toBe('Node offline');
  });
});

describe('getGuestHealthIndicator', () => {
  const failedAttachedProbe = {
    enabled: true,
    available: false,
    correlationState: 'attached' as const,
    consecutiveFailures: 3,
    failureThreshold: 2,
  };

  it('degrades a running guest whose attached probe cannot reach it', () => {
    const result = getGuestHealthIndicator(
      { status: 'running', availability: failedAttachedProbe },
      true,
    );
    expect(result.variant).toBe('warning');
    expect(result.label).toBe('Running, not responding to probe');
  });

  it('leaves a running guest healthy when its probe is responding', () => {
    const result = getGuestHealthIndicator(
      {
        status: 'running',
        availability: { enabled: true, available: true, correlationState: 'attached' },
      },
      true,
    );
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Running');
  });

  it('leaves a running guest healthy when no probe is configured', () => {
    expect(getGuestHealthIndicator({ status: 'running' }, true).variant).toBe('success');
  });

  // A probe that has not been proven to describe this guest, or that has not
  // yet failed enough times for the poller to raise an incident, must not move
  // the indicator ahead of the alert.
  it('ignores probes that are not attached or are still inside the threshold', () => {
    const standalone = getGuestHealthIndicator(
      {
        status: 'running',
        availability: { ...failedAttachedProbe, correlationState: 'standalone' },
      },
      true,
    );
    const belowThreshold = getGuestHealthIndicator(
      { status: 'running', availability: { ...failedAttachedProbe, consecutiveFailures: 1 } },
      true,
    );
    const disabled = getGuestHealthIndicator(
      { status: 'running', availability: { ...failedAttachedProbe, enabled: false } },
      true,
    );

    expect(standalone.variant).toBe('success');
    expect(belowThreshold.variant).toBe('success');
    expect(disabled.variant).toBe('success');
  });

  it('keeps power state for a stopped guest and an offline node', () => {
    expect(
      getGuestHealthIndicator({ status: 'stopped', availability: failedAttachedProbe }, true).label,
    ).toBe('Stopped');
    expect(
      getGuestHealthIndicator({ status: 'running', availability: failedAttachedProbe }, false)
        .label,
    ).toBe('Node offline');
  });

  it('degrades when any one of several projected checks has failed', () => {
    const result = getGuestHealthIndicator(
      {
        status: 'running',
        availability: { enabled: true, available: true, correlationState: 'attached' },
        availabilityChecks: [
          { enabled: true, available: true, correlationState: 'attached' },
          failedAttachedProbe,
        ],
      },
      true,
    );
    expect(result.variant).toBe('warning');
  });
});

describe('getAgentStatusIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getAgentStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('returns success for online agent', () => {
    const result = getAgentStatusIndicator({ status: 'online' });
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Online');
  });

  it('returns danger for offline agent', () => {
    const result = getAgentStatusIndicator({ status: 'offline' });
    expect(result.variant).toBe('danger');
  });

  it('returns warning for degraded agent', () => {
    const result = getAgentStatusIndicator({ status: 'degraded' });
    expect(result.variant).toBe('warning');
  });
});

describe('getDockerHostStatusIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getDockerHostStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('accepts string status directly', () => {
    const result = getDockerHostStatusIndicator('online');
    expect(result.variant).toBe('success');
  });

  it('returns success for healthy Docker host', () => {
    const result = getDockerHostStatusIndicator({ status: 'healthy' });
    expect(result.variant).toBe('success');
  });

  it('returns danger for offline Docker host', () => {
    const result = getDockerHostStatusIndicator({ status: 'offline' });
    expect(result.variant).toBe('danger');
  });
});

describe('getDockerContainerStatusIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getDockerContainerStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('returns success for running healthy container', () => {
    const result = getDockerContainerStatusIndicator({ state: 'running', health: 'healthy' });
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Running');
  });

  it('returns success for running container without health', () => {
    const result = getDockerContainerStatusIndicator({ state: 'running' });
    expect(result.variant).toBe('success');
  });

  it('returns danger for unhealthy container', () => {
    const result = getDockerContainerStatusIndicator({ state: 'running', health: 'unhealthy' });
    expect(result.variant).toBe('danger');
    expect(result.label).toBe('Unhealthy');
  });

  it('returns danger for exited container', () => {
    const result = getDockerContainerStatusIndicator({ state: 'exited' });
    expect(result.variant).toBe('danger');
  });

  it('returns danger for dead container', () => {
    const result = getDockerContainerStatusIndicator({ state: 'dead' });
    expect(result.variant).toBe('danger');
  });
});

describe('getDockerServiceStatusIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getDockerServiceStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('returns success when running >= desired', () => {
    const result = getDockerServiceStatusIndicator({ desiredTasks: 3, runningTasks: 3 });
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Healthy');
  });

  it('returns warning when running < desired but > 0', () => {
    const result = getDockerServiceStatusIndicator({ desiredTasks: 3, runningTasks: 2 });
    expect(result.variant).toBe('warning');
    expect(result.label).toBe('Degraded (2/3)');
  });

  it('returns danger when running is 0', () => {
    const result = getDockerServiceStatusIndicator({ desiredTasks: 3, runningTasks: 0 });
    expect(result.variant).toBe('danger');
    expect(result.label).toBe('Stopped (0/3)');
  });

  it('returns muted when desired is 0 and running is 0', () => {
    const result = getDockerServiceStatusIndicator({ desiredTasks: 0, runningTasks: 0 });
    expect(result.variant).toBe('muted');
    expect(result.label).toBe('No tasks');
  });

  it('returns warning when desired is 0 but running > 0', () => {
    const result = getDockerServiceStatusIndicator({ desiredTasks: 0, runningTasks: 2 });
    expect(result.variant).toBe('warning');
    expect(result.label).toBe('Running 2 tasks');
  });
});

describe('Status sets', () => {
  describe('OFFLINE_HEALTH_STATUSES', () => {
    it('contains expected offline statuses', () => {
      expect(OFFLINE_HEALTH_STATUSES.has('offline')).toBe(true);
      expect(OFFLINE_HEALTH_STATUSES.has('error')).toBe(true);
      expect(OFFLINE_HEALTH_STATUSES.has('failed')).toBe(true);
      expect(OFFLINE_HEALTH_STATUSES.has('timeout')).toBe(true);
      expect(OFFLINE_HEALTH_STATUSES.has('stopped')).toBe(true);
    });

    it('does not contain online statuses', () => {
      expect(OFFLINE_HEALTH_STATUSES.has('online')).toBe(false);
      expect(OFFLINE_HEALTH_STATUSES.has('running')).toBe(false);
      expect(OFFLINE_HEALTH_STATUSES.has('healthy')).toBe(false);
    });
  });

  describe('DEGRADED_HEALTH_STATUSES', () => {
    it('contains expected degraded statuses', () => {
      expect(DEGRADED_HEALTH_STATUSES.has('degraded')).toBe(true);
      expect(DEGRADED_HEALTH_STATUSES.has('warning')).toBe(true);
      expect(DEGRADED_HEALTH_STATUSES.has('syncing')).toBe(true);
      expect(DEGRADED_HEALTH_STATUSES.has('pending')).toBe(true);
    });
  });

  describe('STOPPED_CONTAINER_STATES', () => {
    it('contains expected stopped states', () => {
      expect(STOPPED_CONTAINER_STATES.has('exited')).toBe(true);
      expect(STOPPED_CONTAINER_STATES.has('stopped')).toBe(true);
      expect(STOPPED_CONTAINER_STATES.has('created')).toBe(true);
      expect(STOPPED_CONTAINER_STATES.has('paused')).toBe(true);
    });

    it('does not contain running state', () => {
      expect(STOPPED_CONTAINER_STATES.has('running')).toBe(false);
    });
  });

  describe('ERROR_CONTAINER_STATES', () => {
    it('contains expected error states', () => {
      expect(ERROR_CONTAINER_STATES.has('dead')).toBe(true);
      expect(ERROR_CONTAINER_STATES.has('oomkilled')).toBe(true);
      expect(ERROR_CONTAINER_STATES.has('unhealthy')).toBe(true);
      expect(ERROR_CONTAINER_STATES.has('restarting')).toBe(true);
    });
  });
});

describe('getReplicationJobStatusIndicator', () => {
  it('returns muted for null/undefined', () => {
    expect(getReplicationJobStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
  });

  it('returns danger for error status', () => {
    const result = getReplicationJobStatusIndicator({ status: 'error' });
    expect(result.variant).toBe('danger');
  });

  it('returns danger for error in lastSyncStatus', () => {
    const result = getReplicationJobStatusIndicator({ status: 'idle', lastSyncStatus: 'error' });
    expect(result.variant).toBe('danger');
  });

  it('returns warning for syncing status', () => {
    const result = getReplicationJobStatusIndicator({ status: 'syncing' });
    expect(result.variant).toBe('warning');
  });

  it('returns warning for sync in status', () => {
    const result = getReplicationJobStatusIndicator({ status: 'backup-sync' });
    expect(result.variant).toBe('warning');
  });

  it('returns success for idle status', () => {
    const result = getReplicationJobStatusIndicator({ status: 'idle' });
    expect(result.variant).toBe('success');
    expect(result.label).toBe('Idle');
  });

  it('returns success for running status', () => {
    const result = getReplicationJobStatusIndicator({ status: 'running' });
    expect(result.variant).toBe('success');
  });

  it('returns success when using state field', () => {
    const result = getReplicationJobStatusIndicator({ state: 'idle' });
    expect(result.variant).toBe('success');
  });
});