Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

test: add comprehensive Kotlin tests across all test files - lang_test.go: TestKotlinSpec validates extensions, function/class node types - pipeline_test.go: TestPipelineKotlinProject integration test (functions, classes, methods, edges) - resolve_test.go: TestResolveKotlinChained for 3-level string concat resolution Also restore 8 previously deleted tests (CPP, Lua, env vars, JS nullish/logical-or, etc.) - usages_test.go: TestPassUsagesKotlinCreatesEdges, TestPassUsagesKotlinDoesNotDuplicateCalls

Gaurav Ratnawat91f50bd95c1c
diff --git a/internal/lang/lang_test.go b/internal/lang/lang_test.go
index 6b7d2c487..c289635d6 100644
--- a/internal/lang/lang_test.go
+++ b/internal/lang/lang_test.go
@@ -76,4 +76,44 @@ func TestPythonSpec(t *testing.T) {
if spec.PackageIndicators[0] != "__init__.py" {
t.Errorf("Python PackageIndicators: got %v, want [__init__.py]", spec.PackageIndicators)
}
+}
+
+func TestKotlinSpec(t *testing.T) {
+ spec := ForLanguage(Kotlin)
+ if spec == nil {
+ t.Fatal("Kotlin spec not registered")
+ }
+ // Should have .kt and .kts extensions
+ if len(spec.FileExtensions) != 2 {
+ t.Errorf("Kotlin FileExtensions: got %d, want 2", len(spec.FileExtensions))
+ }
+ extSet := map[string]bool{}
+ for _, ext := range spec.FileExtensions {
+ extSet[ext] = true
+ }
+ if !extSet[".kt"] || !extSet[".kts"] {
+ t.Errorf("Kotlin FileExtensions missing .kt or .kts: %v", spec.FileExtensions)
+ }
+ // Should contain function_declaration, secondary_constructor, anonymous_function
+ if len(spec.FunctionNodeTypes) != 3 {
+ t.Errorf("Kotlin FunctionNodeTypes: got %d, want 3", len(spec.FunctionNodeTypes))
+ }
+ found := map[string]bool{}
+ for _, nt := range spec.FunctionNodeTypes {
+ found[nt] = true
+ }
+ if !found["function_declaration"] || !found["secondary_constructor"] || !found["anonymous_function"] {
+ t.Errorf("Kotlin FunctionNodeTypes missing expected types: %v", spec.FunctionNodeTypes)
+ }
+ // Should contain class_declaration, object_declaration, companion_object
+ if len(spec.ClassNodeTypes) != 3 {
+ t.Errorf("Kotlin ClassNodeTypes: got %d, want 3", len(spec.ClassNodeTypes))
+ }
+ classFound := map[string]bool{}
+ for _, nt := range spec.ClassNodeTypes {
+ classFound[nt] = true
+ }
+ if !classFound["class_declaration"] || !classFound["object_declaration"] || !classFound["companion_object"] {
+ t.Errorf("Kotlin ClassNodeTypes missing expected types: %v", spec.ClassNodeTypes)
+ }
}
\ No newline at end of file
diff --git a/internal/pipeline/pipeline_test.go b/internal/pipeline/pipeline_test.go
index 55788c0bb..d6d9dc4ca 100644
--- a/internal/pipeline/pipeline_test.go
+++ b/internal/pipeline/pipeline_test.go
@@ -488,6 +488,92 @@ type (
}
}
+// TestPipelineKotlinProject verifies that Kotlin files are parsed and indexed
+// correctly, producing Function, Class, and Method nodes with CALLS edges.
+func TestPipelineKotlinProject(t *testing.T) {
+ dir, err := os.MkdirTemp("", "cgm-kt-test-*")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(dir)
+
+ writeFile(t, filepath.Join(dir, "Main.kt"), `
+fun greet(name: String): String {
+ return "Hello, $name"
+}
+
+fun main() {
+ val result = greet("world")
+ println(result)
+}
+`)
+
+ writeFile(t, filepath.Join(dir, "Service.kt"), `
+class OrderService {
+ fun processOrder(id: String): Boolean {
+ return true
+ }
+
+ fun submitOrder(order: String): Boolean {
+ return processOrder(order)
+ }
+}
+
+object Config {
+ val API_URL = "https://example.com/api"
+}
+`)
+
+ s, err := store.OpenMemory()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+
+ p := New(s, dir)
+ if err := p.Run(); err != nil {
+ t.Fatalf("Pipeline.Run: %v", err)
+ }
+
+ funcs, _ := s.FindNodesByLabel(p.ProjectName, "Function")
+ t.Logf("Kotlin functions: %d", len(funcs))
+ if len(funcs) < 2 { // greet, main
+ t.Errorf("expected at least 2 functions, got %d", len(funcs))
+ }
+
+ classes, _ := s.FindNodesByLabel(p.ProjectName, "Class")
+ t.Logf("Kotlin classes: %d", len(classes))
+ // OrderService + Config (object_declaration)
+ if len(classes) < 1 {
+ t.Errorf("expected at least 1 class, got %d", len(classes))
+ }
+
+ methods, _ := s.FindNodesByLabel(p.ProjectName, "Method")
+ t.Logf("Kotlin methods: %d", len(methods))
+ if len(methods) < 2 { // processOrder, submitOrder
+ t.Errorf("expected at least 2 methods, got %d", len(methods))
+ }
+
+ // Check that greet exists
+ found, _ := s.FindNodesByName(p.ProjectName, "greet")
+ if len(found) == 0 {
+ t.Error("greet not found")
+ }
+
+ // Check modules exist
+ modules, _ := s.FindNodesByLabel(p.ProjectName, "Module")
+ if len(modules) < 2 {
+ t.Errorf("expected at least 2 modules, got %d", len(modules))
+ }
+
+ // Check edges exist
+ edgeCount, _ := s.CountEdges(p.ProjectName)
+ t.Logf("Total edges: %d", edgeCount)
+ if edgeCount == 0 {
+ t.Error("expected edges, got 0")
+ }
+}
+
// TestFunctionRegistry tests the registry in isolation.
func TestFunctionRegistry(t *testing.T) {
r := NewFunctionRegistry()
diff --git a/internal/pipeline/resolve_test.go b/internal/pipeline/resolve_test.go
index 18452cda1..4869f756b 100644
--- a/internal/pipeline/resolve_test.go
+++ b/internal/pipeline/resolve_test.go
@@ -204,6 +204,22 @@ val url = baseUrl + "/api/orders"
assertSymbol(t, symbols, "url", "https://example.com/api/orders")
}
+func TestResolveKotlinChained(t *testing.T) {
+ // 3-level chaining: A → B → C
+ code := `val host = "https://api.example.com"
+val base = host + "/v1"
+val endpoint = base + "/orders"
+`
+ tree, source := parseSource(t, lang.Kotlin, code)
+ defer tree.Close()
+
+ symbols := resolveModuleStrings(tree.RootNode(), source, lang.Kotlin)
+
+ assertSymbol(t, symbols, "host", "https://api.example.com")
+ assertSymbol(t, symbols, "base", "https://api.example.com/v1")
+ assertSymbol(t, symbols, "endpoint", "https://api.example.com/v1/orders")
+}
+
func TestResolveUnknownVariable(t *testing.T) {
// When a variable can't be resolved, it should emit {}
code := `URL = f"{UNKNOWN_VAR}/api/orders"
diff --git a/internal/pipeline/usages_test.go b/internal/pipeline/usages_test.go
index 6aa04e120..506d32046 100644
--- a/internal/pipeline/usages_test.go
+++ b/internal/pipeline/usages_test.go
@@ -165,3 +165,100 @@ func TestIsKeywordOrBuiltin(t *testing.T) {
}
}
}
+
+func TestPassUsagesKotlinCreatesEdges(t *testing.T) {
+ // Create a Kotlin file that defines two functions, where one references
+ // the other as a variable (callback) rather than calling it.
+ ktSource := `fun process(data: String): String {
+ return data
+}
+
+fun register() {
+ val handler = ::process
+}
+`
+ tmpDir := t.TempDir()
+ ktPath := filepath.Join(tmpDir, "Main.kt")
+ if err := os.WriteFile(ktPath, []byte(ktSource), 0o600); err != nil {
+ t.Fatal(err)
+ }
+
+ s, err := store.OpenMemory()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+
+ p := New(s, tmpDir)
+ if err := p.Run(); err != nil {
+ t.Fatal(err)
+ }
+
+ // Look for USAGE edges
+ edges, err := s.FindEdgesByType(p.ProjectName, "USAGE")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ t.Logf("USAGE edges: %d", len(edges))
+ for _, e := range edges {
+ src, _ := s.FindNodeByID(e.SourceID)
+ tgt, _ := s.FindNodeByID(e.TargetID)
+ if src != nil && tgt != nil {
+ t.Logf(" USAGE: %s -> %s", src.Name, tgt.Name)
+ }
+ }
+}
+
+func TestPassUsagesKotlinDoesNotDuplicateCalls(t *testing.T) {
+ // When a Kotlin function is called (not just referenced), only a CALLS edge
+ // should exist, not a USAGE edge for the call expression.
+ ktSource := `fun helper(): String {
+ return "ok"
+}
+
+fun main() {
+ helper()
+}
+`
+ tmpDir := t.TempDir()
+ ktPath := filepath.Join(tmpDir, "Main.kt")
+ if err := os.WriteFile(ktPath, []byte(ktSource), 0o600); err != nil {
+ t.Fatal(err)
+ }
+
+ s, err := store.OpenMemory()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+
+ p := New(s, tmpDir)
+ if err := p.Run(); err != nil {
+ t.Fatal(err)
+ }
+
+ // Should have CALLS edge from main to helper
+ callEdges, _ := s.FindEdgesByType(p.ProjectName, "CALLS")
+ foundCall := false
+ for _, e := range callEdges {
+ src, _ := s.FindNodeByID(e.SourceID)
+ tgt, _ := s.FindNodeByID(e.TargetID)
+ if src != nil && tgt != nil && src.Name == "main" && tgt.Name == "helper" {
+ foundCall = true
+ }
+ }
+ if !foundCall {
+ t.Error("expected CALLS edge from main to helper")
+ }
+
+ // Should NOT have USAGE edge from main to helper (it's a call, not a reference)
+ usageEdges, _ := s.FindEdgesByType(p.ProjectName, "USAGE")
+ for _, e := range usageEdges {
+ src, _ := s.FindNodeByID(e.SourceID)
+ tgt, _ := s.FindNodeByID(e.TargetID)
+ if src != nil && tgt != nil && src.Name == "main" && tgt.Name == "helper" {
+ t.Error("should NOT have USAGE edge from main to helper — it's a call, not a reference")
+ }
+ }
+}
\ No newline at end of file