|
| 1 | +package benchmark_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/tae2089/code-context-graph/internal/benchmark" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMatchFiles_AllMatch(t *testing.T) { |
| 11 | + result := benchmark.RunResult{ |
| 12 | + FilesRead: []string{"internal/webhook/handler.go", "internal/webhook/syncqueue.go"}, |
| 13 | + } |
| 14 | + query := benchmark.Query{ |
| 15 | + ExpectedFiles: []string{"internal/webhook/handler.go", "internal/webhook/syncqueue.go"}, |
| 16 | + } |
| 17 | + ratio := benchmark.MatchFiles(result, query) |
| 18 | + if ratio != 1.0 { |
| 19 | + t.Errorf("FileHitRatio: got %.3f, want 1.0", ratio) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestMatchFiles_PartialMatch(t *testing.T) { |
| 24 | + result := benchmark.RunResult{ |
| 25 | + FilesRead: []string{"internal/webhook/handler.go"}, |
| 26 | + } |
| 27 | + query := benchmark.Query{ |
| 28 | + ExpectedFiles: []string{"internal/webhook/handler.go", "internal/webhook/syncqueue.go", "internal/webhook/auth.go"}, |
| 29 | + } |
| 30 | + ratio := benchmark.MatchFiles(result, query) |
| 31 | + want := 1.0 / 3.0 |
| 32 | + if ratio < want-0.001 || ratio > want+0.001 { |
| 33 | + t.Errorf("FileHitRatio: got %.3f, want %.3f", ratio, want) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestMatchFiles_NoExpected(t *testing.T) { |
| 38 | + result := benchmark.RunResult{FilesRead: []string{"any.go"}} |
| 39 | + query := benchmark.Query{ExpectedFiles: nil} |
| 40 | + ratio := benchmark.MatchFiles(result, query) |
| 41 | + if ratio != 1.0 { |
| 42 | + t.Errorf("FileHitRatio: got %.3f, want 1.0 when no expected files", ratio) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestMatchFiles_AbsolutePathSuffix(t *testing.T) { |
| 47 | + result := benchmark.RunResult{ |
| 48 | + FilesRead: []string{"/Users/user/repo/internal/webhook/handler.go"}, |
| 49 | + } |
| 50 | + query := benchmark.Query{ |
| 51 | + ExpectedFiles: []string{"internal/webhook/handler.go"}, |
| 52 | + } |
| 53 | + ratio := benchmark.MatchFiles(result, query) |
| 54 | + if ratio != 1.0 { |
| 55 | + t.Errorf("FileHitRatio: got %.3f, want 1.0 for absolute path suffix match", ratio) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestMatchSymbols_InAnswer(t *testing.T) { |
| 60 | + result := benchmark.RunResult{Answer: "WebhookHandler handles push events via SyncQueue"} |
| 61 | + query := benchmark.Query{ExpectedSymbols: []string{"WebhookHandler", "SyncQueue"}} |
| 62 | + ratio := benchmark.MatchSymbols(result, query) |
| 63 | + if ratio != 1.0 { |
| 64 | + t.Errorf("SymbolHitRatio: got %.3f, want 1.0", ratio) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestMatchSymbols_PartialMatch(t *testing.T) { |
| 69 | + result := benchmark.RunResult{Answer: "WebhookHandler handles events"} |
| 70 | + query := benchmark.Query{ExpectedSymbols: []string{"WebhookHandler", "SyncQueue"}} |
| 71 | + ratio := benchmark.MatchSymbols(result, query) |
| 72 | + if ratio != 0.5 { |
| 73 | + t.Errorf("SymbolHitRatio: got %.3f, want 0.5", ratio) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestCountCcgToolCalls(t *testing.T) { |
| 78 | + result := benchmark.RunResult{ |
| 79 | + ToolCalls: []benchmark.ToolCall{ |
| 80 | + {Tool: "mcp__ccg__search"}, |
| 81 | + {Tool: "mcp__ccg__get_node"}, |
| 82 | + {Tool: "Read"}, |
| 83 | + {Tool: "Grep"}, |
| 84 | + }, |
| 85 | + } |
| 86 | + count := benchmark.CountCcgToolCalls(result) |
| 87 | + if count != 2 { |
| 88 | + t.Errorf("CcgToolCalls: got %d, want 2", count) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestComputeMatch_FullResult(t *testing.T) { |
| 93 | + result := benchmark.RunResult{ |
| 94 | + QueryID: "q1", |
| 95 | + FilesRead: []string{"internal/webhook/handler.go"}, |
| 96 | + Answer: "WebhookHandler 구현이 " + strings.Repeat("x", 10), |
| 97 | + InputTokens: 200, |
| 98 | + ToolCalls: []benchmark.ToolCall{ |
| 99 | + {Tool: "mcp__ccg__search"}, |
| 100 | + {Tool: "Read"}, |
| 101 | + }, |
| 102 | + } |
| 103 | + query := benchmark.Query{ |
| 104 | + ID: "q1", |
| 105 | + ExpectedFiles: []string{"internal/webhook/handler.go"}, |
| 106 | + ExpectedSymbols: []string{"WebhookHandler"}, |
| 107 | + } |
| 108 | + m := benchmark.ComputeMatch(result, query) |
| 109 | + if m.QueryID != "q1" { |
| 110 | + t.Errorf("QueryID: got %q", m.QueryID) |
| 111 | + } |
| 112 | + if m.FileHitRatio != 1.0 { |
| 113 | + t.Errorf("FileHitRatio: got %.3f, want 1.0", m.FileHitRatio) |
| 114 | + } |
| 115 | + if m.SymbolHitRatio != 1.0 { |
| 116 | + t.Errorf("SymbolHitRatio: got %.3f, want 1.0", m.SymbolHitRatio) |
| 117 | + } |
| 118 | + if m.TotalToolCalls != 2 { |
| 119 | + t.Errorf("TotalToolCalls: got %d, want 2", m.TotalToolCalls) |
| 120 | + } |
| 121 | + if m.CcgToolCalls != 1 { |
| 122 | + t.Errorf("CcgToolCalls: got %d, want 1", m.CcgToolCalls) |
| 123 | + } |
| 124 | + if m.TotalInputTokens != 200 { |
| 125 | + t.Errorf("TotalInputTokens: got %d, want 200", m.TotalInputTokens) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestAnalyzeRun_MultipleResults(t *testing.T) { |
| 130 | + run := &benchmark.BenchmarkRun{ |
| 131 | + Mode: "with-ccg", |
| 132 | + Results: []benchmark.RunResult{ |
| 133 | + {QueryID: "q1", FilesRead: []string{"a.go"}, Answer: "Foo"}, |
| 134 | + {QueryID: "q2", FilesRead: []string{"b.go"}, Answer: "Bar"}, |
| 135 | + }, |
| 136 | + } |
| 137 | + corpus := &benchmark.Corpus{ |
| 138 | + Queries: []benchmark.Query{ |
| 139 | + {ID: "q1", ExpectedFiles: []string{"a.go"}, ExpectedSymbols: []string{"Foo"}}, |
| 140 | + {ID: "q2", ExpectedFiles: []string{"b.go"}, ExpectedSymbols: []string{"Bar"}}, |
| 141 | + }, |
| 142 | + } |
| 143 | + matches := benchmark.AnalyzeRun(run, corpus) |
| 144 | + if len(matches) != 2 { |
| 145 | + t.Errorf("expected 2 matches, got %d", len(matches)) |
| 146 | + } |
| 147 | +} |
0 commit comments