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.

9.8 KB
Raw Blame
import { cleanup, fireEvent, render, screen, within } from '@solidjs/testing-library';
import type { JSX } from 'solid-js';
import { Route, Router } from '@solidjs/router';
import { afterEach, describe, expect, it } from 'vitest';

import { VsphereActivityTable } from '@/features/vmware/VsphereActivityTable';
import { buildVmwareActivityRows, filterVmwareActivity } from '@/features/vmware/vmwarePageModel';
import type { Resource } from '@/types/resource';

// Record drawers link to the object they are about, which needs a router.
const renderInRouter = (component: () => JSX.Element) =>
  render(() => (
    <Router>
      <Route path="/*" component={component} />
    </Router>
  ));

const makeVm = (overrides: Partial<Resource> = {}): Resource =>
  ({
    id: 'vm-201',
    type: 'vm',
    name: 'warehouse-api-01',
    displayName: 'warehouse-api-01',
    status: 'online',
    platformType: 'vmware-vsphere',
    platformScopes: ['vmware-vsphere'],
    sourceType: 'api',
    vmware: {
      entityType: 'vm',
      managedObjectId: 'vm-201',
      connectionName: 'lab-vcenter',
      vcenterHost: 'vcsa.lab.local',
      datacenterName: 'Primary DC',
      clusterName: 'Production Cluster',
    },
    recentChanges: [
      {
        id: 'activity-task-reconfigure',
        observedAt: '2026-05-21T10:15:00Z',
        occurredAt: '2026-05-21T10:15:00Z',
        resourceId: 'vm-201',
        kind: 'activity',
        sourceType: 'platform_event',
        sourceAdapter: 'vmware_adapter',
        confidence: 'high',
        reason: 'Reconfigure virtual machine (error)',
        metadata: {
          activity_type: 'vmware_task',
          activity_native_id: 'task-901',
          activity_title: 'Reconfigure virtual machine',
          activity_state: 'error',
          activity_message: 'Permission denied while reconfiguring VM',
          vmwareTaskDescription: 'Reconfigure virtual machine CPU reservation',
          vmwareManagedObjectId: 'vm-201',
          vmwareEntityType: 'vm',
        },
      },
      {
        id: 'activity-event-powered-on',
        observedAt: '2026-05-21T10:05:00Z',
        occurredAt: '2026-05-21T10:05:00Z',
        resourceId: 'vm-201',
        kind: 'activity',
        sourceType: 'platform_event',
        sourceAdapter: 'vmware_adapter',
        confidence: 'high',
        actor: '[email protected]',
        reason: 'Virtual machine warehouse-api-01 was powered on',
        metadata: {
          activity_type: 'vmware_event',
          activity_native_id: 'event-501',
          // The provider titles an event with vCenter's message, keeping the
          // event class in vmwareEventType.
          activity_title: 'Virtual machine warehouse-api-01 was powered on',
          activity_message: 'Virtual machine warehouse-api-01 was powered on',
          vmwareEventType: 'VmPoweredOnEvent',
          vmwareEventMessage: 'Virtual machine warehouse-api-01 was powered on',
          vmwareEventUser: '[email protected]',
          vmwareManagedObjectId: 'vm-201',
          vmwareEntityType: 'vm',
        },
      },
    ],
    ...overrides,
  }) as Resource;

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

