From fd00f4215bbc360345f833eee8632627aad590dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 2 Apr 2026 23:48:18 +0200 Subject: [PATCH] refactor(tests): migrate hardcoded test tags to shared constants Replace 41 hardcoded tag/token strings across 13 test files with imports from ChargingStationTestConstants. Includes test utility factories. Normalize import paths to shortest relative form and merge duplicate imports. --- ...uestService-RemoteStartTransaction.test.ts | 23 ++++++++++--------- ...IncomingRequestService-Reservation.test.ts | 15 ++++++------ .../OCPP16Integration-Transactions.test.ts | 6 ++--- .../1.6/OCPP16RequestService-Payloads.test.ts | 5 ++-- ...OCPP16ResponseService-Transactions.test.ts | 13 ++++++----- .../ocpp/1.6/OCPP16SchemaValidation.test.ts | 5 ++-- .../ocpp/1.6/OCPP16TestUtils.ts | 6 ++--- .../OCPPConnectorStatusOperations.test.ts | 3 ++- .../auth/adapters/OCPP16AuthAdapter.test.ts | 5 ++-- .../ocpp/auth/helpers/MockFactories.ts | 5 ++-- .../auth/services/OCPPAuthServiceImpl.test.ts | 3 ++- .../strategies/RemoteAuthStrategy.test.ts | 5 ++-- 12 files changed, 52 insertions(+), 42 deletions(-) diff --git a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStartTransaction.test.ts b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStartTransaction.test.ts index eb1ea3f6..40dd71d6 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStartTransaction.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStartTransaction.test.ts @@ -23,6 +23,7 @@ import { setupConnectorWithTransaction, standardCleanup, } from '../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js' import { createOCPP16IncomingRequestTestContext, createOCPP16ListenerStation, @@ -46,7 +47,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const { station, testableService } = testContext const request: RemoteStartTransactionRequest = { connectorId: 0, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -62,7 +63,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const { station, testableService } = testContext const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -83,7 +84,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async } const request: RemoteStartTransactionRequest = { - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -98,7 +99,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async // Arrange const { station, testableService } = testContext const request: RemoteStartTransactionRequest = { - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -121,7 +122,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -144,7 +145,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -160,7 +161,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const { station, testableService } = testContext const request: RemoteStartTransactionRequest = { connectorId: 99, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } // Act @@ -200,7 +201,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } const response = { status: GenericStatus.Accepted } @@ -225,7 +226,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async // Arrange const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } const response = { status: GenericStatus.Rejected } @@ -251,7 +252,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } const response = { status: GenericStatus.Accepted } @@ -287,7 +288,7 @@ await describe('OCPP16IncomingRequestService — RemoteStartTransaction', async const request: RemoteStartTransactionRequest = { connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } const response = { status: GenericStatus.Accepted } 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 9c0a3a2f..b58a0d96 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Reservation.test.ts @@ -21,6 +21,7 @@ import { OCPP16StandardParametersKey, } from '../../../../src/types/index.js' import { standardCleanup } from '../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js' import { createOCPP16IncomingRequestTestContext, type OCPP16IncomingRequestTestContext, @@ -81,7 +82,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { // Arrange const { station, testableService } = context enableReservationProfile(context) - const reservation = ReservationFixtures.createReservation(1, 1, 'TEST-TAG-001') + const reservation = ReservationFixtures.createReservation(1, 1, TEST_ID_TAG) const request: OCPP16ReserveNowRequest = { connectorId: reservation.connectorId, expiryDate: reservation.expiryDate, @@ -104,7 +105,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { const request: OCPP16ReserveNowRequest = { connectorId: 0, expiryDate: new Date(Date.now() + 3600000), - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, reservationId: 10, } @@ -123,7 +124,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { const request: OCPP16ReserveNowRequest = { connectorId: 0, expiryDate: new Date(Date.now() + 3600000), - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, reservationId: 10, } @@ -146,7 +147,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { const request: OCPP16ReserveNowRequest = { connectorId: 1, expiryDate: new Date(Date.now() + 3600000), - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, reservationId: 2, } @@ -165,7 +166,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { const request: OCPP16ReserveNowRequest = { connectorId: 1, expiryDate: new Date(Date.now() + 3600000), - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, reservationId: 3, } @@ -183,7 +184,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { const request: OCPP16ReserveNowRequest = { connectorId: 99, expiryDate: new Date(Date.now() + 3600000), - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, reservationId: 4, } @@ -207,7 +208,7 @@ await describe('OCPP16IncomingRequestService — Reservation', async () => { enableReservationProfile(context) // First create a reservation via ReserveNow - const reservation = ReservationFixtures.createReservation(1, 42, 'TEST-TAG-001') + const reservation = ReservationFixtures.createReservation(1, 42, TEST_ID_TAG) const reserveRequest: OCPP16ReserveNowRequest = { connectorId: reservation.connectorId, expiryDate: reservation.expiryDate, diff --git a/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts b/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts index d171b36c..1e5fc229 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts @@ -39,7 +39,7 @@ import { setupConnectorWithTransaction, standardCleanup, } from '../../../helpers/TestLifecycleHelpers.js' -import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConstants.js' +import { TEST_CHARGING_STATION_BASE_NAME, TEST_ID_TAG } from '../../ChargingStationTestConstants.js' import { createMockChargingStation } from '../../ChargingStationTestUtils.js' /** @@ -121,7 +121,7 @@ await describe('OCPP16 Integration — Transaction Lifecycle', async () => { await it('should complete full transaction lifecycle: RemoteStart → StartTransaction accepted → StopTransaction', async () => { const connectorId = 1 const transactionId = 42 - const idTag = 'TEST-TAG-001' + const idTag = TEST_ID_TAG // Step 1: RemoteStartTransaction — CSMS asks station to start charging const remoteStartRequest: RemoteStartTransactionRequest = { @@ -240,7 +240,7 @@ await describe('OCPP16 Integration — Transaction Lifecycle', async () => { // Act const request: RemoteStartTransactionRequest = { connectorId, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, } const response = await testableService.handleRequestRemoteStartTransaction(station, request) diff --git a/tests/charging-station/ocpp/1.6/OCPP16RequestService-Payloads.test.ts b/tests/charging-station/ocpp/1.6/OCPP16RequestService-Payloads.test.ts index 43b0d56e..04b83190 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16RequestService-Payloads.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16RequestService-Payloads.test.ts @@ -27,6 +27,7 @@ import { type OCPP16StopTransactionRequest, } from '../../../../src/types/index.js' import { standardCleanup } from '../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js' import { createOCPP16RequestTestContext } from './OCPP16TestUtils.js' await describe('OCPP16RequestService — buildRequestPayload', async () => { @@ -183,12 +184,12 @@ await describe('OCPP16RequestService — buildRequestPayload', async () => { const payload = testableRequestService.buildRequestPayload( station, OCPP16RequestCommand.START_TRANSACTION, - { connectorId: 1, idTag: 'TEST-TAG-001' } + { connectorId: 1, idTag: TEST_ID_TAG } ) as OCPP16StartTransactionRequest assert.notStrictEqual(payload, undefined) assert.strictEqual(payload.connectorId, 1) - assert.strictEqual(payload.idTag, 'TEST-TAG-001') + assert.strictEqual(payload.idTag, TEST_ID_TAG) assert.strictEqual(typeof payload.meterStart, 'number') assert.notStrictEqual(payload.timestamp, undefined) }) 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 d389eaa4..6799a5d5 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16ResponseService-Transactions.test.ts @@ -27,6 +27,7 @@ import { setupConnectorWithTransaction, standardCleanup, } from '../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js' import { createOCPP16ResponseTestContext, setMockRequestHandler } from './OCPP16TestUtils.js' await describe('OCPP16ResponseService — StartTransaction and StopTransaction', async () => { @@ -72,7 +73,7 @@ await describe('OCPP16ResponseService — StartTransaction and StopTransaction', const transactionId = 42 const requestPayload: OCPP16StartTransactionRequest = { connectorId, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, meterStart: 0, timestamp: new Date(), } @@ -96,7 +97,7 @@ await describe('OCPP16ResponseService — StartTransaction and StopTransaction', } assert.strictEqual(connectorStatus.transactionId, transactionId) assert.strictEqual(connectorStatus.transactionStarted, true) - assert.strictEqual(connectorStatus.transactionIdTag, 'TEST-TAG-001') + assert.strictEqual(connectorStatus.transactionIdTag, TEST_ID_TAG) assert.strictEqual(connectorStatus.transactionEnergyActiveImportRegisterValue, 0) }) @@ -106,7 +107,7 @@ await describe('OCPP16ResponseService — StartTransaction and StopTransaction', const connectorId = 1 const requestPayload: OCPP16StartTransactionRequest = { connectorId, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, meterStart: 0, timestamp: new Date(), } @@ -142,13 +143,13 @@ await describe('OCPP16ResponseService — StartTransaction and StopTransaction', connectorStatus.reservation = { connectorId, expiryDate: new Date(Date.now() + 3600000), - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, reservationId, } } const requestPayload: OCPP16StartTransactionRequest = { connectorId, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, meterStart: 0, reservationId, timestamp: new Date(), @@ -182,7 +183,7 @@ await describe('OCPP16ResponseService — StartTransaction and StopTransaction', const requestTimestamp = new Date('2025-01-01T12:00:00Z') const requestPayload: OCPP16StartTransactionRequest = { connectorId, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, meterStart: 500, timestamp: requestTimestamp, } diff --git a/tests/charging-station/ocpp/1.6/OCPP16SchemaValidation.test.ts b/tests/charging-station/ocpp/1.6/OCPP16SchemaValidation.test.ts index 1bbd1f74..afbecfcb 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16SchemaValidation.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16SchemaValidation.test.ts @@ -19,6 +19,7 @@ import { OCPP16ResponseService } from '../../../../src/charging-station/ocpp/1.6 import { OCPP16ServiceUtils } from '../../../../src/charging-station/ocpp/1.6/OCPP16ServiceUtils.js' import { OCPP16IncomingRequestCommand, OCPP16RequestCommand } from '../../../../src/types/index.js' import { standardCleanup } from '../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG } from '../../ChargingStationTestConstants.js' const AjvConstructor = _Ajv.default const ajvFormats = _ajvFormats.default @@ -126,7 +127,7 @@ await describe('OCPP16SchemaValidation', async () => { await it('should pass validation when Authorize has valid idTag', () => { const validate = makeValidator('Authorize.json') - const valid = validate({ idTag: 'TEST-TAG-001' }) + const valid = validate({ idTag: TEST_ID_TAG }) assert.strictEqual(valid, true) }) @@ -166,7 +167,7 @@ await describe('OCPP16SchemaValidation', async () => { const validate = makeValidator('StartTransaction.json') const valid = validate({ connectorId: 1, - idTag: 'TEST-TAG-001', + idTag: TEST_ID_TAG, meterStart: 0, timestamp: '2025-03-10T12:00:00Z', }) diff --git a/tests/charging-station/ocpp/1.6/OCPP16TestUtils.ts b/tests/charging-station/ocpp/1.6/OCPP16TestUtils.ts index f435e3de..2d1043e5 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16TestUtils.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16TestUtils.ts @@ -37,7 +37,7 @@ import { OCPPVersion, } from '../../../../src/types/index.js' import { Constants } from '../../../../src/utils/index.js' -import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConstants.js' +import { TEST_CHARGING_STATION_BASE_NAME, TEST_ID_TAG } from '../../ChargingStationTestConstants.js' import { createMockChargingStation, type MockChargingStation, @@ -429,7 +429,7 @@ export const ReservationFixtures = { createReservation: ( connectorId = 1, reservationId = 1, - idTag = 'TEST-TAG-001', + idTag = TEST_ID_TAG, expiryDate = new Date(Date.now() + 3600000) ) => ({ connectorId, @@ -453,7 +453,7 @@ export const ResetFixtures = { } as const export const TransactionFixtures = { - createStartTransactionParams: (connectorId = 1, idTag = 'TEST-TAG-001') => ({ + createStartTransactionParams: (connectorId = 1, idTag = TEST_ID_TAG) => ({ connectorId, idTag, }), diff --git a/tests/charging-station/ocpp/OCPPConnectorStatusOperations.test.ts b/tests/charging-station/ocpp/OCPPConnectorStatusOperations.test.ts index 530fa077..d6cebe70 100644 --- a/tests/charging-station/ocpp/OCPPConnectorStatusOperations.test.ts +++ b/tests/charging-station/ocpp/OCPPConnectorStatusOperations.test.ts @@ -26,6 +26,7 @@ import { createStationWithRequestHandler, standardCleanup, } from '../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG } from '../ChargingStationTestConstants.js' await describe('OCPPConnectorStatusOperations', async () => { afterEach(() => { @@ -136,7 +137,7 @@ await describe('OCPPConnectorStatusOperations', async () => { connectorStatus.reservation = { connectorId: 1, expiryDate: new Date().toISOString(), - idTag: 'TEST-TAG', + idTag: TEST_ID_TAG, reservationId: 1, } as unknown as Reservation connectorStatus.status = ConnectorStatusEnum.Occupied diff --git a/tests/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.test.ts b/tests/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.test.ts index 5340bde9..82ac7d53 100644 --- a/tests/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.test.ts +++ b/tests/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.test.ts @@ -18,6 +18,7 @@ import { } from '../../../../../src/charging-station/ocpp/auth/types/AuthTypes.js' import { OCPP16AuthorizationStatus, OCPPVersion } from '../../../../../src/types/index.js' import { standardCleanup } from '../../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG_VALID } from '../../../ChargingStationTestConstants.js' import { createMockAuthorizationResult, createMockIdentifier } from '../helpers/MockFactories.js' await describe('OCPP16AuthAdapter', async () => { @@ -96,7 +97,7 @@ await describe('OCPP16AuthAdapter', async () => { await describe('isValidIdentifier', async () => { await it('should validate correct OCPP 1.6 identifier', () => { - const identifier = createMockIdentifier('VALID_TAG') + const identifier = createMockIdentifier(TEST_ID_TAG_VALID) assert.strictEqual(adapter.isValidIdentifier(identifier), true) }) @@ -149,7 +150,7 @@ await describe('OCPP16AuthAdapter', async () => { await describe('authorizeRemote', async () => { await it('should perform remote authorization successfully', async () => { - const identifier = createMockIdentifier('VALID_TAG') + const identifier = createMockIdentifier(TEST_ID_TAG_VALID) const result = await adapter.authorizeRemote(identifier, 1, 123) diff --git a/tests/charging-station/ocpp/auth/helpers/MockFactories.ts b/tests/charging-station/ocpp/auth/helpers/MockFactories.ts index f1ca5865..a1a17f17 100644 --- a/tests/charging-station/ocpp/auth/helpers/MockFactories.ts +++ b/tests/charging-station/ocpp/auth/helpers/MockFactories.ts @@ -30,6 +30,7 @@ import { type OCPP20IdTokenType, OCPPVersion, } from '../../../../../src/types/index.js' +import { TEST_ID_TAG } from '../../../ChargingStationTestConstants.js' /** * Factory functions for creating test mocks and fixtures @@ -38,12 +39,12 @@ import { /** * Create a mock Identifier for any OCPP version. - * @param value - Identifier token value (defaults to 'TEST-TAG-001') + * @param value - Identifier token value (defaults to TEST_ID_TAG) * @param type - Identifier type enum value (defaults to ID_TAG) * @returns Mock Identifier configured for testing */ export const createMockIdentifier = ( - value = 'TEST-TAG-001', + value = TEST_ID_TAG, type: IdentifierType = IdentifierType.ID_TAG ): Identifier => ({ type, diff --git a/tests/charging-station/ocpp/auth/services/OCPPAuthServiceImpl.test.ts b/tests/charging-station/ocpp/auth/services/OCPPAuthServiceImpl.test.ts index 82de9891..554c36e4 100644 --- a/tests/charging-station/ocpp/auth/services/OCPPAuthServiceImpl.test.ts +++ b/tests/charging-station/ocpp/auth/services/OCPPAuthServiceImpl.test.ts @@ -18,6 +18,7 @@ import { } from '../../../../../src/charging-station/ocpp/auth/types/AuthTypes.js' import { OCPPVersion } from '../../../../../src/types/index.js' import { standardCleanup } from '../../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG_VALID } from '../../../ChargingStationTestConstants.js' import { createMockAuthServiceTestStation, getTestAuthCache } from '../helpers/MockFactories.js' await describe('OCPPAuthServiceImpl', async () => { @@ -205,7 +206,7 @@ await describe('OCPPAuthServiceImpl', async () => { const identifier: Identifier = { type: IdentifierType.ID_TAG, - value: 'VALID_TAG', + value: TEST_ID_TAG_VALID, } const result = await authService.authorize({ diff --git a/tests/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.test.ts b/tests/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.test.ts index f6fb2d6a..cafb2446 100644 --- a/tests/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.test.ts +++ b/tests/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.test.ts @@ -21,6 +21,7 @@ import { } from '../../../../../src/charging-station/ocpp/auth/types/AuthTypes.js' import { OCPPVersion } from '../../../../../src/types/index.js' import { standardCleanup } from '../../../../helpers/TestLifecycleHelpers.js' +import { TEST_ID_TAG_INVALID } from '../../../ChargingStationTestConstants.js' import { createMockAuthCache, createMockAuthorizationResult, @@ -241,11 +242,11 @@ await describe('RemoteAuthStrategy', async () => { authorizationCacheLifetime: 300, }) const request = createMockAuthRequest({ - identifier: createMockIdentifier('INVALID_TAG', IdentifierType.ID_TAG), + identifier: createMockIdentifier(TEST_ID_TAG_INVALID, IdentifierType.ID_TAG), }) await strategy.authenticate(request, config) - assert.strictEqual(cachedKey, 'INVALID_TAG') + assert.strictEqual(cachedKey, TEST_ID_TAG_INVALID) assert.strictEqual(cachedValue?.status, AuthorizationStatus.INVALID) assert.strictEqual(cachedTtl, 300) }) -- 2.43.0