chore(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / tests / utils / ErrorUtils.test.ts
CommitLineData
ff40d2cc 1/* eslint-disable @typescript-eslint/no-unsafe-member-access */
d05b53c7 2import { expect } from 'expect'
0749233f 3import { describe, it } from 'node:test'
d05b53c7 4
2d4928a7 5import type { ChargingStation } from '../../src/charging-station/index.js'
0749233f 6
2d4928a7
JB
7import {
8 FileType,
9 GenericStatus,
10 IncomingRequestCommand,
11 MessageType,
d1f5bfd8 12 RequestCommand,
2d4928a7
JB
13} from '../../src/types/index.js'
14import {
15 handleFileException,
16 handleIncomingRequestError,
d1f5bfd8 17 handleSendMessageError,
2d4928a7 18} from '../../src/utils/ErrorUtils.js'
ff40d2cc 19import { logger } from '../../src/utils/Logger.js'
d05b53c7
JB
20
21await describe('ErrorUtils test suite', async () => {
2d4928a7 22 const chargingStation = {
d1f5bfd8 23 logPrefix: () => 'CS-TEST |',
2d4928a7
JB
24 } as ChargingStation
25
ff40d2cc
JB
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')
b49550e2
JB
31 const error = new Error()
32 error.code = 'ENOENT'
33 expect(() => {
0acbf5e6 34 handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {})
b49550e2
JB
35 }).toThrow(error)
36 expect(() => {
0acbf5e6 37 handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
d1f5bfd8 38 throwError: false,
b49550e2
JB
39 })
40 }).not.toThrow()
ff40d2cc
JB
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 |', {
d1f5bfd8 45 consoleOut: true,
ff40d2cc
JB
46 })
47 }).toThrow(error)
48 expect(() => {
49 handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
d1f5bfd8 50 consoleOut: true,
0749233f 51 throwError: false,
ff40d2cc
JB
52 })
53 }).not.toThrow()
54 expect(console.warn.mock.calls.length).toBe(1)
55 expect(console.error.mock.calls.length).toBe(1)
b49550e2
JB
56 })
57
ff40d2cc
JB
58 await it('Verify handleSendMessageError()', t => {
59 t.mock.method(logger, 'error')
60 t.mock.method(chargingStation, 'logPrefix')
2d4928a7
JB
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)
ff40d2cc
JB
79 expect(chargingStation.logPrefix.mock.calls.length).toBe(2)
80 expect(logger.error.mock.calls.length).toBe(2)
2d4928a7
JB
81 })
82
ff40d2cc
JB
83 await it('Verify handleIncomingRequestError()', t => {
84 t.mock.method(logger, 'error')
85 t.mock.method(chargingStation, 'logPrefix')
2d4928a7
JB
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, {
d1f5bfd8 92 throwError: false,
2d4928a7
JB
93 })
94 }).not.toThrow()
95 const errorResponse = {
d1f5bfd8 96 status: GenericStatus.Rejected,
2d4928a7
JB
97 }
98 expect(
99 handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, {
d1f5bfd8 100 errorResponse,
0749233f 101 throwError: false,
2d4928a7
JB
102 })
103 ).toStrictEqual(errorResponse)
ff40d2cc
JB
104 expect(chargingStation.logPrefix.mock.calls.length).toBe(3)
105 expect(logger.error.mock.calls.length).toBe(3)
2d4928a7 106 })
d05b53c7 107})