@@ -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,27 @@ describe("CodebaseSearchTool", () => {
174188 expect ( task . say ) . not . toHaveBeenCalled ( )
175189 } )
176190
177- it ( "reports disabled indexing without searching" , async ( ) => {
191+ it ( "reports configuration that has never loaded before checking settings or services" , async ( ) => {
192+ Object . defineProperties ( manager , {
193+ isConfigurationLoaded : { value : false } ,
194+ isFeatureEnabled : { value : false } ,
195+ isFeatureConfigured : { value : false } ,
196+ isInitialized : { value : false } ,
197+ } )
198+
199+ await tool . execute ( { query } , task , callbacks )
200+
201+ expect ( callbacks . handleError ) . toHaveBeenCalledExactlyOnceWith (
202+ toolNamesSchema . enum . codebase_search ,
203+ new Error ( "Code Indexing configuration has not been loaded for this workspace." ) ,
204+ )
205+ expect ( manager . initialize ) . not . toHaveBeenCalled ( )
206+ expect ( manager . searchIndex ) . not . toHaveBeenCalled ( )
207+ expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
208+ } )
209+
210+ it . each ( [ true , false ] ) ( "reports disabled indexing without searching (initialized: %s)" , async ( initialized ) => {
211+ Object . defineProperty ( manager , "isInitialized" , { value : initialized } )
178212 Object . defineProperty ( manager , "isFeatureEnabled" , { value : false } )
179213
180214 await tool . execute ( { query } , task , callbacks )
@@ -190,20 +224,38 @@ describe("CodebaseSearchTool", () => {
190224 expect ( task . say ) . not . toHaveBeenCalled ( )
191225 } )
192226
193- it ( "reports missing index configuration without searching" , async ( ) => {
194- Object . defineProperty ( manager , "isFeatureConfigured" , { value : false } )
227+ it . each ( [ true , false ] ) (
228+ "reports missing index configuration without searching (initialized: %s)" ,
229+ async ( initialized ) => {
230+ Object . defineProperty ( manager , "isInitialized" , { value : initialized } )
231+ Object . defineProperty ( manager , "isFeatureConfigured" , { value : false } )
232+
233+ await tool . execute ( { query } , task , callbacks )
234+
235+ expect ( callbacks . handleError ) . toHaveBeenCalledExactlyOnceWith (
236+ toolNamesSchema . enum . codebase_search ,
237+ new Error ( "Code Indexing is not configured (Missing OpenAI Key or Qdrant URL)." ) ,
238+ )
239+ expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
240+ expect ( task . consecutiveMistakeCount ) . toBe ( 0 )
241+ expect ( manager . searchIndex ) . not . toHaveBeenCalled ( )
242+ expect ( vscode . workspace . asRelativePath ) . not . toHaveBeenCalled ( )
243+ expect ( task . say ) . not . toHaveBeenCalled ( )
244+ } ,
245+ )
246+
247+ it ( "reports configured but unready services without initializing or searching" , async ( ) => {
248+ Object . defineProperty ( manager , "isInitialized" , { value : false } )
195249
196250 await tool . execute ( { query } , task , callbacks )
197251
252+ expect ( manager . initialize ) . not . toHaveBeenCalled ( )
198253 expect ( callbacks . handleError ) . toHaveBeenCalledExactlyOnceWith (
199254 toolNamesSchema . enum . codebase_search ,
200- new Error ( "Code Indexing is not configured (Missing OpenAI Key or Qdrant URL) ." ) ,
255+ new Error ( "Code Indexing is not initialized for this workspace ." ) ,
201256 )
202- expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
203- expect ( task . consecutiveMistakeCount ) . toBe ( 0 )
204257 expect ( manager . searchIndex ) . not . toHaveBeenCalled ( )
205- expect ( vscode . workspace . asRelativePath ) . not . toHaveBeenCalled ( )
206- expect ( task . say ) . not . toHaveBeenCalled ( )
258+ expect ( callbacks . pushToolResult ) . not . toHaveBeenCalled ( )
207259 } )
208260
209261 it . each ( [ undefined , "src" , "" ] ) (
@@ -214,6 +266,7 @@ describe("CodebaseSearchTool", () => {
214266 return [ ]
215267 } )
216268 await tool . execute ( { query, path } , task , callbacks )
269+ expect ( CodeIndexManagerRegistry . getOrCreate ) . toHaveBeenCalledExactlyOnceWith ( context , "/task" )
217270 expect ( manager . searchIndex ) . toHaveBeenCalledExactlyOnceWith ( query , path )
218271 expect ( callbacks . pushToolResult ) . toHaveBeenCalledExactlyOnceWith (
219272 `No relevant code snippets found for the query: "${ query } "` ,
@@ -253,6 +306,8 @@ describe("CodebaseSearchTool", () => {
253306 . mockReturnValueOnce ( "src/result.ts" )
254307 . mockReturnValueOnce ( "lib/other.ts" )
255308 await tool . execute ( { query } , task , callbacks )
309+ expect ( CodeIndexManagerRegistry . getOrCreate ) . toHaveBeenCalledExactlyOnceWith ( context , "/task" )
310+ expect ( manager . searchIndex ) . toHaveBeenCalledExactlyOnceWith ( query , undefined )
256311 expect ( vscode . workspace . asRelativePath ) . toHaveBeenCalledTimes ( 2 )
257312 expect ( vscode . workspace . asRelativePath ) . toHaveBeenNthCalledWith ( 1 , "/task/src/result.ts" , false )
258313 expect ( vscode . workspace . asRelativePath ) . toHaveBeenNthCalledWith ( 2 , "/task/lib/other.ts" , false )
0 commit comments