@@ -236,4 +236,52 @@ describe("MultiIndexRunner.refreshIndexList", () => {
236236 // In fixed mode, the list should remain unchanged even though pytorch was deleted
237237 expect ( runner . indexNames ) . toEqual ( [ "pytorch" , "react" ] ) ;
238238 } ) ;
239+
240+ it ( "in discovery mode, refreshIndexList prunes stale cache entries" , async ( ) => {
241+ const store = createMockStoreWithIndexes ( [ "pytorch" , "react" , "docs" ] ) ;
242+
243+ const runner = await MultiIndexRunner . create ( {
244+ store,
245+ // No indexNames = discovery mode
246+ } ) ;
247+
248+ expect ( runner . indexNames ) . toEqual ( [ "pytorch" , "react" , "docs" ] ) ;
249+
250+ // Manually populate cache with mock clients (avoid initialization)
251+ const mockClient = { initialized : true } as any ;
252+ ( runner as any ) . clientCache . set ( "pytorch" , mockClient ) ;
253+ ( runner as any ) . clientCache . set ( "react" , mockClient ) ;
254+ ( runner as any ) . clientCache . set ( "docs" , mockClient ) ;
255+
256+ // Verify cache has all three entries
257+ expect ( ( runner as any ) . clientCache . size ) . toBe ( 3 ) ;
258+
259+ // Simulate store losing the "docs" index
260+ ( store . list as any ) . mockResolvedValue ( [ "pytorch" , "react" ] ) ;
261+ ( store . loadSearch as any ) . mockImplementation ( ( name : string ) => {
262+ if ( [ "pytorch" , "react" ] . includes ( name ) ) {
263+ return Promise . resolve ( {
264+ version : 1 ,
265+ contextState : { version : 1 } as any ,
266+ source : {
267+ type : "github" ,
268+ config : { owner : "test" , repo : name } ,
269+ syncedAt : new Date ( ) . toISOString ( ) ,
270+ } ,
271+ } ) ;
272+ }
273+ return Promise . resolve ( null ) ;
274+ } ) ;
275+
276+ await runner . refreshIndexList ( ) ;
277+
278+ // Index list should be updated
279+ expect ( runner . indexNames ) . toEqual ( [ "pytorch" , "react" ] ) ;
280+
281+ // Cache should be pruned - only pytorch and react remain
282+ expect ( ( runner as any ) . clientCache . size ) . toBe ( 2 ) ;
283+ expect ( ( runner as any ) . clientCache . has ( "pytorch" ) ) . toBe ( true ) ;
284+ expect ( ( runner as any ) . clientCache . has ( "react" ) ) . toBe ( true ) ;
285+ expect ( ( runner as any ) . clientCache . has ( "docs" ) ) . toBe ( false ) ;
286+ } ) ;
239287} ) ;
0 commit comments