-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaccumulator_test.go
More file actions
269 lines (213 loc) · 6.67 KB
/
Copy pathaccumulator_test.go
File metadata and controls
269 lines (213 loc) · 6.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package sai
import (
"testing"
"time"
)
func TestAccumulatorRecordsStats(t *testing.T) {
acc := NewAccumulator()
event := makeEvent("curl", "api.openai.com", "tls")
acc.Record(event)
if acc.Count("curl", "api.openai.com") != 1 {
t.Error("count should be 1 after recording")
}
acc.Record(event)
acc.Record(event)
if acc.Count("curl", "api.openai.com") != 3 {
t.Error("count should be 3 after recording 3 times")
}
stats := acc.Stats("curl", "api.openai.com")
if stats == nil {
t.Fatal("stats should not be nil")
}
if stats.Count != 3 {
t.Errorf("stats.Count = %d, want 3", stats.Count)
}
if stats.Sources["tls"] != 3 {
t.Errorf("stats.Sources[tls] = %d, want 3", stats.Sources["tls"])
}
}
func TestAccumulatorExtractsExtras(t *testing.T) {
acc := NewAccumulator()
event := makeEventWithExtras("curl", "api.openai.com", map[string]string{
"bytes_in": "1000",
"bytes_out": "500",
"packets_in": "100",
"packets_out": "50",
"duration_ms": "5000",
"concurrent": "3",
"programmatic": "true",
})
acc.Record(event)
stats := acc.Stats("curl", "api.openai.com")
if stats == nil {
t.Fatal("stats should not be nil")
}
if stats.TotalBytesIn != 1000 {
t.Errorf("TotalBytesIn = %d, want 1000", stats.TotalBytesIn)
}
if stats.TotalBytesOut != 500 {
t.Errorf("TotalBytesOut = %d, want 500", stats.TotalBytesOut)
}
if stats.TotalPacketsIn != 100 {
t.Errorf("TotalPacketsIn = %d, want 100", stats.TotalPacketsIn)
}
if stats.TotalPacketsOut != 50 {
t.Errorf("TotalPacketsOut = %d, want 50", stats.TotalPacketsOut)
}
if stats.TotalDurationMs != 5000 {
t.Errorf("TotalDurationMs = %d, want 5000", stats.TotalDurationMs)
}
if stats.MaxConcurrent != 3 {
t.Errorf("MaxConcurrent = %d, want 3", stats.MaxConcurrent)
}
if !stats.IsProgrammatic {
t.Error("IsProgrammatic should be true")
}
}
func TestKnownAgentReturnsOne(t *testing.T) {
fs := NewOverrides()
fs.AddAgent("claude", "claude*", []string{"*.anthropic.com"})
acc := NewAccumulatorWithOverrides(fs, NewClassifierRegistry())
event := makeEvent("claude-code", "api.anthropic.com", "tls")
acc.Record(event)
conf := acc.AIScore("claude-code", "api.anthropic.com")
if conf != 1.0 {
t.Errorf("known agent confidence = %v, want 1.0", conf)
}
}
func TestNonAIReturnsZero(t *testing.T) {
fs := NewOverrides()
fs.AddNoise("google.com")
acc := NewAccumulatorWithOverrides(fs, NewClassifierRegistry())
event := makeEvent("chrome", "google.com", "tls")
acc.Record(event)
conf := acc.AIScore("chrome", "google.com")
if conf != 0.0 {
t.Errorf("non-AI confidence = %v, want 0.0", conf)
}
}
func TestHeuristicScoreFromRegistry(t *testing.T) {
fs := NewOverrides()
registry := NewClassifierRegistry()
registry.Add(NewDefaultClassifier())
acc := NewAccumulatorWithOverrides(fs, registry)
event := makeEventWithExtras("unknown", "api.example.com", map[string]string{
"bytes_in": "100000",
"bytes_out": "1000",
"packets_in": "1000",
"packets_out": "10",
"duration_ms": "10000",
})
acc.Record(event)
conf := acc.AIScore("unknown", "api.example.com")
if conf <= 0 {
t.Errorf("heuristic confidence should be > 0, got %v", conf)
}
}
func TestAIScoreCappedAt099(t *testing.T) {
fs := NewOverrides()
registry := NewClassifierRegistry()
registry.Add(&mockClassifier{name: "high", conf: 1.5})
acc := NewAccumulatorWithOverrides(fs, registry)
event := makeEvent("test", "example.com", "tls")
acc.Record(event)
conf := acc.AIScore("test", "example.com")
if conf > 0.99 {
t.Errorf("heuristic confidence should be capped at 0.99, got %v", conf)
}
}
func TestStatsCopied(t *testing.T) {
acc := NewAccumulator()
event := makeEvent("curl", "api.openai.com", "tls")
acc.Record(event)
stats1 := acc.Stats("curl", "api.openai.com")
stats2 := acc.Stats("curl", "api.openai.com")
stats1.Count = 999
stats1.Sources["modified"] = 1
if stats2.Count == 999 {
t.Error("modifying returned stats should not affect internal state")
}
if stats2.Sources["modified"] == 1 {
t.Error("modifying returned sources map should not affect internal state")
}
}
func TestAccumulatorReset(t *testing.T) {
acc := NewAccumulator()
event := makeEvent("curl", "api.openai.com", "tls")
acc.Record(event)
if acc.Count("curl", "api.openai.com") != 1 {
t.Error("count should be 1")
}
acc.Reset()
if acc.Count("curl", "api.openai.com") != 0 {
t.Error("count should be 0 after reset")
}
if acc.Stats("curl", "api.openai.com") != nil {
t.Error("stats should be nil after reset")
}
}
func TestAccumulatorDomainNormalization(t *testing.T) {
acc := NewAccumulator()
acc.Record(makeEvent("curl", "WWW.EXAMPLE.COM", "tls"))
acc.Record(makeEvent("curl", "www.example.com", "tls"))
acc.Record(makeEvent("curl", "example.com", "tls"))
if acc.Count("curl", "example.com") != 3 {
t.Error("all normalized domains should accumulate to same key")
}
}
func TestAccumulatorNoStats(t *testing.T) {
acc := NewAccumulator()
stats := acc.Stats("nonexistent", "nowhere.com")
if stats != nil {
t.Error("stats for non-recorded pair should be nil")
}
count := acc.Count("nonexistent", "nowhere.com")
if count != 0 {
t.Error("count for non-recorded pair should be 0")
}
}
func TestAccumulatorFirstLastSeen(t *testing.T) {
acc := NewAccumulator()
first := time.Now()
event1 := Event{Timestamp: first, Process: "curl", Domain: "example.com", Source: "tls"}
acc.Record(event1)
later := first.Add(time.Hour)
event2 := Event{Timestamp: later, Process: "curl", Domain: "example.com", Source: "tls"}
acc.Record(event2)
stats := acc.Stats("curl", "example.com")
if !stats.FirstSeen.Equal(first) {
t.Errorf("FirstSeen = %v, want %v", stats.FirstSeen, first)
}
if !stats.LastSeen.Equal(later) {
t.Errorf("LastSeen = %v, want %v", stats.LastSeen, later)
}
}
type mockSignals struct {
agents map[string]string
nonAIDom map[string]bool
}
func (m *mockSignals) MatchAgent(process, domain string) string {
return m.agents[process+":"+domain]
}
func (m *mockSignals) IsNonAIDomain(domain string) bool {
return m.nonAIDom[domain]
}
func TestCustomSignalsImplementation(t *testing.T) {
signals := &mockSignals{
agents: map[string]string{"claude-code:api.anthropic.com": "claude"},
nonAIDom: map[string]bool{"google.com": true},
}
acc := NewAccumulatorWithSignals(signals, NewClassifierRegistry())
event := makeEvent("claude-code", "api.anthropic.com", "tls")
acc.Record(event)
conf := acc.AIScore("claude-code", "api.anthropic.com")
if conf != 1.0 {
t.Errorf("custom signals agent confidence = %v, want 1.0", conf)
}
event2 := makeEvent("chrome", "google.com", "tls")
acc.Record(event2)
conf2 := acc.AIScore("chrome", "google.com")
if conf2 != 0.0 {
t.Errorf("custom signals non-AI confidence = %v, want 0.0", conf2)
}
}