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.

15.8 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { Connection, ConnectionAgentIdentity } from '@/api/connections';
import {
  agentAttachmentProblem,
  connectionAgentIdentitySummary,
  fleetGovernanceSignalsForConnection,
  primaryRowProblem,
  visibleFleetGovernanceSignals,
  type FleetGovernanceSignal,
} from '../connectionsTableModel';

const connectionFixture = (overrides: Partial<Connection> = {}): Connection => ({
  id: 'agent:host-1',
  type: 'agent',
  name: 'host-1',
  address: 'host-1',
  state: 'active',
  stateReason: '',
  enabled: true,
  surfaces: ['host'],
  scope: { host: true },
  lastSeen: '2026-04-23T12:00:00Z',
  lastError: null,
  source: 'agent',
  capabilities: { supportsPause: false, supportsScope: false, supportsTest: false },
  ...overrides,
});

describe('visibleFleetGovernanceSignals', () => {
  it('names a pending attached agent without calling it an outage', () => {
    expect(agentAttachmentProblem(connectionFixture({ state: 'pending' }))).toMatchObject({
      label: 'Waiting for first report',
      tone: 'warning',
    });
    expect(agentAttachmentProblem(connectionFixture({ state: 'unreachable' }))).toMatchObject({
      label: 'Agent unreachable',
      tone: 'critical',
    });
  });
  it('surfaces the failed module and its operator-facing reason', () => {
    const signals = fleetGovernanceSignalsForConnection(
      connectionFixture({
        agentModules: [
          {
            name: 'docker',
            enabled: true,
            state: 'retrying',
            lastError: 'cannot connect to /var/run/docker.sock',
            updatedAt: '2026-07-09T12:00:00Z',
          },
        ],
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'current',
          adapterHealth: 'degraded',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'current',
          remoteControl: 'disabled',
        },
      }),
    );

    expect(visibleFleetGovernanceSignals(signals)).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          key: 'module-health',
          label: 'Docker module retrying',
          detail: 'cannot connect to /var/run/docker.sock',
          tone: 'warning',
        }),
      ]),
    );
  });

  it('keeps updater failures and disabled auto-update actionable', () => {
    const failed = fleetGovernanceSignalsForConnection(
      connectionFixture({
        agentUpdate: {
          state: 'error',
          autoUpdate: true,
          lastError: 'signature verification failed',
        },
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'behind',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'failed',
          remoteControl: 'disabled',
        },
      }),
    );
    const disabled = fleetGovernanceSignalsForConnection(
      connectionFixture({
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'behind',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'disabled',
          remoteControl: 'disabled',
        },
      }),
    );

    expect(failed.find((signal) => signal.key === 'updates')).toMatchObject({
      label: 'Agent update failed',
      detail: 'signature verification failed',
      tone: 'critical',
    });
    expect(disabled.find((signal) => signal.key === 'updates')).toMatchObject({
      label: 'Auto-update off',
      tone: 'warning',
    });
  });

  it('hides passive agent config fingerprint handshakes while preserving raw fleet facts', () => {
    const rawSignals = fleetGovernanceSignalsForConnection(
      connectionFixture({
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'current',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'current',
          remoteControl: 'disabled',
          configDrift: {
            status: 'pending',
            reason:
              'Pulse has not received a comparable applied agent configuration fingerprint yet',
          },
          rollout: {
            status: 'pending',
            reason: 'waiting for the agent to report an applied configuration fingerprint',
          },
          credentialHealth: { status: 'verified', kind: 'agent-token' },
          commandPolicy: {
            status: 'disabled',
            desired: 'unknown',
            applied: 'disabled',
            enforcement: 'not-applicable',
          },
        },
      }),
    );

    expect(rawSignals.map((signal) => signal.label)).toEqual(
      expect.arrayContaining(['Config pending', 'Rollout pending', 'Remote control disabled']),
    );
    expect(visibleFleetGovernanceSignals(rawSignals).map((signal) => signal.label)).toEqual([]);
  });

  it('does not turn default unmanaged agent config into setup attention', () => {
    const rawSignals = fleetGovernanceSignalsForConnection(
      connectionFixture({
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'current',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'current',
          remoteControl: 'disabled',
          configDrift: {
            status: 'not-applicable',
            reason: 'no managed agent configuration override is assigned',
          },
          rollout: {
            status: 'current',
            stage: 'applied',
            reason: 'no managed agent configuration rollout is assigned',
          },
          credentialHealth: { status: 'verified', kind: 'agent-token' },
          commandPolicy: {
            status: 'disabled',
            desired: 'unknown',
            applied: 'disabled',
            enforcement: 'not-applicable',
          },
        },
      }),
    );

    expect(rawSignals.map((signal) => signal.label)).toEqual(
      expect.arrayContaining(['No config drift', 'Rollout current', 'Remote control disabled']),
    );
    expect(visibleFleetGovernanceSignals(rawSignals).map((signal) => signal.label)).toEqual([]);
  });

  it('keeps actionable config and rollout warnings visible', () => {
    const rawSignals = fleetGovernanceSignalsForConnection(
      connectionFixture({
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'current',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'current',
          remoteControl: 'disabled',
          configDrift: {
            status: 'pending',
            reason: 'Desired config v2 has been assigned and the agent has not applied it yet.',
          },
          rollout: {
            status: 'pending',
            reason: 'The staged rollout is waiting for the next canary batch.',
          },
          credentialHealth: { status: 'verified', kind: 'agent-token' },
          commandPolicy: {
            status: 'disabled',
            desired: 'disabled',
            applied: 'disabled',
            enforcement: 'in-sync',
          },
        },
      }),
    );

    expect(visibleFleetGovernanceSignals(rawSignals).map((signal) => signal.label)).toEqual([
      'Config pending',
      'Rollout pending',
    ]);
  });

  it('does not hide unrelated warning signals just because their detail mentions fingerprints', () => {
    const signals: FleetGovernanceSignal[] = [
      {
        key: 'credentials',
        label: 'Credentials expiring',
        detail: 'Token rotation must refresh the credential fingerprint before expiry.',
        tone: 'warning',
      },
      {
        key: 'config-drift',
        label: 'Config pending',
        detail: 'Pulse has not received a comparable applied agent configuration fingerprint yet.',
        tone: 'warning',
      },
    ];

    expect(visibleFleetGovernanceSignals(signals).map((signal) => signal.label)).toEqual([
      'Credentials expiring',
    ]);
  });

  it('returns no chips when every warning was a passive handshake', () => {
    const signals: FleetGovernanceSignal[] = [
      {
        key: 'config-drift',
        label: 'Config pending',
        detail: 'Pulse has not received a comparable applied agent config fingerprint yet.',
        tone: 'warning',
      },
      {
        key: 'rollout',
        label: 'Rollout pending',
        detail: 'Waiting for the agent to report an applied configuration fingerprint.',
        tone: 'warning',
      },
    ];

    expect(visibleFleetGovernanceSignals(signals)).toEqual([]);
  });

  it('hides rollout fallback copy when a passive config confirmation marks the handshake', () => {
    const rawSignals = fleetGovernanceSignalsForConnection(
      connectionFixture({
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'current',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'current',
          remoteControl: 'disabled',
          configDrift: {
            status: 'pending',
            reason:
              'Pulse has not received a comparable applied agent configuration fingerprint yet',
          },
          rollout: {
            status: 'pending',
          },
          credentialHealth: { status: 'verified', kind: 'agent-token' },
          commandPolicy: {
            status: 'disabled',
            desired: 'unknown',
            applied: 'disabled',
            enforcement: 'not-applicable',
          },
        },
      }),
    );

    expect(visibleFleetGovernanceSignals(rawSignals).map((signal) => signal.label)).toEqual([]);
  });
});

