refactor: cleanup default params in error handlers
[e-mobility-charging-stations-simulator.git] / tests / utils / ErrorUtils.test.ts
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 } from '../../src/utils/ErrorUtils.js'
19 import { logger } from '../../src/utils/Logger.js'
20
21 await describe('ErrorUtils test suite', async () => {
22 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
23 const chargingStation = {
24 logPrefix: () => 'CS-TEST |'
25 } as ChargingStation
26
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')
32 const error = new Error()
33 error.code = 'ENOENT'
34 expect(() => {
35 handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {})
36 }).toThrow(error)
37 expect(() => {
38 handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
39 throwError: false
40 })
41 }).not.toThrow()
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)
57 })
58
59 await it('Verify handleSendMessageError()', t => {
60 t.mock.method(logger, 'error')
61 t.mock.method(chargingStation, 'logPrefix')
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)
80 expect(chargingStation.logPrefix.mock.calls.length).toBe(2)
81 expect(logger.error.mock.calls.length).toBe(2)
82 })
83
84 await it('Verify handleIncomingRequestError()', t => {
85 t.mock.method(logger, 'error')
86 t.mock.method(chargingStation, 'logPrefix')
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)
105 expect(chargingStation.logPrefix.mock.calls.length).toBe(3)
106 expect(logger.error.mock.calls.length).toBe(3)
107 })
108 })