]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(tests): extract hardcoded values to constants
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 3 Apr 2026 17:36:08 +0000 (19:36 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 3 Apr 2026 17:36:48 +0000 (19:36 +0200)
- extract TEST_TIMEOUT_MS constant for 9 setTimeout(30000) calls in
  UIMCPServer.test.ts
- use Constants.DEFAULT_ATG_CONFIGURATION instead of inline ATG config
  objects in SharedLRUCache, ChargingStationConfigurationUtils, and
  AutomaticTransactionGenerator tests
- add TEST_RESERVATION_EXPIRY_MS constant and replace 6 hardcoded
  3600000 values in reservation and transaction tests

tests/charging-station/AutomaticTransactionGenerator.test.ts
tests/charging-station/ChargingStationTestConstants.ts
tests/charging-station/SharedLRUCache.test.ts
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts
tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts
tests/charging-station/ui-server/UIMCPServer.test.ts
tests/utils/ChargingStationConfigurationUtils.test.ts

index eee728ae29b0b0658f7caac348f0802711483238..3928f1c9fb620de63bdbf3cdaf223fbfcaf34d4d 100644 (file)
@@ -22,6 +22,7 @@ import type { ChargingStation } from '../../src/charging-station/index.js'
 import { AutomaticTransactionGenerator } from '../../src/charging-station/AutomaticTransactionGenerator.js'
 import { BaseError } from '../../src/exception/index.js'
 import { type StartTransactionResult } from '../../src/types/index.js'
+import { Constants } from '../../src/utils/Constants.js'
 import { flushMicrotasks } from '../helpers/TestLifecycleHelpers.js'
 import { createMockChargingStation, standardCleanup } from './ChargingStationTestUtils.js'
 
@@ -37,15 +38,10 @@ function addATGMethodsToStation (station: ChargingStation): void {
     getAutomaticTransactionGeneratorStatuses: () => undefined | unknown[]
   }
   stationExt.getAutomaticTransactionGeneratorConfiguration = () => ({
+    ...Constants.DEFAULT_ATG_CONFIGURATION,
     enable: true,
     idTagDistribution: 'random',
-    maxDelayBetweenTwoTransactions: 30,
-    maxDuration: 120,
-    minDelayBetweenTwoTransactions: 15,
-    minDuration: 60,
-    probabilityOfStart: 1,
     requireAuthorize: false,
-    stopAbsoluteDuration: false,
     stopAfterHours: 1,
   })
   stationExt.getAutomaticTransactionGeneratorStatuses = () => undefined
index 0ee64e3e7f42199c3aa89cb85b6dbeb5fa70202a..06baa4d72eabc51d6670296f340fadf849d8a4a4 100644 (file)
@@ -94,6 +94,7 @@ export const TEST_RATE_LIMIT_WINDOW_MS = 1000
  */
 export const TEST_CUSTOM_HEARTBEAT_INTERVAL_SECONDS = 120
 export const TEST_REJECTED_HEARTBEAT_INTERVAL_SECONDS = TEST_ONE_HOUR_SECONDS
+export const TEST_RESERVATION_EXPIRY_MS = TEST_ONE_HOUR_MS
 
 /**
  * OCPP 2.0 Value Size Limits
index e5465df5cf2c0b0a283adc777f82c17617fb82c6..3d5cc30530eecece64dc7392a49d7d25913a7d6a 100644 (file)
@@ -20,6 +20,7 @@ import type {
 import { Bootstrap } from '../../src/charging-station/Bootstrap.js'
 import { SharedLRUCache } from '../../src/charging-station/SharedLRUCache.js'
 import { StandardParametersKey } from '../../src/types/index.js'
+import { Constants } from '../../src/utils/Constants.js'
 import { standardCleanup } from '../helpers/TestLifecycleHelpers.js'
 
 interface BootstrapStatic {
@@ -33,7 +34,7 @@ interface BootstrapStatic {
  */
 function createCacheableConfiguration (hash: string): ChargingStationConfiguration {
   return {
-    automaticTransactionGenerator: { enable: false, maxDuration: 120, minDuration: 60 },
+    automaticTransactionGenerator: { ...Constants.DEFAULT_ATG_CONFIGURATION },
     configurationHash: hash,
     configurationKey: [
       { key: StandardParametersKey.HeartbeatInterval, readonly: false, value: '60' },
index b58a0d96334a531ebb785a50d43629e3d91df9ba..0d4254f6b33e1dbd518085b1fefc3901d1624e5b 100644 (file)
@@ -21,7 +21,7 @@ import {
   OCPP16StandardParametersKey,
 } from '../../../../src/types/index.js'
 import { standardCleanup } from '../../../helpers/TestLifecycleHelpers.js'
-import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js'
+import { TEST_ID_TAG, TEST_RESERVATION_EXPIRY_MS } from '../../ChargingStationTestConstants.js'
 import {
   createOCPP16IncomingRequestTestContext,
   type OCPP16IncomingRequestTestContext,
@@ -104,7 +104,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => {
       enableReservationProfile(context, true)
       const request: OCPP16ReserveNowRequest = {
         connectorId: 0,
-        expiryDate: new Date(Date.now() + 3600000),
+        expiryDate: new Date(Date.now() + TEST_RESERVATION_EXPIRY_MS),
         idTag: TEST_ID_TAG,
         reservationId: 10,
       }
@@ -123,7 +123,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => {
       enableReservationProfile(context, false)
       const request: OCPP16ReserveNowRequest = {
         connectorId: 0,
-        expiryDate: new Date(Date.now() + 3600000),
+        expiryDate: new Date(Date.now() + TEST_RESERVATION_EXPIRY_MS),
         idTag: TEST_ID_TAG,
         reservationId: 10,
       }
@@ -146,7 +146,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => {
       }
       const request: OCPP16ReserveNowRequest = {
         connectorId: 1,
-        expiryDate: new Date(Date.now() + 3600000),
+        expiryDate: new Date(Date.now() + TEST_RESERVATION_EXPIRY_MS),
         idTag: TEST_ID_TAG,
         reservationId: 2,
       }
@@ -165,7 +165,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => {
       upsertConfigurationKey(station, OCPP16StandardParametersKey.SupportedFeatureProfiles, 'Core')
       const request: OCPP16ReserveNowRequest = {
         connectorId: 1,
-        expiryDate: new Date(Date.now() + 3600000),
+        expiryDate: new Date(Date.now() + TEST_RESERVATION_EXPIRY_MS),
         idTag: TEST_ID_TAG,
         reservationId: 3,
       }
@@ -183,7 +183,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => {
       enableReservationProfile(context)
       const request: OCPP16ReserveNowRequest = {
         connectorId: 99,
-        expiryDate: new Date(Date.now() + 3600000),
+        expiryDate: new Date(Date.now() + TEST_RESERVATION_EXPIRY_MS),
         idTag: TEST_ID_TAG,
         reservationId: 4,
       }
index 6799a5d5b06bd937847257d106b34ea26fd2aa26..4e388cd286921a4b34a83ed183bcfdf131d7c81a 100644 (file)
@@ -27,7 +27,7 @@ import {
   setupConnectorWithTransaction,
   standardCleanup,
 } from '../../../helpers/TestLifecycleHelpers.js'
-import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js'
+import { TEST_ID_TAG, TEST_RESERVATION_EXPIRY_MS } from '../../ChargingStationTestConstants.js'
 import { createOCPP16ResponseTestContext, setMockRequestHandler } from './OCPP16TestUtils.js'
 
 await describe('OCPP16ResponseService — StartTransaction and StopTransaction', async () => {
@@ -142,7 +142,7 @@ await describe('OCPP16ResponseService — StartTransaction and StopTransaction',
       if (connectorStatus != null) {
         connectorStatus.reservation = {
           connectorId,
-          expiryDate: new Date(Date.now() + 3600000),
+          expiryDate: new Date(Date.now() + TEST_RESERVATION_EXPIRY_MS),
           idTag: TEST_ID_TAG,
           reservationId,
         }
index 8bae9e8a1008b7dd766ebe793d5aa6591448de3d..3c5df4bce3a2c7dac1e377207556f3bf4b516718 100644 (file)
@@ -46,6 +46,8 @@ import {
   createMockUIServerConfiguration,
 } from './UIServerTestUtils.js'
 
+const TEST_TIMEOUT_MS = 30_000
+
 class TestableUIMCPServer extends UIMCPServer {
   public constructor (config: UIServerConfiguration) {
     super(config, createMockBootstrap())
@@ -242,7 +244,7 @@ await describe('UIMCPServer', async () => {
     })
 
     await it('should return true when uuid is in pendingMcpRequests', () => {
-      const timeout = setTimeout(() => undefined, 30000)
+      const timeout = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
       pendingMap.set(TEST_UUID, {
         reject: (_error: Error) => undefined,
@@ -260,7 +262,7 @@ await describe('UIMCPServer', async () => {
   await describe('sendResponse Promise bridge', async () => {
     await it('should resolve pending Promise when sendResponse called with matching UUID', () => {
       let resolvedPayload: ResponsePayload | undefined
-      const timeout = setTimeout(() => undefined, 30000)
+      const timeout = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
       pendingMap.set(TEST_UUID, {
         reject: (_error: Error) => undefined,
@@ -280,7 +282,7 @@ await describe('UIMCPServer', async () => {
     await it('should clear timeout when resolving pending request', t => {
       const clearTimeoutMock = t.mock.method(globalThis, 'clearTimeout')
 
-      const timeout = setTimeout(() => undefined, 30000)
+      const timeout = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
       pendingMap.set(TEST_UUID, {
         reject: (_error: Error) => undefined,
@@ -294,7 +296,7 @@ await describe('UIMCPServer', async () => {
     })
 
     await it('should delete pending entry after resolve', () => {
-      const timeout = setTimeout(() => undefined, 30000)
+      const timeout = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
       pendingMap.set(TEST_UUID, {
         reject: (_error: Error) => undefined,
@@ -331,8 +333,8 @@ await describe('UIMCPServer', async () => {
   await describe('stop cleanup', async () => {
     await it('should reject all pending requests on stop', () => {
       const rejectedErrors: Error[] = []
-      const timeout1 = setTimeout(() => undefined, 30000)
-      const timeout2 = setTimeout(() => undefined, 30000)
+      const timeout1 = setTimeout(() => undefined, TEST_TIMEOUT_MS)
+      const timeout2 = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
 
       pendingMap.set(TEST_UUID, {
@@ -364,8 +366,8 @@ await describe('UIMCPServer', async () => {
     await it('should clear all timeouts on stop', t => {
       const clearTimeoutMock = t.mock.method(globalThis, 'clearTimeout')
 
-      const timeout1 = setTimeout(() => undefined, 30000)
-      const timeout2 = setTimeout(() => undefined, 30000)
+      const timeout1 = setTimeout(() => undefined, TEST_TIMEOUT_MS)
+      const timeout2 = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
 
       pendingMap.set(TEST_UUID, {
@@ -385,7 +387,7 @@ await describe('UIMCPServer', async () => {
     })
 
     await it('should clear pending map on stop', () => {
-      const timeout = setTimeout(() => undefined, 30000)
+      const timeout = setTimeout(() => undefined, TEST_TIMEOUT_MS)
       const pendingMap = server.getPendingMcpRequestsMap()
 
       pendingMap.set(TEST_UUID, {
index 6e2a7ac9a553d7b274c64a563b6fa7bd580c4c37..0a9eff82c4dce4c1172b542ce4e721499ecc7b96 100644 (file)
@@ -20,6 +20,7 @@ import {
   buildEvseEntries,
   buildEvsesStatus,
 } from '../../src/utils/ChargingStationConfigurationUtils.js'
+import { Constants } from '../../src/utils/Constants.js'
 import {
   cleanupChargingStation,
   createMockChargingStation,
@@ -314,7 +315,7 @@ await describe('ChargingStationConfigurationUtils', async () => {
 
   await describe('buildChargingStationAutomaticTransactionGeneratorConfiguration', async () => {
     await it('should return ATG configuration when present', () => {
-      const atgConfiguration = { enable: true, maxDuration: 120, minDuration: 60 }
+      const atgConfiguration = { ...Constants.DEFAULT_ATG_CONFIGURATION, enable: true }
       const { station } = createMockChargingStation({ connectorsCount: 0 })
       testStation = station
       station.automaticTransactionGenerator = {