@@ -332,6 +332,72 @@ describe("popup.js", () => {
332332
333333 expect ( result ) . toEqual ( { } ) ;
334334 } ) ;
335+
336+ test ( "getWarmState returns empty object when extension context is invalid" , async ( ) => {
337+ const originalId = chrome . runtime . id ;
338+ delete chrome . runtime . id ;
339+
340+ const result = await popup . getWarmState ( ) ;
341+
342+ expect ( result ) . toEqual ( { } ) ;
343+ chrome . runtime . id = originalId ;
344+ } ) ;
345+
346+ test ( "getWarmState retries on chrome.runtime.lastError" , async ( ) => {
347+ let callCount = 0 ;
348+ chrome . runtime . sendMessage . mockImplementation ( ( message , callback ) => {
349+ if ( message . action === "getWarmState" ) {
350+ callCount ++ ;
351+ if ( callCount < 3 ) {
352+ // Simulate async behavior with setTimeout to allow retry to work
353+ setTimeout ( ( ) => {
354+ Object . defineProperty ( chrome . runtime , "lastError" , {
355+ value : { message : "Service worker not responding" } ,
356+ configurable : true ,
357+ } ) ;
358+ callback ( null ) ;
359+ Object . defineProperty ( chrome . runtime , "lastError" , {
360+ value : null ,
361+ configurable : true ,
362+ } ) ;
363+ } , 0 ) ;
364+ } else {
365+ setTimeout ( ( ) => {
366+ callback ( { searchHistoryList : [ "success" ] } ) ;
367+ } , 0 ) ;
368+ }
369+ }
370+ return true ;
371+ } ) ;
372+
373+ const result = await popup . getWarmState ( ) ;
374+
375+ expect ( result ) . toEqual ( { searchHistoryList : [ "success" ] } ) ;
376+ expect ( callCount ) . toBe ( 3 ) ;
377+ } ) ;
378+
379+ test ( "getWarmState returns empty object after all retries exhausted" , async ( ) => {
380+ chrome . runtime . sendMessage . mockImplementation ( ( message , callback ) => {
381+ if ( message . action === "getWarmState" ) {
382+ setTimeout ( ( ) => {
383+ Object . defineProperty ( chrome . runtime , "lastError" , {
384+ value : { message : "Service worker not responding" } ,
385+ configurable : true ,
386+ } ) ;
387+ callback ( null ) ;
388+ Object . defineProperty ( chrome . runtime , "lastError" , {
389+ value : null ,
390+ configurable : true ,
391+ } ) ;
392+ } , 0 ) ;
393+ }
394+ return true ;
395+ } ) ;
396+
397+ const result = await popup . getWarmState ( 0 ) ; // 0 retries = single attempt only
398+
399+ expect ( result ) . toEqual ( { } ) ;
400+ } ) ;
335401 } ) ;
336402
337403 describe ( "fetchData" , ( ) => {
0 commit comments