From: Jérôme Benoit Date: Mon, 20 Jul 2026 10:50:38 +0000 (+0200) Subject: test: use shared constants and enums instead of hardcoded literals (#2040) X-Git-Tag: cli@v4.11.0~11 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=503d71c848d5e5749efd21c6b1b4b85ef1f3602e;p=e-mobility-charging-stations-simulator.git test: use shared constants and enums instead of hardcoded literals (#2040) Replace hardcoded literals in recent tests with the shared constants/enums they duplicate (single source of truth, no behavior change; values are byte-equivalent): - OCPP20 PostStopResurrection: GenericStatus.Accepted / ReportBaseEnumType.FullInventory instead of raw 'Accepted' / 'FullInventory' - OCPP20 RequestStartTransaction: OCPP20ChargingRateUnitEnumType.A instead of 'A' as OCPP20ChargingRateUnitEnumType casts - OCPP16 SmartCharging: TEST_ONE_HOUR_SECONDS instead of the 3600 duration literal - UIHttpServer: ProcedureName.* instead of raw procedure-name strings - Remove the now-unused TEST_PROCEDURES test constant (ProcedureName is the canonical source) --- diff --git a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-SmartCharging.test.ts b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-SmartCharging.test.ts index 80f9cf21..a8280011 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-SmartCharging.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-SmartCharging.test.ts @@ -23,6 +23,7 @@ import { OCPP16StandardParametersKey, } from '../../../../src/types/index.js' import { standardCleanup } from '../../../helpers/TestLifecycleHelpers.js' +import { TEST_ONE_HOUR_SECONDS } from '../../ChargingStationTestConstants.js' import { ChargingProfileFixtures, createOCPP16IncomingRequestTestContext, @@ -37,7 +38,7 @@ const attachComposableProfile = ( profile: OCPP16ChargingProfile ): void => { profile.chargingSchedule.startSchedule = new Date() - profile.chargingSchedule.duration = 3600 + profile.chargingSchedule.duration = TEST_ONE_HOUR_SECONDS const connectorStatus = station.getConnectorStatus(connectorId) if (connectorStatus != null) { connectorStatus.chargingProfiles = [profile] @@ -278,14 +279,14 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { ) const profile = ChargingProfileFixtures.createTxDefaultProfile() profile.chargingSchedule.startSchedule = new Date() - profile.chargingSchedule.duration = 3600 + profile.chargingSchedule.duration = TEST_ONE_HOUR_SECONDS const connectorStatus = station.getConnectorStatus(1) if (connectorStatus != null) { connectorStatus.chargingProfiles = [profile] } const request: OCPP16GetCompositeScheduleRequest = { connectorId: 1, - duration: 3600, + duration: TEST_ONE_HOUR_SECONDS, } // Act @@ -308,7 +309,7 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { ) const request: OCPP16GetCompositeScheduleRequest = { connectorId: 99, - duration: 3600, + duration: TEST_ONE_HOUR_SECONDS, } // Act @@ -328,7 +329,7 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { ) const request: OCPP16GetCompositeScheduleRequest = { connectorId: 0, - duration: 3600, + duration: TEST_ONE_HOUR_SECONDS, } // Act @@ -357,7 +358,7 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { } const request: OCPP16GetCompositeScheduleRequest = { connectorId: 1, - duration: 3600, + duration: TEST_ONE_HOUR_SECONDS, } // Act @@ -373,7 +374,7 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { upsertConfigurationKey(station, OCPP16StandardParametersKey.SupportedFeatureProfiles, 'Core') const request: OCPP16GetCompositeScheduleRequest = { connectorId: 1, - duration: 3600, + duration: TEST_ONE_HOUR_SECONDS, } // Act @@ -396,7 +397,10 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { 'Core,SmartCharging' ) attachComposableProfile(station, 3, ChargingProfileFixtures.createTxDefaultProfile()) - const request: OCPP16GetCompositeScheduleRequest = { connectorId: 0, duration: 3600 } + const request: OCPP16GetCompositeScheduleRequest = { + connectorId: 0, + duration: TEST_ONE_HOUR_SECONDS, + } // Act const response = testableService.handleRequestGetCompositeSchedule(station, request) @@ -428,7 +432,10 @@ await describe('OCPP16IncomingRequestService — SmartCharging', async () => { } } attachComposableProfile(station, 0, ChargingProfileFixtures.createChargePointMaxProfile()) - const request: OCPP16GetCompositeScheduleRequest = { connectorId: 0, duration: 3600 } + const request: OCPP16GetCompositeScheduleRequest = { + connectorId: 0, + duration: TEST_ONE_HOUR_SECONDS, + } // Act const response = testableService.handleRequestGetCompositeSchedule(station, request) diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-PostStopResurrection.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-PostStopResurrection.test.ts index 9d64b3b8..f5d051e2 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-PostStopResurrection.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-PostStopResurrection.test.ts @@ -17,7 +17,7 @@ import { afterEach, beforeEach, describe, it, mock } from 'node:test' import type { ChargingStation } from '../../../../src/charging-station/index.js' import { OCPP20IncomingRequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.js' -import { OCPPVersion } from '../../../../src/types/index.js' +import { GenericStatus, OCPPVersion, ReportBaseEnumType } from '../../../../src/types/index.js' import { Constants } from '../../../../src/utils/index.js' import { flushMicrotasks, @@ -171,8 +171,8 @@ await describe('OCPP20IncomingRequestService — post-stop resurrection guard', await plumbing.sendNotifyReportRequest( station, - { reportBase: 'FullInventory', requestId: 1 }, - { status: 'Accepted' } + { reportBase: ReportBaseEnumType.FullInventory, requestId: 1 }, + { status: GenericStatus.Accepted } ) assert.strictEqual(requestHandlerMock.mock.callCount(), 0) @@ -190,8 +190,8 @@ await describe('OCPP20IncomingRequestService — post-stop resurrection guard', await plumbing.sendNotifyReportRequest( station, - { reportBase: 'FullInventory', requestId: 42 }, - { status: 'Accepted' } + { reportBase: ReportBaseEnumType.FullInventory, requestId: 42 }, + { status: GenericStatus.Accepted } ) assert.strictEqual(requestHandlerMock.mock.callCount(), 0) diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts index e6746c01..6fc458f3 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts @@ -11,7 +11,6 @@ import type { ChargingStation } from '../../../../src/charging-station/index.js' import type { EvseStatus, OCPP20ChargingProfileType, - OCPP20ChargingRateUnitEnumType, OCPP20RequestStartTransactionRequest, OCPP20RequestStartTransactionResponse, OCPP20TransactionEventOptions, @@ -30,6 +29,7 @@ import { AttributeEnumType, OCPP20ChargingProfileKindEnumType, OCPP20ChargingProfilePurposeEnumType, + OCPP20ChargingRateUnitEnumType, OCPP20ComponentName, OCPP20IdTokenEnumType, OCPP20IncomingRequestCommand, @@ -278,7 +278,7 @@ await describe('F01 & F02 - Remote Start Transaction', async () => { chargingProfilePurpose: OCPP20ChargingProfilePurposeEnumType.TxProfile, chargingSchedule: [ { - chargingRateUnit: 'A' as OCPP20ChargingRateUnitEnumType, + chargingRateUnit: OCPP20ChargingRateUnitEnumType.A, chargingSchedulePeriod: [ { limit: 30, @@ -319,7 +319,7 @@ await describe('F01 & F02 - Remote Start Transaction', async () => { chargingProfilePurpose: OCPP20ChargingProfilePurposeEnumType.TxDefaultProfile, chargingSchedule: [ { - chargingRateUnit: 'A' as OCPP20ChargingRateUnitEnumType, + chargingRateUnit: OCPP20ChargingRateUnitEnumType.A, chargingSchedulePeriod: [ { limit: 25, @@ -361,7 +361,7 @@ await describe('F01 & F02 - Remote Start Transaction', async () => { chargingProfilePurpose: OCPP20ChargingProfilePurposeEnumType.TxProfile, chargingSchedule: [ { - chargingRateUnit: 'A' as OCPP20ChargingRateUnitEnumType, + chargingRateUnit: OCPP20ChargingRateUnitEnumType.A, chargingSchedulePeriod: [ { limit: 32, @@ -433,7 +433,7 @@ await describe('F01 & F02 - Remote Start Transaction', async () => { chargingProfilePurpose: OCPP20ChargingProfilePurposeEnumType.TxProfile, chargingSchedule: [ { - chargingRateUnit: 'A' as OCPP20ChargingRateUnitEnumType, + chargingRateUnit: OCPP20ChargingRateUnitEnumType.A, chargingSchedulePeriod: [ { limit: 30, diff --git a/tests/charging-station/ui-server/UIHttpServer.test.ts b/tests/charging-station/ui-server/UIHttpServer.test.ts index f67a0c9e..f4e4f979 100644 --- a/tests/charging-station/ui-server/UIHttpServer.test.ts +++ b/tests/charging-station/ui-server/UIHttpServer.test.ts @@ -15,7 +15,12 @@ import type { ResponsePayload, UIServerConfiguration, UUIDv4 } from '../../../sr import { UIHttpServer } from '../../../src/charging-station/ui-server/UIHttpServer.js' import { DEFAULT_COMPRESSION_THRESHOLD_BYTES } from '../../../src/charging-station/ui-server/UIServerSecurity.js' -import { ApplicationProtocol, ProtocolVersion, ResponseStatus } from '../../../src/types/index.js' +import { + ApplicationProtocol, + ProcedureName, + ProtocolVersion, + ResponseStatus, +} from '../../../src/types/index.js' import { Constants, logger } from '../../../src/utils/index.js' import { createLoggerMocks, @@ -386,7 +391,7 @@ await describe('UIHttpServer', async () => { const res = new MockServerResponse() broadcastServer.emitRequest( - buildProcedureRequest('stopChargingStation', { + buildProcedureRequest(ProcedureName.STOP_CHARGING_STATION, { hashIds: [TEST_HASH_ID, TEST_HASH_ID_2], }), res @@ -428,7 +433,7 @@ await describe('UIHttpServer', async () => { const res = new MockServerResponse() broadcastServer.emitRequest( - buildProcedureRequest('stopChargingStation', { + buildProcedureRequest(ProcedureName.STOP_CHARGING_STATION, { hashIds: [TEST_HASH_ID, TEST_HASH_ID_2], }), res @@ -471,7 +476,7 @@ await describe('UIHttpServer', async () => { broadcastServer.emitRequest( buildProcedureRequest( - 'stopChargingStation', + ProcedureName.STOP_CHARGING_STATION, { hashIds: [TEST_HASH_ID] }, { 'accept-encoding': 'gzip' } ), @@ -522,7 +527,7 @@ await describe('UIHttpServer', async () => { const res = new MockServerResponse() broadcastServer.emitRequest( - buildProcedureRequest('stopChargingStation', { hashIds: [TEST_HASH_ID] }), + buildProcedureRequest(ProcedureName.STOP_CHARGING_STATION, { hashIds: [TEST_HASH_ID] }), res ) @@ -552,7 +557,10 @@ await describe('UIHttpServer', async () => { try { const res = new MockServerResponse() - broadcastServer.emitRequest(buildProcedureRequest('listChargingStations', {}), res) + broadcastServer.emitRequest( + buildProcedureRequest(ProcedureName.LIST_CHARGING_STATIONS, {}), + res + ) await awaitFinish(res) assert.strictEqual(res.statusCode, 200) @@ -571,7 +579,7 @@ await describe('UIHttpServer', async () => { const res = new MockServerResponse() broadcastServer.emitRequest( - buildProcedureRequest('stopChargingStation', { hashIds: [TEST_HASH_ID] }), + buildProcedureRequest(ProcedureName.STOP_CHARGING_STATION, { hashIds: [TEST_HASH_ID] }), res ) diff --git a/tests/charging-station/ui-server/UIServerTestConstants.ts b/tests/charging-station/ui-server/UIServerTestConstants.ts index 90bec33d..71e3684f 100644 --- a/tests/charging-station/ui-server/UIServerTestConstants.ts +++ b/tests/charging-station/ui-server/UIServerTestConstants.ts @@ -8,13 +8,5 @@ import type { UUIDv4 } from '../../../src/types/index.js' export const TEST_UUID = '550e8400-e29b-41d4-a716-446655440000' as UUIDv4 export const TEST_UUID_2 = '6ba7b810-9dad-41d1-80b4-00c04fd430c8' as UUIDv4 -export const TEST_PROCEDURES = { - AUTHORIZE: 'Authorize', - DELETE_CHARGING_STATIONS: 'deleteChargingStations', - LIST_CHARGING_STATIONS: 'listChargingStations', - START_CHARGING_STATION: 'startChargingStation', - STOP_CHARGING_STATION: 'stopChargingStation', -} as const - export const TEST_HASH_ID = 'test-station-001' as const export const TEST_HASH_ID_2 = 'test-station-002' as const