From: Jérôme Benoit Date: Fri, 3 Apr 2026 17:36:08 +0000 (+0200) Subject: refactor(tests): extract hardcoded values to constants X-Git-Tag: ocpp-server@v4.3.0~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7c477d252ce4529cf4b257c225f7c7da6a5f4b34;p=e-mobility-charging-stations-simulator.git refactor(tests): extract hardcoded values to constants - 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 --- diff --git a/tests/charging-station/AutomaticTransactionGenerator.test.ts b/tests/charging-station/AutomaticTransactionGenerator.test.ts index eee728ae..3928f1c9 100644 --- a/tests/charging-station/AutomaticTransactionGenerator.test.ts +++ b/tests/charging-station/AutomaticTransactionGenerator.test.ts @@ -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 diff --git a/tests/charging-station/ChargingStationTestConstants.ts b/tests/charging-station/ChargingStationTestConstants.ts index 0ee64e3e..06baa4d7 100644 --- a/tests/charging-station/ChargingStationTestConstants.ts +++ b/tests/charging-station/ChargingStationTestConstants.ts @@ -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 diff --git a/tests/charging-station/SharedLRUCache.test.ts b/tests/charging-station/SharedLRUCache.test.ts index e5465df5..3d5cc305 100644 --- a/tests/charging-station/SharedLRUCache.test.ts +++ b/tests/charging-station/SharedLRUCache.test.ts @@ -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' }, diff --git a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts index b58a0d96..0d4254f6 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts @@ -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, } diff --git a/tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts b/tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts index 6799a5d5..4e388cd2 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts @@ -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, } diff --git a/tests/charging-station/ui-server/UIMCPServer.test.ts b/tests/charging-station/ui-server/UIMCPServer.test.ts index 8bae9e8a..3c5df4bc 100644 --- a/tests/charging-station/ui-server/UIMCPServer.test.ts +++ b/tests/charging-station/ui-server/UIMCPServer.test.ts @@ -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, { diff --git a/tests/utils/ChargingStationConfigurationUtils.test.ts b/tests/utils/ChargingStationConfigurationUtils.test.ts index 6e2a7ac9..0a9eff82 100644 --- a/tests/utils/ChargingStationConfigurationUtils.test.ts +++ b/tests/utils/ChargingStationConfigurationUtils.test.ts @@ -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 = {