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.

4.6 KB
Raw Blame
import { describe, it, expect, beforeEach, vi } from 'vitest';
import {
  celsiusToFahrenheit,
  formatTemperature,
  getTemperatureSymbol,
  getTemperatureTextClass,
  temperatureStore,
} from '../temperature';

describe('temperature', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    localStorage.clear();
  });

  describe('celsiusToFahrenheit', () => {
    it('converts correctly', () => {
      expect(celsiusToFahrenheit(0)).toBe(32);
      expect(celsiusToFahrenheit(100)).toBe(212);
      expect(celsiusToFahrenheit(-40)).toBe(-40);
      expect(celsiusToFahrenheit(37)).toBeCloseTo(98.6);
    });
  });

  describe('formatTemperature', () => {
    it('formats Celsius correctly by default', () => {
      temperatureStore.setUnit('celsius');
      expect(formatTemperature(25)).toBe('25°C');
      expect(formatTemperature(25.6, { decimals: 1 })).toBe('25.6°C');
      expect(formatTemperature(25.6, { decimals: 0 })).toBe('26°C');
    });

    it('formats Fahrenheit correctly', () => {
      temperatureStore.setUnit('fahrenheit');
      expect(formatTemperature(0)).toBe('32°F');
      expect(formatTemperature(100)).toBe('212°F');
    });

    it('handles null/undefined/NaN', () => {
      expect(formatTemperature(null)).toBe('—');
      expect(formatTemperature(undefined)).toBe('—');
      expect(formatTemperature(NaN)).toBe('—');
    });

    it('respects showUnit option', () => {
      temperatureStore.setUnit('celsius');
      expect(formatTemperature(25, { showUnit: false })).toBe('25');
    });
  });

  describe('getTemperatureSymbol', () => {
    it('returns correct symbol', () => {
      temperatureStore.setUnit('celsius');
      expect(getTemperatureSymbol()).toBe('°C');
      temperatureStore.setUnit('fahrenheit');
      expect(getTemperatureSymbol()).toBe('°F');
    });
  });

  describe('getTemperatureTextClass', () => {
    it('returns muted tone for nullish or invalid values', () => {
      expect(getTemperatureTextClass(null)).toBe('text-muted');
      expect(getTemperatureTextClass(undefined)).toBe('text-muted');
      expect(getTemperatureTextClass(NaN)).toBe('text-muted');
    });

    it('returns green for safe temperatures', () => {
      expect(getTemperatureTextClass(45)).toContain('text-green-600');
    });

    it('returns amber for elevated temperatures', () => {
      expect(getTemperatureTextClass(75)).toContain('text-amber-600');
    });

    it('returns red for high temperatures', () => {
      expect(getTemperatureTextClass(80)).toContain('text-red-600');
    });

    it('colours no disk reading whose disk temperature alerting is off', () => {
      expect(getTemperatureTextClass(80, null, 'diskTemperature')).toBe('text-base-content');
      // Node temperatures keep the factory colours without configured thresholds.
      expect(getTemperatureTextClass(80, null, 'temperature')).toContain('text-red-600');
    });

    it('honors configured warning and critical thresholds', () => {
      const thresholds = { warning: 80, critical: 85 };

      expect(getTemperatureTextClass(76, thresholds)).toContain('text-green-600');
      expect(getTemperatureTextClass(80, thresholds)).toContain('text-amber-600');
      expect(getTemperatureTextClass(85, thresholds)).toContain('text-red-600');
    });

    it("keeps an open alert's tone while the reading holds under its trigger", () => {
      const thresholds = { warning: 75, critical: 80 };
      // minipc: alert open at 80°C, now reading 72°C on its way down.
      expect(getTemperatureTextClass(72, thresholds)).toContain('text-green-600');
      expect(getTemperatureTextClass(72, thresholds, 'temperature', 'warning')).toContain(
        'text-amber-600',
      );
      expect(getTemperatureTextClass(72, thresholds, 'temperature', 'critical')).toContain(
        'text-red-600',
      );
      // The reading still wins when it is worse than the alert.
      expect(getTemperatureTextClass(86, thresholds, 'temperature', 'warning')).toContain(
        'text-red-600',
      );
    });

    it('uses disk temperature defaults for disk temperature displays', () => {
      expect(getTemperatureTextClass(49, undefined, 'diskTemperature')).toContain('text-green-600');
      expect(getTemperatureTextClass(50, undefined, 'diskTemperature')).toContain('text-amber-600');
      expect(getTemperatureTextClass(55, undefined, 'diskTemperature')).toContain('text-red-600');
    });
  });

  describe('temperatureStore persistence', () => {
    it('saves to localStorage', () => {
      const spy = vi.spyOn(Storage.prototype, 'setItem');
      temperatureStore.setUnit('fahrenheit');
      expect(spy).toHaveBeenCalledWith('temperatureUnit', 'fahrenheit');
    });
  });
});