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.

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

import type { Resource } from '@/types/resource';
import { KubernetesEventsTable } from '../KubernetesEventsTable';

const makeResource = ({
  id,
  type,
  ...overrides
}: Partial<Resource> & Pick<Resource, 'id' | 'type'>): Resource => ({
  id,
  name: id,
  displayName: id,
  platformId: 'cluster-1',
  platformType: 'kubernetes',
  sourceType: 'agent',
  sources: ['kubernetes'],
  status: 'degraded',
  type,
  lastSeen: 1_700_000_000_000,
  ...overrides,
});

afterEach(() => {
  cleanup();
  vi.useRealTimers();
});

describe('KubernetesEventsTable', () => {
  it('renders Kubernetes Event fields with severity, newest-first ordering, and message context', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-05-24T15:00:00Z'));

    render(() => (
      <KubernetesEventsTable
        resources={[
          makeResource({
            id: 'normal-event',
            type: 'k8s-event',
            status: 'degraded',
            kubernetes: {
              clusterName: 'prod',
              namespace: 'apps',
              resourceKind: 'Event',
              eventType: 'Normal',
              reason: 'Scheduled',
              involvedKind: 'Pod',
              involvedName: 'checkout-web-456',
              count: 1,
              eventTime: '2026-05-24T12:00:00Z',
              message: 'Successfully assigned apps/checkout-web-456 to prod-k8s-01',
            },
          }),
          makeResource({
            id: 'warning-event',
            type: 'k8s-event',
            status: 'online',
            kubernetes: {
              clusterName: 'prod',
              namespace: 'apps',
              resourceKind: 'Event',
              eventType: 'Warning',
              reason: 'FailedScheduling',
              involvedKind: 'Pod',
              involvedName: 'checkout-api-123',
              count: 3,
              eventTime: '2026-05-24T13:00:00Z',
              message: '0/3 nodes are available',
            },
          }),
        ]}
        emptyIcon={<span />}
        emptyTitle="No events"
        emptyDescription="No events"
        showToolbar={false}
      />
    ));

    expect(screen.getByText('Event')).toBeInTheDocument();
    expect(screen.getByText('Type')).toBeInTheDocument();
    expect(screen.getByText('Reason')).toBeInTheDocument();
    expect(screen.getByText('Object')).toBeInTheDocument();
    expect(screen.getByText('Observed')).toBeInTheDocument();
    expect(screen.getByTitle('Warning')).toHaveClass('bg-amber-500');
    expect(screen.getByTitle('Normal')).toHaveClass('bg-slate-400');
    expect(screen.getByText('Warning')).toBeInTheDocument();
    expect(screen.getByText('FailedScheduling')).toBeInTheDocument();
    expect(screen.getByText('Pod/checkout-api-123')).toBeInTheDocument();
    expect(screen.getByText('3')).toBeInTheDocument();
    // The cell humanizes the observed time; the raw timestamp stays on the
    // hover title.
    expect(screen.getByText('2h ago')).toBeInTheDocument();
    expect(screen.getByTitle('2026-05-24T13:00:00Z')).toBeInTheDocument();
    expect(screen.getByText('0/3 nodes are available')).toBeInTheDocument();

    expect(
      Array.from(document.querySelectorAll('[data-kubernetes-event-row]')).map((row) =>
        row.getAttribute('data-kubernetes-event-row'),
      ),
    ).toEqual(['warning-event', 'normal-event']);
  });
  it('opens an event as a record about its involved object', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-05-24T15:00:00Z'));

    render(() => (
      <Router>
        <Route
          path="/"
          component={() => (
            <KubernetesEventsTable
              resources={[
                makeResource({
                  id: 'warning-event',
                  name: 'checkout-api-123.17f',
                  displayName: 'checkout-api-123.17f',
                  type: 'k8s-event',
                  kubernetes: {
                    clusterName: 'prod',
                    namespace: 'apps',
                    resourceKind: 'Event',
                    eventType: 'Warning',
                    reason: 'FailedScheduling',
                    involvedKind: 'Pod',
                    involvedName: 'checkout-api-123',
                    count: 3,
                    firstSeen: '2026-05-24T12:00:00Z',
                    createdAt: '2026-05-24T13:00:00Z',
                    message: '0/3 nodes are available',
                  },
                }),
              ]}
              emptyIcon={<span />}
              emptyTitle="No events"
              emptyDescription="No events"
              showToolbar={false}
            />
          )}
        />
      </Router>
    ));

    const disclosure = screen.getByRole('button', {
      name: 'Expand details for checkout-api-123.17f',
    });
    fireEvent.click(disclosure);

    const panel = document.querySelector<HTMLElement>('[data-testid="kubernetes-event-detail"]')!;
    const detail = within(panel);
    expect(disclosure).toHaveAttribute('aria-controls', 'kubernetes-event-drawer-warning-event');
    expect(detail.getByRole('heading', { name: 'Pod/checkout-api-123' })).toBeInTheDocument();
    expect(detail.getByLabelText('Warning')).toBeInTheDocument();
    expect(detail.getByText('0/3 nodes are available')).toBeInTheDocument();
    expect(detail.getByText('FailedScheduling')).toBeInTheDocument();
    expect(detail.getByText('3 times')).toBeInTheDocument();
    expect(detail.getByText('First seen')).toBeInTheDocument();
    expect(detail.getAllByTitle('2026-05-24T13:00:00Z')[0]).toHaveTextContent(/\(2h ago\)$/);
    expect(detail.getByRole('link', { name: 'Open checkout-api-123' })).toHaveAttribute(
      'href',
      '/kubernetes/workloads?q=checkout-api-123',
    );
    // An event is a record, not a monitored object: no Manage tab or
    // maintenance window for something Kubernetes discards within the hour.
    expect(detail.queryByRole('tab', { name: 'Manage' })).toBeNull();

    fireEvent.click(detail.getByRole('button', { name: 'Collapse Pod/checkout-api-123 details' }));
    expect(document.querySelector('[data-testid="kubernetes-event-detail"]')).toBeNull();
  });
});