|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/tae2089/code-context-graph/internal/benchmark" |
| 11 | + "github.com/tae2089/code-context-graph/internal/ctxns" |
| 12 | + "github.com/tae2089/code-context-graph/internal/model" |
| 13 | +) |
| 14 | + |
| 15 | +func TestBenchmarkTokenBench_RespectsNamespace(t *testing.T) { |
| 16 | + deps, stdout, stderr, db := setupSearchTest(t) |
| 17 | + |
| 18 | + nodeA := model.Node{Namespace: "ns-a", Name: "SharedA", QualifiedName: "pkg.SharedA", Kind: model.NodeKindFunction, FilePath: "a.go", StartLine: 1, EndLine: 1, Language: "go"} |
| 19 | + nodeB := model.Node{Namespace: "ns-b", Name: "SharedB", QualifiedName: "pkg.SharedB", Kind: model.NodeKindFunction, FilePath: "b.go", StartLine: 1, EndLine: 1, Language: "go"} |
| 20 | + if err := db.Create(&nodeA).Error; err != nil { |
| 21 | + t.Fatalf("create nodeA: %v", err) |
| 22 | + } |
| 23 | + if err := db.Create(&nodeB).Error; err != nil { |
| 24 | + t.Fatalf("create nodeB: %v", err) |
| 25 | + } |
| 26 | + if err := db.Create(&model.SearchDocument{Namespace: "ns-a", NodeID: nodeA.ID, Content: "sharedterm alpha", Language: "go"}).Error; err != nil { |
| 27 | + t.Fatalf("create docA: %v", err) |
| 28 | + } |
| 29 | + if err := db.Create(&model.SearchDocument{Namespace: "ns-b", NodeID: nodeB.ID, Content: "sharedterm beta", Language: "go"}).Error; err != nil { |
| 30 | + t.Fatalf("create docB: %v", err) |
| 31 | + } |
| 32 | + if err := deps.SearchBackend.Rebuild(ctxns.WithNamespace(context.Background(), "ns-a"), db); err != nil { |
| 33 | + t.Fatalf("rebuild ns-a search: %v", err) |
| 34 | + } |
| 35 | + if err := deps.SearchBackend.Rebuild(ctxns.WithNamespace(context.Background(), "ns-b"), db); err != nil { |
| 36 | + t.Fatalf("rebuild ns-b search: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + root := t.TempDir() |
| 40 | + if err := os.WriteFile(filepath.Join(root, "b.go"), []byte("package p\nfunc SharedB() {}\n"), 0o644); err != nil { |
| 41 | + t.Fatalf("write repo file: %v", err) |
| 42 | + } |
| 43 | + corpusPath := filepath.Join(t.TempDir(), "queries.yaml") |
| 44 | + if err := os.WriteFile(corpusPath, []byte(`queries: |
| 45 | + - id: q1 |
| 46 | + description: sharedterm |
| 47 | + expected_symbols: |
| 48 | + - SharedB |
| 49 | +`), 0o644); err != nil { |
| 50 | + t.Fatalf("write corpus: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + if err := executeCmd(deps, stdout, stderr, "--namespace", "ns-b", "benchmark", "token-bench", "--corpus", corpusPath, "--repo", root, "--exts", ".go"); err != nil { |
| 54 | + t.Fatalf("token-bench: %v\nstderr=%s", err, stderr.String()) |
| 55 | + } |
| 56 | + |
| 57 | + var results []benchmark.TokenBenchResult |
| 58 | + if err := json.Unmarshal(stdout.Bytes(), &results); err != nil { |
| 59 | + t.Fatalf("decode output: %v\nstdout=%s", err, stdout.String()) |
| 60 | + } |
| 61 | + if len(results) != 1 { |
| 62 | + t.Fatalf("results = %d, want 1", len(results)) |
| 63 | + } |
| 64 | + if results[0].ResultCount == 0 || results[0].SymbolsHit != 1 { |
| 65 | + t.Fatalf("token-bench did not use ns-b search results: %+v", results[0]) |
| 66 | + } |
| 67 | +} |
0 commit comments