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.

7.6 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor } from '@solidjs/testing-library';
import { afterEach, describe, expect, it, vi } from 'vitest';

vi.mock('@/api/guestMetadata', () => ({
  GuestMetadataAPI: {
    getMetadata: vi.fn(async () => ({ id: 'guest-1', customUrl: '' })),
    updateMetadata: vi.fn(async () => ({ id: 'guest-1', customUrl: '' })),
  },
}));

vi.mock('@/api/agentMetadata', () => ({
  AgentMetadataAPI: {
    getMetadata: vi.fn(async () => ({ id: 'host-1', customUrl: '' })),
    updateMetadata: vi.fn(async () => ({ id: 'host-1', customUrl: '' })),
  },
}));

vi.mock('@/api/dockerMetadata', () => ({
  DockerMetadataAPI: {
    getMetadata: vi.fn(async () => ({ id: 'docker-host-1:container:container-1', customUrl: '' })),
    updateMetadata: vi.fn(async () => ({
      id: 'docker-host-1:container:container-1',
      customUrl: '',
    })),
  },
}));

vi.mock('@/api/dockerHostMetadata', () => ({
  DockerHostMetadataAPI: {
    getMetadata: vi.fn(async () => ({ customUrl: '' })),
    updateMetadata: vi.fn(async () => ({ customUrl: '' })),
  },
}));

vi.mock('@/utils/clipboard', () => ({
  copyToClipboard: vi.fn(async () => true),
}));

import { AgentMetadataAPI } from '@/api/agentMetadata';
import { DockerMetadataAPI } from '@/api/dockerMetadata';
import { DockerHostMetadataAPI } from '@/api/dockerHostMetadata';
import { WebInterfaceUrlField } from '@/components/shared/WebInterfaceUrlField';
import { RESOURCE_METADATA_CHANGED_EVENT } from '@/utils/resourceMetadataEvents';
import { syncSessionCapabilities } from '@/stores/sessionCapabilities';

