@@ -2,9 +2,15 @@ import { describe, expect, it } from 'vitest';
2
2
import {
3
3
EARTH_RADIUS ,
4
4
getClosestAwsRegion ,
5
+ getCountryCode ,
5
6
getCountryCoordinates ,
6
7
getDistance ,
8
+ TRACE_URL ,
7
9
} from './regions.js' ;
10
+ import { http , HttpResponse } from 'msw' ;
11
+ import { setupServer } from 'msw/node' ;
12
+
13
+ const COUNTRY_CODE = 'US' ;
8
14
9
15
describe ( 'getDistance' , ( ) => {
10
16
it ( 'should return 0 for the same coordinates' , ( ) => {
@@ -61,37 +67,55 @@ describe('getDistance', () => {
61
67
} ) ;
62
68
63
69
describe ( 'getClosestRegion' , ( ) => {
64
- it ( 'should find the closest AWS region to a specific location' , async ( ) => {
70
+ it ( 'should find the closest AWS region to a specific location' , ( ) => {
65
71
const tokyo = { lat : 35.6762 , lng : 139.6503 } ;
66
- const result = await getClosestAwsRegion ( tokyo ) ;
72
+ const result = getClosestAwsRegion ( tokyo ) ;
67
73
expect ( result . code ) . toBe ( 'ap-northeast-1' ) ; // Tokyo region
68
74
} ) ;
69
75
70
- it ( 'should find the correct AWS region for European location' , async ( ) => {
76
+ it ( 'should find the correct AWS region for European location' , ( ) => {
71
77
const london = { lat : 51.5074 , lng : - 0.1278 } ;
72
- const result = await getClosestAwsRegion ( london ) ;
78
+ const result = getClosestAwsRegion ( london ) ;
73
79
expect ( result . code ) . toBe ( 'eu-west-2' ) ; // London region
74
80
} ) ;
75
81
76
- it ( 'should find the correct AWS region for US West Coast location' , async ( ) => {
82
+ it ( 'should find the correct AWS region for US West Coast location' , ( ) => {
77
83
const sanFrancisco = { lat : 37.7749 , lng : - 122.4194 } ;
78
- const result = await getClosestAwsRegion ( sanFrancisco ) ;
84
+ const result = getClosestAwsRegion ( sanFrancisco ) ;
79
85
expect ( result . code ) . toBe ( 'us-west-1' ) ; // North California region
80
86
} ) ;
81
87
82
- it ( 'should find the correct AWS region for Sydney location' , async ( ) => {
88
+ it ( 'should find the correct AWS region for Sydney location' , ( ) => {
83
89
const sydney = { lat : - 33.8688 , lng : 151.2093 } ;
84
- const result = await getClosestAwsRegion ( sydney ) ;
90
+ const result = getClosestAwsRegion ( sydney ) ;
85
91
expect ( result . code ) . toBe ( 'ap-southeast-2' ) ; // Sydney region
86
92
} ) ;
87
93
88
- it ( 'should find the correct AWS region for a location in South America' , async ( ) => {
94
+ it ( 'should find the correct AWS region for a location in South America' , ( ) => {
89
95
const saoPaulo = { lat : - 23.5505 , lng : - 46.6333 } ;
90
- const result = await getClosestAwsRegion ( saoPaulo ) ;
96
+ const result = getClosestAwsRegion ( saoPaulo ) ;
91
97
expect ( result . code ) . toBe ( 'sa-east-1' ) ; // São Paulo region
92
98
} ) ;
93
99
} ) ;
94
100
101
+ describe ( 'getCountryCode' , ( ) => {
102
+ const handlers = [
103
+ http . get ( TRACE_URL , ( ) => {
104
+ return HttpResponse . text (
105
+ `fl=123abc\nvisit_scheme=https\nloc=${ COUNTRY_CODE } \ntls=TLSv1.3\nhttp=http/2`
106
+ ) ;
107
+ } ) ,
108
+ ] ;
109
+
110
+ const server = setupServer ( ...handlers ) ;
111
+ server . listen ( { onUnhandledRequest : 'error' } ) ;
112
+
113
+ it ( 'should return the correct country code for a given location' , async ( ) => {
114
+ const code = await getCountryCode ( ) ;
115
+ expect ( code ) . toEqual ( COUNTRY_CODE ) ;
116
+ } ) ;
117
+ } ) ;
118
+
95
119
describe ( 'getCountryCoordinates' , ( ) => {
96
120
it ( 'should throw an error for an invalid country code' , async ( ) => {
97
121
const invalidCountryCode = 'INVALID_CODE' ;
0 commit comments