|
| 1 | +import { MonitoringService } from '../monitoring'; |
| 2 | +import type { TransactionEvent } from '../types'; |
| 3 | + |
| 4 | +const makeEvent = ( |
| 5 | + status: TransactionEvent['status'], |
| 6 | + gasUsed?: number, |
| 7 | + id = Math.random().toString(36) |
| 8 | +): TransactionEvent => ({ |
| 9 | + id, |
| 10 | + subscriptionId: 'sub-1', |
| 11 | + amount: 10, |
| 12 | + currency: 'USD', |
| 13 | + status, |
| 14 | + timestamp: Date.now(), |
| 15 | + gasUsed, |
| 16 | +}); |
| 17 | + |
| 18 | +describe('MonitoringService', () => { |
| 19 | + let svc: MonitoringService; |
| 20 | + beforeEach(() => { |
| 21 | + svc = new MonitoringService(); |
| 22 | + }); |
| 23 | + |
| 24 | + // ── Transaction recording ───────────────────────────────────────────────── |
| 25 | + |
| 26 | + it('records transactions and reflects them in dashboard', () => { |
| 27 | + svc.recordTransaction(makeEvent('success')); |
| 28 | + svc.recordTransaction(makeEvent('success')); |
| 29 | + const dash = svc.getDashboard(); |
| 30 | + expect(dash.totalTransactions).toBe(2); |
| 31 | + expect(dash.failureCount).toBe(0); |
| 32 | + expect(dash.successRate).toBe(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('tracks failed transactions', () => { |
| 36 | + svc.recordTransaction(makeEvent('success')); |
| 37 | + svc.recordTransaction(makeEvent('failed')); |
| 38 | + const dash = svc.getDashboard(); |
| 39 | + expect(dash.failureCount).toBe(1); |
| 40 | + expect(dash.successRate).toBe(0.5); |
| 41 | + }); |
| 42 | + |
| 43 | + it('computes average gas used', () => { |
| 44 | + svc.recordTransaction(makeEvent('success', 100_000)); |
| 45 | + svc.recordTransaction(makeEvent('success', 300_000)); |
| 46 | + expect(svc.getDashboard().avgGasUsed).toBe(200_000); |
| 47 | + }); |
| 48 | + |
| 49 | + // ── Anomaly detection ───────────────────────────────────────────────────── |
| 50 | + |
| 51 | + it('raises critical alert when failure rate exceeds 30 %', () => { |
| 52 | + // 4 failures out of 5 = 80 % |
| 53 | + for (let i = 0; i < 4; i++) svc.recordTransaction(makeEvent('failed')); |
| 54 | + svc.recordTransaction(makeEvent('success')); |
| 55 | + const alerts = svc.getActiveAlerts(); |
| 56 | + expect(alerts.some((a) => a.ruleId === 'high-failure-rate')).toBe(true); |
| 57 | + expect(alerts.find((a) => a.ruleId === 'high-failure-rate')?.severity).toBe('critical'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('raises warning alert when avg gas exceeds 500 000', () => { |
| 61 | + svc.recordTransaction(makeEvent('success', 600_000)); |
| 62 | + expect(svc.getActiveAlerts().some((a) => a.ruleId === 'gas-spike')).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it('does not raise duplicate alerts for the same open rule', () => { |
| 66 | + for (let i = 0; i < 6; i++) svc.recordTransaction(makeEvent('failed')); |
| 67 | + const alerts = svc.getActiveAlerts().filter((a) => a.ruleId === 'high-failure-rate'); |
| 68 | + expect(alerts).toHaveLength(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does not alert when failure rate is below threshold', () => { |
| 72 | + svc.recordTransaction(makeEvent('success')); |
| 73 | + svc.recordTransaction(makeEvent('success')); |
| 74 | + expect(svc.getActiveAlerts().some((a) => a.ruleId === 'high-failure-rate')).toBe(false); |
| 75 | + }); |
| 76 | + |
| 77 | + // ── Alert resolution ────────────────────────────────────────────────────── |
| 78 | + |
| 79 | + it('resolves an alert by id', () => { |
| 80 | + for (let i = 0; i < 4; i++) svc.recordTransaction(makeEvent('failed')); |
| 81 | + svc.recordTransaction(makeEvent('success')); |
| 82 | + const alert = svc.getActiveAlerts().find((a) => a.ruleId === 'high-failure-rate')!; |
| 83 | + svc.resolveAlert(alert.id); |
| 84 | + expect(svc.getActiveAlerts().some((a) => a.id === alert.id)).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + // ── Custom rules ────────────────────────────────────────────────────────── |
| 88 | + |
| 89 | + it('supports adding a custom alert rule', () => { |
| 90 | + svc.addRule({ |
| 91 | + id: 'custom-rule', |
| 92 | + name: 'Custom Rule', |
| 93 | + severity: 'info', |
| 94 | + message: 'Custom triggered', |
| 95 | + evaluate: () => true, |
| 96 | + }); |
| 97 | + svc.recordTransaction(makeEvent('success')); |
| 98 | + expect(svc.getActiveAlerts().some((a) => a.ruleId === 'custom-rule')).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('supports removing a rule', () => { |
| 102 | + svc.removeRule('gas-spike'); |
| 103 | + svc.recordTransaction(makeEvent('success', 999_999)); |
| 104 | + expect(svc.getActiveAlerts().some((a) => a.ruleId === 'gas-spike')).toBe(false); |
| 105 | + }); |
| 106 | + |
| 107 | + // ── Dashboard ───────────────────────────────────────────────────────────── |
| 108 | + |
| 109 | + it('dashboard returns empty state when no events recorded', () => { |
| 110 | + const dash = svc.getDashboard(); |
| 111 | + expect(dash.totalTransactions).toBe(0); |
| 112 | + expect(dash.successRate).toBe(1); |
| 113 | + expect(dash.activeAlerts).toHaveLength(0); |
| 114 | + }); |
| 115 | +}); |
0 commit comments