describe('primaryRowProblem', () => {
  const outdatedFleet = (updateStatus: 'update-available' | 'failed' | 'disabled') =>
    connectionFixture({
      agentUpdate:
        updateStatus === 'failed'
          ? { state: 'error', autoUpdate: true, lastError: 'signature verification failed' }
          : undefined,
      fleet: {
        enrollmentState: 'enrolled',
        livenessState: 'active',
        versionDrift: 'behind',
        adapterHealth: 'healthy',
        configRollout: 'reported',
        credentialStatus: 'verified',
        updateStatus,
        remoteControl: 'disabled',
      },
    });

  it('leaves an outdated agent to its Agent update badge instead of naming a problem', () => {
    const highlights = visibleFleetGovernanceSignals(
      fleetGovernanceSignalsForConnection(outdatedFleet('update-available')),
    );
    // The Manage drawer keeps the structured signals.
    expect(highlights.map((signal) => signal.label)).toEqual([
      'Version behind',
      'Update available',
    ]);
    expect(primaryRowProblem(highlights)).toBeUndefined();
  });

  it("finds a failed update on a second attached agent behind another agent's version lag", () => {
    const signals = [
      ...fleetGovernanceSignalsForConnection(outdatedFleet('update-available')),
      ...fleetGovernanceSignalsForConnection({
        ...outdatedFleet('failed'),
        id: 'agent:host-2',
        name: 'host-2',
      }),
    ];
    expect(primaryRowProblem(visibleFleetGovernanceSignals(signals, Infinity))).toMatchObject({
      label: 'Agent update failed',
      tone: 'critical',
    });
    // The capped highlight list leads with the problem, not the maintenance.
    expect(visibleFleetGovernanceSignals(signals)[0]?.label).toBe('Agent update failed');
  });

  it('still names a failed update or disabled auto-update, which need a different action', () => {
    expect(
      primaryRowProblem(
        visibleFleetGovernanceSignals(fleetGovernanceSignalsForConnection(outdatedFleet('failed'))),
      ),
    ).toMatchObject({ label: 'Agent update failed', tone: 'critical' });
    expect(
      primaryRowProblem(
        visibleFleetGovernanceSignals(
          fleetGovernanceSignalsForConnection(outdatedFleet('disabled')),
        ),
      ),
    ).toMatchObject({ label: 'Auto-update off', tone: 'warning' });
  });
});

