@@ -9,7 +9,7 @@ import { AWSError } from 'aws-sdk'
99import * as sinon from 'sinon'
1010import { DefaultEc2MetadataClient } from '../../shared/clients/ec2MetadataClient'
1111import * as vscode from 'vscode'
12- import { UserActivity , getComputeRegion , initializeComputeRegion } from '../../shared/extensionUtilities'
12+ import { UserActivity , getComputeRegion , initializeComputeRegion , isCn } from '../../shared/extensionUtilities'
1313import { isDifferentVersion , setMostRecentVersion } from '../../shared/extensionUtilities'
1414import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
1515import { extensionVersion } from '../../shared/vscode/env'
@@ -135,6 +135,65 @@ describe('initializeComputeRegion, getComputeRegion', async function () {
135135 } )
136136} )
137137
138+ describe ( 'isCn' , function ( ) {
139+ let sandbox : sinon . SinonSandbox
140+ const metadataService = new DefaultEc2MetadataClient ( )
141+
142+ beforeEach ( function ( ) {
143+ sandbox = sinon . createSandbox ( )
144+ } )
145+
146+ afterEach ( function ( ) {
147+ sandbox . restore ( )
148+ } )
149+
150+ it ( 'returns false when compute region is not initialized' , async function ( ) {
151+ // Reset the compute region to undefined first
152+ const utils = require ( '../../shared/extensionUtilities' )
153+ Object . defineProperty ( utils , 'computeRegion' , {
154+ value : undefined ,
155+ configurable : true ,
156+ } )
157+
158+ const result = isCn ( )
159+
160+ assert . strictEqual ( result , false , 'isCn() should return false when compute region is not initialized' )
161+ } )
162+
163+ it ( 'returns true for CN regions' , async function ( ) {
164+ sandbox . stub ( metadataService , 'getInstanceIdentity' ) . resolves ( { region : 'cn-north-1' } )
165+ await initializeComputeRegion ( metadataService , false , true )
166+
167+ const result = isCn ( )
168+
169+ assert . strictEqual ( result , true , 'isCn() should return true for China regions' )
170+ } )
171+
172+ it ( 'returns false for non-CN regions' , async function ( ) {
173+ sandbox . stub ( metadataService , 'getInstanceIdentity' ) . resolves ( { region : 'us-east-1' } )
174+ await initializeComputeRegion ( metadataService , false , true )
175+
176+ const result = isCn ( )
177+
178+ assert . strictEqual ( result , false , 'isCn() should return false for non-China regions' )
179+ } )
180+
181+ it ( 'returns false when an error occurs' , async function ( ) {
182+ const utils = require ( '../../shared/extensionUtilities' )
183+
184+ // Restore original getComputeRegion if it was stubbed
185+ if ( utils . getComputeRegion . restore ) {
186+ utils . getComputeRegion . restore ( )
187+ }
188+
189+ sandbox . stub ( utils , 'getComputeRegion' ) . throws ( new Error ( 'Test error' ) )
190+
191+ const result = isCn ( )
192+
193+ assert . strictEqual ( result , false , 'isCn() should return false when an error occurs' )
194+ } )
195+ } )
196+
138197describe ( 'UserActivity' , function ( ) {
139198 let count : number
140199 let sandbox : sinon . SinonSandbox
0 commit comments