11import { expect , test , describe , beforeEach , mock } from "bun:test" ;
22import { config , _invalidateConfigCache } from "@/lib/config" ;
33
4- interface PingOptions {
5- address : string ;
6- port ?: number ;
7- timeout ?: number ;
8- attempts ?: number ;
9- }
10-
114interface HostStatus {
12- host : string ;
13- latency : number | null ;
14- isDown : boolean ;
15- lastChecked : number ;
5+ host : string
6+ latency : number | null
7+ isDown : boolean
8+ lastChecked : number
169}
1710
18- // Mock the tcp-ping module to simulate different latency scenarios
19- mock . module ( "@network-utils/tcp-ping" , ( ) => ( {
20- ping : async ( options : PingOptions ) => {
21- const host = options . address ;
22-
23- // Simulate different latency scenarios
24- if ( host === "192.168.0.191" ) {
25- return {
26- averageLatency : 20 ,
27- errors : [ ] ,
28- maximumLatency : 25 ,
29- minimumLatency : 15 ,
30- options
31- } ;
32- } else if ( host === "192.168.0.192" ) {
33- return {
34- averageLatency : 30 ,
35- errors : [ ] ,
36- maximumLatency : 35 ,
37- minimumLatency : 25 ,
38- options
39- } ;
40- } else if ( host === "192.168.0.193" ) {
41- // Simulate host down
42- return {
43- averageLatency : null ,
44- errors : [ { attempt : 1 , error : new Error ( "Request timeout" ) } ] ,
45- maximumLatency : 0 ,
46- minimumLatency : null ,
47- options
48- } ;
49- } else {
50- // Default case for other hosts
51- return {
52- averageLatency : 50 ,
53- errors : [ ] ,
54- maximumLatency : 55 ,
55- minimumLatency : 45 ,
56- options
57- } ;
11+ // Mock the ping module to simulate different latency scenarios
12+ mock . module ( 'ping' , ( ) => ( {
13+ default : {
14+ promise : {
15+ probe : async ( host : string ) => {
16+ // Simulate different latency scenarios
17+ if ( host === "192.168.0.191" ) {
18+ return {
19+ alive : true ,
20+ time : 20
21+ } ;
22+ } else if ( host === "192.168.0.192" ) {
23+ return {
24+ alive : true ,
25+ time : 30
26+ } ;
27+ } else if ( host === "192.168.0.193" ) {
28+ // Simulate host down
29+ return {
30+ alive : false ,
31+ time : 'unknown'
32+ } ;
33+ } else {
34+ // Default case for other hosts
35+ return {
36+ alive : true ,
37+ time : 50
38+ } ;
39+ }
40+ }
5841 }
5942 }
6043} ) ) ;
@@ -130,33 +113,26 @@ describe("RADIUS Host Selector", () => {
130113 } ) ;
131114
132115 test ( "should demonstrate host selection logic with mocked responses" , async ( ) => {
133- // Test the tcp- ping mock directly to verify it's working as expected
134- const { ping } = await import ( "@network-utils/tcp- ping" ) ;
116+ // Test the ping mock directly to verify it's working as expected
117+ const ping = await import ( ' ping' ) ;
135118
136119 // Test various scenarios to validate our mock logic
137120 const scenarios = [
138121 // Normal latency responses
139- { host : "192.168.0.191" , expectedLatency : 20 , expectedDown : false , description : "fastest host" } ,
140- { host : "192.168.0.192" , expectedLatency : 30 , expectedDown : false , description : "slower host" } ,
141- { host : "192.168.0.193" , expectedLatency : null , expectedDown : true , description : "down host" } ,
142- { host : "10.0.0.100" , expectedLatency : 50 , expectedDown : false , description : "default case" }
122+ { host : "192.168.0.191" , expectedTime : 20 , expectedAlive : true , description : "fastest host" } ,
123+ { host : "192.168.0.192" , expectedTime : 30 , expectedAlive : true , description : "slower host" } ,
124+ { host : "192.168.0.193" , expectedTime : 'unknown' , expectedAlive : false , description : "down host" } ,
125+ { host : "10.0.0.100" , expectedTime : 50 , expectedAlive : true , description : "default case" }
143126 ] ;
144127
145128 for ( const scenario of scenarios ) {
146- const result = await ping ( {
147- address : scenario . host ,
148- port : 1812 ,
149- timeout : 5000 ,
150- attempts : 1
129+ const result = await ping . default . promise . probe ( scenario . host , {
130+ timeout : 5
151131 } ) ;
152132
153- if ( scenario . expectedDown ) {
154- expect ( result . averageLatency ) . toBeNull ( ) ;
155- expect ( result . errors . length ) . toBeGreaterThan ( 0 ) ;
156- } else {
157- expect ( result . averageLatency ) . toBe ( scenario . expectedLatency as number ) ;
158- expect ( result . errors ) . toEqual ( [ ] ) ;
159- }
133+ expect ( result . alive ) . toBe ( scenario . expectedAlive ) ;
134+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
135+ ( expect ( result . time ) as any ) . toEqual ( scenario . expectedTime ) ;
160136 }
161137 } ) ;
162138
@@ -220,35 +196,29 @@ describe("RADIUS Host Selector", () => {
220196
221197 test ( "should select second host when it has lowest latency" , async ( ) => {
222198 // Mock where the SECOND host has the lowest latency, not the first
223- mock . module ( "@network-utils/tcp-ping" , ( ) => ( {
224- ping : async ( options : PingOptions ) => {
225- const host = options . address ;
226- if ( host === "192.168.0.191" ) {
227- return {
228- averageLatency : 50 , // First host is slowest
229- errors : [ ] ,
230- maximumLatency : 55 ,
231- minimumLatency : 45 ,
232- options
233- } ;
234- } else if ( host === "192.168.0.192" ) {
235- return {
236- averageLatency : 15 , // Second host is fastest
237- errors : [ ] ,
238- maximumLatency : 20 ,
239- minimumLatency : 10 ,
240- options
241- } ;
242- } else if ( host === "192.168.0.193" ) {
243- return {
244- averageLatency : 30 , // Third host is medium
245- errors : [ ] ,
246- maximumLatency : 35 ,
247- minimumLatency : 25 ,
248- options
249- } ;
199+ mock . module ( 'ping' , ( ) => ( {
200+ default : {
201+ promise : {
202+ probe : async ( host : string ) => {
203+ if ( host === "192.168.0.191" ) {
204+ return {
205+ alive : true ,
206+ time : 50 // First host is slowest
207+ } ;
208+ } else if ( host === "192.168.0.192" ) {
209+ return {
210+ alive : true ,
211+ time : 15 // Second host is fastest
212+ } ;
213+ } else if ( host === "192.168.0.193" ) {
214+ return {
215+ alive : true ,
216+ time : 30 // Third host is medium
217+ } ;
218+ }
219+ return { alive : true , time : 100 } ;
220+ }
250221 }
251- return { averageLatency : 100 , errors : [ ] , maximumLatency : 100 , minimumLatency : 100 , options } ;
252222 }
253223 } ) ) ;
254224
@@ -274,35 +244,29 @@ describe("RADIUS Host Selector", () => {
274244
275245 test ( "should select third host when it has lowest latency" , async ( ) => {
276246 // Mock where the THIRD host has the lowest latency
277- mock . module ( "@network-utils/tcp-ping" , ( ) => ( {
278- ping : async ( options : PingOptions ) => {
279- const host = options . address ;
280- if ( host === "192.168.0.191" ) {
281- return {
282- averageLatency : 45 , // First host is medium
283- errors : [ ] ,
284- maximumLatency : 50 ,
285- minimumLatency : 40 ,
286- options
287- } ;
288- } else if ( host === "192.168.0.192" ) {
289- return {
290- averageLatency : 60 , // Second host is slowest
291- errors : [ ] ,
292- maximumLatency : 65 ,
293- minimumLatency : 55 ,
294- options
295- } ;
296- } else if ( host === "192.168.0.193" ) {
297- return {
298- averageLatency : 12 , // Third host is fastest
299- errors : [ ] ,
300- maximumLatency : 15 ,
301- minimumLatency : 10 ,
302- options
303- } ;
247+ mock . module ( 'ping' , ( ) => ( {
248+ default : {
249+ promise : {
250+ probe : async ( host : string ) => {
251+ if ( host === "192.168.0.191" ) {
252+ return {
253+ alive : true ,
254+ time : 45 // First host is medium
255+ } ;
256+ } else if ( host === "192.168.0.192" ) {
257+ return {
258+ alive : true ,
259+ time : 60 // Second host is slowest
260+ } ;
261+ } else if ( host === "192.168.0.193" ) {
262+ return {
263+ alive : true ,
264+ time : 12 // Third host is fastest
265+ } ;
266+ }
267+ return { alive : true , time : 100 } ;
268+ }
304269 }
305- return { averageLatency : 100 , errors : [ ] , maximumLatency : 100 , minimumLatency : 100 , options } ;
306270 }
307271 } ) ) ;
308272
0 commit comments