| 1 | import * as fs from "fs"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | |
| 6 | import { FileIndexService } from "./FileIndexService.js"; |
| 7 | |
| 8 | // Mock the file watcher |
| 9 | vi.mock("../util/fileWatcher.js", () => ({ |
| 10 | getFileWatcher: () => ({ |
| 11 | startWatching: vi.fn(), |
| 12 | stopWatching: vi.fn(), |
| 13 | onChange: vi.fn(() => () => {}), // Return unsubscribe function |
| 14 | }), |
| 15 | })); |
| 16 | |
| 17 | // Mock git detection |
| 18 | vi.mock("../util/git.js", () => ({ |
| 19 | isGitRepo: vi.fn(() => false), |
| 20 | })); |
| 21 | |
| 22 | describe("FileIndexService", () => { |
| 23 | let service: FileIndexService; |
| 24 | let tempDir: string; |
| 25 | let originalCwd: string; |
| 26 | |
| 27 | beforeEach(() => { |
| 28 | originalCwd = process.cwd(); |
| 29 | tempDir = fs.mkdtempSync(path.join(process.cwd(), "test-fileindex-")); |
| 30 | process.chdir(tempDir); |
| 31 | |
| 32 | service = new FileIndexService(); |
| 33 | }); |
| 34 | |
| 35 | afterEach(async () => { |
| 36 | await service.cleanup(); |
| 37 | process.chdir(originalCwd); |
| 38 | if (fs.existsSync(tempDir)) { |
| 39 | fs.rmSync(tempDir, { recursive: true, force: true }); |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | it("should initialize and index files", async () => { |
| 44 | // Create test files |
| 45 | fs.writeFileSync("test.js", 'console.log("test");'); |
| 46 | fs.writeFileSync("test.ts", 'console.log("test");'); |
| 47 | fs.writeFileSync("README.md", "# Test"); |
| 48 | |
| 49 | // Initialize the service |
| 50 | await service.initialize(); |
| 51 | |
| 52 | // Should have indexed the files |
| 53 | const files = service.getFiles(); |
| 54 | expect(files.length).toBeGreaterThan(0); |
| 55 | |
| 56 | const filePaths = files.map((f) => f.path); |
| 57 | expect(filePaths).toContain("test.js"); |
| 58 | expect(filePaths).toContain("test.ts"); |
| 59 | expect(filePaths).toContain("README.md"); |
| 60 | }); |
| 61 | |
| 62 | it("should filter files correctly with fzf", async () => { |
| 63 | // Create test files |
| 64 | fs.writeFileSync("component.tsx", "export default () => <div></div>"); |
| 65 | fs.writeFileSync("utils.ts", "export const util = () => {}"); |
| 66 | fs.writeFileSync("readme.md", "# Documentation"); |
| 67 | |
| 68 | await service.initialize(); |
| 69 | |
| 70 | // Test filtering with more specific terms that won't fuzzy match |
| 71 | const tsxFiles = await service.filterFiles("tsx"); |
| 72 | expect(tsxFiles.some((f) => f.path === "component.tsx")).toBe(true); |
| 73 | expect(tsxFiles.some((f) => f.path === "utils.ts")).toBe(false); |
| 74 | expect(tsxFiles.some((f) => f.path === "readme.md")).toBe(false); |
| 75 | |
| 76 | // Test filtering by name |
| 77 | const componentFiles = await service.filterFiles("component"); |
| 78 | expect(componentFiles.some((f) => f.path === "component.tsx")).toBe(true); |
| 79 | expect(componentFiles.some((f) => f.path === "utils.ts")).toBe(false); |
| 80 | expect(componentFiles.some((f) => f.path === "readme.md")).toBe(false); |
| 81 | }); |
| 82 | |
| 83 | it("should handle empty directory", async () => { |
| 84 | await service.initialize(); |
| 85 | |
| 86 | const files = service.getFiles(); |
| 87 | expect(files).toEqual([]); |
| 88 | expect(service.isIndexing()).toBe(false); |
| 89 | expect(service.getError()).toBeNull(); |
| 90 | }); |
| 91 | |
| 92 | it("should ignore specified patterns", async () => { |
| 93 | // Create files that should be ignored |
| 94 | fs.mkdirSync("node_modules"); |
| 95 | fs.writeFileSync( |
| 96 | path.join("node_modules", "package.js"), |
| 97 | "module.exports = {}", |
| 98 | ); |
| 99 | |
| 100 | fs.mkdirSync("dist"); |
| 101 | fs.writeFileSync(path.join("dist", "bundle.js"), 'console.log("bundle")'); |
| 102 | |
| 103 | // Create files that should be included |
| 104 | fs.writeFileSync("src.js", 'console.log("src")'); |
| 105 | |
| 106 | await service.initialize(); |
| 107 | |
| 108 | const files = service.getFiles(); |
| 109 | const filePaths = files.map((f) => f.path); |
| 110 | |
| 111 | // Should include src.js |
| 112 | expect(filePaths).toContain("src.js"); |
| 113 | |
| 114 | // Should not include ignored files |
| 115 | expect(filePaths.some((p) => p.includes("node_modules"))).toBe(false); |
| 116 | expect(filePaths.some((p) => p.includes("dist"))).toBe(false); |
| 117 | }); |
| 118 | |
| 119 | it("should sort files correctly with no filter", async () => { |
| 120 | // Create files in non-alphabetical order |
| 121 | fs.writeFileSync("zebra.js", 'console.log("zebra")'); |
| 122 | fs.writeFileSync("alpha.js", 'console.log("alpha")'); |
| 123 | fs.writeFileSync("beta.js", 'console.log("beta")'); |
| 124 | |
| 125 | await service.initialize(); |
| 126 | |
| 127 | const files = await service.filterFiles(""); |
| 128 | expect(files[0].path).toBe("alpha.js"); |
| 129 | expect(files[1].path).toBe("beta.js"); |
| 130 | expect(files[2].path).toBe("zebra.js"); |
| 131 | }); |
| 132 | |
| 133 | it("should include files with matching names and paths", async () => { |
| 134 | // Create nested structure |
| 135 | fs.mkdirSync("src"); |
| 136 | fs.writeFileSync(path.join("src", "test-helper.js"), "helper"); |
| 137 | fs.writeFileSync("test.js", "test file"); |
| 138 | |
| 139 | await service.initialize(); |
| 140 | |
| 141 | const filtered = await service.filterFiles("test"); |
| 142 | |
| 143 | // Should include both files that match the filter |
| 144 | expect(filtered.length).toBeGreaterThanOrEqual(2); |
| 145 | const filePaths = filtered.map((f) => f.path); |
| 146 | expect(filePaths).toContain("test.js"); |
| 147 | // Normalize path separators for cross-platform compatibility |
| 148 | expect(filePaths).toContain(path.join("src", "test-helper.js")); |
| 149 | }); |
| 150 | |
| 151 | it("should respect limit parameter", async () => { |
| 152 | // Create many files |
| 153 | for (let i = 0; i < 20; i++) { |
| 154 | fs.writeFileSync(`file${i}.js`, `console.log("file${i}")`); |
| 155 | } |
| 156 | |
| 157 | await service.initialize(); |
| 158 | |
| 159 | const files = await service.filterFiles("", 5); |
| 160 | expect(files.length).toBe(5); |
| 161 | }); |
| 162 | |
| 163 | it("should handle refresh correctly", async () => { |
| 164 | // Create initial file |
| 165 | fs.writeFileSync("initial.js", 'console.log("initial")'); |
| 166 | |
| 167 | await service.initialize(); |
| 168 | expect(service.getFiles().length).toBe(1); |
| 169 | |
| 170 | // Add another file |
| 171 | fs.writeFileSync("added.js", 'console.log("added")'); |
| 172 | |
| 173 | // Refresh should pick up the new file |
| 174 | await service.refreshIndex(); |
| 175 | expect(service.getFiles().length).toBe(2); |
| 176 | }); |
| 177 | |
| 178 | it("should handle fuzzy search with AsyncFzf", async () => { |
| 179 | // Create test files for fuzzy matching |
| 180 | fs.writeFileSync("user-service.ts", "export class UserService {}"); |
| 181 | fs.writeFileSync("user-model.ts", "export interface User {}"); |
| 182 | fs.writeFileSync("config.json", "{}"); |
| 183 | |
| 184 | await service.initialize(); |
| 185 | |
| 186 | // Test fuzzy search (should match both user files) |
| 187 | const userFiles = await service.filterFiles("usmo"); // us(er) mo(del) |
| 188 | expect(userFiles.length).equal(1); |
| 189 | expect(userFiles.some((f) => f.path === "user-model.ts")).toBe(true); |
| 190 | }); |
| 191 | |
| 192 | it("should find files with @ symbols when searched with @ pattern", async () => { |
| 193 | // Create test file with @ symbols in the name |
| 194 | fs.writeFileSync("test@123.txt", "test content"); |
| 195 | fs.writeFileSync("regular-file.js", "normal file"); |
| 196 | fs.writeFileSync("other@456.md", "other content"); |
| 197 | |
| 198 | await service.initialize(); |
| 199 | |
| 200 | // Search for "test@123" - should match "test@123.txt" |
| 201 | const results = await service.filterFiles("test@123"); |
| 202 | |
| 203 | expect(results.length).equal(1); |
| 204 | expect(results.some((f) => f.path === "test@123.txt")).toBe(true); |
| 205 | |
| 206 | // Should not match files that don't contain the @ pattern |
| 207 | expect(results.some((f) => f.path === "regular-file.js")).toBe(false); |
| 208 | }); |
| 209 | }); |