forked from Streampay-Org/StreamPay-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.test.ts
More file actions
134 lines (116 loc) · 4.67 KB
/
Copy pathdetector.test.ts
File metadata and controls
134 lines (116 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { AnomalyDetector } from "./detector";
import { MetricSnapshot } from "./types";
import { auditLogStore } from "./app/lib/audit-log";
describe("AnomalyDetector", () => {
const tenantId = "tenant_test_1";
const thresholds = {
creationBurstLimit: 10,
settleRateLimit: 5,
submissionFailureThreshold: 0.05,
maxDlqDepth: 10,
cancelBurstLimit: 5
};
beforeEach(() => {
AnomalyDetector.resetCancelHistory();
auditLogStore.reset([]);
});
const createSnapshot = (
creations: number,
settles: number,
submissionsTotal = 0,
submissionsFailed = 0,
dlqDepth = 0,
streamCancels = 0,
timestamp = Date.now()
): MetricSnapshot => ({
tenantId,
streamCreations: creations,
settleAttempts: settles,
stellarSubmissionsTotal: submissionsTotal,
stellarSubmissionsFailed: submissionsFailed,
oldestPendingJobSeconds: 0,
dlqDepth: dlqDepth,
p95SettlementLatencySeconds: 0,
streamCancels,
timestamp,
});
it("passes under normal load", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(5, 2, 100, 2, 0), thresholds);
expect(alerts).toHaveLength(0);
});
it("detects stream creation bursts", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(15, 2), thresholds);
expect(alerts).toHaveLength(1);
expect(alerts[0].ruleName).toBe("STREAM_CREATION_BURST");
expect(alerts[0].observedValue).toBe(15);
});
it("detects settle rate spikes", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(5, 10), thresholds);
expect(alerts).toHaveLength(1);
expect(alerts[0].ruleName).toBe("SETTLE_RATE_SPIKE");
});
it("detects high submission failure rate", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(5, 2, 100, 10, 0), thresholds);
expect(alerts).toHaveLength(1);
expect(alerts[0].ruleName).toBe("HIGH_SUBMISSION_FAILURE_RATE");
expect(alerts[0].observedValue).toBe(0.1);
});
it("detects DLQ depth exceeded", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(5, 2, 100, 2, 15), thresholds);
expect(alerts).toHaveLength(1);
expect(alerts[0].ruleName).toBe("DLQ_DEPTH_EXCEEDED");
expect(alerts[0].observedValue).toBe(15);
});
it("detects multiple anomalies simultaneously", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(20, 20, 100, 20, 20), thresholds);
expect(alerts).toHaveLength(4);
});
it("respects the whitelist/snooze mechanism", () => {
AnomalyDetector.setWhitelist(tenantId, true);
const alerts = AnomalyDetector.evaluate(createSnapshot(100, 100, 100, 50, 50, 10), thresholds);
expect(alerts).toHaveLength(0);
// Cleanup
AnomalyDetector.setWhitelist(tenantId, false);
});
it("passes under normal cancels limit", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(0, 0, 0, 0, 0, 3), thresholds);
expect(alerts).toHaveLength(0);
expect(auditLogStore.count()).toBe(0);
});
it("detects stream cancel burst", () => {
const alerts = AnomalyDetector.evaluate(createSnapshot(0, 0, 0, 0, 0, 6), thresholds);
expect(alerts).toHaveLength(1);
expect(alerts[0].ruleName).toBe("STREAM_CANCEL_BURST");
expect(alerts[0].observedValue).toBe(6);
expect(alerts[0].threshold).toBe(5);
// Verify audit log
expect(auditLogStore.count()).toBe(1);
const log = auditLogStore.list()[0];
expect(log.action).toBe("security.anomaly.cancel_burst");
expect(log.target.id).toBe(tenantId);
expect(log.metadata?.observedValue).toBe(6);
});
it("detects stream cancel burst over sliding window (multiple evaluations)", () => {
const now = Date.now();
// First evaluation: 3 cancels at t=now
let alerts = AnomalyDetector.evaluate(createSnapshot(0, 0, 0, 0, 0, 3, now), thresholds);
expect(alerts).toHaveLength(0);
expect(auditLogStore.count()).toBe(0);
// Second evaluation: 3 cancels at t=now + 10s
alerts = AnomalyDetector.evaluate(createSnapshot(0, 0, 0, 0, 0, 3, now + 10000), thresholds);
expect(alerts).toHaveLength(1);
expect(alerts[0].ruleName).toBe("STREAM_CANCEL_BURST");
expect(alerts[0].observedValue).toBe(6);
expect(auditLogStore.count()).toBe(1);
});
it("does not alert if cancels are spread out beyond 1 minute", () => {
const now = Date.now();
// First evaluation: 3 cancels at t=now
let alerts = AnomalyDetector.evaluate(createSnapshot(0, 0, 0, 0, 0, 3, now), thresholds);
expect(alerts).toHaveLength(0);
// Second evaluation: 3 cancels at t=now + 70s
alerts = AnomalyDetector.evaluate(createSnapshot(0, 0, 0, 0, 0, 3, now + 70000), thresholds);
expect(alerts).toHaveLength(0);
expect(auditLogStore.count()).toBe(0);
});
});