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.

11.2 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { Resource } from '@/types/resource';
import {
  getAgentFirstReportStatusIndicator,
  getActionableAgentIdFromResource,
  getActionableDockerRuntimeIdFromResource,
  getActionableKubernetesClusterIdFromResource,
  getExplicitAgentIdFromResource,
  getExplicitResourceClusterName,
  getPreferredResourceClusterName,
  hasDockerWorkloadsScope,
  hasAgentFacet,
  hasPulseAgentSourceEvidence,
  isAgentFacetInfrastructureResource,
  isAgentProfileAssignableResource,
  isPulseAgentPlatformResource,
  isTrueNASSystemResource,
} from '@/utils/agentResources';

const makeResource = (overrides: Partial<Resource> = {}): Resource =>
  ({
    id: 'resource-1',
    type: 'agent',
    name: 'resource-1',
    displayName: 'resource-1',
    platformId: 'resource-1',
    platformType: 'agent',
    sourceType: 'agent',
    status: 'online',
    lastSeen: Date.now(),
    ...overrides,
  }) as Resource;

describe('agentResources', () => {
  it('labels an unobserved startup agent without overriding a provider verdict', () => {
    const pending = { agent: { status: 'pending', lastSeen: '2026-10-24T12:00:00Z' } };
    expect(
      getAgentFirstReportStatusIndicator(
        makeResource({ status: 'unknown', sourceStatus: pending }),
      ),
    ).toEqual({
      variant: 'info',
      label: 'Waiting for first report',
    });
    for (const status of ['online', 'offline', 'warning'] as const) {
      expect(
        getAgentFirstReportStatusIndicator(makeResource({ status, sourceStatus: pending })),
      ).toBeUndefined();
    }
    expect(getAgentFirstReportStatusIndicator(makeResource({ status: 'unknown' }))).toBeUndefined();
  });
  it('resolves explicit agent ids from canonical and platform facets', () => {
    expect(
      getExplicitAgentIdFromResource(
        makeResource({
          agent: { agentId: 'agent-direct' },
        }),
      ),
    ).toBe('agent-direct');

    expect(
      getExplicitAgentIdFromResource(
        makeResource({
          platformData: {
            agent: { agentId: 'agent-platform' },
          },
        }),
      ),
    ).toBe('agent-platform');

    expect(
      getExplicitAgentIdFromResource(
        makeResource({
          type: 'k8s-cluster',
          kubernetes: { agentId: 'agent-k8s' },
        }),
      ),
    ).toBe('agent-k8s');

    expect(
      getExplicitAgentIdFromResource(
        makeResource({
          platformData: {
            linkedAgentId: 'agent-linked',
          },
        }),
      ),
    ).toBe('agent-linked');
  });

  it('falls back to discovery coordinates for actionable agent ids', () => {
    expect(
      getActionableAgentIdFromResource(
        makeResource({
          discoveryTarget: {
            resourceType: 'agent',
            resourceId: 'agent-discovery',
            agentId: 'agent-discovery-fallback',
          },
        }),
      ),
    ).toBe('agent-discovery');

    expect(
      getActionableAgentIdFromResource(
        makeResource({
          discoveryTarget: {
            resourceType: 'vm',
            resourceId: 'vm-100',
            agentId: 'agent-from-target',
          },
        }),
      ),
    ).toBe('agent-from-target');
  });

  it('resolves actionable docker runtime and kubernetes cluster ids', () => {
    expect(
      getActionableDockerRuntimeIdFromResource(
        makeResource({
          type: 'docker-host',
          platformData: {
            docker: { hostSourceId: 'docker-runtime-1' },
          },
        }),
      ),
    ).toBe('docker-runtime-1');

    expect(
      getActionableDockerRuntimeIdFromResource(
        makeResource({
          type: 'docker-host',
          identity: { machineId: 'docker-runtime-machine-id' },
          discoveryTarget: {
            resourceType: 'agent',
            resourceId: 'resource-agent-id',
            agentId: 'resource-agent-id',
          },
        }),
      ),
    ).toBe('docker-runtime-machine-id');

    expect(
      getActionableDockerRuntimeIdFromResource(
        makeResource({
          type: 'agent',
          metricsTarget: { resourceType: 'docker-host', resourceId: 'docker-runtime-2' },
        }),
      ),
    ).toBe('docker-runtime-2');

    expect(
      getActionableKubernetesClusterIdFromResource(
        makeResource({
          type: 'k8s-cluster',
          kubernetes: { clusterId: 'cluster-1' },
        }),
      ),
    ).toBe('cluster-1');

    expect(
      getActionableKubernetesClusterIdFromResource(
        makeResource({
          type: 'k8s-cluster',
          kubernetes: {
            clusterName: 'cluster-a',
            context: 'cluster-context',
            clusterId: 'cluster-a-id',
          },
        }),
      ),
    ).toBe('cluster-a');
  });

  it('resolves preferred kubernetes cluster names from the shared helper', () => {
    expect(
      getPreferredResourceClusterName(
        makeResource({
          type: 'k8s-cluster',
          kubernetes: {
            clusterName: 'cluster-a',
            context: 'cluster-context',
            clusterId: 'cluster-a-id',
          },
          name: 'fallback-name',
        }),
      ),
    ).toBe('cluster-a');

    expect(
      getPreferredResourceClusterName(
        makeResource({
          type: 'k8s-cluster',
          name: 'cluster-by-name',
        }),
      ),
    ).toBe('cluster-by-name');

    expect(
      getPreferredResourceClusterName(
        makeResource({
          type: 'vm',
          proxmox: {
            clusterName: 'proxmox-cluster',
          },
        }),
      ),
    ).toBe('proxmox-cluster');
  });

  it('keeps explicit cluster membership separate from display-name fallback', () => {
    expect(
      getExplicitResourceClusterName(
        makeResource({
          type: 'docker-host',
          name: 'ops-services-01',
        }),
      ),
    ).toBeUndefined();

    expect(
      getExplicitResourceClusterName(
        makeResource({
          type: 'agent',
          identity: {
            clusterName: 'Core Fabric',
          },
        }),
      ),
    ).toBe('Core Fabric');
  });

  it('detects docker workloads scope from explicit docker facets instead of source lists', () => {
    expect(
      hasDockerWorkloadsScope(
        makeResource({
          type: 'agent',
          platformData: {
            docker: { hostname: 'tower' },
          },
        }),
      ),
    ).toBe(true);

    expect(
      hasDockerWorkloadsScope(
        makeResource({
          type: 'agent',
          platformData: {
            agent: { hostname: 'tower' },
          },
        }),
      ),
    ).toBe(false);

    expect(
      hasDockerWorkloadsScope(
        makeResource({
          type: 'agent',
          docker: {},
          platformData: {
            docker: {},
            agent: { hostname: 'tower' },
          },
        }),
      ),
    ).toBe(false);
  });

  it('detects agent facets without relying on node-only typing', () => {
    expect(
      hasAgentFacet(
        makeResource({
          type: 'pbs',
          platformData: {
            agentId: 'pbs-agent',
          },
        }),
      ),
    ).toBe(true);

    expect(
      hasAgentFacet(
        makeResource({
          type: 'agent',
          platformData: {
            linkedAgentId: 'agent-linked',
          },
        }),
      ),
    ).toBe(true);

    expect(
      hasAgentFacet(
        makeResource({
          type: 'docker-host',
          platformData: {
            docker: { hostSourceId: 'docker-1' },
          },
        }),
      ),
    ).toBe(false);
  });

  it('distinguishes infrastructure agent-facet resources from profile-assignable resources', () => {
    expect(
      isAgentFacetInfrastructureResource(
        makeResource({
          type: 'pmg',
          platformData: {
            agent: { agentId: 'pmg-agent' },
          },
        }),
      ),
    ).toBe(true);

    expect(
      isAgentFacetInfrastructureResource(
        makeResource({
          type: 'docker-host',
          agent: { agentId: 'docker-agent' },
        }),
      ),
    ).toBe(false);

    expect(
      isAgentProfileAssignableResource(
        makeResource({
          type: 'docker-host',
        }),
      ),
    ).toBe(true);

    expect(
      isAgentProfileAssignableResource(
        makeResource({
          type: 'vm',
        }),
      ),
    ).toBe(false);
  });

  it('recognizes canonical TrueNAS systems through agent typing and platform ownership', () => {
    expect(
      isTrueNASSystemResource(
        makeResource({
          type: 'agent',
          platformType: 'truenas',
        }),
      ),
    ).toBe(true);

    expect(
      isTrueNASSystemResource(
        makeResource({
          type: 'agent',
          platformData: {
            sources: ['agent', 'truenas'],
          },
        }),
      ),
    ).toBe(true);

    expect(
      isTrueNASSystemResource(
        makeResource({
          type: 'agent',
          platformType: 'agent',
        }),
      ),
    ).toBe(false);
  });

  it('admits only agent-primary machines to the Agents platform page', () => {
    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'agent',
          platformType: 'agent',
          sourceType: 'agent',
          sources: ['agent'],
          platformScopes: ['agent'],
        }),
      ),
    ).toBe(true);

    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'agent',
          platformType: 'agent',
          sourceType: 'hybrid',
          sources: ['agent', 'docker'],
          platformScopes: ['agent', 'docker'],
        }),
      ),
    ).toBe(true);

    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'agent',
          platformType: 'proxmox-pve',
          sourceType: 'hybrid',
          sources: ['proxmox', 'agent'],
          platformScopes: ['agent', 'proxmox-pve'],
        }),
      ),
    ).toBe(false);

    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'agent',
          platformType: 'vmware-vsphere',
          sourceType: 'api',
          sources: ['vmware'],
          platformScopes: ['agent', 'vmware-vsphere'],
          agent: { agentId: 'vc-host-101', platform: 'vmware-vsphere' },
        }),
      ),
    ).toBe(false);

    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'pod',
          platformType: 'agent',
          sourceType: 'agent',
          sources: ['agent'],
        }),
      ),
    ).toBe(false);
  });

  it('keeps legacy source-less agent platform rows visible without admitting provider evidence', () => {
    expect(
      hasPulseAgentSourceEvidence(
        makeResource({
          type: 'agent',
          platformType: 'agent',
          sourceType: 'agent',
          sources: undefined,
        }),
      ),
    ).toBe(true);

    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'agent',
          platformType: 'agent',
          sourceType: 'api',
          sources: undefined,
        }),
      ),
    ).toBe(true);

    expect(
      isPulseAgentPlatformResource(
        makeResource({
          type: 'agent',
          platformType: 'agent',
          sourceType: 'agent',
          sources: undefined,
          platformData: {
            sourceStatus: { kubernetes: { status: 'online' } },
          },
        }),
      ),
    ).toBe(false);
  });
});