test: improve ErrorUtils coverage
[e-mobility-charging-stations-simulator.git] / tests / utils / ErrorUtils.test.ts
index 907d05fad5f136fbfed17f7fe527a813aa8114e9..59c455f94df9f7ae4aa9a70efda1acb1ab19b45e 100644 (file)
@@ -2,10 +2,27 @@ import { describe, it } from 'node:test'
 
 import { expect } from 'expect'
 
-import { FileType } from '../../src/types/index.js'
-import { handleFileException, 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'
 
 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()', () => {
     const error = new Error()
     error.code = 'ENOENT'
@@ -19,6 +36,48 @@ await describe('ErrorUtils test suite', async () => {
     }).not.toThrow()
   })
 
+  await it('Verify handleSendMessageError()', () => {
+    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)
+  })
+
+  await it('Verify handleIncomingRequestError()', () => {
+    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)
+  })
+
   await it('Verify setDefaultErrorParams()', () => {
     expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false })
     expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({