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 { createEffect, createRoot } from 'solid-js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { NotificationsAPI } from '@/api/notifications';
import { notificationStore } from '@/stores/notifications';
import { useNotificationDeliveryHealth } from '../useNotificationDeliveryHealth';
vi.mock('@/api/notifications', () => ({
NotificationsAPI: {
getHealth: vi.fn(),
dismissTerminalFailures: vi.fn(),
retryTerminalFailures: vi.fn(),
},
}));
vi.mock('@/stores/notifications', () => ({
notificationStore: { success: vi.fn(), error: vi.fn() },
}));
const healthWith = (status: string) => ({ queue: { status, failed: 3, deadLetter: 1 } }) as never;
describe('useNotificationDeliveryHealth', () => {
beforeEach(() => {
vi.mocked(notificationStore.success).mockReset();
vi.mocked(notificationStore.error).mockReset();
vi.mocked(NotificationsAPI.getHealth).mockReset();
vi.mocked(NotificationsAPI.dismissTerminalFailures).mockReset();
vi.mocked(NotificationsAPI.retryTerminalFailures).mockReset();
});
it('stays silent before the first load resolves so it cannot flash a warning', () =>
createRoot(async (dispose) => {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue(healthWith('degraded'));
const state = useNotificationDeliveryHealth();
expect(state.deliveryNeedsAttention()).toBe(false);
await state.loadDeliveryHealth();
expect(state.deliveryNeedsAttention()).toBe(true);
dispose();
}));
it('does not raise attention for a healthy queue', () =>
createRoot(async (dispose) => {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue(healthWith('healthy'));
const state = useNotificationDeliveryHealth();
await state.loadDeliveryHealth();
expect(state.deliveryNeedsAttention()).toBe(false);
expect(state.deliveryHealthUnavailable()).toBe(false);
dispose();
}));
it('treats an unreachable health endpoint as needing attention', () =>
createRoot(async (dispose) => {
vi.mocked(NotificationsAPI.getHealth).mockRejectedValue(new Error('network down'));
const state = useNotificationDeliveryHealth();
await state.loadDeliveryHealth();
expect(state.deliveryHealthUnavailable()).toBe(true);
expect(state.deliveryNeedsAttention()).toBe(true);
expect(state.deliveryHealth()).toBeNull();
dispose();
}));
it('never drops attention while a degraded queue becomes unreadable', () =>
createRoot(async (dispose) => {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce(healthWith('degraded'));
const state = useNotificationDeliveryHealth();
await state.loadDeliveryHealth();
const seen: boolean[] = [];
createEffect(() => seen.push(state.deliveryNeedsAttention()));
vi.mocked(NotificationsAPI.getHealth).mockRejectedValueOnce(new Error('network down'));
await state.loadDeliveryHealth();
// The snapshot and its availability flip together, so the warning (and
// the delivery evidence mounted beside it) is never torn down mid-refresh.
expect(seen).toEqual([true]);
expect(state.deliveryHealthUnavailable()).toBe(true);
dispose();
}));
it('treats a queue the server reports as unavailable as needing attention', () =>
createRoot(async (dispose) => {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue(healthWith('unavailable'));
const state = useNotificationDeliveryHealth();
await state.loadDeliveryHealth();
expect(state.deliveryNeedsAttention()).toBe(true);
dispose();
}));
it.each([
['healthy', 'degraded'],
['degraded', 'healthy'],
['error', 'healthy'],
['healthy', 'error'],
])('ignores older %s completion after newer %s result', (older, newer) =>
createRoot(async (dispose) => {
let resolve!: (value: Awaited<ReturnType<typeof NotificationsAPI.getHealth>>) => void;
let reject!: (error: Error) => void;
vi.mocked(NotificationsAPI.getHealth).mockReturnValueOnce(
new Promise((yes, no) => {
resolve = yes;
reject = no;
}),
);
const state = useNotificationDeliveryHealth();
const pending = state.loadDeliveryHealth();
if (newer === 'error') {
vi.mocked(NotificationsAPI.getHealth).mockRejectedValueOnce(new Error('new failure'));
} else {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce(healthWith(newer));
}
await state.loadDeliveryHealth();
if (older === 'error') reject(new Error('old failure'));
else resolve(healthWith(older));
await pending;
expect(state.deliveryHealth()?.queue.status).toBe(newer === 'error' ? undefined : newer);
expect(state.deliveryHealthUnavailable()).toBe(newer === 'error');
expect(state.deliveryNeedsAttention()).toBe(newer !== 'healthy');
expect(state.refreshingDeliveryHealth()).toBe(false);
dispose();
}),
);
it.each(['healthy', 'error'])(
'keeps loading and first-load silence when older %s finishes first',
(older) =>
createRoot(async (dispose) => {
let finishOld!: () => void;
let finishNew!: () => void;
vi.mocked(NotificationsAPI.getHealth)
.mockReturnValueOnce(
new Promise((resolve, reject) => {
finishOld = () =>
older === 'error'
? reject(new Error('old failure'))
: resolve(healthWith('healthy'));
}),
)
.mockReturnValueOnce(
new Promise((resolve) => {
finishNew = () => resolve(healthWith('degraded'));
}),
);
const state = useNotificationDeliveryHealth();
const oldRequest = state.loadDeliveryHealth();
const newRequest = state.loadDeliveryHealth();
finishOld();
await oldRequest;
expect(state.refreshingDeliveryHealth()).toBe(true);
expect(state.deliveryHealth()).toBeNull();
expect(state.deliveryNeedsAttention()).toBe(false);
finishNew();
await newRequest;
expect(state.refreshingDeliveryHealth()).toBe(false);
expect(state.deliveryNeedsAttention()).toBe(true);
dispose();
}),
);
it.each(['dismissTerminalFailures', 'retryTerminalFailures'] as const)(
'keeps post-%s health when a pre-action request finishes late',
(action) =>
createRoot(async (dispose) => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true);
try {
const state = useNotificationDeliveryHealth();
vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce({
queue: { status: 'degraded', attentionRequired: 2 },
} as never);
await state.loadDeliveryHealth();
let finishOld!: (health: Awaited<ReturnType<typeof NotificationsAPI.getHealth>>) => void;
vi.mocked(NotificationsAPI.getHealth)
.mockReturnValueOnce(
new Promise((resolve) => {
finishOld = resolve;
}),
)
.mockResolvedValueOnce(healthWith('healthy'));
const pending = state.loadDeliveryHealth();
vi.mocked(NotificationsAPI[action]).mockResolvedValueOnce({ affected: 2 } as never);
await state[action]();
expect(state.deliveryNeedsAttention()).toBe(false);
finishOld(healthWith('degraded'));
await pending;
expect(state.deliveryHealth()?.queue.status).toBe('healthy');
expect(state.deliveryNeedsAttention()).toBe(false);
} finally {
confirmSpy.mockRestore();
dispose();
}
}),
);
describe.each(['dismissTerminalFailures', 'retryTerminalFailures'] as const)('%s', (action) => {
it('keeps uncertainty after a failed post-action refresh even when older healthy evidence arrives', () =>
createRoot(async (dispose) => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true);
const onAfterQueueAction = vi.fn().mockResolvedValue(undefined);
let finishOld!: (health: Awaited<ReturnType<typeof NotificationsAPI.getHealth>>) => void;
try {
vi.mocked(NotificationsAPI.getHealth)
.mockResolvedValueOnce({
queue: { status: 'degraded', attentionRequired: 2 },
} as never)
.mockReturnValueOnce(
new Promise((resolve) => {
finishOld = resolve;
}),
)
.mockRejectedValueOnce(new Error('post-action connection lost'));
vi.mocked(NotificationsAPI[action]).mockResolvedValueOnce({ affected: 2 } as never);
const state = useNotificationDeliveryHealth({ onAfterQueueAction });
await state.loadDeliveryHealth();
const oldRequest = state.loadDeliveryHealth();
await state[action]();
expect(state.deliveryHealthUnavailable()).toBe(true);
finishOld(healthWith('healthy'));
await oldRequest;
expect(NotificationsAPI[action]).toHaveBeenCalledOnce();
expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(3);
expect(onAfterQueueAction).toHaveBeenCalledOnce();
// A failed health read must not reclassify an accepted queue mutation.
expect(notificationStore.success).toHaveBeenCalledWith(
action === 'retryTerminalFailures'
? '2 retained deliveries queued for retry.'
: '2 retained failures dismissed.',
);
expect(notificationStore.error).not.toHaveBeenCalled();
expect(state.deliveryHealth()).toBeNull();
expect(state.deliveryHealthUnavailable()).toBe(true);
expect(state.deliveryNeedsAttention()).toBe(true);
expect(state.refreshingDeliveryHealth()).toBe(false);
expect(state.retryingTerminalFailures()).toBe(false);
expect(state.dismissingTerminalFailures()).toBe(false);
} finally {
confirmSpy.mockRestore();
dispose();
}
}));
it.each(['cancelled', 'rejected'] as const)(
'preserves retained failure evidence when the action is %s',
(outcome) =>
createRoot(async (dispose) => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(outcome !== 'cancelled');
const onAfterQueueAction = vi.fn();
try {
const health = { queue: { status: 'degraded', attentionRequired: 2 } } as never;
vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce(health);
const state = useNotificationDeliveryHealth({ onAfterQueueAction });
await state.loadDeliveryHealth();
let rejectAction!: (reason: Error) => void;
vi.mocked(NotificationsAPI[action]).mockReturnValueOnce(
new Promise((_, reject) => {
rejectAction = reject;
}),
);
const pending = state[action]();
const busy =
action === 'dismissTerminalFailures'
? state.dismissingTerminalFailures
: state.retryingTerminalFailures;
expect(busy()).toBe(outcome === 'rejected');
if (outcome === 'rejected') rejectAction(new Error('request rejected'));
await pending;
expect(confirmSpy).toHaveBeenCalledOnce();
expect(NotificationsAPI[action]).toHaveBeenCalledTimes(outcome === 'rejected' ? 1 : 0);
expect(NotificationsAPI.getHealth).toHaveBeenCalledTimes(1);
expect(onAfterQueueAction).not.toHaveBeenCalled();
expect(notificationStore.success).not.toHaveBeenCalled();
expect(notificationStore.error).toHaveBeenCalledTimes(outcome === 'rejected' ? 1 : 0);
if (outcome === 'rejected') {
expect(notificationStore.error).toHaveBeenCalledWith(
action === 'retryTerminalFailures'
? 'Unable to retry retained notification deliveries.'
: 'Unable to dismiss retained notification failures.',
);
}
expect(state.deliveryHealth()).toBe(health);
expect(state.deliveryNeedsAttention()).toBe(true);
expect(state.deliveryHealthUnavailable()).toBe(false);
expect(busy()).toBe(false);
} finally {
confirmSpy.mockRestore();
dispose();
}
}),
);
});
describe.each(['dismissTerminalFailures', 'retryTerminalFailures'] as const)(
'%s feedback',
(action) => {
it('retains failed action information across time, healthy reads and cancellation until cleared or superseded', () =>
createRoot(async (dispose) => {
vi.useFakeTimers();
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true);
try {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
queue: { status: 'degraded', attentionRequired: 2 },
} as never);
vi.mocked(NotificationsAPI[action]).mockRejectedValueOnce(new Error('failed'));
const state = useNotificationDeliveryHealth();
await state.loadDeliveryHealth();
await state[action]();
const failure = state.queueActionFeedback();
expect(failure).toMatch(/^Unable to/);
await vi.advanceTimersByTimeAsync(11000);
vi.mocked(NotificationsAPI.getHealth).mockResolvedValueOnce(healthWith('healthy'));
await state.loadDeliveryHealth();
expect(state.deliveryNeedsAttention()).toBe(false);
expect(state.queueActionFeedback()).toBe(failure);
await state.loadDeliveryHealth();
confirmSpy.mockReturnValueOnce(false);
await state[action]();
expect(state.queueActionFeedback()).toBe(failure);
state.clearQueueActionFeedback();
expect(state.queueActionFeedback()).toBeNull();
vi.mocked(NotificationsAPI[action]).mockRejectedValueOnce(new Error('again'));
await state[action]();
expect(state.queueActionFeedback()).toBe(failure);
vi.mocked(NotificationsAPI[action]).mockResolvedValueOnce({ affected: 2 } as never);
await state[action]();
expect(state.queueActionFeedback()).toBeNull();
} finally {
vi.useRealTimers();
confirmSpy.mockRestore();
dispose();
}
}));
it.each(['throw', 'reject'] as const)(
'does not misreport an accepted mutation when the activity callback fails: %s',
(failure) =>
createRoot(async (dispose) => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true);
try {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
queue: { status: 'degraded', attentionRequired: 2 },
} as never);
vi.mocked(NotificationsAPI[action]).mockResolvedValue({ affected: 2 } as never);
const onAfterQueueAction = () => {
if (failure === 'throw') throw new Error('activity failed');
return Promise.reject(new Error('activity failed'));
};
const state = useNotificationDeliveryHealth({ onAfterQueueAction });
await state.loadDeliveryHealth();
await state[action]();
expect(notificationStore.success).toHaveBeenCalledOnce();
expect(notificationStore.error).not.toHaveBeenCalled();
expect(state.queueActionFeedback()).toMatch(
/^The queue action succeeded, but notification activity/,
);
expect(state.retryingTerminalFailures()).toBe(false);
expect(state.dismissingTerminalFailures()).toBe(false);
} finally {
confirmSpy.mockRestore();
dispose();
}
}),
);
it.each(['clear', 'newer'] as const)(
'does not restore stale callback feedback after %s',
(next) =>
createRoot(async (dispose) => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true);
try {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
queue: { status: 'degraded', attentionRequired: 2 },
} as never);
vi.mocked(NotificationsAPI[action]).mockResolvedValue({ affected: 2 } as never);
let rejectOld!: (error: Error) => void;
const onAfterQueueAction = vi.fn().mockImplementationOnce(
() =>
new Promise((_, reject) => {
rejectOld = reject;
}),
);
const state = useNotificationDeliveryHealth({ onAfterQueueAction });
await state.loadDeliveryHealth();
const pending = state[action]();
await Promise.resolve();
await Promise.resolve();
if (next === 'clear') state.clearQueueActionFeedback();
else {
vi.mocked(NotificationsAPI[action]).mockRejectedValueOnce(
new Error('new action failed'),
);
await state[action]();
}
const current = state.queueActionFeedback();
rejectOld(new Error('old activity failed'));
await pending;
expect(state.queueActionFeedback()).toBe(current);
if (next === 'newer') expect(current).toMatch(/^Unable to/);
} finally {
confirmSpy.mockRestore();
dispose();
}
}),
);
},
);
it('does not let an older rejected mutation overwrite the newer action failure', () =>
createRoot(async (dispose) => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true);
try {
vi.mocked(NotificationsAPI.getHealth).mockResolvedValue({
queue: { status: 'degraded', attentionRequired: 2 },
} as never);
let rejectOld!: (error: Error) => void;
vi.mocked(NotificationsAPI.retryTerminalFailures).mockReturnValueOnce(
new Promise((_, reject) => {
rejectOld = reject;
}),
);
vi.mocked(NotificationsAPI.dismissTerminalFailures).mockRejectedValueOnce(
new Error('new failure'),
);
const state = useNotificationDeliveryHealth();
await state.loadDeliveryHealth();
const old = state.retryTerminalFailures();
await state.dismissTerminalFailures();
rejectOld(new Error('old failure'));
await old;
expect(state.queueActionFeedback()).toBe(
'Unable to dismiss retained notification failures.',
);
} finally {
confirmSpy.mockRestore();
dispose();
}
}));
});