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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
formatPowerOnHours,
estimateTextWidth,
formatAnomalyRatio,
getShortImageName,
normalizeDiskArray,
ANOMALY_SEVERITY_CLASS,
} from '@/utils/format';
describe('formatPowerOnHours', () => {
it('returns hours for less than 24 hours', () => {
expect(formatPowerOnHours(0)).toBe('0 hours');
expect(formatPowerOnHours(1)).toBe('1 hours');
expect(formatPowerOnHours(23)).toBe('23 hours');
});
it('returns days for 24+ hours', () => {
expect(formatPowerOnHours(24)).toBe('1 days');
expect(formatPowerOnHours(48)).toBe('2 days');
expect(formatPowerOnHours(168)).toBe('7 days');
});
it('returns years for 8760+ hours', () => {
expect(formatPowerOnHours(8760)).toBe('1.0 years');
expect(formatPowerOnHours(17520)).toBe('2.0 years');
expect(formatPowerOnHours(43800)).toBe('5.0 years');
});
it('returns condensed format when condensed is true', () => {
expect(formatPowerOnHours(0, true)).toBe('0h');
expect(formatPowerOnHours(24, true)).toBe('1d');
expect(formatPowerOnHours(8760, true)).toBe('1.0y');
});
});
describe('estimateTextWidth', () => {
// jsdom has no layout, so these read the measured fallback advances
// (macOS system font at 10px) rather than a live probe.
it('returns zero for empty text', () => {
expect(estimateTextWidth('')).toBe(0);
});
it('sums per-glyph advances and rounds up', () => {
expect(estimateTextWidth('a')).toBe(6); // 5.89
expect(estimateTextWidth('abc')).toBe(19); // 5.89 + 6.51 + 5.9 = 18.3
});
it('tells wide glyphs from narrow ones at the same length', () => {
expect(estimateTextWidth('888')).toBe(21); // 3 x 6.88
expect(estimateTextWidth('...')).toBe(11); // 3 x 3.43
expect(estimateTextWidth('MMM')).toBe(28); // 3 x 9.08
expect(estimateTextWidth('iii')).toBe(9); // 3 x 2.86
});
it('measures detail at normal weight after the semibold value', () => {
// 60% is 23.7 semibold, " (265 GB/440 GB)" 86.89 at normal weight. The
// browser renders that label 110.1px wide.
expect(estimateTextWidth('60%', { detail: ' (265 GB/440 GB)' })).toBe(111);
expect(estimateTextWidth('265 GB')).toBe(38);
expect(estimateTextWidth('', { detail: '265 GB' })).toBe(37);
});
it('scales with the label font size', () => {
expect(estimateTextWidth('sda1 50%')).toBe(52); // 51.03
expect(estimateTextWidth('sda1 50%', { fontPx: 9 })).toBe(46); // 45.93
});
});
describe('estimateTextWidth with layout', () => {
afterEach(() => {
vi.restoreAllMocks();
vi.resetModules();
});
it('reads glyph advances from one hidden probe and reuses them', async () => {
// A stand-in font: digits 8px, capitals 9px, everything else 4px, each
// probe cell holding its glyph sixteen times.
const rect = vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (
this: Element,
) {
const text = this.textContent ?? '';
const advance = /\d/.test(text[0]) ? 8 : /[A-Z]/.test(text[0]) ? 9 : 4;
return { width: text.length * advance } as DOMRect;
});
vi.resetModules();
const { estimateTextWidth: measured } = await import('@/utils/format');
// 8 digits, 4 capitals, 7 others.
expect(measured('60%', { detail: ' (265 GB/440 GB)' })).toBe(8 * 8 + 4 * 9 + 7 * 4);
// U+0020..U+007E at both weights, plus the ten tabular digits.
expect(rect).toHaveBeenCalledTimes(95 + 10 + 95);
expect(document.body.childElementCount).toBe(0);
measured('73%', { detail: ' (475 GB/648 GB)' });
expect(rect).toHaveBeenCalledTimes(95 + 10 + 95);
});
it('keeps the measured table when a stubbed layout reports every box alike', async () => {
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
width: 100,
} as DOMRect);
vi.resetModules();
const { estimateTextWidth: measured } = await import('@/utils/format');
expect(measured('60%', { detail: ' (265 GB/440 GB)' })).toBe(111);
});
});
describe('formatAnomalyRatio', () => {
it('returns null for null/undefined input', () => {
expect(formatAnomalyRatio(null)).toBeNull();
expect(formatAnomalyRatio(undefined)).toBeNull();
});
it('returns null when baseline_mean is 0', () => {
expect(formatAnomalyRatio({ baseline_mean: 0, current_value: 100 })).toBeNull();
});
it('returns 2x+ ratio as formatted number', () => {
expect(formatAnomalyRatio({ baseline_mean: 50, current_value: 100 })).toBe('2.0x');
expect(formatAnomalyRatio({ baseline_mean: 25, current_value: 100 })).toBe('4.0x');
});
it('returns ↑↑ for 1.5x-2x ratio', () => {
expect(formatAnomalyRatio({ baseline_mean: 50, current_value: 90 })).toBe('↑↑');
expect(formatAnomalyRatio({ baseline_mean: 40, current_value: 70 })).toBe('↑↑');
});
it('returns ↑ for less than 1.5x ratio', () => {
expect(formatAnomalyRatio({ baseline_mean: 100, current_value: 140 })).toBe('↑');
expect(formatAnomalyRatio({ baseline_mean: 100, current_value: 120 })).toBe('↑');
});
});
describe('ANOMALY_SEVERITY_CLASS', () => {
it('has critical class', () => {
expect(ANOMALY_SEVERITY_CLASS.critical).toBe('text-red-400');
});
it('has high class', () => {
expect(ANOMALY_SEVERITY_CLASS.high).toBe('text-orange-400');
});
it('has medium class', () => {
expect(ANOMALY_SEVERITY_CLASS.medium).toBe('text-yellow-400');
});
it('has low class', () => {
expect(ANOMALY_SEVERITY_CLASS.low).toBe('text-blue-400');
});
});
describe('getShortImageName', () => {
it('returns dash for undefined', () => {
expect(getShortImageName(undefined)).toBe('—');
});
it('returns dash for empty string', () => {
expect(getShortImageName('')).toBe('—');
});
it('returns full name for simple image', () => {
expect(getShortImageName('nginx:latest')).toBe('nginx:latest');
});
it('returns image leaf and tag for registry URLs', () => {
expect(getShortImageName('ghcr.io/owner/image:tag')).toBe('image:tag');
expect(getShortImageName('docker.io/library/nginx:latest')).toBe('nginx:latest');
});
it('strips sha256 digest', () => {
expect(getShortImageName('nginx:latest@sha256:abc123')).toBe('nginx:latest');
});
it('handles complex registry paths', () => {
expect(getShortImageName('registry.example.com/foo/bar/myapp:v1.0')).toBe('myapp:v1.0');
});
});
describe('normalizeDiskArray', () => {
it('returns undefined for null/undefined input', () => {
expect(normalizeDiskArray(undefined)).toBeUndefined();
expect(normalizeDiskArray(null as any)).toBeUndefined();
});
it('returns undefined for empty array', () => {
expect(normalizeDiskArray([])).toBeUndefined();
});
it('normalizes disk with used and total', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', total: 1000, used: 500 }]);
expect(result).toHaveLength(1);
expect(result?.[0].total).toBe(1000);
expect(result?.[0].used).toBe(500);
expect(result?.[0].free).toBe(500);
expect(result?.[0].usage).toBe(50);
});
it('calculates free from total and used when free is missing', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', total: 1000, used: 300 }]);
expect(result?.[0].free).toBe(700);
});
it('handles missing free and calculates correctly', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', total: 1000 }]);
expect(result?.[0].free).toBe(1000);
expect(result?.[0].usage).toBe(0);
});
it('handles zero total gracefully', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', total: 0, used: 0 }]);
expect(result?.[0].free).toBe(0);
expect(result?.[0].usage).toBe(0);
});
it('preserves the unknown-usage sentinel instead of computing a percent', () => {
// Config-only LXC mounts (#1477) arrive with capacity but usage -1.
const result = normalizeDiskArray([
{ mountpoint: '/srv/archive', filesystem: 'mp0', total: 1000, usage: -1 },
]);
expect(result?.[0].usage).toBe(-1);
expect(result?.[0].total).toBe(1000);
expect(result?.[0].used).toBe(0);
});
it('uses filesystem type when available', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', filesystem: 'ext4' }]);
expect(result?.[0].type).toBe('ext4');
});
it('uses type as fallback when filesystem missing', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', type: 'xfs' }]);
expect(result?.[0].type).toBe('xfs');
});
it('preserves mountpoint', () => {
const result = normalizeDiskArray([{ device: '/dev/sda', mountpoint: '/home' }]);
expect(result?.[0].mountpoint).toBe('/home');
});
it('filters macOS APFS companion volumes and simulator runtime images', () => {
const result = normalizeDiskArray([
{
device: '/dev/disk3s1s1',
mountpoint: '/',
filesystem: 'apfs',
total: 245107195904,
used: 213573853184,
},
{
device: '/dev/disk8s1',
mountpoint: '/Library/Developer/CoreSimulator/Volumes/iOS_23C54',
filesystem: 'apfs',
total: 17645436928,
used: 17188392960,
},
{
device: '/dev/disk3s5',
mountpoint: '/System/Volumes/Data',
filesystem: 'apfs',
total: 245107195904,
used: 213573853184,
},
{
device: '/dev/disk7s1',
mountpoint: '/Volumes/Development',
filesystem: 'apfs',
total: 999995129856,
used: 204885471232,
},
]);
expect(result?.map((disk) => disk.mountpoint)).toEqual(['/', '/Volumes/Development']);
});
it('filters platform plumbing while preserving operational Windows and Linux disks', () => {
const result = normalizeDiskArray([
{
device: '/dev/sda1',
mountpoint: '/boot/firmware',
filesystem: 'vfat',
total: 536870912,
used: 67108864,
},
{
device: '/dev/sda2',
mountpoint: '/boot',
filesystem: 'ext4',
total: 2147483648,
used: 268435456,
},
{
device: 'C:',
mountpoint: 'C:\\',
filesystem: 'NTFS',
total: 536870912000,
used: 268435456000,
},
{
device: 'D:',
mountpoint: 'D:\\',
filesystem: 'NTFS',
total: 1099511627776,
used: 549755813888,
},
{
device: '\\\\?\\Volume{system-reserved}\\',
mountpoint: 'System Reserved',
filesystem: 'NTFS',
total: 524288000,
used: 104857600,
},
{
device: 'pmxcfs',
mountpoint: '/etc/pve',
filesystem: 'fuse',
total: 134217728,
used: 262144,
},
]);
expect(result?.map((disk) => disk.mountpoint)).toEqual(['/boot', 'C:\\', 'D:\\']);
});
it('returns undefined when all disk rows are non-operational macOS mounts', () => {
expect(
normalizeDiskArray([
{
device: '/dev/disk3s2',
mountpoint: '/System/Volumes/Preboot',
filesystem: 'apfs',
total: 245107195904,
used: 213573853184,
},
]),
).toBeUndefined();
});
});