|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { CodeIndexSearchService } from "../search-service" |
| 3 | +import { CodeIndexConfigManager } from "../config-manager" |
| 4 | +import { CodeIndexStateManager } from "../state-manager" |
| 5 | +import { IEmbedder } from "../interfaces/embedder" |
| 6 | +import { IVectorStore } from "../interfaces/vector-store" |
| 7 | +import { TelemetryService } from "@roo-code/telemetry" |
| 8 | +import { TelemetryEventName } from "@roo-code/types" |
| 9 | + |
| 10 | +// Mock dependencies |
| 11 | +vi.mock("@roo-code/telemetry") |
| 12 | + |
| 13 | +describe("CodeIndexSearchService", () => { |
| 14 | + let searchService: CodeIndexSearchService |
| 15 | + let mockConfigManager: any |
| 16 | + let mockStateManager: any |
| 17 | + let mockEmbedder: any |
| 18 | + let mockVectorStore: any |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks() |
| 22 | + |
| 23 | + // Setup mock config manager |
| 24 | + mockConfigManager = { |
| 25 | + isFeatureEnabled: true, |
| 26 | + isFeatureConfigured: true, |
| 27 | + currentSearchMinScore: 0.5, |
| 28 | + currentSearchMaxResults: 10, |
| 29 | + } |
| 30 | + |
| 31 | + // Setup mock state manager |
| 32 | + mockStateManager = { |
| 33 | + getCurrentStatus: vi.fn(() => ({ systemStatus: "Indexed" })), |
| 34 | + setSystemState: vi.fn(), |
| 35 | + } |
| 36 | + |
| 37 | + // Setup mock embedder |
| 38 | + mockEmbedder = { |
| 39 | + createEmbeddings: vi.fn().mockResolvedValue({ |
| 40 | + embeddings: [[0.1, 0.2, 0.3]], |
| 41 | + }), |
| 42 | + } |
| 43 | + |
| 44 | + // Setup mock vector store |
| 45 | + mockVectorStore = { |
| 46 | + search: vi.fn().mockResolvedValue([ |
| 47 | + { |
| 48 | + score: 0.9, |
| 49 | + payload: { |
| 50 | + filePath: "/test/file.ts", |
| 51 | + startLine: 1, |
| 52 | + endLine: 10, |
| 53 | + codeChunk: "test code", |
| 54 | + }, |
| 55 | + }, |
| 56 | + ]), |
| 57 | + } |
| 58 | + |
| 59 | + // Setup mock telemetry |
| 60 | + const mockTelemetryInstance = { |
| 61 | + captureEvent: vi.fn(), |
| 62 | + } |
| 63 | + vi.spyOn(TelemetryService, "instance", "get").mockReturnValue(mockTelemetryInstance as any) |
| 64 | + |
| 65 | + searchService = new CodeIndexSearchService( |
| 66 | + mockConfigManager as CodeIndexConfigManager, |
| 67 | + mockStateManager as CodeIndexStateManager, |
| 68 | + mockEmbedder as IEmbedder, |
| 69 | + mockVectorStore as IVectorStore, |
| 70 | + ) |
| 71 | + }) |
| 72 | + |
| 73 | + describe("searchIndex", () => { |
| 74 | + it("should throw error when feature is disabled", async () => { |
| 75 | + mockConfigManager.isFeatureEnabled = false |
| 76 | + |
| 77 | + await expect(searchService.searchIndex("test query")).rejects.toThrow( |
| 78 | + "Code index feature is disabled or not configured.", |
| 79 | + ) |
| 80 | + }) |
| 81 | + |
| 82 | + it("should throw error when feature is not configured", async () => { |
| 83 | + mockConfigManager.isFeatureConfigured = false |
| 84 | + |
| 85 | + await expect(searchService.searchIndex("test query")).rejects.toThrow( |
| 86 | + "Code index feature is disabled or not configured.", |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + it("should perform search successfully when in Indexed state", async () => { |
| 91 | + const query = "test query" |
| 92 | + const results = await searchService.searchIndex(query) |
| 93 | + |
| 94 | + expect(mockEmbedder.createEmbeddings).toHaveBeenCalledWith([query]) |
| 95 | + expect(mockVectorStore.search).toHaveBeenCalledWith([0.1, 0.2, 0.3], undefined, 0.5, 10) |
| 96 | + expect(results).toHaveLength(1) |
| 97 | + expect(results[0].score).toBe(0.9) |
| 98 | + }) |
| 99 | + |
| 100 | + it("should handle directory prefix correctly", async () => { |
| 101 | + const query = "test query" |
| 102 | + const directoryPrefix = "src/components" |
| 103 | + |
| 104 | + await searchService.searchIndex(query, directoryPrefix) |
| 105 | + |
| 106 | + expect(mockVectorStore.search).toHaveBeenCalledWith([0.1, 0.2, 0.3], "src/components", 0.5, 10) |
| 107 | + }) |
| 108 | + |
| 109 | + it("should NOT throw error when in Error state (state checking moved to tool)", async () => { |
| 110 | + mockStateManager.getCurrentStatus.mockReturnValue({ systemStatus: "Error" }) |
| 111 | + |
| 112 | + // Should not throw, as state checking is now handled in the tool |
| 113 | + const results = await searchService.searchIndex("test query") |
| 114 | + expect(results).toHaveLength(1) |
| 115 | + }) |
| 116 | + |
| 117 | + it("should handle embedding generation failure", async () => { |
| 118 | + mockEmbedder.createEmbeddings.mockResolvedValue({ embeddings: [] }) |
| 119 | + |
| 120 | + await expect(searchService.searchIndex("test query")).rejects.toThrow( |
| 121 | + "Failed to generate embedding for query.", |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + it("should capture telemetry and set error state on search failure", async () => { |
| 126 | + const error = new Error("Vector store error") |
| 127 | + mockVectorStore.search.mockRejectedValue(error) |
| 128 | + |
| 129 | + await expect(searchService.searchIndex("test query")).rejects.toThrow("Vector store error") |
| 130 | + |
| 131 | + expect(mockStateManager.setSystemState).toHaveBeenCalledWith("Error", "Search failed: Vector store error") |
| 132 | + expect(TelemetryService.instance.captureEvent).toHaveBeenCalledWith(TelemetryEventName.CODE_INDEX_ERROR, { |
| 133 | + error: "Vector store error", |
| 134 | + stack: expect.any(String), |
| 135 | + location: "searchIndex", |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + it("should work correctly when in Indexing state", async () => { |
| 140 | + mockStateManager.getCurrentStatus.mockReturnValue({ systemStatus: "Indexing" }) |
| 141 | + |
| 142 | + // Should not throw, as state checking is now handled in the tool |
| 143 | + const results = await searchService.searchIndex("test query") |
| 144 | + expect(results).toHaveLength(1) |
| 145 | + }) |
| 146 | + |
| 147 | + it("should work correctly when in Standby state", async () => { |
| 148 | + mockStateManager.getCurrentStatus.mockReturnValue({ systemStatus: "Standby" }) |
| 149 | + |
| 150 | + // Should not throw, as state checking is now handled in the tool |
| 151 | + const results = await searchService.searchIndex("test query") |
| 152 | + expect(results).toHaveLength(1) |
| 153 | + }) |
| 154 | + }) |
| 155 | +}) |
0 commit comments