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.

8.7 KB
Raw Blame
import { cleanup, fireEvent, render, screen, within } from '@solidjs/testing-library';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { createSignal } from 'solid-js';

vi.mock('@/components/Workloads/StackedMemoryBar', () => ({
  StackedMemoryBar: () => <div data-testid="stacked-memory-bar" />,
}));

vi.mock('@/components/shared/responsive', () => ({
  ResponsiveMetricCell: () => <div data-testid="responsive-metric-cell" />,
}));

vi.mock('@/contexts/appRuntime', () => ({
  useWebSocket: () => ({ activeAlerts: {} as Record<string, never> }),
}));
vi.mock('@/stores/alertsActivation', () => ({
  useAlertsActivation: () => ({
    detectionEnabled: () => true,
    getMetricThresholds: () => ({ warning: 80, critical: 85 }),
  }),
}));

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

const makeHost = (overrides: Partial<Resource> & Pick<Resource, 'id'>): Resource =>
  ({
    type: 'agent',
    name: overrides.id,
    displayName: overrides.id,
    status: 'online',
    platformId: 'vmware-1',
    platformType: 'vmware-vsphere',
    platformScopes: ['vmware-vsphere'],
    sourceType: 'api',
    sources: ['vmware-vsphere'],
    lastSeen: 1_700_000_000_000,
    cpu: { current: 12 },
    memory: { current: 40 },
    agent: { osVersion: 'VMware ESXi 8.0.3' },
    vmware: {
      entityType: 'host',
      managedObjectId: overrides.id,
      datacenterName: 'Primary DC',
      clusterName: 'Production',
      powerState: 'poweredOn',
      vcenterHost: 'vcsa.lab.local',
    },
    ...overrides,
  }) as Resource;

afterEach(() => {
  cleanup();
});

