| 1 | /* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 2 | import { describe, it } from 'node:test' |
| 3 | |
| 4 | import { expect } from 'expect' |
| 5 | |
| 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' |
| 20 | import { logger } from '../../src/utils/Logger.js' |
| 21 | |
| 22 | await describe('ErrorUtils test suite', async () => { |
| 23 | // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 24 | const chargingStation = { |
| 25 | logPrefix: () => 'CS-TEST |' |
| 26 | } as ChargingStation |
| 27 | |
| 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') |
| 33 | const error = new Error() |
| 34 | error.code = 'ENOENT' |
| 35 | expect(() => { |
| 36 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {}) |
| 37 | }).toThrow(error) |
| 38 | expect(() => { |
| 39 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { |
| 40 | throwError: false |
| 41 | }) |
| 42 | }).not.toThrow() |
| 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) |
| 58 | }) |
| 59 | |
| 60 | await it('Verify handleSendMessageError()', t => { |
| 61 | t.mock.method(logger, 'error') |
| 62 | t.mock.method(chargingStation, 'logPrefix') |
| 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) |
| 81 | expect(chargingStation.logPrefix.mock.calls.length).toBe(2) |
| 82 | expect(logger.error.mock.calls.length).toBe(2) |
| 83 | }) |
| 84 | |
| 85 | await it('Verify handleIncomingRequestError()', t => { |
| 86 | t.mock.method(logger, 'error') |
| 87 | t.mock.method(chargingStation, 'logPrefix') |
| 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) |
| 106 | expect(chargingStation.logPrefix.mock.calls.length).toBe(3) |
| 107 | expect(logger.error.mock.calls.length).toBe(3) |
| 108 | }) |
| 109 | |
| 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 | }) |