|
| 1 | +package indexer |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + // Import parsers so they register via init() |
| 9 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/css" |
| 10 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/docs" |
| 11 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/go" |
| 12 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/html" |
| 13 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/javascript" |
| 14 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/php" |
| 15 | + _ "github.com/doITmagic/rag-code-mcp/pkg/parser/python" |
| 16 | +) |
| 17 | + |
| 18 | +// createFile is a test helper that creates a file with minimal content. |
| 19 | +func createFile(t *testing.T, path string) { |
| 20 | + t.Helper() |
| 21 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + if err := os.WriteFile(path, []byte("// test"), 0o644); err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestCountAllFiles_Breakdown(t *testing.T) { |
| 30 | + root := t.TempDir() |
| 31 | + svc := &Service{} // CountAllFiles doesn't need embedder or store |
| 32 | + |
| 33 | + // Create a mixed JS/TS workspace |
| 34 | + createFile(t, filepath.Join(root, "src", "app.ts")) |
| 35 | + createFile(t, filepath.Join(root, "src", "utils.ts")) |
| 36 | + createFile(t, filepath.Join(root, "src", "types.tsx")) |
| 37 | + createFile(t, filepath.Join(root, "src", "legacy.js")) |
| 38 | + createFile(t, filepath.Join(root, "src", "config.mjs")) |
| 39 | + createFile(t, filepath.Join(root, "src", "App.vue")) |
| 40 | + |
| 41 | + // Create Go files |
| 42 | + createFile(t, filepath.Join(root, "backend", "main.go")) |
| 43 | + createFile(t, filepath.Join(root, "backend", "handler.go")) |
| 44 | + |
| 45 | + // Create docs |
| 46 | + createFile(t, filepath.Join(root, "README.md")) |
| 47 | + createFile(t, filepath.Join(root, "config.yaml")) |
| 48 | + |
| 49 | + // Create PHP |
| 50 | + createFile(t, filepath.Join(root, "web", "index.php")) |
| 51 | + |
| 52 | + result := svc.CountAllFiles(root, nil) |
| 53 | + |
| 54 | + // Verify total counts |
| 55 | + if result.Counts["javascript"] != 6 { |
| 56 | + t.Errorf("javascript count: got %d, want 6", result.Counts["javascript"]) |
| 57 | + } |
| 58 | + if result.Counts["go"] != 2 { |
| 59 | + t.Errorf("go count: got %d, want 2", result.Counts["go"]) |
| 60 | + } |
| 61 | + if result.Counts["docs"] != 2 { |
| 62 | + t.Errorf("docs count: got %d, want 2", result.Counts["docs"]) |
| 63 | + } |
| 64 | + if result.Counts["php"] != 1 { |
| 65 | + t.Errorf("php count: got %d, want 1", result.Counts["php"]) |
| 66 | + } |
| 67 | + |
| 68 | + // Verify JavaScript breakdown — the key test! |
| 69 | + jsBd := result.Breakdowns["javascript"] |
| 70 | + if jsBd == nil { |
| 71 | + t.Fatal("expected javascript breakdown to be non-nil") |
| 72 | + } |
| 73 | + if jsBd[".ts"] != 2 { |
| 74 | + t.Errorf("javascript .ts: got %d, want 2", jsBd[".ts"]) |
| 75 | + } |
| 76 | + if jsBd[".tsx"] != 1 { |
| 77 | + t.Errorf("javascript .tsx: got %d, want 1", jsBd[".tsx"]) |
| 78 | + } |
| 79 | + if jsBd[".js"] != 1 { |
| 80 | + t.Errorf("javascript .js: got %d, want 1", jsBd[".js"]) |
| 81 | + } |
| 82 | + if jsBd[".mjs"] != 1 { |
| 83 | + t.Errorf("javascript .mjs: got %d, want 1", jsBd[".mjs"]) |
| 84 | + } |
| 85 | + if jsBd[".vue"] != 1 { |
| 86 | + t.Errorf("javascript .vue: got %d, want 1", jsBd[".vue"]) |
| 87 | + } |
| 88 | + |
| 89 | + // Verify Go breakdown (single extension) |
| 90 | + goBd := result.Breakdowns["go"] |
| 91 | + if goBd[".go"] != 2 { |
| 92 | + t.Errorf("go .go: got %d, want 2", goBd[".go"]) |
| 93 | + } |
| 94 | + |
| 95 | + // Verify docs breakdown |
| 96 | + docsBd := result.Breakdowns["docs"] |
| 97 | + if docsBd[".md"] != 1 { |
| 98 | + t.Errorf("docs .md: got %d, want 1", docsBd[".md"]) |
| 99 | + } |
| 100 | + if docsBd[".yaml"] != 1 { |
| 101 | + t.Errorf("docs .yaml: got %d, want 1", docsBd[".yaml"]) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestCountAllFiles_ExcludePatterns(t *testing.T) { |
| 106 | + root := t.TempDir() |
| 107 | + svc := &Service{} |
| 108 | + |
| 109 | + createFile(t, filepath.Join(root, "src", "app.ts")) |
| 110 | + createFile(t, filepath.Join(root, "src", "utils.js")) |
| 111 | + createFile(t, filepath.Join(root, "dist", "bundle.js")) // should be excluded |
| 112 | + createFile(t, filepath.Join(root, "build", "output.js")) // should be excluded |
| 113 | + |
| 114 | + result := svc.CountAllFiles(root, []string{"dist", "build"}) |
| 115 | + |
| 116 | + if result.Counts["javascript"] != 2 { |
| 117 | + t.Errorf("javascript count with excludes: got %d, want 2", result.Counts["javascript"]) |
| 118 | + } |
| 119 | + |
| 120 | + jsBd := result.Breakdowns["javascript"] |
| 121 | + if jsBd[".ts"] != 1 { |
| 122 | + t.Errorf("javascript .ts with excludes: got %d, want 1", jsBd[".ts"]) |
| 123 | + } |
| 124 | + if jsBd[".js"] != 1 { |
| 125 | + t.Errorf("javascript .js with excludes: got %d, want 1", jsBd[".js"]) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestCountAllFiles_EmptyDir(t *testing.T) { |
| 130 | + root := t.TempDir() |
| 131 | + svc := &Service{} |
| 132 | + |
| 133 | + result := svc.CountAllFiles(root, nil) |
| 134 | + |
| 135 | + if len(result.Counts) != 0 { |
| 136 | + t.Errorf("expected empty counts for empty dir, got %v", result.Counts) |
| 137 | + } |
| 138 | + if len(result.Breakdowns) != 0 { |
| 139 | + t.Errorf("expected empty breakdowns for empty dir, got %v", result.Breakdowns) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestCountAllFiles_NodeModulesExcluded(t *testing.T) { |
| 144 | + root := t.TempDir() |
| 145 | + svc := &Service{} |
| 146 | + |
| 147 | + createFile(t, filepath.Join(root, "src", "app.ts")) |
| 148 | + createFile(t, filepath.Join(root, "node_modules", "lib", "index.js")) // auto-excluded |
| 149 | + |
| 150 | + result := svc.CountAllFiles(root, nil) |
| 151 | + |
| 152 | + if result.Counts["javascript"] != 1 { |
| 153 | + t.Errorf("expected 1 (node_modules excluded), got %d", result.Counts["javascript"]) |
| 154 | + } |
| 155 | + if result.Breakdowns["javascript"][".ts"] != 1 { |
| 156 | + t.Errorf("expected .ts=1, got %d", result.Breakdowns["javascript"][".ts"]) |
| 157 | + } |
| 158 | +} |
0 commit comments