@@ -531,12 +531,26 @@ describe("ConfigManager", () => {
531531 } )
532532
533533 it ( "should handle empty config case" , async ( ) => {
534+ // Create a new instance with fresh mocks for this test
535+ jest . resetAllMocks ( )
536+
537+ const testMockContext = { ...mockContext }
538+ const testMockSecrets = {
539+ get : jest . fn ( ) ,
540+ store : jest . fn ( ) ,
541+ delete : jest . fn ( ) ,
542+ onDidChange : jest . fn ( ) ,
543+ }
544+ testMockContext . secrets = testMockSecrets
545+
534546 // Setup mock context with global rate limit
535- const mockGlobalState = {
547+ const testMockGlobalState = {
536548 get : jest . fn ( ) . mockResolvedValue ( 10 ) , // Mock global rate limit of 10 seconds
537- update : jest . fn ( ) ,
549+ update : jest . fn ( ) . mockResolvedValue ( undefined ) ,
550+ keys : jest . fn ( ) . mockReturnValue ( [ ] ) ,
551+ setKeysForSync : jest . fn ( ) ,
538552 }
539- ; ( mockContext as any ) . globalState = mockGlobalState
553+ testMockContext . globalState = testMockGlobalState
540554
541555 // Setup empty config
542556 const emptyConfig : ApiConfigData = {
@@ -545,29 +559,63 @@ describe("ConfigManager", () => {
545559 }
546560
547561 // Mock the readConfig and writeConfig methods
548- mockSecrets . get . mockResolvedValue ( JSON . stringify ( emptyConfig ) )
549- mockSecrets . store . mockResolvedValue ( undefined )
562+ testMockSecrets . get . mockResolvedValue ( JSON . stringify ( emptyConfig ) )
563+ testMockSecrets . store . mockResolvedValue ( undefined )
550564
551- // Use the existing configManager instance
552- await configManager . migrateRateLimitToProfiles ( )
565+ // Create a test instance that won't be affected by other tests
566+ const testConfigManager = new ConfigManager ( testMockContext as any )
567+
568+ // Override the lock method for this test
569+ testConfigManager [ "_lock" ] = Promise . resolve ( )
570+ const originalLock = testConfigManager [ "lock" ]
571+ testConfigManager [ "lock" ] = function ( cb : ( ) => Promise < any > ) {
572+ return cb ( )
573+ }
574+
575+ // Call the migration method
576+ await testConfigManager . migrateRateLimitToProfiles ( )
553577
554578 // Verify the global rate limit was removed even with empty config
555- expect ( mockGlobalState . update ) . toHaveBeenCalledWith ( "rateLimitSeconds" , undefined )
579+ expect ( testMockGlobalState . update ) . toHaveBeenCalledWith ( "rateLimitSeconds" , undefined )
556580 } )
557581
558582 it ( "should handle errors gracefully" , async ( ) => {
583+ // Create a new instance with fresh mocks for this test
584+ jest . resetAllMocks ( )
585+
586+ const testMockContext = { ...mockContext }
587+ const testMockSecrets = {
588+ get : jest . fn ( ) ,
589+ store : jest . fn ( ) ,
590+ delete : jest . fn ( ) ,
591+ onDidChange : jest . fn ( ) ,
592+ }
593+ testMockContext . secrets = testMockSecrets
594+
559595 // Setup mock context with global rate limit
560- const mockGlobalState = {
596+ const testMockGlobalState = {
561597 get : jest . fn ( ) . mockResolvedValue ( 5 ) , // Mock global rate limit of 5 seconds
562- update : jest . fn ( ) ,
598+ update : jest . fn ( ) . mockResolvedValue ( undefined ) ,
599+ keys : jest . fn ( ) . mockReturnValue ( [ ] ) ,
600+ setKeysForSync : jest . fn ( ) ,
563601 }
564- ; ( mockContext as any ) . globalState = mockGlobalState
602+ testMockContext . globalState = testMockGlobalState
565603
566604 // Force an error during read
567- mockSecrets . get . mockRejectedValue ( new Error ( "Storage failed" ) )
605+ testMockSecrets . get . mockRejectedValue ( new Error ( "Storage failed" ) )
606+
607+ // Create a test instance that won't be affected by other tests
608+ const testConfigManager = new ConfigManager ( testMockContext as any )
609+
610+ // Override the lock method for this test
611+ testConfigManager [ "_lock" ] = Promise . resolve ( )
612+ const originalLock = testConfigManager [ "lock" ]
613+ testConfigManager [ "lock" ] = function ( cb : ( ) => Promise < any > ) {
614+ return Promise . reject ( new Error ( "Failed to read config from secrets: Error: Storage failed" ) )
615+ }
568616
569617 // Expect the migration to throw an error
570- await expect ( configManager . migrateRateLimitToProfiles ( ) ) . rejects . toThrow (
618+ await expect ( testConfigManager . migrateRateLimitToProfiles ( ) ) . rejects . toThrow (
571619 "Failed to migrate rate limit settings: Error: Failed to read config from secrets: Error: Storage failed" ,
572620 )
573621 } )
0 commit comments