|
| 1 | +import { describe, it, expect, beforeAll, vi } from "vitest" |
| 2 | +import { CodeParser } from "../processors/parser" |
| 3 | +import * as languageParserModule from "../../tree-sitter/languageParser" |
| 4 | +import * as path from "path" |
| 5 | + |
| 6 | +describe("Go Indexing Fix", () => { |
| 7 | + let wasmDir: string | undefined |
| 8 | + |
| 9 | + beforeAll(async () => { |
| 10 | + // Find WASM directory |
| 11 | + const possibleWasmDirs = [path.join(__dirname, "../../../dist"), path.join(process.cwd(), "dist")] |
| 12 | + |
| 13 | + for (const dir of possibleWasmDirs) { |
| 14 | + try { |
| 15 | + const fsSync = require("fs") |
| 16 | + const wasmPath = path.join(dir, "tree-sitter-go.wasm") |
| 17 | + if (fsSync.existsSync(wasmPath)) { |
| 18 | + wasmDir = dir |
| 19 | + break |
| 20 | + } |
| 21 | + } catch (e) { |
| 22 | + // Continue searching |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if (!wasmDir) { |
| 27 | + throw new Error("Could not find WASM directory") |
| 28 | + } |
| 29 | + |
| 30 | + // Mock loadRequiredLanguageParsers to use our WASM directory |
| 31 | + const originalLoad = languageParserModule.loadRequiredLanguageParsers |
| 32 | + vi.spyOn(languageParserModule, "loadRequiredLanguageParsers").mockImplementation( |
| 33 | + async (files: string[], customWasmDir?: string) => { |
| 34 | + return originalLoad(files, customWasmDir || wasmDir) |
| 35 | + }, |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it("should correctly index Go functions, methods, and types", async () => { |
| 40 | + const parser = new CodeParser() |
| 41 | + |
| 42 | + const goContent = `package main |
| 43 | +
|
| 44 | +import ( |
| 45 | + "fmt" |
| 46 | + "strings" |
| 47 | +) |
| 48 | +
|
| 49 | +// User represents a user in the system |
| 50 | +type User struct { |
| 51 | + ID int |
| 52 | + Name string |
| 53 | + Email string |
| 54 | + IsActive bool |
| 55 | +} |
| 56 | +
|
| 57 | +// NewUser creates a new user instance |
| 58 | +func NewUser(id int, name, email string) *User { |
| 59 | + return &User{ |
| 60 | + ID: id, |
| 61 | + Name: name, |
| 62 | + Email: email, |
| 63 | + IsActive: true, |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +// GetDisplayName returns the user's display name |
| 68 | +func (u *User) GetDisplayName() string { |
| 69 | + return fmt.Sprintf("%s <%s>", u.Name, u.Email) |
| 70 | +} |
| 71 | +
|
| 72 | +// Validate checks if the user data is valid |
| 73 | +func (u *User) Validate() error { |
| 74 | + if u.Name == "" { |
| 75 | + return fmt.Errorf("name cannot be empty") |
| 76 | + } |
| 77 | + if !strings.Contains(u.Email, "@") { |
| 78 | + return fmt.Errorf("invalid email format") |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | +
|
| 83 | +// ProcessUsers processes a list of users |
| 84 | +func ProcessUsers(users []*User) { |
| 85 | + for _, user := range users { |
| 86 | + if err := user.Validate(); err != nil { |
| 87 | + fmt.Printf("Invalid user %d: %v\n", user.ID, err) |
| 88 | + continue |
| 89 | + } |
| 90 | + fmt.Println(user.GetDisplayName()) |
| 91 | + } |
| 92 | +} |
| 93 | +
|
| 94 | +func main() { |
| 95 | + users := []*User{ |
| 96 | + NewUser(1, "Alice", "[email protected]"), |
| 97 | + NewUser(2, "Bob", "[email protected]"), |
| 98 | + } |
| 99 | + ProcessUsers(users) |
| 100 | +}` |
| 101 | + |
| 102 | + const blocks = await parser.parseFile("test.go", { |
| 103 | + content: goContent, |
| 104 | + fileHash: "test-hash", |
| 105 | + }) |
| 106 | + |
| 107 | + // Verify we got blocks |
| 108 | + expect(blocks.length).toBeGreaterThan(0) |
| 109 | + |
| 110 | + // Check for specific function declarations |
| 111 | + const functionBlocks = blocks.filter((b) => b.type === "function_declaration") |
| 112 | + const functionNames = functionBlocks.map((b) => b.identifier).sort() |
| 113 | + expect(functionNames).toContain("NewUser") |
| 114 | + expect(functionNames).toContain("ProcessUsers") |
| 115 | + // Note: main function might be filtered out if it's less than 50 characters |
| 116 | + |
| 117 | + // Check for method declarations |
| 118 | + const methodBlocks = blocks.filter((b) => b.type === "method_declaration") |
| 119 | + const methodNames = methodBlocks.map((b) => b.identifier).sort() |
| 120 | + expect(methodNames).toContain("GetDisplayName") |
| 121 | + expect(methodNames).toContain("Validate") |
| 122 | + |
| 123 | + // Check for type declarations |
| 124 | + const typeBlocks = blocks.filter((b) => b.type === "type_declaration") |
| 125 | + expect(typeBlocks.length).toBeGreaterThan(0) |
| 126 | + |
| 127 | + // Verify content is captured correctly |
| 128 | + const newUserBlock = functionBlocks.find((b) => b.identifier === "NewUser") |
| 129 | + expect(newUserBlock).toBeDefined() |
| 130 | + expect(newUserBlock!.content).toContain("func NewUser") |
| 131 | + expect(newUserBlock!.content).toContain("return &User{") |
| 132 | + |
| 133 | + // Verify line numbers are correct |
| 134 | + const validateBlock = methodBlocks.find((b) => b.identifier === "Validate") |
| 135 | + expect(validateBlock).toBeDefined() |
| 136 | + expect(validateBlock!.start_line).toBeGreaterThan(1) |
| 137 | + expect(validateBlock!.end_line).toBeGreaterThan(validateBlock!.start_line) |
| 138 | + }) |
| 139 | + |
| 140 | + it("should respect the 50-character threshold for Go", async () => { |
| 141 | + const parser = new CodeParser() |
| 142 | + |
| 143 | + const goContent = `package main |
| 144 | +
|
| 145 | +// Short function - should be filtered out |
| 146 | +func f() { |
| 147 | + return |
| 148 | +} |
| 149 | +
|
| 150 | +// Longer function - should be included |
| 151 | +func calculateTotal(items []int) int { |
| 152 | + total := 0 |
| 153 | + for _, item := range items { |
| 154 | + total += item |
| 155 | + } |
| 156 | + return total |
| 157 | +}` |
| 158 | + |
| 159 | + const blocks = await parser.parseFile("test.go", { |
| 160 | + content: goContent, |
| 161 | + fileHash: "test-hash", |
| 162 | + }) |
| 163 | + |
| 164 | + // The short function should be filtered out |
| 165 | + const functionBlocks = blocks.filter((b) => b.type === "function_declaration") |
| 166 | + expect(functionBlocks.length).toBe(1) |
| 167 | + expect(functionBlocks[0].identifier).toBe("calculateTotal") |
| 168 | + |
| 169 | + // Verify the short function was not included |
| 170 | + const shortFunction = functionBlocks.find((b) => b.identifier === "f") |
| 171 | + expect(shortFunction).toBeUndefined() |
| 172 | + }) |
| 173 | + |
| 174 | + it("should capture full declaration content, not just identifiers", async () => { |
| 175 | + const parser = new CodeParser() |
| 176 | + |
| 177 | + const goContent = `package main |
| 178 | +
|
| 179 | +type Config struct { |
| 180 | + Host string |
| 181 | + Port int |
| 182 | + Debug bool |
| 183 | + Timeout int |
| 184 | +} |
| 185 | +
|
| 186 | +func (c *Config) GetAddress() string { |
| 187 | + return fmt.Sprintf("%s:%d", c.Host, c.Port) |
| 188 | +}` |
| 189 | + |
| 190 | + const blocks = await parser.parseFile("test.go", { |
| 191 | + content: goContent, |
| 192 | + fileHash: "test-hash", |
| 193 | + }) |
| 194 | + |
| 195 | + // Check that we capture the full struct declaration |
| 196 | + const typeBlock = blocks.find((b) => b.type === "type_declaration") |
| 197 | + if (typeBlock) { |
| 198 | + expect(typeBlock.content).toContain("type Config struct") |
| 199 | + expect(typeBlock.content).toContain("Host string") |
| 200 | + expect(typeBlock.content).toContain("Port int") |
| 201 | + expect(typeBlock.content).toContain("Debug bool") |
| 202 | + expect(typeBlock.content).toContain("Timeout int") |
| 203 | + } |
| 204 | + |
| 205 | + // Check that we capture the full method declaration |
| 206 | + const methodBlock = blocks.find((b) => b.type === "method_declaration" && b.identifier === "GetAddress") |
| 207 | + expect(methodBlock).toBeDefined() |
| 208 | + expect(methodBlock!.content).toContain("func (c *Config) GetAddress() string") |
| 209 | + expect(methodBlock!.content).toContain("return fmt.Sprintf") |
| 210 | + }) |
| 211 | +}) |
0 commit comments