1 /* eslint-disable @typescript-eslint/no-unsafe-member-access */
2 import { describe
, it
} from
'node:test'
4 import { expect
} from
'expect'
6 import type { ChargingStation
} from
'../../src/charging-station/index.js'
10 IncomingRequestCommand
,
13 } from
'../../src/types/index.js'
16 handleIncomingRequestError
,
17 handleSendMessageError
,
19 } from
'../../src/utils/ErrorUtils.js'
20 import { logger
} from
'../../src/utils/Logger.js'
22 await describe('ErrorUtils test suite', async () => {
23 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
24 const chargingStation
= {
25 logPrefix
: () => 'CS-TEST |'
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()
36 handleFileException('path/to/module.js', FileType
.Authorization
, error
, 'log prefix |', {})
39 handleFileException('path/to/module.js', FileType
.Authorization
, error
, 'log prefix |', {
43 expect(logger
.warn
.mock
.calls
.length
).toBe(1)
44 expect(logger
.error
.mock
.calls
.length
).toBe(1)
46 handleFileException('path/to/module.js', FileType
.Authorization
, error
, 'log prefix |', {
51 handleFileException('path/to/module.js', FileType
.Authorization
, error
, 'log prefix |', {
56 expect(console
.warn
.mock
.calls
.length
).toBe(1)
57 expect(console
.error
.mock
.calls
.length
).toBe(1)
60 await it('Verify handleSendMessageError()', t
=> {
61 t
.mock
.method(logger
, 'error')
62 t
.mock
.method(chargingStation
, 'logPrefix')
63 const error
= new Error()
65 handleSendMessageError(
67 RequestCommand
.BOOT_NOTIFICATION
,
68 MessageType
.CALL_MESSAGE
,
73 handleSendMessageError(
75 RequestCommand
.BOOT_NOTIFICATION
,
76 MessageType
.CALL_MESSAGE
,
81 expect(chargingStation
.logPrefix
.mock
.calls
.length
).toBe(2)
82 expect(logger
.error
.mock
.calls
.length
).toBe(2)
85 await it('Verify handleIncomingRequestError()', t
=> {
86 t
.mock
.method(logger
, 'error')
87 t
.mock
.method(chargingStation
, 'logPrefix')
88 const error
= new Error()
90 handleIncomingRequestError(chargingStation
, IncomingRequestCommand
.CLEAR_CACHE
, error
)
93 handleIncomingRequestError(chargingStation
, IncomingRequestCommand
.CLEAR_CACHE
, error
, {
97 const errorResponse
= {
98 status: GenericStatus
.Rejected
101 handleIncomingRequestError(chargingStation
, IncomingRequestCommand
.CLEAR_CACHE
, error
, {
105 ).toStrictEqual(errorResponse
)
106 expect(chargingStation
.logPrefix
.mock
.calls
.length
).toBe(3)
107 expect(logger
.error
.mock
.calls
.length
).toBe(3)
110 await it('Verify setDefaultErrorParams()', () => {
111 expect(setDefaultErrorParams({})).toStrictEqual({ throwError
: true, consoleOut
: false })
112 expect(setDefaultErrorParams({ throwError
: false })).toStrictEqual({
116 expect(setDefaultErrorParams({ throwError
: false, consoleOut
: true })).toStrictEqual({
120 expect(setDefaultErrorParams({ throwError
: true, consoleOut
: true })).toStrictEqual({
124 expect(setDefaultErrorParams({}, { throwError
: false, consoleOut
: false })).toStrictEqual({