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.

2.4 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { Resource } from '@/types/resource';

import {
  agentDiskResourceId,
  hostOverrideIdCandidates,
  matchesThresholdResourceSearch,
  normalizeStorageStatus,
  uniqueIds,
} from '../thresholdsResourceModel';

describe('thresholdsResourceModel', () => {
  it('deduplicates candidate identifiers while preserving order', () => {
    expect(uniqueIds(' agent-1 ', undefined, 'agent-1', 'agent-2', '')).toEqual([
      'agent-1',
      'agent-2',
    ]);
  });

  it('builds host override candidates from the canonical resource identifiers', () => {
    const resource = {
      id: 'agent-runtime',
      type: 'agent',
      discoveryTarget: {
        resourceType: 'agent',
        resourceId: 'agent-discovery',
        agentId: 'agent-discovery',
      },
      agent: {
        agentId: 'agent-runtime',
      },
      platformData: {
        agent: {
          agentId: 'agent-platform',
        },
        agentId: 'agent-platform',
      },
    } as unknown as Resource;

    expect(hostOverrideIdCandidates(resource)).toEqual([
      'agent-discovery',
      'agent-runtime',
      'agent-platform',
    ]);
  });

  it('sanitizes agent disk ids with the backend-compatible label rules', () => {
    expect(agentDiskResourceId('agent-1', '/var/lib/docker', '')).toBe(
      'agent:agent-1/disk:var-lib-docker',
    );
    expect(agentDiskResourceId('agent-1', '', '/dev/sda1')).toBe('agent:agent-1/disk:dev-sda1');
  });

  it('normalizes storage status to the table availability contract', () => {
    expect(normalizeStorageStatus('online')).toBe('available');
    expect(normalizeStorageStatus('running')).toBe('available');
    expect(normalizeStorageStatus('offline')).toBe('offline');
    expect(normalizeStorageStatus(undefined)).toBe('offline');
  });

  // A resource search opened from elsewhere carries the registry name, which
  // for hosts can be the full hostname rather than the shown label.
  it('matches a resource search against its shown, display and raw names', () => {
    const host = { name: 'pve1', displayName: 'Primary node', rawName: 'pve1.lab.example' };
    expect(matchesThresholdResourceSearch(host, '')).toBe(true);
    expect(matchesThresholdResourceSearch(host, 'pve1.lab')).toBe(true);
    expect(matchesThresholdResourceSearch(host, 'primary')).toBe(true);
    expect(matchesThresholdResourceSearch(host, 'pve2')).toBe(false);
  });
});