1 import { describe
, it
} from
'node:test'
3 import { hoursToMilliseconds
, hoursToSeconds
} from
'date-fns'
4 import { expect
} from
'expect'
6 import { Constants
} from
'../../src/utils/Constants.js'
13 extractTimeSeriesValues
,
14 formatDurationMilliSeconds
,
15 formatDurationSeconds
,
35 } from
'../../src/utils/Utils.js'
37 await describe('Utils test suite', async () => {
38 await it('Verify generateUUID()/validateUUID()', () => {
39 const uuid
= generateUUID()
40 expect(uuid
).toBeDefined()
41 expect(uuid
.length
).toEqual(36)
42 expect(validateUUID(uuid
)).toBe(true)
43 expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true)
44 expect(validateUUID('')).toBe(false)
45 // Shall invalidate Nil UUID
46 expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false)
47 expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false)
50 await it('Verify sleep()', async () => {
51 const start
= performance
.now()
53 const stop
= performance
.now()
54 expect(stop
- start
).toBeGreaterThanOrEqual(1000)
57 await it('Verify formatDurationMilliSeconds()', () => {
58 expect(formatDurationMilliSeconds(0)).toBe('0 seconds')
59 expect(formatDurationMilliSeconds(900)).toBe('0 seconds')
60 expect(formatDurationMilliSeconds(1000)).toBe('1 second')
61 expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours')
64 await it('Verify formatDurationSeconds()', () => {
65 expect(formatDurationSeconds(0)).toBe('0 seconds')
66 expect(formatDurationSeconds(0.9)).toBe('0 seconds')
67 expect(formatDurationSeconds(1)).toBe('1 second')
68 expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours')
71 await it('Verify isValidTime()', () => {
72 expect(isValidDate(undefined)).toBe(false)
73 expect(isValidDate(-1)).toBe(true)
74 expect(isValidDate(0)).toBe(true)
75 expect(isValidDate(1)).toBe(true)
76 expect(isValidDate(-0.5)).toBe(true)
77 expect(isValidDate(0.5)).toBe(true)
78 expect(isValidDate(new Date())).toBe(true)
81 await it('Verify convertToDate()', () => {
82 expect(convertToDate(undefined)).toBe(undefined)
83 expect(convertToDate(null)).toBe(null)
84 expect(() => convertToDate('')).toThrow(new Error("Cannot convert to date: ''"))
85 expect(() => convertToDate('00:70:61')).toThrow(new Error("Cannot convert to date: '00:70:61'"))
86 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'))
87 expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z'))
88 const dateStr
= '2020-01-01T00:00:00.000Z'
89 let date
= convertToDate(dateStr
)
90 expect(date
).toBeInstanceOf(Date)
91 expect(date
).toStrictEqual(new Date(dateStr
))
92 date
= convertToDate(new Date(dateStr
))
93 expect(date
).toBeInstanceOf(Date)
94 expect(date
).toStrictEqual(new Date(dateStr
))
97 await it('Verify convertToInt()', () => {
98 expect(convertToInt(undefined)).toBe(0)
99 expect(convertToInt(null)).toBe(0)
100 expect(convertToInt(0)).toBe(0)
101 const randomInteger
= getRandomInteger()
102 expect(convertToInt(randomInteger
)).toEqual(randomInteger
)
103 expect(convertToInt('-1')).toBe(-1)
104 expect(convertToInt('1')).toBe(1)
105 expect(convertToInt('1.1')).toBe(1)
106 expect(convertToInt('1.9')).toBe(1)
107 expect(convertToInt('1.999')).toBe(1)
108 expect(convertToInt(-1)).toBe(-1)
109 expect(convertToInt(1)).toBe(1)
110 expect(convertToInt(1.1)).toBe(1)
111 expect(convertToInt(1.9)).toBe(1)
112 expect(convertToInt(1.999)).toBe(1)
115 }).toThrow("Cannot convert to integer: 'NaN'")
118 await it('Verify convertToFloat()', () => {
119 expect(convertToFloat(undefined)).toBe(0)
120 expect(convertToFloat(null)).toBe(0)
121 expect(convertToFloat(0)).toBe(0)
122 const randomFloat
= getRandomFloat()
123 expect(convertToFloat(randomFloat
)).toEqual(randomFloat
)
124 expect(convertToFloat('-1')).toBe(-1)
125 expect(convertToFloat('1')).toBe(1)
126 expect(convertToFloat('1.1')).toBe(1.1)
127 expect(convertToFloat('1.9')).toBe(1.9)
128 expect(convertToFloat('1.999')).toBe(1.999)
129 expect(convertToFloat(-1)).toBe(-1)
130 expect(convertToFloat(1)).toBe(1)
131 expect(convertToFloat(1.1)).toBe(1.1)
132 expect(convertToFloat(1.9)).toBe(1.9)
133 expect(convertToFloat(1.999)).toBe(1.999)
135 convertToFloat('NaN')
136 }).toThrow("Cannot convert to float: 'NaN'")
139 await it('Verify convertToBoolean()', () => {
140 expect(convertToBoolean(undefined)).toBe(false)
141 expect(convertToBoolean(null)).toBe(false)
142 expect(convertToBoolean('true')).toBe(true)
143 expect(convertToBoolean('false')).toBe(false)
144 expect(convertToBoolean('TRUE')).toBe(true)
145 expect(convertToBoolean('FALSE')).toBe(false)
146 expect(convertToBoolean('1')).toBe(true)
147 expect(convertToBoolean('0')).toBe(false)
148 expect(convertToBoolean(1)).toBe(true)
149 expect(convertToBoolean(0)).toBe(false)
150 expect(convertToBoolean(true)).toBe(true)
151 expect(convertToBoolean(false)).toBe(false)
152 expect(convertToBoolean('')).toBe(false)
153 expect(convertToBoolean('NoNBoolean')).toBe(false)
156 await it('Verify secureRandom()', () => {
157 const random
= secureRandom()
158 expect(typeof random
=== 'number').toBe(true)
159 expect(random
).toBeGreaterThanOrEqual(0)
160 expect(random
).toBeLessThan(1)
163 await it('Verify getRandomInteger()', () => {
164 let randomInteger
= getRandomInteger()
165 expect(Number.isSafeInteger(randomInteger
)).toBe(true)
166 expect(randomInteger
).toBeGreaterThanOrEqual(0)
167 expect(randomInteger
).toBeLessThanOrEqual(Constants
.MAX_RANDOM_INTEGER
)
168 expect(randomInteger
).not
.toEqual(getRandomInteger())
169 randomInteger
= getRandomInteger(0, -Constants
.MAX_RANDOM_INTEGER
)
170 expect(randomInteger
).toBeGreaterThanOrEqual(-Constants
.MAX_RANDOM_INTEGER
)
171 expect(randomInteger
).toBeLessThanOrEqual(0)
172 expect(() => getRandomInteger(0, 1)).toThrow(
173 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1'
175 expect(() => getRandomInteger(-1)).toThrow(
176 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0'
178 expect(() => getRandomInteger(Constants
.MAX_RANDOM_INTEGER
+ 1)).toThrow(
179 `The value of "max" is out of range. It must be <= ${
180 Constants.MAX_RANDOM_INTEGER + 1
181 }. Received 281_474_976_710_656`
183 randomInteger
= getRandomInteger(2, 1)
184 expect(randomInteger
).toBeGreaterThanOrEqual(1)
185 expect(randomInteger
).toBeLessThanOrEqual(2)
188 randomInteger
= getRandomInteger(maximum
, minimum
)
189 expect(randomInteger
).toBeLessThanOrEqual(Math.floor(maximum
))
190 expect(randomInteger
).toBeGreaterThanOrEqual(Math.ceil(minimum
))
193 await it('Verify roundTo()', () => {
194 expect(roundTo(0, 2)).toBe(0)
195 expect(roundTo(0.5, 0)).toBe(1)
196 expect(roundTo(0.5, 2)).toBe(0.5)
197 expect(roundTo(-0.5, 0)).toBe(-1)
198 expect(roundTo(-0.5, 2)).toBe(-0.5)
199 expect(roundTo(1.005, 0)).toBe(1)
200 expect(roundTo(1.005, 2)).toBe(1.01)
201 expect(roundTo(2.175, 2)).toBe(2.18)
202 expect(roundTo(5.015, 2)).toBe(5.02)
203 expect(roundTo(-1.005, 2)).toBe(-1.01)
204 expect(roundTo(-2.175, 2)).toBe(-2.18)
205 expect(roundTo(-5.015, 2)).toBe(-5.02)
208 await it('Verify getRandomFloat()', () => {
209 let randomFloat
= getRandomFloat()
210 expect(typeof randomFloat
=== 'number').toBe(true)
211 expect(randomFloat
).toBeGreaterThanOrEqual(0)
212 expect(randomFloat
).toBeLessThanOrEqual(Number.MAX_VALUE
)
213 expect(randomFloat
).not
.toEqual(getRandomFloat())
214 expect(() => getRandomFloat(0, 1)).toThrow(new RangeError('Invalid interval'))
215 expect(() => getRandomFloat(Number.MAX_VALUE
, -Number.MAX_VALUE
)).toThrow(
216 new RangeError('Invalid interval')
218 randomFloat
= getRandomFloat(0, -Number.MAX_VALUE
)
219 expect(randomFloat
).toBeGreaterThanOrEqual(-Number.MAX_VALUE
)
220 expect(randomFloat
).toBeLessThanOrEqual(0)
223 await it('Verify extractTimeSeriesValues()', () => {
224 expect(extractTimeSeriesValues([])).toEqual([])
225 expect(extractTimeSeriesValues([{ timestamp
: Date.now(), value
: 1.1 }])).toEqual([1.1])
227 extractTimeSeriesValues([
228 { timestamp
: Date.now(), value
: 1.1 },
229 { timestamp
: Date.now(), value
: 2.2 }
231 ).toEqual([1.1, 2.2])
234 await it('Verify isObject()', () => {
235 expect(isObject('test')).toBe(false)
236 expect(isObject(undefined)).toBe(false)
237 expect(isObject(null)).toBe(false)
238 expect(isObject(0)).toBe(false)
239 expect(isObject([])).toBe(false)
240 expect(isObject([0, 1])).toBe(false)
241 expect(isObject(['0', '1'])).toBe(false)
242 expect(isObject({})).toBe(true)
243 expect(isObject({ 1: 1 })).toBe(true)
244 expect(isObject({ 1: '1' })).toBe(true)
245 expect(isObject(new Map())).toBe(true)
246 expect(isObject(new Set())).toBe(true)
247 expect(isObject(new WeakMap())).toBe(true)
248 expect(isObject(new WeakSet())).toBe(true)
251 await it('Verify cloneObject()', () => {
253 expect(cloneObject(obj
)).toStrictEqual(obj
)
254 expect(cloneObject(obj
) === obj
).toBe(false)
255 const nestedObj
= { 1: obj
, 2: obj
}
256 expect(cloneObject(nestedObj
)).toStrictEqual(nestedObj
)
257 expect(cloneObject(nestedObj
) === nestedObj
).toBe(false)
259 expect(cloneObject(array
)).toStrictEqual(array
)
260 expect(cloneObject(array
) === array
).toBe(false)
261 const objArray
= [obj
, obj
]
262 expect(cloneObject(objArray
)).toStrictEqual(objArray
)
263 expect(cloneObject(objArray
) === objArray
).toBe(false)
264 const date
= new Date()
265 expect(cloneObject(date
)).toStrictEqual(date
)
266 expect(cloneObject(date
) === date
).toBe(false)
267 const map
= new Map([['1', '2']])
268 expect(cloneObject(map
)).toStrictEqual({})
269 const set
= new Set(['1'])
270 expect(cloneObject(set
)).toStrictEqual({})
271 // The URL object seems to have not enumerable properties
272 const url
= new URL('https://domain.tld')
273 expect(cloneObject(url
)).toStrictEqual({})
274 const weakMap
= new WeakMap([[{ 1: 1 }, { 2: 2 }]])
275 expect(cloneObject(weakMap
)).toStrictEqual({})
276 const weakSet
= new WeakSet([{ 1: 1 }, { 2: 2 }])
277 expect(cloneObject(weakSet
)).toStrictEqual({})
280 await it('Verify hasOwnProp()', () => {
281 expect(hasOwnProp('test', '')).toBe(false)
282 expect(hasOwnProp(undefined, '')).toBe(false)
283 expect(hasOwnProp(null, '')).toBe(false)
284 expect(hasOwnProp([], '')).toBe(false)
285 expect(hasOwnProp({}, '')).toBe(false)
286 expect(hasOwnProp({ 1: 1 }, 1)).toBe(true)
287 expect(hasOwnProp({ 1: 1 }, '1')).toBe(true)
288 expect(hasOwnProp({ 1: 1 }, 2)).toBe(false)
289 expect(hasOwnProp({ 1: 1 }, '2')).toBe(false)
290 expect(hasOwnProp({ 1: '1' }, '1')).toBe(true)
291 expect(hasOwnProp({ 1: '1' }, 1)).toBe(true)
292 expect(hasOwnProp({ 1: '1' }, '2')).toBe(false)
293 expect(hasOwnProp({ 1: '1' }, 2)).toBe(false)
296 await it('Verify isEmptyString()', () => {
297 expect(isEmptyString('')).toBe(true)
298 expect(isEmptyString(' ')).toBe(true)
299 expect(isEmptyString(' ')).toBe(true)
300 expect(isEmptyString('test')).toBe(false)
301 expect(isEmptyString(' test')).toBe(false)
302 expect(isEmptyString('test ')).toBe(false)
303 expect(isEmptyString(undefined)).toBe(true)
304 expect(isEmptyString(null)).toBe(true)
305 expect(isEmptyString(0)).toBe(false)
306 expect(isEmptyString({})).toBe(false)
307 expect(isEmptyString([])).toBe(false)
308 expect(isEmptyString(new Map())).toBe(false)
309 expect(isEmptyString(new Set())).toBe(false)
310 expect(isEmptyString(new WeakMap())).toBe(false)
311 expect(isEmptyString(new WeakSet())).toBe(false)
314 await it('Verify isNotEmptyString()', () => {
315 expect(isNotEmptyString('')).toBe(false)
316 expect(isNotEmptyString(' ')).toBe(false)
317 expect(isNotEmptyString(' ')).toBe(false)
318 expect(isNotEmptyString('test')).toBe(true)
319 expect(isNotEmptyString(' test')).toBe(true)
320 expect(isNotEmptyString('test ')).toBe(true)
321 expect(isNotEmptyString(undefined)).toBe(false)
322 expect(isNotEmptyString(null)).toBe(false)
323 expect(isNotEmptyString(0)).toBe(false)
324 expect(isNotEmptyString({})).toBe(false)
325 expect(isNotEmptyString([])).toBe(false)
326 expect(isNotEmptyString(new Map())).toBe(false)
327 expect(isNotEmptyString(new Set())).toBe(false)
328 expect(isNotEmptyString(new WeakMap())).toBe(false)
329 expect(isNotEmptyString(new WeakSet())).toBe(false)
332 await it('Verify isEmptyArray()', () => {
333 expect(isEmptyArray([])).toBe(true)
334 expect(isEmptyArray([1, 2])).toBe(false)
335 expect(isEmptyArray(['1', '2'])).toBe(false)
336 expect(isEmptyArray(undefined)).toBe(false)
337 expect(isEmptyArray(null)).toBe(false)
338 expect(isEmptyArray('')).toBe(false)
339 expect(isEmptyArray('test')).toBe(false)
340 expect(isEmptyArray(0)).toBe(false)
341 expect(isEmptyArray({})).toBe(false)
342 expect(isEmptyArray(new Map())).toBe(false)
343 expect(isEmptyArray(new Set())).toBe(false)
344 expect(isEmptyArray(new WeakMap())).toBe(false)
345 expect(isEmptyArray(new WeakSet())).toBe(false)
348 await it('Verify isNotEmptyArray()', () => {
349 expect(isNotEmptyArray([])).toBe(false)
350 expect(isNotEmptyArray([1, 2])).toBe(true)
351 expect(isNotEmptyArray(['1', '2'])).toBe(true)
352 expect(isNotEmptyArray(undefined)).toBe(false)
353 expect(isNotEmptyArray(null)).toBe(false)
354 expect(isNotEmptyArray('')).toBe(false)
355 expect(isNotEmptyArray('test')).toBe(false)
356 expect(isNotEmptyArray(0)).toBe(false)
357 expect(isNotEmptyArray({})).toBe(false)
358 expect(isNotEmptyArray(new Map())).toBe(false)
359 expect(isNotEmptyArray(new Set())).toBe(false)
360 expect(isNotEmptyArray(new WeakMap())).toBe(false)
361 expect(isNotEmptyArray(new WeakSet())).toBe(false)
364 await it('Verify isEmptyObject()', () => {
365 expect(isEmptyObject({})).toBe(true)
366 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false)
367 expect(isEmptyObject(new Map())).toBe(false)
368 expect(isEmptyObject(new Set())).toBe(false)
369 expect(isEmptyObject(new WeakMap())).toBe(false)
370 expect(isEmptyObject(new WeakSet())).toBe(false)
373 await it('Verify isArraySorted()', () => {
375 isArraySorted([], (a
, b
) => {
380 isArraySorted([1], (a
, b
) => {
384 expect(isArraySorted
<number>([1, 2, 3, 4, 5], (a
, b
) => a
- b
)).toBe(true)
385 expect(isArraySorted
<number>([1, 2, 3, 5, 4], (a
, b
) => a
- b
)).toBe(false)
386 expect(isArraySorted
<number>([2, 1, 3, 4, 5], (a
, b
) => a
- b
)).toBe(false)
389 await it('Verify once()', () => {
391 const fn
= (): number => ++called
392 const onceFn
= once(fn
, this)
393 const result1
= onceFn()
394 expect(called
).toBe(1)
395 expect(result1
).toBe(1)
396 const result2
= onceFn()
397 expect(called
).toBe(1)
398 expect(result2
).toBe(1)
399 const result3
= onceFn()
400 expect(called
).toBe(1)
401 expect(result3
).toBe(1)
404 await it('Verify min()', () => {
405 expect(min()).toBe(Infinity)
406 expect(min(0, 1)).toBe(0)
407 expect(min(1, 0)).toBe(0)
408 expect(min(0, -1)).toBe(-1)
409 expect(min(-1, 0)).toBe(-1)
412 await it('Verify max()', () => {
413 expect(max()).toBe(-Infinity)
414 expect(max(0, 1)).toBe(1)
415 expect(max(1, 0)).toBe(1)
416 expect(max(0, -1)).toBe(0)
417 expect(max(-1, 0)).toBe(0)