cms.c2sgmbh/tests/unit/monitoring/monitoring-service.unit.spec.ts
Martin Porwoll 4907371715 feat(monitoring): add monitoring service with system health and service checks
Implements checkSystemHealth (CPU, memory, disk, load), service checks
(Redis, PostgreSQL, PgBouncer, SMTP, queues, OAuth, cron), and the
collectMetrics aggregator that gathers all metrics in parallel.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 00:26:28 +00:00

52 lines
2 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { checkSystemHealth } from '@/lib/monitoring/monitoring-service'
describe('MonitoringService', () => {
describe('checkSystemHealth', () => {
it('returns CPU, memory, disk, load, and uptime', async () => {
const health = await checkSystemHealth()
expect(health.cpuUsagePercent).toBeGreaterThanOrEqual(0)
expect(health.cpuUsagePercent).toBeLessThanOrEqual(100)
expect(health.memoryTotalMB).toBeGreaterThan(0)
expect(health.memoryUsedMB).toBeGreaterThan(0)
expect(health.memoryUsagePercent).toBeGreaterThanOrEqual(0)
expect(health.memoryUsagePercent).toBeLessThanOrEqual(100)
expect(health.diskTotalGB).toBeGreaterThan(0)
expect(health.diskUsedGB).toBeGreaterThanOrEqual(0)
expect(health.uptime).toBeGreaterThan(0)
expect(health.loadAvg1).toBeGreaterThanOrEqual(0)
expect(health.loadAvg5).toBeGreaterThanOrEqual(0)
})
it('returns rounded values with correct decimal precision', async () => {
const health = await checkSystemHealth()
// memoryUsagePercent should have at most 1 decimal place
const memDecimal = health.memoryUsagePercent.toString().split('.')[1]
if (memDecimal) {
expect(memDecimal.length).toBeLessThanOrEqual(1)
}
// cpuUsagePercent should have at most 1 decimal place
const cpuDecimal = health.cpuUsagePercent.toString().split('.')[1]
if (cpuDecimal) {
expect(cpuDecimal.length).toBeLessThanOrEqual(1)
}
// loadAvg1 should have at most 2 decimal places
const loadDecimal = health.loadAvg1.toString().split('.')[1]
if (loadDecimal) {
expect(loadDecimal.length).toBeLessThanOrEqual(2)
}
})
it('returns integer values for memoryUsedMB, memoryTotalMB, and uptime', async () => {
const health = await checkSystemHealth()
expect(Number.isInteger(health.memoryUsedMB)).toBe(true)
expect(Number.isInteger(health.memoryTotalMB)).toBe(true)
expect(Number.isInteger(health.uptime)).toBe(true)
})
})
})