describe('VsphereHostsTable', () => {
  it('hides the normal power column on phones while retaining VM count and uptime', () => {
    const { container } = render(() => (
      <VsphereHostsTable
        hosts={[makeHost({ id: 'host-1' })]}
        scope={[makeHost({ id: 'host-1' })]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
        showToolbar={false}
      />
    ));

    const headers = [...container.querySelectorAll('thead th')];
    expect(headers.find((header) => header.textContent?.includes('Host'))).toHaveClass(
      'platform-table-mobile-w-30',
    );
    expect(headers.find((header) => header.textContent?.includes('Power'))).toHaveClass(
      'hidden',
      'md:table-cell',
    );
    expect(headers.find((header) => header.textContent?.includes('Up'))).toHaveClass('w-[16%]');
  });

  it('shows only exceptional power states beside the host name on phones', () => {
    const poweredOn = makeHost({ id: 'host-on' });
    const poweredOff = makeHost({
      id: 'host-off',
      vmware: { ...makeHost({ id: 'host-off' }).vmware!, powerState: 'poweredOff' },
    });
    const suspended = makeHost({
      id: 'host-suspended',
      vmware: { ...makeHost({ id: 'host-suspended' }).vmware!, powerState: 'suspended' },
    });
    const unknown = makeHost({
      id: 'host-unknown',
      vmware: { ...makeHost({ id: 'host-unknown' }).vmware!, powerState: undefined },
    });

    const { container } = render(() => (
      <VsphereHostsTable
        hosts={[poweredOn, poweredOff, suspended, unknown]}
        scope={[poweredOn, poweredOff, suspended, unknown]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
        showToolbar={false}
      />
    ));

    expect(
      container.querySelector(
        '[data-vsphere-host-row="host-on"] [data-vsphere-host-power-exception]',
      ),
    ).toBeNull();
    expect(
      container.querySelector(
        '[data-vsphere-host-row="host-off"] [data-vsphere-host-power-exception="off"]',
      ),
    ).toHaveTextContent('Off');
    expect(
      container.querySelector(
        '[data-vsphere-host-row="host-suspended"] [data-vsphere-host-power-exception="suspended"]',
      ),
    ).toHaveTextContent('Suspended');
    expect(
      container.querySelector(
        '[data-vsphere-host-row="host-unknown"] [data-vsphere-host-power-exception="unknown"]',
      ),
    ).toHaveTextContent('Unknown');
  });

  it('treats an impaired vSphere source as degraded for the row indicator and health filter', async () => {
    const healthy = makeHost({ id: 'host-healthy', name: 'esxi-healthy.lab.local' });
    const impaired = makeHost({
      id: 'host-impaired',
      name: 'esxi-impaired.lab.local',
      platformData: {
        sourceStatus: {
          vmware: { status: 'offline' },
        },
      },
    });

    const { container } = render(() => (
      <VsphereHostsTable
        hosts={[healthy, impaired]}
        scope={[healthy, impaired]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
      />
    ));

    const impairedRow = container.querySelector('[data-vsphere-host-row="host-impaired"]');
    expect(impairedRow).not.toBeNull();
    expect(impairedRow?.querySelector('[title="degraded"]')).not.toBeNull();
    expect(container.querySelector('[data-vsphere-host-row="host-healthy"]')).not.toBeNull();

    await fireEvent.click(
      within(screen.getByRole('group', { name: 'Status' })).getByRole('button', {
        name: /^Degraded, \d+$/,
      }),
    );

    expect(container.querySelector('[data-vsphere-host-row="host-impaired"]')).not.toBeNull();
    expect(container.querySelector('[data-vsphere-host-row="host-healthy"]')).toBeNull();
    expect(screen.getByText('1 of 2 hosts')).toBeInTheDocument();
  });
  it('shows the vCenter column by connection name only when several vCenters are connected', () => {
    const lab = makeHost({ id: 'host-1' });
    lab.vmware = { ...lab.vmware!, connectionId: 'vc-lab', connectionName: 'Lab vCenter' };
    const edge = makeHost({ id: 'host-2' });
    edge.vmware = {
      ...edge.vmware!,
      connectionId: 'vc-edge',
      connectionName: 'Edge vCenter',
      vcenterHost: 'vcsa-edge.lab.local',
    };

    const single = render(() => (
      <VsphereHostsTable
        hosts={[lab]}
        scope={[lab]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
        showToolbar={false}
      />
    ));
    expect(screen.queryByRole('columnheader', { name: /vCenter/ })).not.toBeInTheDocument();
    single.unmount();

    render(() => (
      <VsphereHostsTable
        hosts={[lab, edge]}
        scope={[lab, edge]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
        showToolbar={false}
      />
    ));
    expect(screen.getByRole('columnheader', { name: /vCenter/ })).toBeInTheDocument();
    expect(screen.getByText('Edge vCenter')).toHaveAttribute('title', 'vcsa-edge.lab.local');
    expect(screen.getByText('Lab vCenter')).toBeInTheDocument();
  });

  it('narrows the VMs from a row click and keeps the drawer on the chevron', () => {
    const host = makeHost({ id: 'esxi-01' });
    const onShow = vi.fn();
    const [selectedId, setSelectedId] = createSignal<string | null>(null);
    const { container } = render(() => (
      <VsphereHostsTable
        hosts={[host]}
        scope={[host]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
        showToolbar={false}
        vmScope={{ sectionId: 'vsphere-vms-section', selectedHostId: selectedId, onShow }}
      />
    ));

    const row = container.querySelector('[data-vsphere-host-row="esxi-01"]')!;
    const chevron = row.querySelector('[data-row-action="true"]')!;
    expect(chevron).not.toHaveClass('sr-only');

    fireEvent.click(row);
    expect(onShow).toHaveBeenCalledWith(host);
    expect(chevron).toHaveAttribute('aria-expanded', 'false');

    const nameButton = screen.getByRole('button', { name: 'Show VMs on esxi-01' });
    expect(nameButton).toHaveAttribute('aria-controls', 'vsphere-vms-section');
    fireEvent.click(nameButton);
    expect(onShow).toHaveBeenCalledTimes(2);

    setSelectedId('esxi-01');
    expect(row).toHaveAttribute('data-summary-row-active', 'true');
    expect(nameButton).toHaveAttribute('aria-pressed', 'true');

    fireEvent.click(chevron);
    expect(chevron).toHaveAttribute('aria-expanded', 'true');
    expect(onShow).toHaveBeenCalledTimes(2);
  });

  it('opens the drawer from a row click where nothing is scoped', () => {
    const host = makeHost({ id: 'esxi-02' });
    const { container } = render(() => (
      <VsphereHostsTable
        hosts={[host]}
        scope={[host]}
        emptyIcon={<span />}
        emptyTitle="No hosts"
        emptyDescription="No hosts"
        showToolbar={false}
      />
    ));

    expect(screen.queryByRole('button', { name: /^Show VMs on/ })).toBeNull();
    const row = container.querySelector('[data-vsphere-host-row="esxi-02"]')!;
    fireEvent.click(row);
    expect(row.querySelector('[data-row-action="true"]')).toHaveAttribute('aria-expanded', 'true');
  });
});