X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Futils%2FErrorUtils.test.ts;h=edf088cf8cf8785605c570c5728616063601ef5d;hb=ffccbf2287d74bfde06179427a1eabad986d1235;hp=fd932b41ab5ae31e1011e9efa5e6d8f593f97a7c;hpb=d05b53c7a03b8fad2e106caee68d5871cc6aac6e;p=e-mobility-charging-stations-simulator.git diff --git a/tests/utils/ErrorUtils.test.ts b/tests/utils/ErrorUtils.test.ts index fd932b41..edf088cf 100644 --- a/tests/utils/ErrorUtils.test.ts +++ b/tests/utils/ErrorUtils.test.ts @@ -1,10 +1,112 @@ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { describe, it } from 'node:test' import { expect } from 'expect' -import { setDefaultErrorParams } from '../../src/utils/ErrorUtils.js' +import type { ChargingStation } from '../../src/charging-station/index.js' +import { + FileType, + GenericStatus, + IncomingRequestCommand, + MessageType, + RequestCommand +} from '../../src/types/index.js' +import { + handleFileException, + handleIncomingRequestError, + handleSendMessageError, + setDefaultErrorParams +} from '../../src/utils/ErrorUtils.js' +import { logger } from '../../src/utils/Logger.js' await describe('ErrorUtils test suite', async () => { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + const chargingStation = { + logPrefix: () => 'CS-TEST |' + } as ChargingStation + + await it('Verify handleFileException()', t => { + t.mock.method(console, 'warn') + t.mock.method(console, 'error') + t.mock.method(logger, 'warn') + t.mock.method(logger, 'error') + const error = new Error() + error.code = 'ENOENT' + expect(() => { + handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {}) + }).toThrow(error) + expect(() => { + handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { + throwError: false + }) + }).not.toThrow() + expect(logger.warn.mock.calls.length).toBe(1) + expect(logger.error.mock.calls.length).toBe(1) + expect(() => { + handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { + consoleOut: true + }) + }).toThrow(error) + expect(() => { + handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { + throwError: false, + consoleOut: true + }) + }).not.toThrow() + expect(console.warn.mock.calls.length).toBe(1) + expect(console.error.mock.calls.length).toBe(1) + }) + + await it('Verify handleSendMessageError()', t => { + t.mock.method(logger, 'error') + t.mock.method(chargingStation, 'logPrefix') + const error = new Error() + expect(() => { + handleSendMessageError( + chargingStation, + RequestCommand.BOOT_NOTIFICATION, + MessageType.CALL_MESSAGE, + error + ) + }).not.toThrow() + expect(() => { + handleSendMessageError( + chargingStation, + RequestCommand.BOOT_NOTIFICATION, + MessageType.CALL_MESSAGE, + error, + { throwError: true } + ) + }).toThrow(error) + expect(chargingStation.logPrefix.mock.calls.length).toBe(2) + expect(logger.error.mock.calls.length).toBe(2) + }) + + await it('Verify handleIncomingRequestError()', t => { + t.mock.method(logger, 'error') + t.mock.method(chargingStation, 'logPrefix') + const error = new Error() + expect(() => { + handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error) + }).toThrow(error) + expect(() => { + handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { + throwError: false + }) + }).not.toThrow() + const errorResponse = { + status: GenericStatus.Rejected + } + expect( + handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { + throwError: false, + errorResponse + }) + ).toStrictEqual(errorResponse) + expect(chargingStation.logPrefix.mock.calls.length).toBe(3) + expect(logger.error.mock.calls.length).toBe(3) + }) + await it('Verify setDefaultErrorParams()', () => { expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false }) expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({