@@ -200,12 +200,48 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
200200 expect ( lastCall [ 0 ] ) . toBe ( "Error" )
201201 } )
202202
203- it ( "should call clearCollection() and clear cache when an error occurs after initialize() succeeds (indexing started)" , async ( ) => {
204- // Arrange: initialize succeeds; fail soon after to enter error path with indexingStarted=true
205- vectorStore . initialize . mockResolvedValue ( false ) // existing collection
206- vectorStore . hasIndexedData . mockResolvedValue ( false ) // force full scan path
207- vectorStore . markIndexingIncomplete . mockRejectedValue ( new Error ( "mark incomplete failure" ) )
203+ it . each ( [ false , true ] ) (
204+ "only cleans up a failed full scan when the collection was created by this run: %s" ,
205+ async ( created ) => {
206+ vectorStore . initialize . mockResolvedValue ( created )
207+ vectorStore . hasIndexedData . mockResolvedValue ( false ) // force full scan path
208+ vectorStore . markIndexingIncomplete . mockRejectedValue ( new Error ( "mark incomplete failure" ) )
209+
210+ const orchestrator = new CodeIndexOrchestrator (
211+ configManager ,
212+ stateManager ,
213+ workspacePath ,
214+ cacheManager ,
215+ vectorStore ,
216+ scanner ,
217+ fileWatcher ,
218+ )
219+
220+ // Act
221+ await orchestrator . startIndexing ( )
222+
223+ expect ( vectorStore . clearCollection ) . toHaveBeenCalledTimes ( created ? 1 : 0 )
224+ // A new collection clears stale cache at initialization and again on failure.
225+ expect ( cacheManager . clearCacheFile ) . toHaveBeenCalledTimes ( created ? 2 : 0 )
226+
227+ // Error state should be set
228+ expect ( stateManager . setSystemState ) . toHaveBeenCalled ( )
229+ const lastCall = stateManager . setSystemState . mock . calls [ stateManager . setSystemState . mock . calls . length - 1 ]
230+ expect ( lastCall [ 0 ] ) . toBe ( "Error" )
231+ } ,
232+ )
208233
234+ it ( "preserves an existing index after an incremental failure and a failed full-scan retry" , async ( ) => {
235+ let complete = true
236+ vectorStore . initialize . mockResolvedValue ( false )
237+ vectorStore . hasIndexedData . mockImplementation ( async ( ) => complete )
238+ vectorStore . markIndexingIncomplete . mockImplementation ( async ( ) => {
239+ complete = false
240+ } )
241+ scanner . scanDirectory . mockImplementation ( async ( _dir : string , onError : ( error : Error ) => void ) => {
242+ onError ( new Error ( "embedding failed" ) )
243+ return { stats : { processed : 0 , skipped : 0 } , totalBlockCount : 0 }
244+ } )
209245 const orchestrator = new CodeIndexOrchestrator (
210246 configManager ,
211247 stateManager ,
@@ -216,17 +252,23 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
216252 fileWatcher ,
217253 )
218254
219- // Act
220- await orchestrator . startIndexing ( )
221-
222- // Assert: cleanup gated behind indexingStarted should have happened
223- expect ( vectorStore . clearCollection ) . toHaveBeenCalledTimes ( 1 )
224- expect ( cacheManager . clearCacheFile ) . toHaveBeenCalledTimes ( 1 )
225-
226- // Error state should be set
227- expect ( stateManager . setSystemState ) . toHaveBeenCalled ( )
228- const lastCall = stateManager . setSystemState . mock . calls [ stateManager . setSystemState . mock . calls . length - 1 ]
229- expect ( lastCall [ 0 ] ) . toBe ( "Error" )
255+ for ( let attempt = 1 ; attempt <= 2 ; attempt ++ ) {
256+ await orchestrator . startIndexing ( )
257+ expect ( complete ) . toBe ( false )
258+ expect ( orchestrator . state ) . toBe ( "Error" )
259+ expect ( scanner . scanDirectory ) . toHaveBeenCalledTimes ( attempt )
260+ expect ( vectorStore . markIndexingIncomplete ) . toHaveBeenCalledTimes ( attempt )
261+ expect ( vectorStore . clearCollection ) . not . toHaveBeenCalled ( )
262+ expect ( cacheManager . clearCacheFile ) . not . toHaveBeenCalled ( )
263+ expect ( vectorStore . markIndexingComplete ) . not . toHaveBeenCalled ( )
264+ expect ( fileWatcher . initialize ) . not . toHaveBeenCalled ( )
265+ }
266+ expect ( stateManager . setSystemState ) . toHaveBeenCalledWith ( "Indexing" , "Checking for new or modified files..." )
267+ expect ( stateManager . setSystemState ) . toHaveBeenCalledWith (
268+ "Indexing" ,
269+ "Services ready. Starting workspace scan..." ,
270+ )
271+ expect ( stateManager . setSystemState ) . not . toHaveBeenCalledWith ( "Indexed" , expect . any ( String ) )
230272 } )
231273
232274 it ( "collects batch errors from full scan and transitions to Error when all blocks fail" , async ( ) => {
0 commit comments