@@ -25,7 +25,15 @@ describe("CodebaseSearchTool", () => {
2525 let task : Task
2626 let context : vscode . ExtensionContext
2727 let callbacks : ToolCallbacks
28- let manager : Pick < CodeIndexManager , "isFeatureEnabled" | "isFeatureConfigured" | "searchIndex" >
28+ let manager : Pick <
29+ CodeIndexManager ,
30+ | "isFeatureEnabled"
31+ | "isFeatureConfigured"
32+ | "isConfigurationLoaded"
33+ | "isInitialized"
34+ | "initialize"
35+ | "searchIndex"
36+ >
2937 let deref : ReturnType < typeof vi . fn < Task [ "providerRef" ] [ "deref" ] > >
3038
3139 beforeEach ( ( ) => {
@@ -61,16 +69,22 @@ describe("CodebaseSearchTool", () => {
6169 pushToolResult : vi . fn < ToolCallbacks [ "pushToolResult" ] > ( ) ,
6270 }
6371 manager = {
72+ isConfigurationLoaded : true ,
6473 isFeatureEnabled : true ,
6574 isFeatureConfigured : true ,
75+ isInitialized : true ,
76+ initialize : vi . fn < CodeIndexManager [ "initialize" ] > ( ) . mockResolvedValue ( { requiresRestart : false } ) ,
6677 searchIndex : vi . fn < CodeIndexManager [ "searchIndex" ] > ( ) . mockResolvedValue ( [ ] ) ,
6778 }
6879 vi . mocked ( CodeIndexManagerRegistry . getOrCreate ) . mockReturnValue ( manager as CodeIndexManager )
6980 vi . mocked ( getWorkspacePath ) . mockReturnValue ( "/fallback" )
7081 vi . mocked ( vscode . workspace . asRelativePath ) . mockReturnValue ( "src/result.ts" )
7182 } )
7283
73- afterEach ( ( ) => vi . restoreAllMocks ( ) )
84+ afterEach ( ( ) => {
85+ expect ( manager . initialize ) . not . toHaveBeenCalled ( )
86+ vi . restoreAllMocks ( )
87+ } )
7488
7589 function result ( overrides : Partial < VectorStoreSearchResult > = { } ) : VectorStoreSearchResult {
7690 return {
@@ -174,7 +188,8 @@ describe("CodebaseSearchTool", () => {
174188 expect ( task . say ) . not . toHaveBeenCalled ( )
175189 } )
176190
177- it ( "reports disabled indexing without searching" , async ( ) => {
191+ it . each ( [ true , false ] ) ( "reports disabled indexing without searching (initialized: %s)" , async ( initialized ) => {
192+ Object . defineProperty ( manager , "isInitialized" , { value : initialized } )
178193 Object . defineProperty ( manager , "isFeatureEnabled" , { value : false } )
179194
180195 await tool . execute ( { query } , task , callbacks )
@@ -190,20 +205,38 @@ describe("CodebaseSearchTool", () => {
190205 expect ( task . say ) . not . toHaveBeenCalled ( )
191206 } )
192207
193- it ( "reports missing index configuration without searching" , async ( ) => {
194- Object . defineProperty ( manager , "isFeatureConfigured" , { value : false } )
208+ it . each ( [ true , false ] ) (
209+ "reports missing index configuration without searching (initialized: %s)" ,
210+ async ( initialized ) => {
211+ Object . defineProperty ( manager , "isInitialized" , { value : initialized } )
212+ Object . defineProperty ( manager , "isFeatureConfigured" , { value : false } )
213+
214+ await tool . execute ( { query } , task , callbacks )
215+
216+ expect ( callbacks . handleError ) . toHaveBeenCalledExactlyOnceWith (
217+ toolNamesSchema . enum . codebase_search ,
218+ new Error ( "Code Indexing is not configured (Missing OpenAI Key or Qdrant URL)." ) ,
219+ )
220+ expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
221+ expect ( task . consecutiveMistakeCount ) . toBe ( 0 )
222+ expect ( manager . searchIndex ) . not . toHaveBeenCalled ( )
223+ expect ( vscode . workspace . asRelativePath ) . not . toHaveBeenCalled ( )
224+ expect ( task . say ) . not . toHaveBeenCalled ( )
225+ } ,
226+ )
227+
228+ it ( "reports configured but unready services without initializing or searching" , async ( ) => {
229+ Object . defineProperty ( manager , "isInitialized" , { value : false } )
195230
196231 await tool . execute ( { query } , task , callbacks )
197232
233+ expect ( manager . initialize ) . not . toHaveBeenCalled ( )
198234 expect ( callbacks . handleError ) . toHaveBeenCalledExactlyOnceWith (
199235 toolNamesSchema . enum . codebase_search ,
200- new Error ( "Code Indexing is not configured (Missing OpenAI Key or Qdrant URL) ." ) ,
236+ new Error ( "Code Indexing is not initialized for this workspace ." ) ,
201237 )
202- expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
203- expect ( task . consecutiveMistakeCount ) . toBe ( 0 )
204238 expect ( manager . searchIndex ) . not . toHaveBeenCalled ( )
205- expect ( vscode . workspace . asRelativePath ) . not . toHaveBeenCalled ( )
206- expect ( task . say ) . not . toHaveBeenCalled ( )
239+ expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
207240 } )
208241
209242 it . each ( [ undefined , "src" , "" ] ) (
@@ -214,6 +247,7 @@ describe("CodebaseSearchTool", () => {
214247 return [ ]
215248 } )
216249 await tool . execute ( { query, path } , task , callbacks )
250+ expect ( CodeIndexManagerRegistry . getOrCreate ) . toHaveBeenCalledExactlyOnceWith ( context , "/task" )
217251 expect ( manager . searchIndex ) . toHaveBeenCalledExactlyOnceWith ( query , path )
218252 expect ( callbacks . pushToolResult ) . toHaveBeenCalledExactlyOnceWith (
219253 `No relevant code snippets found for the query: "${ query } "` ,
@@ -253,6 +287,8 @@ describe("CodebaseSearchTool", () => {
253287 . mockReturnValueOnce ( "src/result.ts" )
254288 . mockReturnValueOnce ( "lib/other.ts" )
255289 await tool . execute ( { query } , task , callbacks )
290+ expect ( CodeIndexManagerRegistry . getOrCreate ) . toHaveBeenCalledExactlyOnceWith ( context , "/task" )
291+ expect ( manager . searchIndex ) . toHaveBeenCalledExactlyOnceWith ( query , undefined )
256292 expect ( vscode . workspace . asRelativePath ) . toHaveBeenCalledTimes ( 2 )
257293 expect ( vscode . workspace . asRelativePath ) . toHaveBeenNthCalledWith ( 1 , "/task/src/result.ts" , false )
258294 expect ( vscode . workspace . asRelativePath ) . toHaveBeenNthCalledWith ( 2 , "/task/lib/other.ts" , false )
0 commit comments