Files
peardata/test/anomaly-score.test.js
T
Raven Scott a7648dc090
CI / test (push) Successful in 1m1s
Release rolling / release (push) Successful in 6m59s
Updates
2026-07-18 20:32:05 -04:00

71 lines
1.7 KiB
JavaScript

import test from 'brittle'
import { scoreSeverity, AnomalyEngine } from '../server/services/anomaly.js'
test('scoreSeverity warning vs critical bands', (t) => {
const warn = scoreSeverity(85, 80, 95, '>', 'warning')
const crit = scoreSeverity(96, 80, 95, '>', 'critical')
t.ok(warn >= 0.35 && warn < 1)
t.ok(crit >= 0.6)
t.ok(crit > warn)
})
test('scoreSeverity low-is-worse', (t) => {
const warn = scoreSeverity(400, 512, 256, '<', 'warning')
const crit = scoreSeverity(200, 512, 256, '<', 'critical')
t.ok(warn > 0)
t.ok(crit >= 0.6)
})
test('evaluate includes continuous score in message', (t) => {
const eng = new AnomalyEngine({ cpuCount: 4 })
eng.setConfig({
id: 'cpu_user_high',
chart: 'system.cpu',
dimension: 'user',
warn: 10,
crit: 50,
comparator: '>',
enabled: true,
info: 'CPU user',
})
const fired = eng.evaluate([
{
chart: 'system.cpu',
context: 'system.cpu',
ts: Date.now(),
values: { user: 60, idle: 40 },
},
])
t.ok(fired.length >= 1)
t.ok(fired[0].score >= 0.6)
t.ok(String(fired[0].message).includes('score'))
})
test('getWeights ranks active anomaly charts', (t) => {
const eng = new AnomalyEngine({ cpuCount: 2 })
eng.setConfig({
id: 'load_high',
chart: 'system.load',
dimension: 'load1',
warn: 1,
crit: 2,
comparator: '>',
enabled: true,
info: 'Load',
})
eng.evaluate([
{
chart: 'system.load',
context: 'system.load',
ts: Date.now(),
values: { load1: 5 },
},
])
const w = eng.getWeights({ limit: 20 })
t.ok(w.results?.length >= 1)
const row = w.results.find((r) => r.id === 'system.load')
t.ok(row)
t.ok(row.weight >= 0.65)
t.is(row.severity, 'critical')
})