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.

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

import { BackupStatusCell } from '../GuestRowCells';

const HOUR_MS = 60 * 60 * 1000;

const renderCell = (lastBackup: string | number | null, backupRunning?: boolean) => {
  const { container } = render(() => (
    <BackupStatusCell lastBackup={lastBackup} backupRunning={backupRunning} />
  ));
  const badge = container.querySelector('button[aria-label^="Backup status"]');
  if (!badge) throw new Error('backup badge not rendered');
  return badge as HTMLElement;
};

describe('BackupStatusCell', () => {
  afterEach(() => {
    cleanup();
  });

  // The healthy state is the overwhelming majority of rows. It carries the
  // verdict in the shield colour alone; the age would be repeated decoration.
  it('renders a bare green shield with no age text when the backup is fresh', () => {
    const badge = renderCell(Date.now() - 2 * HOUR_MS);

    expect(badge.textContent?.trim()).toBe('');
    expect(badge.className).toContain('text-green-600');
    expect(badge.className).not.toContain('rounded-full');
    expect(badge.className).not.toContain('border');
    expect(badge.querySelector('svg')).not.toBeNull();
  });

  it('keeps the age available to assistive tech even when it is not drawn', () => {
    const badge = renderCell(Date.now() - 2 * HOUR_MS);

    expect(badge.getAttribute('aria-label')).toMatch(/^Backup status: fresh, last backup /);
  });

  it('draws the age and pill chrome when the backup is stale', () => {
    const badge = renderCell(Date.now() - 48 * HOUR_MS);

    expect(badge.textContent?.trim()).not.toBe('');
    expect(badge.className).toContain('rounded-full');
    expect(badge.className).toContain('border-yellow-200');
  });

  it('keeps the age and amber pill chrome when the backup is overdue', () => {
    const badge = renderCell(Date.now() - 200 * HOUR_MS);

    expect(badge.textContent?.trim()).not.toBe('');
    expect(badge.className).toContain('rounded-full');
    expect(badge.className).toContain('border-yellow-200');
    expect(badge.className).not.toContain('border-red-200');
  });

  it('keeps a missing completion time cautionary rather than claiming no backups exist', () => {
    const badge = renderCell(null);

    expect(badge.textContent?.trim()).toBe('Unknown');
    expect(badge.className).toContain('rounded-full');
    expect(badge.className).toContain('border-yellow-200');
    expect(badge.className).not.toContain('red');
    expect(badge.getAttribute('aria-label')).toBe('Backup status: no completed backup reported');
  });

  // A backup that merely STARTED must never wear the green shield - the
  // repro was an in-flight PBS snapshot at 9% reading as a healthy backup
  //. Running is its own blue state, and the last COMPLETED age
  // stays available to assistive tech.
  it('shows a blue Running pill instead of the green shield while a backup runs', () => {
    const badge = renderCell(Date.now() - 2 * HOUR_MS, true);

    expect(badge.textContent?.trim()).toBe('Running');
    expect(badge.className).toContain('border-blue-200');
    expect(badge.className).not.toContain('text-green-600');
    expect(badge.getAttribute('aria-label')).toMatch(
      /^Backup status: backup running now, last completed backup /,
    );
  });

  it('shows Running without claiming this is the first backup', () => {
    const badge = renderCell(null, true);

    expect(badge.textContent?.trim()).toBe('Running');
    expect(badge.className).toContain('border-blue-200');
    expect(badge.className).not.toContain('border-red-200');
    expect(badge.getAttribute('aria-label')).toBe(
      'Backup status: backup running now, no completed backup reported',
    );
  });

  it('keeps the completed-backup age when a stale guest starts backing up', () => {
    const badge = renderCell(Date.now() - 200 * HOUR_MS, true);

    // The badge shows Running, not the age - but the age must survive in
    // the accessible label so "running" never erases how old the last
    // completed backup is.
    expect(badge.textContent?.trim()).toBe('Running');
    expect(badge.getAttribute('aria-label')).toContain('last completed backup');
  });
  // #2858: a user on a three-day cycle saw amber after one day and could not
  // find where that limit lives. The dialog names the limit and links to it.
  it('says which age limit made a backup amber and links to where it is set', () => {
    const badge = renderCell(Date.now() - 48 * HOUR_MS);
    fireEvent.click(badge);

    const dialog = screen.getByRole('dialog');
    expect(dialog.querySelector('[data-backup-age-rule]')?.textContent).toBe(
      'Shown amber: more than 1 day old (Fresh Hours).',
    );
    const link = screen.getByRole('link', { name: 'Change when backups count as old' });
    expect(link.getAttribute('href')).toBe('/alerts/thresholds/proxmox#backups');

    fireEvent.click(link);
    expect(screen.queryByRole('dialog')).toBeNull();
  });

  it('offers no age limit or link when no backup has completed', () => {
    fireEvent.click(renderCell(null));

    expect(screen.getByRole('dialog').querySelector('[data-backup-age-rule]')).toBeNull();
    expect(screen.queryByRole('link', { name: 'Change when backups count as old' })).toBeNull();
  });
});