Commit | Line | Data |
---|---|---|
ff40d2cc | 1 | /* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
d05b53c7 JB |
2 | import { describe, it } from 'node:test' |
3 | ||
4 | import { expect } from 'expect' | |
5 | ||
2d4928a7 JB |
6 | import type { ChargingStation } from '../../src/charging-station/index.js' |
7 | import { | |
8 | FileType, | |
9 | GenericStatus, | |
10 | IncomingRequestCommand, | |
11 | MessageType, | |
12 | RequestCommand | |
13 | } from '../../src/types/index.js' | |
14 | import { | |
15 | handleFileException, | |
16 | handleIncomingRequestError, | |
64c14c99 | 17 | handleSendMessageError |
2d4928a7 | 18 | } from '../../src/utils/ErrorUtils.js' |
ff40d2cc | 19 | import { logger } from '../../src/utils/Logger.js' |
d05b53c7 JB |
20 | |
21 | await describe('ErrorUtils test suite', async () => { | |
2d4928a7 JB |
22 | // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
23 | const chargingStation = { | |
24 | logPrefix: () => 'CS-TEST |' | |
25 | } as ChargingStation | |
26 | ||
ff40d2cc JB |
27 | await it('Verify handleFileException()', t => { |
28 | t.mock.method(console, 'warn') | |
29 | t.mock.method(console, 'error') | |
30 | t.mock.method(logger, 'warn') | |
31 | t.mock.method(logger, 'error') | |
b49550e2 JB |
32 | const error = new Error() |
33 | error.code = 'ENOENT' | |
34 | expect(() => { | |
0acbf5e6 | 35 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {}) |
b49550e2 JB |
36 | }).toThrow(error) |
37 | expect(() => { | |
0acbf5e6 | 38 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { |
b49550e2 JB |
39 | throwError: false |
40 | }) | |
41 | }).not.toThrow() | |
ff40d2cc JB |
42 | expect(logger.warn.mock.calls.length).toBe(1) |
43 | expect(logger.error.mock.calls.length).toBe(1) | |
44 | expect(() => { | |
45 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { | |
46 | consoleOut: true | |
47 | }) | |
48 | }).toThrow(error) | |
49 | expect(() => { | |
50 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { | |
51 | throwError: false, | |
52 | consoleOut: true | |
53 | }) | |
54 | }).not.toThrow() | |
55 | expect(console.warn.mock.calls.length).toBe(1) | |
56 | expect(console.error.mock.calls.length).toBe(1) | |
b49550e2 JB |
57 | }) |
58 | ||
ff40d2cc JB |
59 | await it('Verify handleSendMessageError()', t => { |
60 | t.mock.method(logger, 'error') | |
61 | t.mock.method(chargingStation, 'logPrefix') | |
2d4928a7 JB |
62 | const error = new Error() |
63 | expect(() => { | |
64 | handleSendMessageError( | |
65 | chargingStation, | |
66 | RequestCommand.BOOT_NOTIFICATION, | |
67 | MessageType.CALL_MESSAGE, | |
68 | error | |
69 | ) | |
70 | }).not.toThrow() | |
71 | expect(() => { | |
72 | handleSendMessageError( | |
73 | chargingStation, | |
74 | RequestCommand.BOOT_NOTIFICATION, | |
75 | MessageType.CALL_MESSAGE, | |
76 | error, | |
77 | { throwError: true } | |
78 | ) | |
79 | }).toThrow(error) | |
ff40d2cc JB |
80 | expect(chargingStation.logPrefix.mock.calls.length).toBe(2) |
81 | expect(logger.error.mock.calls.length).toBe(2) | |
2d4928a7 JB |
82 | }) |
83 | ||
ff40d2cc JB |
84 | await it('Verify handleIncomingRequestError()', t => { |
85 | t.mock.method(logger, 'error') | |
86 | t.mock.method(chargingStation, 'logPrefix') | |
2d4928a7 JB |
87 | const error = new Error() |
88 | expect(() => { | |
89 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error) | |
90 | }).toThrow(error) | |
91 | expect(() => { | |
92 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { | |
93 | throwError: false | |
94 | }) | |
95 | }).not.toThrow() | |
96 | const errorResponse = { | |
97 | status: GenericStatus.Rejected | |
98 | } | |
99 | expect( | |
100 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { | |
101 | throwError: false, | |
102 | errorResponse | |
103 | }) | |
104 | ).toStrictEqual(errorResponse) | |
ff40d2cc JB |
105 | expect(chargingStation.logPrefix.mock.calls.length).toBe(3) |
106 | expect(logger.error.mock.calls.length).toBe(3) | |
2d4928a7 | 107 | }) |
d05b53c7 | 108 | }) |