|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package agentdrain |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAnomalyDetection_NewTemplate(t *testing.T) { |
| 10 | + d := NewAnomalyDetector(0.4, 2) |
| 11 | + c := &Cluster{ID: 1, Template: []string{"stage=plan"}, Size: 1} |
| 12 | + result := &MatchResult{ClusterID: 1, Similarity: 1.0} |
| 13 | + |
| 14 | + report := d.Analyze(result, true, c) |
| 15 | + |
| 16 | + if !report.IsNewTemplate { |
| 17 | + t.Error("expected IsNewTemplate=true") |
| 18 | + } |
| 19 | + if !report.NewClusterCreated { |
| 20 | + t.Error("expected NewClusterCreated=true") |
| 21 | + } |
| 22 | + if report.AnomalyScore <= 0 { |
| 23 | + t.Errorf("expected positive anomaly score for new template, got %v", report.AnomalyScore) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestAnomalyDetection_LowSimilarity(t *testing.T) { |
| 28 | + d := NewAnomalyDetector(0.4, 2) |
| 29 | + // Size=5 means not rare; not new. |
| 30 | + c := &Cluster{ID: 1, Template: []string{"a", "b", "c"}, Size: 5} |
| 31 | + result := &MatchResult{ClusterID: 1, Similarity: 0.2} |
| 32 | + |
| 33 | + report := d.Analyze(result, false, c) |
| 34 | + |
| 35 | + if !report.LowSimilarity { |
| 36 | + t.Error("expected LowSimilarity=true for similarity below threshold") |
| 37 | + } |
| 38 | + if report.IsNewTemplate { |
| 39 | + t.Error("expected IsNewTemplate=false") |
| 40 | + } |
| 41 | + if report.AnomalyScore <= 0 { |
| 42 | + t.Errorf("expected positive anomaly score, got %v", report.AnomalyScore) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestAnomalyDetection_RareCluster(t *testing.T) { |
| 47 | + d := NewAnomalyDetector(0.4, 2) |
| 48 | + c := &Cluster{ID: 1, Template: []string{"a"}, Size: 1} |
| 49 | + result := &MatchResult{ClusterID: 1, Similarity: 0.9} |
| 50 | + |
| 51 | + report := d.Analyze(result, false, c) |
| 52 | + |
| 53 | + if !report.RareCluster { |
| 54 | + t.Error("expected RareCluster=true for size=1 with rareThreshold=2") |
| 55 | + } |
| 56 | + if report.AnomalyScore <= 0 { |
| 57 | + t.Errorf("expected positive anomaly score, got %v", report.AnomalyScore) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestAnomalyDetection_Normal(t *testing.T) { |
| 62 | + d := NewAnomalyDetector(0.4, 2) |
| 63 | + // High size, high similarity, not new. |
| 64 | + c := &Cluster{ID: 1, Template: []string{"a", "b"}, Size: 100} |
| 65 | + result := &MatchResult{ClusterID: 1, Similarity: 0.9} |
| 66 | + |
| 67 | + report := d.Analyze(result, false, c) |
| 68 | + |
| 69 | + if report.IsNewTemplate { |
| 70 | + t.Error("expected IsNewTemplate=false") |
| 71 | + } |
| 72 | + if report.LowSimilarity { |
| 73 | + t.Error("expected LowSimilarity=false") |
| 74 | + } |
| 75 | + if report.RareCluster { |
| 76 | + t.Error("expected RareCluster=false") |
| 77 | + } |
| 78 | + if report.AnomalyScore > 0 { |
| 79 | + t.Errorf("expected zero anomaly score for normal event, got %v", report.AnomalyScore) |
| 80 | + } |
| 81 | + if report.Reason != "no anomaly detected" { |
| 82 | + t.Errorf("expected 'no anomaly detected', got %q", report.Reason) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestAnalyzeEvent(t *testing.T) { |
| 87 | + cfg := DefaultConfig() |
| 88 | + m, err := NewMiner(cfg) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("NewMiner: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + // First occurrence → new template. |
| 94 | + evt := AgentEvent{ |
| 95 | + Stage: "plan", |
| 96 | + Fields: map[string]string{"action": "start", "model": "gpt-4"}, |
| 97 | + } |
| 98 | + result, report, err := m.AnalyzeEvent(evt) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("AnalyzeEvent: %v", err) |
| 101 | + } |
| 102 | + if result == nil { |
| 103 | + t.Fatal("AnalyzeEvent: expected non-nil result") |
| 104 | + } |
| 105 | + if report == nil { |
| 106 | + t.Fatal("AnalyzeEvent: expected non-nil report") |
| 107 | + } |
| 108 | + if !report.IsNewTemplate { |
| 109 | + t.Error("first event should be a new template") |
| 110 | + } |
| 111 | + |
| 112 | + // Second occurrence of the same event → not new. |
| 113 | + result2, report2, err := m.AnalyzeEvent(evt) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("AnalyzeEvent (second): %v", err) |
| 116 | + } |
| 117 | + if result2 == nil || report2 == nil { |
| 118 | + t.Fatal("AnalyzeEvent (second): expected non-nil results") |
| 119 | + } |
| 120 | + if report2.IsNewTemplate { |
| 121 | + t.Error("second identical event should not be a new template") |
| 122 | + } |
| 123 | +} |
0 commit comments