|
| 1 | +/** |
| 2 | + * Tests for the DeploymentMonitor — sampling, breach/recovery transition |
| 3 | + * signals (alert once, not per sample), one-off events, and sample queries. |
| 4 | + * Uses a real in-memory SQLite database with injected probe runners and a |
| 5 | + * captured signal publisher. |
| 6 | + */ |
| 7 | +import { describe, it, expect, afterEach } from 'vitest' |
| 8 | +import { DeploymentMonitor, type ProbeResult, type ProbeMetrics } from './deployment-monitor.js' |
| 9 | +import type { DeploymentsFile } from './deployment-config.js' |
| 10 | + |
| 11 | +const NOW = new Date('2026-08-30T12:00:00.000Z') |
| 12 | + |
| 13 | +const okResult = (metrics: ProbeMetrics = {}): ProbeResult => ({ ok: true, breaches: [], events: [], metrics }) |
| 14 | +const breachedResult = (breach: string, metrics: ProbeMetrics = {}): ProbeResult => |
| 15 | + ({ ok: false, breaches: [breach], events: [], metrics }) |
| 16 | + |
| 17 | +function makeConfig(): DeploymentsFile { |
| 18 | + return { |
| 19 | + deployments: [{ |
| 20 | + id: 'app-1', |
| 21 | + name: 'Codekin Prod', |
| 22 | + enabled: true, |
| 23 | + probes: [{ type: 'http', url: 'https://example.test/health' }], |
| 24 | + }], |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +describe('DeploymentMonitor', () => { |
| 29 | + let monitor: DeploymentMonitor |
| 30 | + let published: Array<{ kind: string; payload?: Record<string, unknown>; dedupeKey?: string }> |
| 31 | + |
| 32 | + function makeMonitor(httpResults: ProbeResult[], config: DeploymentsFile = makeConfig()) { |
| 33 | + published = [] |
| 34 | + let call = 0 |
| 35 | + monitor = new DeploymentMonitor({ |
| 36 | + dbPath: ':memory:', |
| 37 | + publish: (input) => published.push(input), |
| 38 | + loadConfig: () => config, |
| 39 | + runners: { |
| 40 | + http: async () => httpResults[Math.min(call++, httpResults.length - 1)], |
| 41 | + }, |
| 42 | + }) |
| 43 | + return monitor |
| 44 | + } |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + monitor.close() |
| 48 | + }) |
| 49 | + |
| 50 | + it('samples every enabled probe and records metrics', async () => { |
| 51 | + makeMonitor([okResult({ status: 200, latencyMs: 45 })]) |
| 52 | + await monitor.sampleAll(NOW) |
| 53 | + |
| 54 | + const samples = monitor.latestSamples() |
| 55 | + expect(samples).toHaveLength(1) |
| 56 | + expect(samples[0]).toMatchObject({ |
| 57 | + deploymentId: 'app-1', |
| 58 | + probeKey: 'app-1::http:https://example.test/health', |
| 59 | + ok: true, |
| 60 | + metrics: { status: 200, latencyMs: 45 }, |
| 61 | + }) |
| 62 | + expect(published).toHaveLength(0) |
| 63 | + }) |
| 64 | + |
| 65 | + it('skips disabled deployments', async () => { |
| 66 | + const config = makeConfig() |
| 67 | + config.deployments[0].enabled = false |
| 68 | + makeMonitor([okResult()], config) |
| 69 | + await monitor.sampleAll(NOW) |
| 70 | + expect(monitor.latestSamples()).toHaveLength(0) |
| 71 | + }) |
| 72 | + |
| 73 | + it('signals a breach only on the ok → breached transition, and recovery on the way back', async () => { |
| 74 | + makeMonitor([ |
| 75 | + okResult(), |
| 76 | + breachedResult('http 502'), |
| 77 | + breachedResult('http 502'), |
| 78 | + okResult(), |
| 79 | + ]) |
| 80 | + |
| 81 | + await monitor.sampleAll(NOW) // ok — nothing |
| 82 | + await monitor.sampleAll(new Date(NOW.getTime() + 5 * 60_000)) // breach → signal |
| 83 | + await monitor.sampleAll(new Date(NOW.getTime() + 10 * 60_000)) // still breached → silent |
| 84 | + await monitor.sampleAll(new Date(NOW.getTime() + 15 * 60_000)) // recovered → signal |
| 85 | + |
| 86 | + expect(published.map(p => p.kind)).toEqual(['probe-breach', 'probe-recovered']) |
| 87 | + expect(published[0].payload).toMatchObject({ |
| 88 | + deploymentId: 'app-1', |
| 89 | + deploymentName: 'Codekin Prod', |
| 90 | + breaches: ['http 502'], |
| 91 | + }) |
| 92 | + expect(published[0].dedupeKey).toBe('probe-breach::app-1::http:https://example.test/health') |
| 93 | + }) |
| 94 | + |
| 95 | + it('signals a breach immediately when the first-ever sample is breached', async () => { |
| 96 | + makeMonitor([breachedResult('unreachable: ECONNREFUSED')]) |
| 97 | + await monitor.sampleAll(NOW) |
| 98 | + expect(published.map(p => p.kind)).toEqual(['probe-breach']) |
| 99 | + }) |
| 100 | + |
| 101 | + it('publishes one-off events regardless of breach state', async () => { |
| 102 | + published = [] |
| 103 | + const config: DeploymentsFile = { |
| 104 | + deployments: [{ |
| 105 | + id: 'app-1', name: 'Codekin Prod', enabled: true, |
| 106 | + probes: [{ type: 'pm2', processName: 'codekin' }], |
| 107 | + }], |
| 108 | + } |
| 109 | + let call = 0 |
| 110 | + monitor = new DeploymentMonitor({ |
| 111 | + dbPath: ':memory:', |
| 112 | + publish: (input) => published.push(input), |
| 113 | + loadConfig: () => config, |
| 114 | + runners: { |
| 115 | + pm2: async (_probe, previous) => { |
| 116 | + call++ |
| 117 | + const restarts = call === 1 ? 3 : 4 |
| 118 | + const events: string[] = [] |
| 119 | + const prev = typeof previous?.restarts === 'number' ? previous.restarts : null |
| 120 | + if (prev !== null && restarts > prev) events.push(`restarted (${prev} → ${restarts})`) |
| 121 | + return { ok: true, breaches: [], events, metrics: { status: 'online', restarts, memoryMb: 120 } } |
| 122 | + }, |
| 123 | + }, |
| 124 | + }) |
| 125 | + |
| 126 | + await monitor.sampleAll(NOW) |
| 127 | + await monitor.sampleAll(new Date(NOW.getTime() + 5 * 60_000)) |
| 128 | + |
| 129 | + expect(published.map(p => p.kind)).toEqual(['probe-event']) |
| 130 | + expect(published[0].payload).toMatchObject({ event: 'restarted (3 → 4)' }) |
| 131 | + }) |
| 132 | + |
| 133 | + it('returns sample history newest-first and prunes by retention', async () => { |
| 134 | + makeMonitor([okResult({ status: 200 })]) |
| 135 | + await monitor.sampleAll(NOW) |
| 136 | + await monitor.sampleAll(new Date(NOW.getTime() + 5 * 60_000)) |
| 137 | + |
| 138 | + const key = 'app-1::http:https://example.test/health' |
| 139 | + const history = monitor.listSamples({ probeKey: key }) |
| 140 | + expect(history).toHaveLength(2) |
| 141 | + expect(history[0].id).toBeGreaterThan(history[1].id) |
| 142 | + |
| 143 | + // Both samples are in the past relative to a far-future cutoff → pruned. |
| 144 | + monitor.pruneSamples(-24 * 60 * 60 * 1000) |
| 145 | + expect(monitor.listSamples({ probeKey: key })).toHaveLength(0) |
| 146 | + }) |
| 147 | +}) |
0 commit comments