describe('fleetGovernanceSignalsForConnection connection-type gating', () => {
  it('omits agent-fleet governance signals for pull-based API sources', () => {
    const signals = fleetGovernanceSignalsForConnection(
      connectionFixture({
        id: 'pbs:pbs-docker',
        type: 'pbs',
        name: 'pbs-docker',
        state: 'unreachable',
        source: 'manual',
        fleet: {
          enrollmentState: 'configured',
          livenessState: 'unreachable',
          versionDrift: 'unknown',
          adapterHealth: 'blocked',
          configRollout: 'unknown',
          credentialStatus: 'unknown',
          updateStatus: 'unknown',
          remoteControl: 'not-applicable',
          rollout: { status: 'blocked', reason: 'blocked by the current connection state' },
        },
      }),
    );
    const keys = signals.map((signal) => signal.key);
    // The backend echoed a blocked rollout, but an API source has no agent, so
    // none of the agent-binary / managed-config governance signals must surface
    // (this is what produced the misleading "Rollout blocked" problem line).
    expect(signals.map((signal) => signal.label)).not.toContain('Rollout blocked');
    expect(keys).not.toContain('rollout');
    expect(keys).not.toContain('config-drift');
    expect(keys).not.toContain('version');
    expect(keys).not.toContain('updates');
    expect(keys).not.toContain('command-policy');
    // Source-agnostic signals still apply to API sources.
    expect(keys).toContain('enrollment');
    expect(keys).toContain('liveness');
    expect(keys).toContain('credential-health');
  });

  it('keeps agent-fleet governance signals for agent connections', () => {
    const keys = fleetGovernanceSignalsForConnection(
      connectionFixture({
        fleet: {
          enrollmentState: 'enrolled',
          livenessState: 'active',
          versionDrift: 'behind',
          adapterHealth: 'healthy',
          configRollout: 'reported',
          credentialStatus: 'verified',
          updateStatus: 'update-available',
          remoteControl: 'disabled',
          rollout: { status: 'blocked', reason: 'staged rollout halted' },
        },
      }),
    ).map((signal) => signal.key);
    expect(keys).toContain('rollout');
    expect(keys).toContain('version');
    expect(keys).toContain('command-policy');
  });
});

describe('connectionAgentIdentitySummary', () => {
  const withAgentIdentity = (
    agentIdentity: Partial<ConnectionAgentIdentity> | undefined,
  ): Connection =>
    connectionFixture({ agentIdentity: agentIdentity as ConnectionAgentIdentity | undefined });

  it('uses osName platform identity over the broader agent platform family', () => {
    const summary = connectionAgentIdentitySummary(
      withAgentIdentity({
        platform: 'debian',
        osName: 'Proxmox VE',
        osVersion: '9.1.9',
      }),
    );
    expect(summary).toBe('Proxmox VE 9.1.9');
  });

  it('uses Unraid osName over Linux platform family', () => {
    const summary = connectionAgentIdentitySummary(
      withAgentIdentity({
        platform: 'linux',
        osName: 'Unraid',
        osVersion: '7.2.2',
      }),
    );
    expect(summary).toBe('Unraid 7.2.2');
  });

  it('falls back to prettified platform when osName is missing', () => {
    const summary = connectionAgentIdentitySummary(
      withAgentIdentity({
        platform: 'linux',
        osVersion: '6.1.0',
      }),
    );
    expect(summary).toBe('Linux 6.1.0');
  });

  it('returns null when no agent identity is present', () => {
    const summary = connectionAgentIdentitySummary(withAgentIdentity(undefined));
    expect(summary).toBeNull();
  });
});