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, | |
17 | handleSendMessageError, | |
18 | setDefaultErrorParams | |
19 | } from '../../src/utils/ErrorUtils.js' | |
ff40d2cc | 20 | import { logger } from '../../src/utils/Logger.js' |
d05b53c7 JB |
21 | |
22 | await describe('ErrorUtils test suite', async () => { | |
2d4928a7 JB |
23 | // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
24 | const chargingStation = { | |
25 | logPrefix: () => 'CS-TEST |' | |
26 | } as ChargingStation | |
27 | ||
ff40d2cc JB |
28 | await it('Verify handleFileException()', t => { |
29 | t.mock.method(console, 'warn') | |
30 | t.mock.method(console, 'error') | |
31 | t.mock.method(logger, 'warn') | |
32 | t.mock.method(logger, 'error') | |
b49550e2 JB |
33 | const error = new Error() |
34 | error.code = 'ENOENT' | |
35 | expect(() => { | |
0acbf5e6 | 36 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {}) |
b49550e2 JB |
37 | }).toThrow(error) |
38 | expect(() => { | |
0acbf5e6 | 39 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { |
b49550e2 JB |
40 | throwError: false |
41 | }) | |
42 | }).not.toThrow() | |
ff40d2cc JB |
43 | expect(logger.warn.mock.calls.length).toBe(1) |
44 | expect(logger.error.mock.calls.length).toBe(1) | |
45 | expect(() => { | |
46 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { | |
47 | consoleOut: true | |
48 | }) | |
49 | }).toThrow(error) | |
50 | expect(() => { | |
51 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { | |
52 | throwError: false, | |
53 | consoleOut: true | |
54 | }) | |
55 | }).not.toThrow() | |
56 | expect(console.warn.mock.calls.length).toBe(1) | |
57 | expect(console.error.mock.calls.length).toBe(1) | |
b49550e2 JB |
58 | }) |
59 | ||
ff40d2cc JB |
60 | await it('Verify handleSendMessageError()', t => { |
61 | t.mock.method(logger, 'error') | |
62 | t.mock.method(chargingStation, 'logPrefix') | |
2d4928a7 JB |
63 | const error = new Error() |
64 | expect(() => { | |
65 | handleSendMessageError( | |
66 | chargingStation, | |
67 | RequestCommand.BOOT_NOTIFICATION, | |
68 | MessageType.CALL_MESSAGE, | |
69 | error | |
70 | ) | |
71 | }).not.toThrow() | |
72 | expect(() => { | |
73 | handleSendMessageError( | |
74 | chargingStation, | |
75 | RequestCommand.BOOT_NOTIFICATION, | |
76 | MessageType.CALL_MESSAGE, | |
77 | error, | |
78 | { throwError: true } | |
79 | ) | |
80 | }).toThrow(error) | |
ff40d2cc JB |
81 | expect(chargingStation.logPrefix.mock.calls.length).toBe(2) |
82 | expect(logger.error.mock.calls.length).toBe(2) | |
2d4928a7 JB |
83 | }) |
84 | ||
ff40d2cc JB |
85 | await it('Verify handleIncomingRequestError()', t => { |
86 | t.mock.method(logger, 'error') | |
87 | t.mock.method(chargingStation, 'logPrefix') | |
2d4928a7 JB |
88 | const error = new Error() |
89 | expect(() => { | |
90 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error) | |
91 | }).toThrow(error) | |
92 | expect(() => { | |
93 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { | |
94 | throwError: false | |
95 | }) | |
96 | }).not.toThrow() | |
97 | const errorResponse = { | |
98 | status: GenericStatus.Rejected | |
99 | } | |
100 | expect( | |
101 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { | |
102 | throwError: false, | |
103 | errorResponse | |
104 | }) | |
105 | ).toStrictEqual(errorResponse) | |
ff40d2cc JB |
106 | expect(chargingStation.logPrefix.mock.calls.length).toBe(3) |
107 | expect(logger.error.mock.calls.length).toBe(3) | |
2d4928a7 JB |
108 | }) |
109 | ||
d05b53c7 JB |
110 | await it('Verify setDefaultErrorParams()', () => { |
111 | expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false }) | |
112 | expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({ | |
113 | throwError: false, | |
114 | consoleOut: false | |
115 | }) | |
116 | expect(setDefaultErrorParams({ throwError: false, consoleOut: true })).toStrictEqual({ | |
117 | throwError: false, | |
118 | consoleOut: true | |
119 | }) | |
120 | expect(setDefaultErrorParams({ throwError: true, consoleOut: true })).toStrictEqual({ | |
121 | throwError: true, | |
122 | consoleOut: true | |
123 | }) | |
124 | expect(setDefaultErrorParams({}, { throwError: false, consoleOut: false })).toStrictEqual({ | |
125 | throwError: false, | |
126 | consoleOut: false | |
127 | }) | |
128 | }) | |
129 | }) |