-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoverrides_test.go
More file actions
380 lines (317 loc) · 9.85 KB
/
overrides_test.go
File metadata and controls
380 lines (317 loc) · 9.85 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package sai
import (
"path/filepath"
"testing"
"time"
)
func TestAgentPatternMatching(t *testing.T) {
tests := []struct {
name string
procPattern string
domainPat string
process string
domain string
shouldMatch bool
}{
{"wildcard_any", "*", "*.openai.com", "anyprocess", "api.openai.com", true},
{"suffix_domain_match", "*", "*.openai.com", "curl", "api.openai.com", true},
{"suffix_domain_exact", "*", "*.openai.com", "curl", "openai.com", true},
{"suffix_domain_no_match", "*", "*.openai.com", "curl", "api.closed.com", false},
{"prefix_process_match", "claude*", "*", "claude-code", "api.anthropic.com", true},
{"prefix_process_no_match", "claude*", "*", "cursor", "api.anthropic.com", false},
{"exact_match", "curl", "api.openai.com", "curl", "api.openai.com", true},
{"exact_process_no_match", "curl", "api.openai.com", "wget", "api.openai.com", false},
{"exact_domain_no_match", "curl", "api.openai.com", "curl", "api.anthropic.com", false},
{"substring_process", "python", "*", "python3.10", "api.openai.com", true},
{"substring_domain", "*", "openai", "curl", "api.openai.com", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := NewOverrides()
o.AddAgent("test-agent", tt.procPattern, []string{tt.domainPat})
got := o.MatchAgent(tt.process, tt.domain)
if tt.shouldMatch && got == "" {
t.Errorf("expected match for process=%q domain=%q (proc_pat=%q, dom_pat=%q)",
tt.process, tt.domain, tt.procPattern, tt.domainPat)
}
if !tt.shouldMatch && got != "" {
t.Errorf("expected no match for process=%q domain=%q, got %q", tt.process, tt.domain, got)
}
})
}
}
func TestAgentMatchRequiresBothProcessAndDomain(t *testing.T) {
o := NewOverrides()
o.AddAgent("openai", "python*", []string{"*.openai.com"})
if o.MatchAgent("python3", "api.openai.com") == "" {
t.Error("should match when both process and domain match")
}
if o.MatchAgent("python3", "api.anthropic.com") != "" {
t.Error("should not match when domain doesn't match")
}
if o.MatchAgent("curl", "api.openai.com") != "" {
t.Error("should not match when process doesn't match")
}
}
func TestMatchIsCaseInsensitive(t *testing.T) {
o := NewOverrides()
o.AddAgent("test", "Python*", []string{"*.OpenAI.com"})
if o.MatchAgent("PYTHON3", "API.OPENAI.COM") == "" {
t.Error("match should be case insensitive")
}
if o.MatchAgent("python3", "api.openai.com") == "" {
t.Error("match should work with lowercase")
}
}
func TestNoiseBlocksSubdomains(t *testing.T) {
o := NewOverrides()
o.AddNoise("google.com")
tests := []struct {
domain string
shouldBlock bool
}{
{"google.com", true},
{"api.google.com", true},
{"www.google.com", true},
{"deep.nested.google.com", true},
{"notgoogle.com", false},
{"google.org", false},
}
for _, tt := range tests {
t.Run(tt.domain, func(t *testing.T) {
got := o.IsNoise(tt.domain)
if got != tt.shouldBlock {
t.Errorf("IsNoise(%q) = %v, want %v", tt.domain, got, tt.shouldBlock)
}
})
}
}
func TestOverridesSaveLoad(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "overrides.bin")
o := NewOverrides()
o.AddAgent("claude", "claude*", []string{"*.anthropic.com"})
o.AddAgent("openai", "python*", []string{"*.openai.com", "api.openai.com"})
o.AddNoise("google.com")
o.AddNoise("apple.com")
if err := o.Save(path); err != nil {
t.Fatalf("Save failed: %v", err)
}
loaded := NewOverrides()
if err := loaded.Load(path); err != nil {
t.Fatalf("Load failed: %v", err)
}
if loaded.MatchAgent("claude-code", "api.anthropic.com") == "" {
t.Error("loaded Overrides should match claude agent")
}
if loaded.MatchAgent("python3", "api.openai.com") == "" {
t.Error("loaded Overrides should match openai agent")
}
if !loaded.IsNoise("google.com") {
t.Error("loaded Overrides should have google.com as noise")
}
if !loaded.IsNoise("api.apple.com") {
t.Error("loaded Overrides should block apple.com subdomains")
}
}
func TestDomainNormalization(t *testing.T) {
o := NewOverrides()
o.AddAgent("test", "*", []string{"example.com"})
tests := []struct {
domain string
shouldMatch bool
}{
{"example.com", true},
{"EXAMPLE.COM", true},
{"www.example.com", true},
{"WWW.EXAMPLE.COM", true},
}
for _, tt := range tests {
t.Run(tt.domain, func(t *testing.T) {
got := o.MatchAgent("test", tt.domain)
if tt.shouldMatch && got == "" {
t.Errorf("expected match for %q", tt.domain)
}
})
}
}
func TestMatchAgentPriority(t *testing.T) {
o := NewOverrides()
o.AddAgent("first", "*", []string{"*.example.com"})
o.AddAgent("second", "*", []string{"*.example.com"})
got := o.MatchAgent("test", "api.example.com")
if got != "first" {
t.Errorf("first matching agent should win, got %q", got)
}
}
func TestEmptyOverrides(t *testing.T) {
o := NewOverrides()
if o.MatchAgent("curl", "api.openai.com") != "" {
t.Error("empty Overrides should not match any agent")
}
if o.IsNoise("google.com") {
t.Error("empty Overrides should not have any noise domains")
}
}
func TestAddAgentDomain(t *testing.T) {
o := NewOverrides()
o.AddAgent("test", "curl", []string{"api.example.com"})
o.AddAgentDomain("test", "api.other.com")
if o.MatchAgent("curl", "api.example.com") == "" {
t.Error("should match original domain")
}
if o.MatchAgent("curl", "api.other.com") == "" {
t.Error("should match added domain")
}
}
func TestAddAgentDomainNonExistent(t *testing.T) {
o := NewOverrides()
o.AddAgentDomain("nonexistent", "api.example.com")
agents := o.ListAgents()
if len(agents) != 0 {
t.Error("adding domain to non-existent agent should not create agent")
}
}
func TestRemoveAgent(t *testing.T) {
o := NewOverrides()
o.AddAgent("test", "curl", []string{"api.example.com"})
if o.MatchAgent("curl", "api.example.com") == "" {
t.Error("agent should match before removal")
}
o.RemoveAgent("test")
if o.MatchAgent("curl", "api.example.com") != "" {
t.Error("agent should not match after removal")
}
}
func TestRemoveNoise(t *testing.T) {
o := NewOverrides()
o.AddNoise("google.com")
if !o.IsNoise("google.com") {
t.Error("domain should be noise")
}
o.RemoveNoise("google.com")
if o.IsNoise("google.com") {
t.Error("google.com should not be noise after removal")
}
}
func TestExportImportRoundTrip(t *testing.T) {
o := NewOverrides()
o.AddAgent("claude", "claude*", []string{"*.anthropic.com"})
o.AddAgent("openai", "python*", []string{"*.openai.com", "api.openai.com"})
o.AddNoise("google.com")
o.AddNoise("apple.com")
data := o.Export()
if len(data.Agents) != 2 {
t.Errorf("exported agents = %d, want 2", len(data.Agents))
}
if len(data.Noise) != 2 {
t.Errorf("exported noise = %d, want 2", len(data.Noise))
}
loaded := NewOverrides()
loaded.Import(data)
if loaded.MatchAgent("claude-code", "api.anthropic.com") == "" {
t.Error("imported Overrides should match claude agent")
}
if loaded.MatchAgent("python3", "api.openai.com") == "" {
t.Error("imported Overrides should match openai agent")
}
if !loaded.IsNoise("google.com") {
t.Error("imported Overrides should have google.com as noise")
}
if !loaded.IsNoise("api.apple.com") {
t.Error("imported Overrides should block apple.com subdomains")
}
}
func TestExportImportIsolated(t *testing.T) {
o := NewOverrides()
o.AddAgent("test", "test*", []string{"*.test.com"})
o.AddNoise("example.com")
data := o.Export()
data.Agents[0].Name = "modified"
data.Noise[0] = "modified.com"
if o.MatchAgent("test-app", "api.test.com") != "test" {
t.Error("modifying exported data should not affect original")
}
if !o.IsNoise("example.com") {
t.Error("modifying exported data should not affect original noise")
}
}
func TestAddAgentSetsMetadata(t *testing.T) {
before := time.Now()
o := NewOverrides()
o.AddAgent("test", "test*", []string{"*.test.com"})
after := time.Now()
agent := o.GetAgent("test")
if agent == nil {
t.Fatal("agent not found")
}
if agent.CreatedAt.Before(before) || agent.CreatedAt.After(after) {
t.Errorf("CreatedAt should be between %v and %v, got %v", before, after, agent.CreatedAt)
}
}
func TestMetadataSurvivesSaveLoad(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "overrides.bin")
o := NewOverrides()
o.AddAgent("test", "test*", []string{"*.test.com"})
original := o.GetAgent("test")
if original == nil {
t.Fatal("agent not found")
}
if err := o.Save(path); err != nil {
t.Fatalf("Save failed: %v", err)
}
loaded := NewOverrides()
if err := loaded.Load(path); err != nil {
t.Fatalf("Load failed: %v", err)
}
agent := loaded.GetAgent("test")
if agent == nil {
t.Fatal("loaded agent not found")
}
if !agent.CreatedAt.Equal(original.CreatedAt) {
t.Errorf("CreatedAt not preserved: got %v, want %v", agent.CreatedAt, original.CreatedAt)
}
}
func TestImportPreservesMetadata(t *testing.T) {
createdAt := time.Date(2025, 6, 15, 10, 30, 0, 0, time.UTC)
data := OverridesData{
Agents: []Agent{
{Name: "test", Process: "test*", Domains: []string{"*.test.com"}, CreatedAt: createdAt},
},
}
o := NewOverrides()
o.Import(data)
agent := o.GetAgent("test")
if agent == nil {
t.Fatal("imported agent not found")
}
if !agent.CreatedAt.Equal(createdAt) {
t.Errorf("CreatedAt = %v, want %v", agent.CreatedAt, createdAt)
}
}
func TestBackwardCompatibilityZeroMetadata(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "overrides.bin")
data := OverridesData{
Agents: []Agent{
{Name: "legacy", Process: "legacy*", Domains: []string{"*.legacy.com"}},
},
}
o := NewOverrides()
o.Import(data)
if err := o.Save(path); err != nil {
t.Fatalf("Save failed: %v", err)
}
loaded := NewOverrides()
if err := loaded.Load(path); err != nil {
t.Fatalf("Load failed: %v", err)
}
agent := loaded.GetAgent("legacy")
if agent == nil {
t.Fatal("legacy agent not found")
}
if !agent.CreatedAt.IsZero() {
t.Errorf("CreatedAt should be zero for legacy data, got %v", agent.CreatedAt)
}
}