describe('WebInterfaceUrlField', () => {
  afterEach(() => {
    cleanup();
    vi.clearAllMocks();
    syncSessionCapabilities(null);
  });

  it('renders URL controls for a metadata target', async () => {
    render(() => (
      <WebInterfaceUrlField metadataKind="agent" metadataId="host-1" targetLabel="agent" />
    ));

    expect(await screen.findByText('Web Interface URL')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Save' })).toBeInTheDocument();
  });

  it('shows the saved URL without edit controls to sessions that cannot write metadata', async () => {
    syncSessionCapabilities({ tokenScopes: ['monitoring:read'] });
    render(() => (
      <WebInterfaceUrlField
        metadataKind="guest"
        metadataId="guest-1"
        targetLabel="workload"
        customUrl="https://grafana.example"
      />
    ));

    expect(await screen.findByText('https://grafana.example')).toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Open saved web interface URL' })).toHaveAttribute(
      'href',
      'https://grafana.example',
    );
    expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Save' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Remove' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Use (this|instead)/ })).not.toBeInTheDocument();
    expect(
      screen.getByText('This session can open saved web interface URLs but not change them.'),
    ).toBeInTheDocument();

    syncSessionCapabilities({ tokenScopes: ['monitoring:read', 'monitoring:write'] });
    expect(await screen.findByRole('button', { name: 'Save' })).toBeInTheDocument();
  });

  it('supports embedded rendering with a custom title', async () => {
    const { container } = render(() => (
      <WebInterfaceUrlField
        metadataKind="agent"
        metadataId="host-1"
        targetLabel="agent"
        title="Web interface"
        embedded
      />
    ));

    expect(await screen.findByText('Web interface')).toBeInTheDocument();
    expect(container.querySelector('.shadow-xs')).toBeNull();
  });

  it('saves a host URL through metadata API', async () => {
    render(() => (
      <WebInterfaceUrlField
        metadataKind="agent"
        metadataId="host-1"
        targetLabel="agent"
        customUrl=""
      />
    ));

    const input = await screen.findByPlaceholderText('https://198.51.100.100:8080');
    fireEvent.input(input, { target: { value: 'https://pve1.local:8006' } });
    fireEvent.click(screen.getByRole('button', { name: 'Save' }));

    await waitFor(() => {
      expect(AgentMetadataAPI.updateMetadata).toHaveBeenCalledWith('host-1', {
        customUrl: 'https://pve1.local:8006',
      });
    });
  });

  it('removes a saved URL through the same canonical metadata record', async () => {
    render(() => (
      <WebInterfaceUrlField
        metadataKind="agent"
        metadataId="host-1"
        targetLabel="agent"
        customUrl="https://pve1.local:8006"
      />
    ));

    fireEvent.click(await screen.findByRole('button', { name: 'Remove' }));

    await waitFor(() => {
      expect(AgentMetadataAPI.updateMetadata).toHaveBeenCalledWith('host-1', {
        customUrl: '',
      });
      expect(screen.queryByRole('button', { name: 'Remove' })).toBeNull();
    });
  });

  it('saves a Docker container URL through Docker metadata API', async () => {
    render(() => (
      <WebInterfaceUrlField
        metadataKind="docker"
        metadataId="docker-host-1:container:container-1"
        targetLabel="container"
        customUrl=""
      />
    ));

    const input = await screen.findByPlaceholderText('https://198.51.100.100:8080');
    fireEvent.input(input, { target: { value: 'https://app.internal:9443' } });
    fireEvent.click(screen.getByRole('button', { name: 'Save' }));

    await waitFor(() => {
      expect(DockerMetadataAPI.updateMetadata).toHaveBeenCalledWith(
        'docker-host-1:container:container-1',
        {
          customUrl: 'https://app.internal:9443',
        },
      );
    });
  });

  it('saves a Docker or Podman host URL through runtime metadata', async () => {
    render(() => (
      <WebInterfaceUrlField
        metadataKind="docker-host"
        metadataId="runtime-stable"
        targetLabel="host"
        customUrl=""
      />
    ));

    const input = await screen.findByPlaceholderText('https://198.51.100.100:8080');
    fireEvent.input(input, { target: { value: 'https://portainer.internal:9443' } });
    fireEvent.click(screen.getByRole('button', { name: 'Save' }));

    await waitFor(() => {
      expect(DockerHostMetadataAPI.updateMetadata).toHaveBeenCalledWith('runtime-stable', {
        customUrl: 'https://portainer.internal:9443',
      });
    });
  });

  it('broadcasts saved URL changes for same-tab metadata consumers', async () => {
    const handler = vi.fn();
    window.addEventListener(RESOURCE_METADATA_CHANGED_EVENT, handler);

    try {
      render(() => (
        <WebInterfaceUrlField
          metadataKind="agent"
          metadataId="host-1"
          targetLabel="agent"
          customUrl=""
        />
      ));

      const input = await screen.findByPlaceholderText('https://198.51.100.100:8080');
      fireEvent.input(input, { target: { value: 'https://pve1.local:8006' } });
      fireEvent.click(screen.getByRole('button', { name: 'Save' }));

      await waitFor(() => {
        expect(handler).toHaveBeenCalled();
      });
      const event = handler.mock.calls[0][0] as CustomEvent;
      expect(event.detail).toEqual({
        metadataKind: 'agent',
        metadataId: 'host-1',
        customUrl: 'https://pve1.local:8006',
      });
    } finally {
      window.removeEventListener(RESOURCE_METADATA_CHANGED_EVENT, handler);
    }
  });

  it('defaults guest metadata labels to workload wording', async () => {
    render(() => <WebInterfaceUrlField metadataKind="guest" metadataId="guest-1" customUrl="" />);

    expect(
      await screen.findByText(
        "Add a URL to quickly access this workload's web interface from Pulse.",
      ),
    ).toHaveClass('whitespace-normal');
  });
});