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