|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAppendAndRead(t *testing.T) { |
| 10 | + tmp := t.TempDir() |
| 11 | + |
| 12 | + AppendSearchMetric(tmp, SearchMetric{ |
| 13 | + Tool: "rag_search", Query: "auth", ResultCount: 5, |
| 14 | + TopScore: 0.85, Source: "vector", BytesSaved: 14000, TokensSaved: 3500, ResponseMs: 120, |
| 15 | + }) |
| 16 | + AppendSearchMetric(tmp, SearchMetric{ |
| 17 | + Tool: "rag_search", Query: "config", ResultCount: 0, |
| 18 | + TopScore: 0, Source: "fallback", BytesSaved: 0, TokensSaved: 0, ResponseMs: 80, |
| 19 | + }) |
| 20 | + AppendSearchMetric(tmp, SearchMetric{ |
| 21 | + Tool: "rag_find_usages", Query: "MyFunc", ResultCount: 3, |
| 22 | + TopScore: 0.92, Source: "vector", BytesSaved: 5000, TokensSaved: 1250, ResponseMs: 60, |
| 23 | + }) |
| 24 | + |
| 25 | + // Verify file exists |
| 26 | + path := filepath.Join(tmp, ".ragcode", metricsFile) |
| 27 | + if _, err := os.Stat(path); err != nil { |
| 28 | + t.Fatalf("metrics file not created: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + agg := ReadAggregatedMetrics(tmp) |
| 32 | + if agg == nil { |
| 33 | + t.Fatal("expected aggregated metrics, got nil") |
| 34 | + } |
| 35 | + |
| 36 | + if agg.TotalSearches != 3 { |
| 37 | + t.Errorf("TotalSearches=%d, want 3", agg.TotalSearches) |
| 38 | + } |
| 39 | + if agg.SearchesWithResults != 2 { |
| 40 | + t.Errorf("SearchesWithResults=%d, want 2", agg.SearchesWithResults) |
| 41 | + } |
| 42 | + if agg.FallbackSearches != 1 { |
| 43 | + t.Errorf("FallbackSearches=%d, want 1", agg.FallbackSearches) |
| 44 | + } |
| 45 | + if agg.VectorSearches != 2 { |
| 46 | + t.Errorf("VectorSearches=%d, want 2", agg.VectorSearches) |
| 47 | + } |
| 48 | + if agg.TotalBytesSaved != 19000 { |
| 49 | + t.Errorf("TotalBytesSaved=%d, want 19000", agg.TotalBytesSaved) |
| 50 | + } |
| 51 | + if agg.TotalTokensSaved != 4750 { |
| 52 | + t.Errorf("TotalTokensSaved=%d, want 4750", agg.TotalTokensSaved) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestReadEmptyWorkspace(t *testing.T) { |
| 57 | + tmp := t.TempDir() |
| 58 | + agg := ReadAggregatedMetrics(tmp) |
| 59 | + if agg != nil { |
| 60 | + t.Error("expected nil for workspace without metrics") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestReadEmptyString(t *testing.T) { |
| 65 | + agg := ReadAggregatedMetrics("") |
| 66 | + if agg != nil { |
| 67 | + t.Error("expected nil for empty workspace") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestAppendEmptyWorkspace(t *testing.T) { |
| 72 | + // Should not panic |
| 73 | + AppendSearchMetric("", SearchMetric{Tool: "test"}) |
| 74 | +} |
0 commit comments