@@ -231,6 +231,55 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
231231 } ,
232232 )
233233
234+ it . each ( [ false , true ] ) (
235+ "only cleans up after partial full-scan progress when the collection was created by this run: %s" ,
236+ async ( created ) => {
237+ const failure = new Error ( "batch failure after partial progress" )
238+ vectorStore . initialize . mockResolvedValue ( created )
239+ vectorStore . hasIndexedData . mockResolvedValue ( false )
240+ vectorStore . markIndexingIncomplete . mockResolvedValue ( undefined )
241+ scanner . scanDirectory . mockImplementation (
242+ async (
243+ _dir : string ,
244+ onError : ( error : Error ) => void ,
245+ onIndexed : ( count : number ) => void ,
246+ onParsed : ( count : number ) => void ,
247+ ) => {
248+ onParsed ( 3 )
249+ onIndexed ( 1 )
250+ onError ( failure )
251+ return { stats : { processed : 1 , skipped : 0 } , totalBlockCount : 3 }
252+ } ,
253+ )
254+ const orchestrator = new CodeIndexOrchestrator (
255+ configManager ,
256+ stateManager ,
257+ workspacePath ,
258+ cacheManager ,
259+ vectorStore ,
260+ scanner ,
261+ fileWatcher ,
262+ )
263+
264+ await orchestrator . startIndexing ( )
265+
266+ expect ( scanner . scanDirectory ) . toHaveBeenCalledOnce ( )
267+ expect ( stateManager . reportBlockIndexingProgress ) . toHaveBeenLastCalledWith ( 1 , 3 )
268+ expect ( stateManager . setSystemState ) . toHaveBeenLastCalledWith (
269+ "Error" ,
270+ expect . stringContaining ( failure . message ) ,
271+ )
272+ expect ( stateManager . setSystemState ) . not . toHaveBeenCalledWith ( "Indexed" , expect . any ( String ) )
273+ expect ( vectorStore . clearCollection ) . toHaveBeenCalledTimes ( created ? 1 : 0 )
274+ // New collections clear stale cache before scanning and again during error cleanup.
275+ expect ( cacheManager . clearCacheFile ) . toHaveBeenCalledTimes ( created ? 2 : 0 )
276+ expect ( vectorStore . markIndexingIncomplete ) . toHaveBeenCalledOnce ( )
277+ expect ( vectorStore . markIndexingComplete ) . not . toHaveBeenCalled ( )
278+ expect ( fileWatcher . initialize ) . not . toHaveBeenCalled ( )
279+ expect ( fileWatcher . dispose ) . toHaveBeenCalledOnce ( )
280+ } ,
281+ )
282+
234283 it ( "preserves an existing index after an incremental failure and a failed full-scan retry" , async ( ) => {
235284 let complete = true
236285 vectorStore . initialize . mockResolvedValue ( false )
0 commit comments