Repository files, collaboration, releases and project activity.
archforge-external-status / tests/monitor.test.jsmain
▤ tests/monitor.test.js2980 bytes
19 lines · Select a line number for a permalink
1
'use strict';2
const test=require('node:test'),assert=require('node:assert/strict');3
const {openDatabase,run,one,rows}=require('../src/database');const {syncServices}=require('../src/database');4
const {upsertState,uptimeStats,incidentMaintenanceState}=require('../src/monitor');5
function prep(){const db=openDatabase(':memory:');syncServices(db,[{id:'website',label:'Website',description:'site',category:'Platform',configured:true}]);const cfg={failures:3,recoveries:2,discord:'',smtpAvailable:false};return {db,cfg};}6
test('three failures create exactly one automated incident, recovery needs two successes',()=>{7
const {db,cfg}=prep();let time=1750000000;8
upsertState(db,cfg,'website',{success:false,latency:1,detail:'HTTP 500'},time++);assert.equal(one(db,'SELECT current_state s FROM services').s,'unknown');9
upsertState(db,cfg,'website',{success:false,latency:1,detail:'HTTP 500'},time++);assert.equal(one(db,'SELECT COUNT(*) c FROM incidents').c,0);10
upsertState(db,cfg,'website',{success:false,latency:1,detail:'HTTP 500'},time++);assert.equal(one(db,'SELECT current_state s FROM services').s,'offline');11
upsertState(db,cfg,'website',{success:false,latency:1,detail:'HTTP 500'},time++);assert.equal(one(db,'SELECT COUNT(*) c FROM incidents').c,1);12
upsertState(db,cfg,'website',{success:true,latency:100,detail:'HTTP 200'},time++);assert.equal(one(db,'SELECT current_state s FROM services').s,'offline');13
upsertState(db,cfg,'website',{success:true,latency:100,detail:'HTTP 200'},time++);assert.equal(one(db,'SELECT current_state s FROM services').s,'operational');assert.equal(one(db,'SELECT COUNT(*) c FROM incidents WHERE resolved_at IS NOT NULL').c,1);db.close();14
});15
test('no data days are explicitly unknown, never assumed operational',()=>{const {db}=prep();const data=uptimeStats(db,90);assert.equal(data[0].stats.coverage,0);assert.equal(data[0].stats.uptime,null);assert.equal(data[0].stats.buckets.length,90);assert.ok(data[0].stats.buckets.every(d=>d.state==='unknown'));db.close();});16
test('maintenance windows are independent from health check samples',()=>{const {db}=prep();const n=1750000000;run(db,'INSERT INTO maintenance(title,description,starts_at,ends_at,created_at) VALUES(?,?,?,?,?)','Upgrade','Planned',n,n+3600,n);assert.equal(incidentMaintenanceState(db,n+1).length,1);assert.equal(incidentMaintenanceState(db,n+4000).length,0);db.close();});18
test('certificate warnings become degraded, not offline',()=>{const {db,cfg}=prep();for(let i=0;i<3;i++)upsertState(db,cfg,'website',{success:true,warning:true,latency:2,detail:'Expires in 3 days'});assert.equal(one(db,'SELECT current_state s FROM services').s,'degraded');assert.equal(one(db,'SELECT severity s FROM incidents').s,'degraded');upsertState(db,cfg,'website',{success:true,latency:2,detail:'Valid certificate'});upsertState(db,cfg,'website',{success:true,latency:2,detail:'Valid certificate'});assert.equal(one(db,'SELECT current_state s FROM services').s,'operational');db.close();});