build: switch to NodeNext module resolution
[e-mobility-charging-stations-simulator.git] / tests / utils / Utils.test.ts
1 import { describe, it } from 'node:test';
2
3 import { hoursToMilliseconds, hoursToSeconds } from 'date-fns';
4 import { expect } from 'expect';
5
6 import { Constants } from '../../src/utils/Constants.js';
7 import {
8 cloneObject,
9 convertToBoolean,
10 convertToDate,
11 convertToFloat,
12 convertToInt,
13 extractTimeSeriesValues,
14 formatDurationMilliSeconds,
15 formatDurationSeconds,
16 generateUUID,
17 getRandomFloat,
18 getRandomInteger,
19 hasOwnProp,
20 isArraySorted,
21 isEmptyArray,
22 isEmptyObject,
23 isEmptyString,
24 isIterable,
25 isNotEmptyArray,
26 isNotEmptyString,
27 isNullOrUndefined,
28 isObject,
29 isUndefined,
30 isValidTime,
31 max,
32 min,
33 once,
34 roundTo,
35 secureRandom,
36 sleep,
37 validateUUID,
38 } from '../../src/utils/Utils.js';
39
40 await describe('Utils test suite', async () => {
41 await it('Verify generateUUID()/validateUUID()', () => {
42 const uuid = generateUUID();
43 expect(uuid).toBeDefined();
44 expect(uuid.length).toEqual(36);
45 expect(validateUUID(uuid)).toBe(true);
46 expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true);
47 expect(validateUUID('')).toBe(false);
48 // Shall invalidate Nil UUID
49 expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false);
50 expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false);
51 });
52
53 await it('Verify sleep()', async () => {
54 const start = performance.now();
55 await sleep(1000);
56 const stop = performance.now();
57 expect(stop - start).toBeGreaterThanOrEqual(1000);
58 });
59
60 await it('Verify formatDurationMilliSeconds()', () => {
61 expect(formatDurationMilliSeconds(0)).toBe('0 seconds');
62 expect(formatDurationMilliSeconds(900)).toBe('0 seconds');
63 expect(formatDurationMilliSeconds(1000)).toBe('1 second');
64 expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours');
65 });
66
67 await it('Verify formatDurationSeconds()', () => {
68 expect(formatDurationSeconds(0)).toBe('0 seconds');
69 expect(formatDurationSeconds(0.9)).toBe('0 seconds');
70 expect(formatDurationSeconds(1)).toBe('1 second');
71 expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours');
72 });
73
74 await it('Verify isValidTime()', () => {
75 expect(isValidTime(undefined)).toBe(false);
76 expect(isValidTime(null)).toBe(false);
77 expect(isValidTime('')).toBe(false);
78 expect(isValidTime({})).toBe(false);
79 expect(isValidTime([])).toBe(false);
80 expect(isValidTime(new Map())).toBe(false);
81 expect(isValidTime(new Set())).toBe(false);
82 expect(isValidTime(new WeakMap())).toBe(false);
83 expect(isValidTime(new WeakSet())).toBe(false);
84 expect(isValidTime(-1)).toBe(true);
85 expect(isValidTime(0)).toBe(true);
86 expect(isValidTime(1)).toBe(true);
87 expect(isValidTime(-0.5)).toBe(true);
88 expect(isValidTime(0.5)).toBe(true);
89 expect(isValidTime(new Date())).toBe(true);
90 });
91
92 await it('Verify convertToDate()', () => {
93 expect(convertToDate(undefined)).toBe(undefined);
94 expect(convertToDate(null)).toBe(null);
95 expect(() => convertToDate('')).toThrow(new Error("Cannot convert to date: ''"));
96 expect(() => convertToDate('00:70:61')).toThrow(
97 new Error("Cannot convert to date: '00:70:61'"),
98 );
99 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
100 expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z'));
101 const dateStr = '2020-01-01T00:00:00.000Z';
102 let date = convertToDate(dateStr);
103 expect(date).toBeInstanceOf(Date);
104 expect(date).toStrictEqual(new Date(dateStr));
105 date = convertToDate(new Date(dateStr));
106 expect(date).toBeInstanceOf(Date);
107 expect(date).toStrictEqual(new Date(dateStr));
108 });
109
110 await it('Verify convertToInt()', () => {
111 expect(convertToInt(undefined)).toBe(0);
112 expect(convertToInt(null)).toBe(0);
113 expect(convertToInt(0)).toBe(0);
114 const randomInteger = getRandomInteger();
115 expect(convertToInt(randomInteger)).toEqual(randomInteger);
116 expect(convertToInt('-1')).toBe(-1);
117 expect(convertToInt('1')).toBe(1);
118 expect(convertToInt('1.1')).toBe(1);
119 expect(convertToInt('1.9')).toBe(1);
120 expect(convertToInt('1.999')).toBe(1);
121 expect(convertToInt(-1)).toBe(-1);
122 expect(convertToInt(1)).toBe(1);
123 expect(convertToInt(1.1)).toBe(1);
124 expect(convertToInt(1.9)).toBe(1);
125 expect(convertToInt(1.999)).toBe(1);
126 expect(() => {
127 convertToInt('NaN');
128 }).toThrow("Cannot convert to integer: 'NaN'");
129 });
130
131 await it('Verify convertToFloat()', () => {
132 expect(convertToFloat(undefined)).toBe(0);
133 expect(convertToFloat(null)).toBe(0);
134 expect(convertToFloat(0)).toBe(0);
135 const randomFloat = getRandomFloat();
136 expect(convertToFloat(randomFloat)).toEqual(randomFloat);
137 expect(convertToFloat('-1')).toBe(-1);
138 expect(convertToFloat('1')).toBe(1);
139 expect(convertToFloat('1.1')).toBe(1.1);
140 expect(convertToFloat('1.9')).toBe(1.9);
141 expect(convertToFloat('1.999')).toBe(1.999);
142 expect(convertToFloat(-1)).toBe(-1);
143 expect(convertToFloat(1)).toBe(1);
144 expect(convertToFloat(1.1)).toBe(1.1);
145 expect(convertToFloat(1.9)).toBe(1.9);
146 expect(convertToFloat(1.999)).toBe(1.999);
147 expect(() => {
148 convertToFloat('NaN');
149 }).toThrow("Cannot convert to float: 'NaN'");
150 });
151
152 await it('Verify convertToBoolean()', () => {
153 expect(convertToBoolean(undefined)).toBe(false);
154 expect(convertToBoolean(null)).toBe(false);
155 expect(convertToBoolean('true')).toBe(true);
156 expect(convertToBoolean('false')).toBe(false);
157 expect(convertToBoolean('TRUE')).toBe(true);
158 expect(convertToBoolean('FALSE')).toBe(false);
159 expect(convertToBoolean('1')).toBe(true);
160 expect(convertToBoolean('0')).toBe(false);
161 expect(convertToBoolean(1)).toBe(true);
162 expect(convertToBoolean(0)).toBe(false);
163 expect(convertToBoolean(true)).toBe(true);
164 expect(convertToBoolean(false)).toBe(false);
165 expect(convertToBoolean('')).toBe(false);
166 expect(convertToBoolean('NoNBoolean')).toBe(false);
167 });
168
169 await it('Verify secureRandom()', () => {
170 const random = secureRandom();
171 expect(typeof random === 'number').toBe(true);
172 expect(random).toBeGreaterThanOrEqual(0);
173 expect(random).toBeLessThan(1);
174 });
175
176 await it('Verify getRandomInteger()', () => {
177 let randomInteger = getRandomInteger();
178 expect(Number.isSafeInteger(randomInteger)).toBe(true);
179 expect(randomInteger).toBeGreaterThanOrEqual(0);
180 expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER);
181 expect(randomInteger).not.toEqual(getRandomInteger());
182 randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER);
183 expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER);
184 expect(randomInteger).toBeLessThanOrEqual(0);
185 expect(() => getRandomInteger(0, 1)).toThrow(
186 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1',
187 );
188 expect(() => getRandomInteger(-1)).toThrow(
189 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0',
190 );
191 expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrow(
192 `The value of "max" is out of range. It must be <= ${
193 Constants.MAX_RANDOM_INTEGER + 1
194 }. Received 281_474_976_710_656`,
195 );
196 randomInteger = getRandomInteger(2, 1);
197 expect(randomInteger).toBeGreaterThanOrEqual(1);
198 expect(randomInteger).toBeLessThanOrEqual(2);
199 const maximum = 2.2,
200 minimum = 1.1;
201 randomInteger = getRandomInteger(maximum, minimum);
202 expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum));
203 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum));
204 });
205
206 await it('Verify roundTo()', () => {
207 expect(roundTo(0, 2)).toBe(0);
208 expect(roundTo(0.5, 0)).toBe(1);
209 expect(roundTo(0.5, 2)).toBe(0.5);
210 expect(roundTo(-0.5, 0)).toBe(-1);
211 expect(roundTo(-0.5, 2)).toBe(-0.5);
212 expect(roundTo(1.005, 0)).toBe(1);
213 expect(roundTo(1.005, 2)).toBe(1.01);
214 expect(roundTo(2.175, 2)).toBe(2.18);
215 expect(roundTo(5.015, 2)).toBe(5.02);
216 expect(roundTo(-1.005, 2)).toBe(-1.01);
217 expect(roundTo(-2.175, 2)).toBe(-2.18);
218 expect(roundTo(-5.015, 2)).toBe(-5.02);
219 });
220
221 await it('Verify getRandomFloat()', () => {
222 let randomFloat = getRandomFloat();
223 expect(typeof randomFloat === 'number').toBe(true);
224 expect(randomFloat).toBeGreaterThanOrEqual(0);
225 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
226 expect(randomFloat).not.toEqual(getRandomFloat());
227 expect(() => getRandomFloat(0, 1)).toThrow(new RangeError('Invalid interval'));
228 expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrow(
229 new RangeError('Invalid interval'),
230 );
231 randomFloat = getRandomFloat(0, -Number.MAX_VALUE);
232 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE);
233 expect(randomFloat).toBeLessThanOrEqual(0);
234 });
235
236 await it('Verify extractTimeSeriesValues()', () => {
237 expect(extractTimeSeriesValues([])).toEqual([]);
238 expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1]);
239 expect(
240 extractTimeSeriesValues([
241 { timestamp: Date.now(), value: 1.1 },
242 { timestamp: Date.now(), value: 2.2 },
243 ]),
244 ).toEqual([1.1, 2.2]);
245 });
246
247 await it('Verify isObject()', () => {
248 expect(isObject('test')).toBe(false);
249 expect(isObject(undefined)).toBe(false);
250 expect(isObject(null)).toBe(false);
251 expect(isObject(0)).toBe(false);
252 expect(isObject([])).toBe(false);
253 expect(isObject([0, 1])).toBe(false);
254 expect(isObject(['0', '1'])).toBe(false);
255 expect(isObject({})).toBe(true);
256 expect(isObject({ 1: 1 })).toBe(true);
257 expect(isObject({ '1': '1' })).toBe(true);
258 expect(isObject(new Map())).toBe(true);
259 expect(isObject(new Set())).toBe(true);
260 expect(isObject(new WeakMap())).toBe(true);
261 expect(isObject(new WeakSet())).toBe(true);
262 });
263
264 await it('Verify cloneObject()', () => {
265 const obj = { 1: 1 };
266 expect(cloneObject(obj)).toStrictEqual(obj);
267 expect(cloneObject(obj) === obj).toBe(false);
268 const nestedObj = { 1: obj, 2: obj };
269 expect(cloneObject(nestedObj)).toStrictEqual(nestedObj);
270 expect(cloneObject(nestedObj) === nestedObj).toBe(false);
271 const array = [1, 2];
272 expect(cloneObject(array)).toStrictEqual(array);
273 expect(cloneObject(array) === array).toBe(false);
274 const objArray = [obj, obj];
275 expect(cloneObject(objArray)).toStrictEqual(objArray);
276 expect(cloneObject(objArray) === objArray).toBe(false);
277 const date = new Date();
278 expect(cloneObject(date)).toStrictEqual(date);
279 expect(cloneObject(date) === date).toBe(false);
280 const map = new Map([['1', '2']]);
281 expect(cloneObject(map)).toStrictEqual({});
282 const set = new Set(['1']);
283 expect(cloneObject(set)).toStrictEqual({});
284 // The URL object seems to have not enumerable properties
285 const url = new URL('https://domain.tld');
286 expect(cloneObject(url)).toStrictEqual({});
287 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
288 expect(cloneObject(weakMap)).toStrictEqual({});
289 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
290 expect(cloneObject(weakSet)).toStrictEqual({});
291 });
292
293 await it('Verify hasOwnProp()', () => {
294 expect(hasOwnProp('test', '')).toBe(false);
295 expect(hasOwnProp(undefined, '')).toBe(false);
296 expect(hasOwnProp(null, '')).toBe(false);
297 expect(hasOwnProp([], '')).toBe(false);
298 expect(hasOwnProp({}, '')).toBe(false);
299 expect(hasOwnProp({ 1: 1 }, 1)).toBe(true);
300 expect(hasOwnProp({ 1: 1 }, '1')).toBe(true);
301 expect(hasOwnProp({ 1: 1 }, 2)).toBe(false);
302 expect(hasOwnProp({ 1: 1 }, '2')).toBe(false);
303 expect(hasOwnProp({ '1': '1' }, '1')).toBe(true);
304 expect(hasOwnProp({ '1': '1' }, 1)).toBe(true);
305 expect(hasOwnProp({ '1': '1' }, '2')).toBe(false);
306 expect(hasOwnProp({ '1': '1' }, 2)).toBe(false);
307 });
308
309 await it('Verify isIterable()', () => {
310 expect(isIterable('')).toBe(true);
311 expect(isIterable(' ')).toBe(true);
312 expect(isIterable('test')).toBe(true);
313 expect(isIterable(undefined)).toBe(false);
314 expect(isIterable(null)).toBe(false);
315 expect(isIterable(0)).toBe(false);
316 expect(isIterable([0, 1])).toBe(true);
317 expect(isIterable({ 1: 1 })).toBe(false);
318 expect(isIterable(new Map())).toBe(true);
319 expect(isIterable(new Set())).toBe(true);
320 expect(isIterable(new WeakMap())).toBe(false);
321 expect(isIterable(new WeakSet())).toBe(false);
322 });
323
324 await it('Verify isEmptyString()', () => {
325 expect(isEmptyString('')).toBe(true);
326 expect(isEmptyString(' ')).toBe(true);
327 expect(isEmptyString(' ')).toBe(true);
328 expect(isEmptyString('test')).toBe(false);
329 expect(isEmptyString(' test')).toBe(false);
330 expect(isEmptyString('test ')).toBe(false);
331 expect(isEmptyString(undefined)).toBe(true);
332 expect(isEmptyString(null)).toBe(true);
333 expect(isEmptyString(0)).toBe(false);
334 expect(isEmptyString({})).toBe(false);
335 expect(isEmptyString([])).toBe(false);
336 expect(isEmptyString(new Map())).toBe(false);
337 expect(isEmptyString(new Set())).toBe(false);
338 expect(isEmptyString(new WeakMap())).toBe(false);
339 expect(isEmptyString(new WeakSet())).toBe(false);
340 });
341
342 await it('Verify isNotEmptyString()', () => {
343 expect(isNotEmptyString('')).toBe(false);
344 expect(isNotEmptyString(' ')).toBe(false);
345 expect(isNotEmptyString(' ')).toBe(false);
346 expect(isNotEmptyString('test')).toBe(true);
347 expect(isNotEmptyString(' test')).toBe(true);
348 expect(isNotEmptyString('test ')).toBe(true);
349 expect(isNotEmptyString(undefined)).toBe(false);
350 expect(isNotEmptyString(null)).toBe(false);
351 expect(isNotEmptyString(0)).toBe(false);
352 expect(isNotEmptyString({})).toBe(false);
353 expect(isNotEmptyString([])).toBe(false);
354 expect(isNotEmptyString(new Map())).toBe(false);
355 expect(isNotEmptyString(new Set())).toBe(false);
356 expect(isNotEmptyString(new WeakMap())).toBe(false);
357 expect(isNotEmptyString(new WeakSet())).toBe(false);
358 });
359
360 await it('Verify isUndefined()', () => {
361 expect(isUndefined(undefined)).toBe(true);
362 expect(isUndefined(null)).toBe(false);
363 expect(isUndefined('')).toBe(false);
364 expect(isUndefined(0)).toBe(false);
365 expect(isUndefined({})).toBe(false);
366 expect(isUndefined([])).toBe(false);
367 expect(isUndefined(new Map())).toBe(false);
368 expect(isUndefined(new Set())).toBe(false);
369 expect(isUndefined(new WeakMap())).toBe(false);
370 expect(isUndefined(new WeakSet())).toBe(false);
371 });
372
373 await it('Verify isNullOrUndefined()', () => {
374 expect(isNullOrUndefined(undefined)).toBe(true);
375 expect(isNullOrUndefined(null)).toBe(true);
376 expect(isNullOrUndefined('')).toBe(false);
377 expect(isNullOrUndefined(0)).toBe(false);
378 expect(isNullOrUndefined({})).toBe(false);
379 expect(isNullOrUndefined([])).toBe(false);
380 expect(isNullOrUndefined(new Map())).toBe(false);
381 expect(isNullOrUndefined(new Set())).toBe(false);
382 expect(isNullOrUndefined(new WeakMap())).toBe(false);
383 expect(isNullOrUndefined(new WeakSet())).toBe(false);
384 });
385
386 await it('Verify isEmptyArray()', () => {
387 expect(isEmptyArray([])).toBe(true);
388 expect(isEmptyArray([1, 2])).toBe(false);
389 expect(isEmptyArray(['1', '2'])).toBe(false);
390 expect(isEmptyArray(undefined)).toBe(false);
391 expect(isEmptyArray(null)).toBe(false);
392 expect(isEmptyArray('')).toBe(false);
393 expect(isEmptyArray('test')).toBe(false);
394 expect(isEmptyArray(0)).toBe(false);
395 expect(isEmptyArray({})).toBe(false);
396 expect(isEmptyArray(new Map())).toBe(false);
397 expect(isEmptyArray(new Set())).toBe(false);
398 expect(isEmptyArray(new WeakMap())).toBe(false);
399 expect(isEmptyArray(new WeakSet())).toBe(false);
400 });
401
402 await it('Verify isNotEmptyArray()', () => {
403 expect(isNotEmptyArray([])).toBe(false);
404 expect(isNotEmptyArray([1, 2])).toBe(true);
405 expect(isNotEmptyArray(['1', '2'])).toBe(true);
406 expect(isNotEmptyArray(undefined)).toBe(false);
407 expect(isNotEmptyArray(null)).toBe(false);
408 expect(isNotEmptyArray('')).toBe(false);
409 expect(isNotEmptyArray('test')).toBe(false);
410 expect(isNotEmptyArray(0)).toBe(false);
411 expect(isNotEmptyArray({})).toBe(false);
412 expect(isNotEmptyArray(new Map())).toBe(false);
413 expect(isNotEmptyArray(new Set())).toBe(false);
414 expect(isNotEmptyArray(new WeakMap())).toBe(false);
415 expect(isNotEmptyArray(new WeakSet())).toBe(false);
416 });
417
418 await it('Verify isEmptyObject()', () => {
419 expect(isEmptyObject({})).toBe(true);
420 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
421 expect(isEmptyObject(new Map())).toBe(false);
422 expect(isEmptyObject(new Set())).toBe(false);
423 expect(isEmptyObject(new WeakMap())).toBe(false);
424 expect(isEmptyObject(new WeakSet())).toBe(false);
425 });
426
427 await it('Verify isArraySorted()', () => {
428 expect(
429 isArraySorted([], (a, b) => {
430 return a - b;
431 }),
432 ).toBe(true);
433 expect(
434 isArraySorted([1], (a, b) => {
435 return a - b;
436 }),
437 ).toBe(true);
438 expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
439 expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
440 expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false);
441 });
442
443 await it('Verify once()', () => {
444 let called = 0;
445 const fn = () => ++called;
446 const onceFn = once(fn, this);
447 const result1 = onceFn();
448 expect(called).toBe(1);
449 expect(result1).toBe(1);
450 const result2 = onceFn();
451 expect(called).toBe(1);
452 expect(result2).toBe(1);
453 const result3 = onceFn();
454 expect(called).toBe(1);
455 expect(result3).toBe(1);
456 });
457
458 await it('Verify min()', () => {
459 expect(min()).toBe(Infinity);
460 expect(min(0, 1)).toBe(0);
461 expect(min(1, 0)).toBe(0);
462 expect(min(0, -1)).toBe(-1);
463 expect(min(-1, 0)).toBe(-1);
464 });
465
466 await it('Verify max()', () => {
467 expect(max()).toBe(-Infinity);
468 expect(max(0, 1)).toBe(1);
469 expect(max(1, 0)).toBe(1);
470 expect(max(0, -1)).toBe(0);
471 expect(max(-1, 0)).toBe(0);
472 });
473 });