describe('VsphereActivityTable', () => {
  it('renders native vSphere activity rows with inline details', async () => {
    const activity = buildVmwareActivityRows([makeVm()]);

    renderInRouter(() => (
      <VsphereActivityTable
        activity={activity}
        emptyIcon={<span />}
        emptyTitle="No activity"
        emptyDescription="No activity"
        showToolbar={false}
      />
    ));

    const table = screen.getByRole('table');
    expect(within(table).getByText('Resource')).toBeInTheDocument();
    expect(within(table).getByText('Type')).toBeInTheDocument();
    expect(within(table).getByText('Activity')).toBeInTheDocument();
    expect(within(table).getByText('State')).toBeInTheDocument();
    expect(within(table).getByText('Actor')).toBeInTheDocument();
    expect(within(table).getByText('vCenter')).toBeInTheDocument();
    expect(screen.getAllByText('warehouse-api-01')).toHaveLength(2);
    expect(screen.getByText('Reconfigure virtual machine')).toBeInTheDocument();
    expect(
      screen.getByTitle('Reconfigure virtual machine · Permission denied while reconfiguring VM'),
    ).toBeInTheDocument();
    // The State column and the phone words beside the name both read "Error",
    // in the red its dot shows.
    const state = document.querySelector('[data-vsphere-activity-state="danger"]');
    expect(state).toHaveTextContent('Error');
    expect(state).toHaveClass('text-red-600');
    const phoneError = document.querySelector('[data-vsphere-activity-state-phone="danger"]');
    expect(phoneError).toHaveClass('platform-table-phone-only-inline', 'text-red-600');
    expect(phoneError).toHaveClass('min-w-0', 'truncate');
    expect(phoneError?.parentElement).toHaveClass('items-center');
    // A state that needs no attention reads in its dot alone on a phone.
    for (const phoneState of document.querySelectorAll(
      '[data-vsphere-activity-state-phone]:not([data-vsphere-activity-state-phone="danger"]):not([data-vsphere-activity-state-phone="warning"])',
    )) {
      expect(phoneState).toHaveClass('sr-only');
    }
    expect(screen.getByText('Virtual machine warehouse-api-01 was powered on')).toBeInTheDocument();
    expect(screen.queryByText('VmPoweredOnEvent')).not.toBeInTheDocument();
    expect(screen.getByText('[email protected]')).toBeInTheDocument();

    const row = screen.getByText('Reconfigure virtual machine').closest('tr');
    expect(row).not.toHaveAttribute('aria-expanded');
    expect(row?.querySelector('[data-row-action="true"]')).toHaveAttribute(
      'aria-expanded',
      'false',
    );

    await fireEvent.click(row!);

    expect(row).not.toHaveAttribute('aria-expanded');
    expect(row?.querySelector('[data-row-action="true"]')).toHaveAttribute('aria-expanded', 'true');
    const detail = within(screen.getByTestId('vsphere-activity-detail'));
    expect(detail.getByRole('heading', { level: 2 })).toHaveTextContent('warehouse-api-01');
    expect(detail.getByText('Managed object')).toBeInTheDocument();
    expect(detail.getAllByText('vm-201').length).toBeGreaterThan(0);
    expect(detail.getByText('Native ID')).toBeInTheDocument();
    expect(detail.getByText('task-901')).toBeInTheDocument();
    expect(detail.getByText('Description')).toBeInTheDocument();
    expect(detail.getByText('Reconfigure virtual machine CPU reservation')).toBeInTheDocument();
    // The activity links to the VM it happened to, opening that VM's drawer.
    expect(detail.getByRole('link', { name: 'Open warehouse-api-01' })).toHaveAttribute(
      'href',
      '/vmware/overview?resource=vm-201',
    );

    await fireEvent.click(detail.getByRole('button', { name: /^Collapse .* details$/ }));

    expect(screen.queryByTestId('vsphere-activity-detail')).not.toBeInTheDocument();
    expect(row).not.toHaveAttribute('aria-expanded');
    expect(row?.querySelector('[data-row-action="true"]')).toHaveAttribute(
      'aria-expanded',
      'false',
    );
  });
  it("reads an event as vCenter's message and its time as an age", async () => {
    const vm = makeVm();
    const unset = {
      ...vm.recentChanges![1],
      id: 'activity-event-unset-time',
      observedAt: '1970-01-01T00:00:00Z',
      occurredAt: '1970-01-01T00:00:00Z',
      reason: 'Alarm status changed on warehouse-api-01',
      metadata: {
        ...vm.recentChanges![1].metadata,
        activity_native_id: 'event-502',
        // Stored before the provider titled events with vCenter's message.
        activity_title: 'AlarmStatusChangedEvent',
        activity_message: 'Alarm status changed on warehouse-api-01',
        vmwareEventType: 'AlarmStatusChangedEvent',
        vmwareEventMessage: 'Alarm status changed on warehouse-api-01',
      },
    };
    const activity = buildVmwareActivityRows([
      makeVm({ recentChanges: [...vm.recentChanges!, unset] }),
    ]);

    renderInRouter(() => (
      <VsphereActivityTable
        activity={activity}
        emptyIcon={<span />}
        emptyTitle="No activity"
        emptyDescription="No activity"
        showToolbar={false}
      />
    ));

    // Searching by the event class still finds the row it no longer titles.
    expect(
      filterVmwareActivity(activity, 'VmPoweredOnEvent', 'all').map((row) => row.nativeId),
    ).toEqual(['event-501']);

    const powered = screen.getByText('Virtual machine warehouse-api-01 was powered on');
    // The hover title does not repeat the message the row already shows.
    expect(powered).toHaveAttribute(
      'title',
      'Virtual machine warehouse-api-01 was powered on · event-501',
    );
    const poweredRow = powered.closest('tr')!;
    const when = poweredRow.querySelector('td:last-child span[title]');
    expect(when?.textContent).toMatch(/ago$/);
    expect(when?.getAttribute('title')).toMatch(/2026/);

    const unsetRow = screen.getByText('Alarm status changed on warehouse-api-01').closest('tr')!;
    expect(unsetRow.querySelector('td:last-child')?.textContent).toBe('-');

    await fireEvent.click(poweredRow);
    const detail = within(screen.getByTestId('vsphere-activity-detail'));
    expect(detail.getByText('Event type')).toBeInTheDocument();
    expect(detail.getByText('VmPoweredOnEvent')).toBeInTheDocument();
    expect(detail.queryByText('Message')).not.toBeInTheDocument();
  });
  it('drops the vCenter column when one vCenter is connected', () => {
    renderInRouter(() => (
      <VsphereActivityTable
        activity={buildVmwareActivityRows([makeVm()])}
        emptyIcon={<span />}
        emptyTitle="No activity"
        emptyDescription="No activity"
        showToolbar={false}
        singleVcenter
      />
    ));
    expect(
      within(screen.getByRole('table')).queryByRole('columnheader', { name: /vCenter/ }),
    ).not.toBeInTheDocument();
    expect(screen.queryByText('lab-vcenter')).not.toBeInTheDocument();
  });
});