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.

3.7 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { StorageRecord } from '@/features/storageBackups/models';
import {
  asStorageAlertRecord,
  EMPTY_STORAGE_ALERT_STATE,
  getStorageRecordAlertResourceIds,
  mergeStorageAlertRowState,
} from '@/features/storageBackups/storageAlertState';
import { getStorageAlertForecastDaysToFull } from '@/features/storageBackups/storageRowAlertPresentation';
import type { Alert } from '@/types/api';

const makeRecord = (overrides: Partial<StorageRecord> = {}): StorageRecord => ({
  id: 'storage-1',
  name: 'tank',
  category: 'pool',
  health: 'healthy',
  location: { label: 'truenas01', scope: 'host' },
  capacity: { totalBytes: 100, usedBytes: 40, freeBytes: 60, usagePercent: 40 },
  capabilities: ['capacity'],
  source: {
    platform: 'truenas',
    family: 'onprem',
    origin: 'resource',
    adapterId: 'resource-storage',
  },
  observedAt: Date.now(),
  ...overrides,
});

describe('storageAlertState', () => {
  it('normalizes alert collections into canonical alert records', () => {
    expect(
      asStorageAlertRecord([
        { id: 'a', severity: 'critical' },
        { id: 'b', severity: 'warning' },
      ]),
    ).toEqual({
      a: { id: 'a', severity: 'critical' },
      b: { id: 'b', severity: 'warning' },
    });
    expect(asStorageAlertRecord(null)).toEqual({});
  });

  it('merges alert row state without losing severity or acknowledgement semantics', () => {
    expect(
      mergeStorageAlertRowState(
        {
          ...EMPTY_STORAGE_ALERT_STATE,
          hasAlert: true,
          alertCount: 1,
          severity: 'warning',
          acknowledgedCount: 1,
          hasAcknowledgedOnlyAlert: true,
          headline: 'Over 85% usage limit',
          headlineCompact: 'Over 85%',
        },
        {
          ...EMPTY_STORAGE_ALERT_STATE,
          hasAlert: true,
          alertCount: 2,
          severity: 'critical',
          hasUnacknowledgedAlert: true,
          unacknowledgedCount: 2,
          headline: 'Full within a day',
          headlineCompact: 'Full <1d',
        },
      ),
    ).toEqual({
      hasAlert: true,
      alertCount: 3,
      severity: 'critical',
      hasUnacknowledgedAlert: true,
      unacknowledgedCount: 2,
      acknowledgedCount: 1,
      hasAcknowledgedOnlyAlert: false,
      // The more severe side explains the row.
      headline: 'Full within a day',
      headlineCompact: 'Full <1d',
    });
  });

  it('derives storage alert candidate ids canonically', () => {
    expect(
      getStorageRecordAlertResourceIds(
        makeRecord({
          refs: { resourceId: 'legacy-storage-id', platformEntityId: 'cluster-a' },
          details: { node: 'pve1' },
        }),
      ),
    ).toEqual(['storage-1', 'legacy-storage-id', 'cluster-a-pve1-tank']);
  });
});

// The Storage page headline and the Backups page's PBS datastore rows read the
// same forecast field through this one rule.
describe('getStorageAlertForecastDaysToFull', () => {
  const alert = (metadata?: Record<string, unknown>) => ({ metadata }) as unknown as Alert;

  it('reads a finite, non-negative forecast', () => {
    expect(getStorageAlertForecastDaysToFull(alert({ forecastDaysToFull: 4.2 }))).toBe(4.2);
    expect(getStorageAlertForecastDaysToFull(alert({ forecastDaysToFull: 0 }))).toBe(0);
  });

  it('treats a missing, malformed or negative forecast as none', () => {
    expect(getStorageAlertForecastDaysToFull(alert())).toBeNull();
    expect(getStorageAlertForecastDaysToFull(alert({ forecastDaysToFull: '4' }))).toBeNull();
    expect(getStorageAlertForecastDaysToFull(alert({ forecastDaysToFull: Number.NaN }))).toBeNull();
    expect(getStorageAlertForecastDaysToFull(alert({ forecastDaysToFull: -1 }))).toBeNull();
  });
});