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.

17.2 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import {
  buildTrueNASDetailSections,
  buildTrueNASDetailsSummary,
} from '@/components/Infrastructure/resourceDetailDrawerTrueNASModel';
import { isDrawerContentCard } from '@/components/Infrastructure/resourceDetailDrawerOverviewModel';
import type { Resource } from '@/types/resource';

const baseResource = (overrides: Partial<Resource>): Resource =>
  ({
    id: 'truenas-resource',
    type: 'vm',
    name: 'truenas-resource',
    displayName: 'TrueNAS resource',
    platformId: 'truenas-main',
    platformType: 'truenas',
    sourceType: 'api',
    status: 'online',
    ...overrides,
  }) as Resource;

describe('resourceDetailDrawerTrueNASModel', () => {
  it('summarizes native TrueNAS system metadata for the detail drawer', () => {
    const resource = baseResource({
      type: 'agent',
      displayName: 'truenas-main',
      uptime: 42 * 86_400,
      truenas: {
        hostname: 'truenas-main',
        version: 'TrueNAS-SCALE-24.10.2',
        storageRisk: { level: 'warning' },
        storageRiskSummary: 'ZFS pool archive is DEGRADED',
        storagePostureSummary: 'One pool needs attention',
        protectionReduced: true,
        protectionSummary: 'Snapshots are current but replication is degraded',
        services: [
          { id: '1', service: 'smb', enabled: true, state: 'RUNNING', pids: [2418, 2420] },
          { id: '2', service: 'nfs', enabled: true, state: 'RUNNING', pids: [2501] },
          { id: '3', service: 'ssh', enabled: false, state: 'STOPPED' },
          { id: '4', service: 'smartd', enabled: true, state: 'STOPPED' },
        ],
      },
    });

    expect(buildTrueNASDetailsSummary(resource)).toBe(
      'TrueNAS-SCALE-24.10.2, 42d, 4 services, ZFS pool archive is DEGRADED',
    );
    expect(buildTrueNASDetailSections(resource).map((section) => section.label)).toEqual([
      'System',
      'Storage Health',
      'Services',
    ]);
    expect(buildTrueNASDetailSections(resource)[2]?.rows).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ label: 'Running', value: '2 services' }),
        expect.objectContaining({ label: 'Disabled', value: '1 service' }),
        expect.objectContaining({ label: 'Names', value: 'SMB, NFS, SSH, SMART' }),
      ]),
    );
  });

  it('summarizes native vm.query metadata for the detail drawer', () => {
    const resource = baseResource({
      type: 'vm',
      truenas: {
        vm: {
          name: 'ubuntu-build',
          state: 'RUNNING',
          vcpus: 4,
          memoryBytes: 8 * 1024 ** 3,
          deviceCount: 3,
          diskCount: 1,
          nicCount: 1,
          bootloader: 'UEFI',
          secureBoot: true,
          trustedPlatformModule: true,
        },
      },
    });

    expect(buildTrueNASDetailsSummary(resource)).toBe('Running, 4 vCPU, 8.00 GB, 3 devices');
    expect(buildTrueNASDetailSections(resource).map((section) => section.label)).toEqual([
      'Compute',
      'Runtime',
      'Devices',
      'Flags',
    ]);
  });

  it('says a stopped VM set to autostart should be running', () => {
    const stoppedOnPurpose = baseResource({
      type: 'vm',
      truenas: { vm: { name: 'ubuntu-build', state: 'STOPPED', vcpus: 2, autostart: false } },
    });
    const shouldRun = baseResource({
      type: 'vm',
      truenas: { vm: { name: 'router', state: 'STOPPED', vcpus: 2, autostart: true } },
    });

    expect(buildTrueNASDetailsSummary(stoppedOnPurpose)).toBe('Stopped, 2 vCPU');
    expect(buildTrueNASDetailsSummary(shouldRun)).toBe('Stopped, should be running, 2 vCPU');
    const compute = buildTrueNASDetailSections(shouldRun).find(
      (section) => section.label === 'Compute',
    );
    expect(compute?.rows).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ label: 'Condition', value: 'Should be running' }),
      ]),
    );
    const onPurposeCompute = buildTrueNASDetailSections(stoppedOnPurpose).find(
      (section) => section.label === 'Compute',
    );
    expect(onPurposeCompute?.rows.map((row) => row.label)).not.toContain('Condition');

    // The same rule as the VMs table: libvirt aliases, the resource status
    // when TrueNAS sent no state, and no claim while TrueNAS data is stale.
    const shutOff = baseResource({
      type: 'vm',
      truenas: { vm: { name: 'router', domainState: 'SHUTOFF', autostart: true } },
    });
    const statusOnly = baseResource({
      type: 'vm',
      status: 'offline',
      truenas: { vm: { name: 'router', autostart: true } },
    });
    const stale = baseResource({
      type: 'vm',
      truenas: { vm: { name: 'router', state: 'STOPPED', autostart: true } },
      platformData: { sourceStatus: { truenas: { status: 'stale' } } },
    });
    expect(buildTrueNASDetailsSummary(shutOff)).toContain('should be running');
    expect(buildTrueNASDetailsSummary(statusOnly)).toContain('should be running');
    expect(buildTrueNASDetailsSummary(stale)).not.toContain('should be running');
  });

  it('summarizes native app.query metadata for the detail drawer', () => {
    const resource = baseResource({
      type: 'app-container',
      truenas: {
        app: {
          name: 'nextcloud',
          state: 'RUNNING',
          humanVersion: '29.0.0',
          containerCount: 2,
          upgradeAvailable: true,
          imageUpdatesAvailable: false,
          usedHostIps: ['0.0.0.0'],
          usedPorts: [
            {
              containerPort: 443,
              protocol: 'tcp',
              hostPorts: [{ hostIp: '0.0.0.0', hostPort: 30443 }],
            },
            {
              containerPort: 80,
              protocol: 'tcp',
              hostPorts: [{ hostIp: '0.0.0.0', hostPort: 30080 }],
            },
          ],
          volumes: [{ source: '/mnt/tank/apps/nextcloud', destination: '/data' }],
          images: ['nextcloud:29'],
        },
      },
    });

    expect(buildTrueNASDetailsSummary(resource)).toBe('Running, 2 containers, 2 ports, 1 update');
    expect(buildTrueNASDetailSections(resource).map((section) => section.label)).toEqual([
      'App',
      'Networking',
      'Storage',
    ]);
  });

  it('summarizes native SMB and NFS share metadata for the detail drawer', () => {
    const resource = baseResource({
      type: 'network-share',
      truenas: {
        share: {
          name: 'Media',
          protocol: 'SMB',
          dataset: 'tank/media',
          path: '/mnt/tank/media',
          enabled: true,
          readOnly: false,
          browsable: true,
          accessBasedEnumeration: true,
          auditEnabled: true,
          aliases: ['media'],
        },
      },
    });

    expect(buildTrueNASDetailsSummary(resource)).toBe('SMB, Enabled, tank/media, Read/write');
    expect(buildTrueNASDetailSections(resource).map((section) => section.label)).toEqual([
      'Share',
      'Access',
      'Clients',
    ]);
  });

  it('summarizes native TrueNAS storage pool metadata for the detail drawer', () => {
    const resource = baseResource({
      type: 'storage',
      displayName: 'archive',
      status: 'degraded',
      platformScopes: ['truenas'],
      disk: {
        current: 35.2,
        used: 25.3 * 1024 ** 4,
        total: 72 * 1024 ** 4,
      },
      childCount: 4,
      storage: {
        type: 'zfs-pool',
        topology: 'pool',
        platform: 'truenas',
        protection: 'zfs',
        zfsPoolState: 'DEGRADED',
        risk: {
          level: 'warning',
          reasons: [
            {
              code: 'zfs_pool_state',
              severity: 'warning',
              summary: 'ZFS pool archive is DEGRADED',
            },
          ],
        },
        riskSummary: 'ZFS pool archive is DEGRADED',
        protectionReduced: true,
        protectionSummary: 'ZFS pool archive is DEGRADED',
      },
    });

    expect(buildTrueNASDetailsSummary(resource)).toBe(
      'Pool, DEGRADED, 25.3 TB / 72.0 TB, ZFS pool archive is DEGRADED',
    );
    expect(buildTrueNASDetailSections(resource).map((section) => section.label)).toEqual([
      'Storage',
      'Capacity',
      'Health',
    ]);
    expect(buildTrueNASDetailSections(resource)[0]?.rows).toEqual(
      expect.arrayContaining([expect.objectContaining({ label: 'Protection', value: 'ZFS' })]),
    );
  });

  it('renders actionable canonical and native ZFS evidence without inventing a failed disk', () => {
    const resource = baseResource({
      type: 'storage',
      displayName: 'tank',
      status: 'degraded',
      platformScopes: ['truenas'],
      storage: {
        type: 'zfs-pool',
        topology: 'pool',
        vdevLayout: 'mirror',
        platform: 'truenas',
        zfsPoolState: 'DEGRADED',
        poolHealth: {
          scope: 'pool',
          provider: 'truenas',
          nativeId: 'pool-guid',
          canonicalState: 'DEGRADED',
          nativeState: 'DEGRADED',
          severity: 'critical',
          summary: 'Pool tank is degraded with a missing mirror member',
          recommendation: 'Confirm the affected mirror member in TrueNAS before replacement.',
          source: 'pool.query',
          evidenceCodes: ['zfs_pool_state', 'zfs_device_missing'],
        },
        zfsPool: {
          name: 'tank',
          state: 'DEGRADED',
          status: 'Degraded',
          scan: 'RESILVER SCANNING',
          scanDetails: {
            function: 'RESILVER',
            state: 'SCANNING',
            percentage: 33.3,
          },
          readErrors: 1,
          writeErrors: 0,
          checksumErrors: 2,
          devices: [
            {
              name: 'missing',
              type: 'UNAVAIL_DISK',
              role: 'data',
              parent: 'mirror-0',
              path: '/dev/disk/by-partuuid/missing',
              state: 'UNAVAIL',
              readErrors: 1,
              writeErrors: 0,
              checksumErrors: 0,
              missing: true,
            },
          ],
        },
      },
    });

    const sections = buildTrueNASDetailSections(resource);
    const healthRows = sections.find((section) => section.label === 'Health')?.rows;
    // The pool's health is an Overview card: what is wrong and what to do.
    expect(healthRows).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ label: 'State', value: 'DEGRADED', tone: 'warning' }),
        expect.objectContaining({
          label: 'Scan / resilver',
          value: 'Resilver Scanning (33.3%)',
        }),
        expect.objectContaining({
          label: 'ZFS errors',
          value: 'Read 1 · Write 0 · Checksum 2',
        }),
        expect.objectContaining({
          label: 'Affected vdevs',
          value: '/dev/disk/by-partuuid/missing (Data / Unavail Disk): Missing · R 1 W 0 C 0',
        }),
        expect.objectContaining({
          label: 'Recommended',
          value: 'Confirm the affected mirror member in TrueNAS before replacement.',
        }),
      ]),
    );
    // The canonical state repeats the pool state, so it is not shown twice.
    expect(healthRows?.find((row) => row.label === 'Pool health')).toBeUndefined();
    // Evidence codes and native state stay under Details.
    expect(sections.find((section) => section.label === 'Health evidence')?.rows).toEqual([
      expect.objectContaining({ label: 'Native state', value: 'DEGRADED' }),
      expect.objectContaining({ label: 'Evidence', value: 'zfs_pool_state, zfs_device_missing' }),
      expect.objectContaining({ label: 'Evidence source', value: 'pool.query' }),
    ]);
  });

  it('summarizes native TrueNAS physical disk metadata for the detail drawer', () => {
    const resource = baseResource({
      type: 'physical_disk',
      displayName: 'sdc',
      platformScopes: ['truenas'],
      physicalDisk: {
        devPath: '/dev/sdc',
        model: 'WD Red Pro',
        serial: 'WD-WX12A3456',
        diskType: 'sata',
        sizeBytes: 24 * 1024 ** 4,
        health: 'DEGRADED',
        temperature: 39,
        rpm: 7200,
        smart: {
          powerOnHours: 10_240,
          reallocatedSectors: 4,
          pendingSectors: 1,
        },
      },
    });

    expect(buildTrueNASDetailsSummary(resource)).toBe('SATA, Degraded, 24.0 TB, 39°C');
    expect(buildTrueNASDetailSections(resource).map((section) => section.label)).toEqual([
      'Disk',
      'Health',
      'SMART',
    ]);
  });

  it('shows a retained disk temperature as last known, with its reason and no heat tone', () => {
    // A TrueNAS disk merged with a host agent's row keeps the agent's last
    // reading after the agent stops reporting. It is not current, so it must
    // not read as a disk running hot now, nor sit in the summary as a reading.
    const disk = (state: 'available' | 'unavailable') =>
      baseResource({
        type: 'physical_disk',
        displayName: 'sdc',
        platformScopes: ['truenas'],
        physicalDisk: {
          devPath: '/dev/sdc',
          diskType: 'sata',
          sizeBytes: 24 * 1024 ** 4,
          health: 'PASSED',
          temperature: 61,
          collection: {
            temperature: { state, source: 'agent', reason: 'host agent stopped reporting' },
          },
        },
      });
    const sataTrigger = () => ({ warning: 50, critical: 55 });
    const temperatureRow = (resource: Resource) =>
      buildTrueNASDetailSections(resource, sataTrigger)
        .find((section) => section.label === 'Health')
        ?.rows.find((row) => row.label === 'Temperature');

    expect(temperatureRow(disk('unavailable'))).toMatchObject({
      value: '61°C (last known)',
      tone: 'muted',
      title: 'Last known reading, not current: host agent stopped reporting',
    });
    expect(buildTrueNASDetailsSummary(disk('unavailable'))).toBe('SATA, Passed, 24.0 TB');

    expect(temperatureRow(disk('available'))).toMatchObject({ value: '61°C', tone: 'warning' });
    expect(temperatureRow(disk('available'))?.title).toBeUndefined();
    expect(buildTrueNASDetailsSummary(disk('available'))).toBe('SATA, Passed, 24.0 TB, 61°C');
  });

  it('shows the vdev layout as the storage kind while topology stays the pool discriminator', () => {
    // Regression guard for 599c8e634. `topology` is the cross-provider
    // discriminator ('pool' | 'dataset' | ...); the ZFS layout belongs in
    // `vdevLayout`. The drawer prefers the layout for display but must not
    // depend on it being in `topology`.
    const withLayout = baseResource({
      type: 'storage',
      displayName: 'tank',
      storage: {
        type: 'zfs-pool',
        topology: 'pool',
        vdevLayout: 'raidz2',
        platform: 'truenas',
      },
    } as Partial<Resource>);

    expect(buildTrueNASDetailsSummary(withLayout)).toContain('Raidz2');

    const withoutLayout = baseResource({
      type: 'storage',
      displayName: 'tank',
      storage: {
        type: 'zfs-pool',
        topology: 'pool',
        platform: 'truenas',
      },
    } as Partial<Resource>);

    expect(buildTrueNASDetailsSummary(withoutLayout)).toContain('Pool');
  });

  it('keeps pool health and disk health and SMART as Overview content cards', () => {
    const cards = (resource: Resource) =>
      buildTrueNASDetailSections(resource)
        .filter(isDrawerContentCard)
        .map((section) => section.label);
    const pool = baseResource({
      type: 'storage',
      platformScopes: ['truenas'],
      storage: { type: 'zfs-pool', topology: 'pool', platform: 'truenas', zfsPoolState: 'ONLINE' },
    });
    expect(cards(pool)).toEqual(['Health']);
    // A dataset has no pool health of its own: its state stays in Storage.
    const dataset = baseResource({
      type: 'storage',
      platformScopes: ['truenas'],
      storage: { type: 'zfs-dataset', topology: 'dataset', platform: 'truenas' },
    });
    expect(cards(dataset)).toEqual([]);
    expect(
      buildTrueNASDetailSections(dataset)
        .find((section) => section.label === 'Storage')
        ?.rows.map((row) => row.label),
    ).toContain('State');
    const disk = baseResource({
      type: 'physical_disk',
      platformScopes: ['truenas'],
      physicalDisk: { devPath: '/dev/sdc', health: 'PASSED', smart: { powerOnHours: 10 } },
    });
    expect(cards(disk)).toEqual(['Health', 'SMART']);
  });

  it('states one storage summary once when several fields repeat it', () => {
    const resource = baseResource({
      type: 'agent',
      truenas: {
        storageRisk: { level: 'warning' },
        storageRiskSummary: 'ZFS pool archive is DEGRADED',
        storagePostureSummary: 'ZFS pool archive is DEGRADED',
        protectionSummary: 'ZFS pool archive is DEGRADED',
      },
    });
    const health = buildTrueNASDetailSections(resource).find(
      (section) => section.label === 'Storage Health',
    );
    expect(
      health?.rows
        .filter((row) => row.value === 'ZFS pool archive is DEGRADED')
        .map((row) => row.label),
    ).toEqual(['Storage summary']);
  });

  it('wraps disk identifiers an operator copies', () => {
    const disk = baseResource({
      type: 'physical_disk',
      platformScopes: ['truenas'],
      physicalDisk: { devPath: '/dev/sdc', serial: 'WD-WX12A3456', wwn: '0x5000c500a1b2c3d4' },
    });
    const identity = buildTrueNASDetailSections(disk).find((section) => section.label === 'Disk');
    expect(identity?.rows.filter((row) => row.wrap).map((row) => row.label)).toEqual([
      'Device',
      'Serial',
      'WWN',
    ]);
  });
});