11/* eslint-disable no-magic-numbers */
2- import type dgram from 'node:dgram' ;
3- import { assert , expect , describe , test , beforeEach , vi , afterEach } from 'vitest' ;
2+ import * as dgram from 'node:dgram' ;
3+ import { assert , expect , describe , it , beforeEach , vi , afterEach } from 'vitest' ;
44import { NTPClient } from './index.js' ;
55
66const SECOND_IN_MILLIS = 1000 ;
7- const replyTimeout = 10 * SECOND_IN_MILLIS ;
7+ const replyTimeout = 5 * SECOND_IN_MILLIS ;
88
99describe ( 'NTP' , ( ) => {
1010 beforeEach ( ( ) => {
11- vi . mock ( 'dgram' , async ( ) => {
12- const actual = ( await vi . importActual ( 'dgram' ) ) as typeof dgram ;
13- const socket = actual . createSocket ( 'udp4' ) ;
14- return {
15- ...actual ,
16- send : ( ) => {
17- socket . emit (
18- 'message' ,
19- Buffer . from ( [
20- 0x1c , 0x02 , 0x03 , 0xe8 , 0x00 , 0x00 , 0x02 , 0x1a , 0x00 , 0x00 , 0x05 , 0x12 , 0xc0 , 0x35 , 0x67 , 0x6c , 0xe9 ,
21- 0x03 , 0x76 , 0xff , 0xff , 0x97 , 0x0f , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xe9 , 0x03 ,
22- 0x78 , 0x0f , 0xe9 , 0xd6 , 0x4e , 0x10 , 0xe9 , 0x03 , 0x78 , 0x0f , 0xea , 0x03 , 0x93 , 0xf5 ,
23- ] )
24- ) ;
25- } ,
26- } ;
27- } ) ;
11+ vi . mock ( 'node:dgram' , async ( ) => ( {
12+ ...( await vi . importActual < typeof dgram > ( 'node:dgram' ) ) ,
13+ send : ( ) => {
14+ const socket = dgram . createSocket ( 'udp4' ) ;
15+ socket . emit (
16+ 'message' ,
17+ Buffer . from ( [
18+ 0x1c , 0x02 , 0x03 , 0xe8 , 0x00 , 0x00 , 0x02 , 0x1a , 0x00 , 0x00 , 0x05 , 0x12 , 0xc0 , 0x35 , 0x67 , 0x6c , 0xe9 , 0x03 ,
19+ 0x76 , 0xff , 0xff , 0x97 , 0x0f , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xe9 , 0x03 , 0x78 , 0x0f ,
20+ 0xe9 , 0xd6 , 0x4e , 0x10 , 0xe9 , 0x03 , 0x78 , 0x0f , 0xea , 0x03 , 0x93 , 0xf5 ,
21+ ] )
22+ ) ;
23+ } ,
24+ } ) ) ;
2825 } ) ;
26+
2927 afterEach ( ( ) => {
3028 vi . resetAllMocks ( ) ;
3129 } ) ;
3230
33- test (
31+ it (
3432 'returns the current time' ,
3533 async ( ) => {
36- const ntpClient = new NTPClient ( {
37- replyTimeout,
38- } ) ;
34+ const ntpClient = new NTPClient ( { replyTimeout} ) ;
3935 const data = await ntpClient . getNetworkTime ( ) ;
4036 expect ( data ) . toEqual ( expect . any ( Date ) ) ;
4137 } ,
4238 replyTimeout
4339 ) ;
4440
45- test (
41+ it (
4642 'works with another NTP server' ,
4743 async ( ) => {
4844 const ntpClient = new NTPClient ( {
@@ -55,7 +51,7 @@ describe('NTP', () => {
5551 replyTimeout
5652 ) ;
5753
58- test ( "doesn't work with an invalid NTP server" , async ( ) => {
54+ it ( "doesn't work with an invalid NTP server" , async ( ) => {
5955 try {
6056 const ntpClient = new NTPClient ( {
6157 replyTimeout : SECOND_IN_MILLIS ,
0 commit comments