@@ -9,7 +9,7 @@ import { AWSError } from 'aws-sdk'
9
9
import * as sinon from 'sinon'
10
10
import { DefaultEc2MetadataClient } from '../../shared/clients/ec2MetadataClient'
11
11
import * as vscode from 'vscode'
12
- import { UserActivity , getComputeRegion , initializeComputeRegion } from '../../shared/extensionUtilities'
12
+ import { UserActivity , getComputeRegion , initializeComputeRegion , isCn } from '../../shared/extensionUtilities'
13
13
import { isDifferentVersion , setMostRecentVersion } from '../../shared/extensionUtilities'
14
14
import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
15
15
import { extensionVersion } from '../../shared/vscode/env'
@@ -135,6 +135,73 @@ describe('initializeComputeRegion, getComputeRegion', async function () {
135
135
} )
136
136
} )
137
137
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 defined' , 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 undefined' )
161
+ } )
162
+
163
+ it ( 'returns false when compute region is not initialized' , async function ( ) {
164
+ // Set the compute region to "notInitialized"
165
+ const utils = require ( '../../shared/extensionUtilities' )
166
+ Object . defineProperty ( utils , 'computeRegion' , {
167
+ value : 'notInitialized' ,
168
+ configurable : true ,
169
+ } )
170
+
171
+ const result = isCn ( )
172
+
173
+ assert . strictEqual ( result , false , 'isCn() should return false when compute region is notInitialized' )
174
+ } )
175
+
176
+ it ( 'returns true for CN regions' , async function ( ) {
177
+ sandbox . stub ( metadataService , 'getInstanceIdentity' ) . resolves ( { region : 'cn-north-1' } )
178
+ await initializeComputeRegion ( metadataService , false , true )
179
+
180
+ const result = isCn ( )
181
+
182
+ assert . strictEqual ( result , true , 'isCn() should return true for China regions' )
183
+ } )
184
+
185
+ it ( 'returns false for non-CN regions' , async function ( ) {
186
+ sandbox . stub ( metadataService , 'getInstanceIdentity' ) . resolves ( { region : 'us-east-1' } )
187
+ await initializeComputeRegion ( metadataService , false , true )
188
+
189
+ const result = isCn ( )
190
+
191
+ assert . strictEqual ( result , false , 'isCn() should return false for non-China regions' )
192
+ } )
193
+
194
+ it ( 'returns false when an error occurs' , async function ( ) {
195
+ const utils = require ( '../../shared/extensionUtilities' )
196
+
197
+ sandbox . stub ( utils , 'getComputeRegion' ) . throws ( new Error ( 'Test error' ) )
198
+
199
+ const result = isCn ( )
200
+
201
+ assert . strictEqual ( result , false , 'isCn() should return false when an error occurs' )
202
+ } )
203
+ } )
204
+
138
205
describe ( 'UserActivity' , function ( ) {
139
206
let count : number
140
207
let sandbox : sinon . SinonSandbox
0 commit comments