@@ -9,6 +9,7 @@ import { AuthUtil, RegionProfile, RegionProfileManager, defaultServiceConfig } f
99import { globals } from 'aws-core-vscode/shared'
1010import { constants } from 'aws-core-vscode/auth'
1111import { createTestAuthUtil } from 'aws-core-vscode/test'
12+ import { randomUUID } from 'crypto'
1213
1314const enterpriseSsoStartUrl = 'https://enterprise.awsapps.com/start'
1415const region = 'us-east-1'
@@ -158,7 +159,7 @@ describe('RegionProfileManager', async function () {
158159 } )
159160 } )
160161
161- describe ( 'persistence ' , function ( ) {
162+ describe ( 'persistSelectedRegionProfile ' , function ( ) {
162163 it ( 'persistSelectedRegionProfile' , async function ( ) {
163164 await setupConnection ( 'idc' )
164165 await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
@@ -177,14 +178,13 @@ describe('RegionProfileManager', async function () {
177178
178179 assert . strictEqual ( state [ AuthUtil . instance . profileName ] , profileFoo )
179180 } )
181+ } )
180182
181- it ( ` restoreRegionProfile` , async function ( ) {
182- sinon . stub ( regionProfileManager , 'listRegionProfile' ) . resolves ( [ profileFoo ] )
183+ describe ( ' restoreRegionProfile' , function ( ) {
184+ beforeEach ( async function ( ) {
183185 await setupConnection ( 'idc' )
184- if ( ! AuthUtil . instance . isConnected ( ) ) {
185- fail ( 'connection should not be undefined' )
186- }
187-
186+ } )
187+ it ( 'restores region profile if profile name matches' , async function ( ) {
188188 const state = { } as any
189189 state [ AuthUtil . instance . profileName ] = profileFoo
190190
@@ -194,6 +194,51 @@ describe('RegionProfileManager', async function () {
194194
195195 assert . strictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
196196 } )
197+
198+ it ( 'returns early when no profiles exist' , async function ( ) {
199+ const state = { } as any
200+ state [ AuthUtil . instance . profileName ] = undefined
201+
202+ await globals . globalState . update ( 'aws.amazonq.regionProfiles' , state )
203+
204+ await regionProfileManager . restoreRegionProfile ( )
205+ assert . strictEqual ( regionProfileManager . activeRegionProfile , undefined )
206+ } )
207+
208+ it ( 'returns early when no profile name matches, and multiple profiles exist' , async function ( ) {
209+ const state = { } as any
210+ state [ AuthUtil . instance . profileName ] = undefined
211+ state [ randomUUID ( ) ] = profileFoo
212+
213+ await globals . globalState . update ( 'aws.amazonq.regionProfiles' , state )
214+
215+ await regionProfileManager . restoreRegionProfile ( )
216+ assert . strictEqual ( regionProfileManager . activeRegionProfile , undefined )
217+ } )
218+
219+ it ( 'uses single profile when no profile name matches' , async function ( ) {
220+ const state = { } as any
221+ state [ randomUUID ( ) ] = profileFoo
222+
223+ await globals . globalState . update ( 'aws.amazonq.regionProfiles' , state )
224+
225+ await regionProfileManager . restoreRegionProfile ( )
226+
227+ assert . strictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
228+ } )
229+
230+ it ( 'handles cross-validation failure' , async function ( ) {
231+ const state = {
232+ [ AuthUtil . instance . profileName ] : profileFoo ,
233+ }
234+ sinon . stub ( regionProfileManager , 'loadPersistedRegionProfiles' ) . returns ( state )
235+ sinon . stub ( regionProfileManager , 'getProfiles' ) . resolves ( [ ] ) // No matching profile
236+ const invalidateStub = sinon . stub ( regionProfileManager , 'invalidateProfile' )
237+
238+ await regionProfileManager . restoreRegionProfile ( )
239+
240+ assert . ok ( invalidateStub . calledWith ( profileFoo . arn ) )
241+ } )
197242 } )
198243
199244 describe ( 'invalidate' , function ( ) {
0 commit comments