From: Jérôme Benoit Date: Tue, 3 Mar 2026 18:46:40 +0000 (+0100) Subject: test(ocpp): add OCPPServiceUtils command support validation tests X-Git-Tag: ocpp-server@v3.0.0~23 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=58be9c741e57c87fa993052dc9ea02545255562d;p=e-mobility-charging-stations-simulator.git test(ocpp): add OCPPServiceUtils command support validation tests Test isIncomingRequestCommandSupported, isRequestCommandSupported, isMessageTriggerSupported with 12 tests covering all support config branches. --- diff --git a/tests/charging-station/ocpp/OCPPServiceUtils-validation.test.ts b/tests/charging-station/ocpp/OCPPServiceUtils-validation.test.ts new file mode 100644 index 00000000..baefaf11 --- /dev/null +++ b/tests/charging-station/ocpp/OCPPServiceUtils-validation.test.ts @@ -0,0 +1,219 @@ +/** + * @file Tests for OCPPServiceUtils command/trigger support validation + * @description Verifies static methods that check whether incoming/outgoing commands + * and message triggers are supported by a charging station's configuration. + * + * Covers: + * - OCPPServiceUtils.isIncomingRequestCommandSupported + * - OCPPServiceUtils.isRequestCommandSupported + * - OCPPServiceUtils.isMessageTriggerSupported + */ + +import { expect } from '@std/expect' +import { afterEach, describe, it } from 'node:test' + +import type { ChargingStation } from '../../../src/charging-station/ChargingStation.js' + +import { OCPPServiceUtils } from '../../../src/charging-station/ocpp/OCPPServiceUtils.js' +import { + type IncomingRequestCommand, + type MessageTrigger, + OCPP16IncomingRequestCommand, + OCPP16MessageTrigger, + OCPP16RequestCommand, + type RequestCommand, +} from '../../../src/types/index.js' +import { standardCleanup } from '../../helpers/TestLifecycleHelpers.js' + +/** + * Creates a minimal ChargingStation mock with the given stationInfo. + * @param stationInfo - partial stationInfo to set on the mock + */ +function makeStationMock (stationInfo?: Record): ChargingStation { + return { + logPrefix: () => '[test-station]', + stationInfo, + } as unknown as ChargingStation +} + +await describe('OCPPServiceUtils — command/trigger validation', async () => { + afterEach(() => { + standardCleanup() + }) + + await describe('isIncomingRequestCommandSupported', async () => { + await it('should return true when command is not explicitly disabled', () => { + const station = makeStationMock({ + commandsSupport: { + incomingCommands: { + [OCPP16IncomingRequestCommand.RESET]: true, + }, + }, + }) + + const result = OCPPServiceUtils.isIncomingRequestCommandSupported( + station, + OCPP16IncomingRequestCommand.RESET as IncomingRequestCommand + ) + + expect(result).toBe(true) + }) + + await it('should return false when command is explicitly disabled', () => { + const station = makeStationMock({ + commandsSupport: { + incomingCommands: { + [OCPP16IncomingRequestCommand.RESET]: false, + }, + }, + }) + + const result = OCPPServiceUtils.isIncomingRequestCommandSupported( + station, + OCPP16IncomingRequestCommand.RESET as IncomingRequestCommand + ) + + expect(result).toBe(false) + }) + + await it('should return true when commandsSupport is undefined', () => { + const station = makeStationMock({}) + + const result = OCPPServiceUtils.isIncomingRequestCommandSupported( + station, + OCPP16IncomingRequestCommand.RESET as IncomingRequestCommand + ) + + expect(result).toBe(true) + }) + + await it('should return true when incomingCommands is empty', () => { + const station = makeStationMock({ + commandsSupport: {}, + }) + + const result = OCPPServiceUtils.isIncomingRequestCommandSupported( + station, + OCPP16IncomingRequestCommand.RESET as IncomingRequestCommand + ) + + expect(result).toBe(true) + }) + }) + + await describe('isRequestCommandSupported', async () => { + await it('should return true when command is not explicitly disabled', () => { + const station = makeStationMock({ + commandsSupport: { + outgoingCommands: { + [OCPP16RequestCommand.HEARTBEAT]: true, + }, + }, + }) + + const result = OCPPServiceUtils.isRequestCommandSupported( + station, + OCPP16RequestCommand.HEARTBEAT as RequestCommand + ) + + expect(result).toBe(true) + }) + + await it('should return false when command is explicitly disabled', () => { + const station = makeStationMock({ + commandsSupport: { + outgoingCommands: { + [OCPP16RequestCommand.HEARTBEAT]: false, + }, + }, + }) + + const result = OCPPServiceUtils.isRequestCommandSupported( + station, + OCPP16RequestCommand.HEARTBEAT as RequestCommand + ) + + expect(result).toBe(false) + }) + + await it('should return true when commandsSupport is undefined', () => { + const station = makeStationMock({}) + + const result = OCPPServiceUtils.isRequestCommandSupported( + station, + OCPP16RequestCommand.HEARTBEAT as RequestCommand + ) + + expect(result).toBe(true) + }) + + await it('should return true when outgoingCommands is empty', () => { + const station = makeStationMock({ + commandsSupport: {}, + }) + + const result = OCPPServiceUtils.isRequestCommandSupported( + station, + OCPP16RequestCommand.HEARTBEAT as RequestCommand + ) + + expect(result).toBe(true) + }) + }) + + await describe('isMessageTriggerSupported', async () => { + await it('should return true when trigger is not explicitly disabled', () => { + const station = makeStationMock({ + messageTriggerSupport: { + [OCPP16MessageTrigger.Heartbeat]: true, + }, + }) + + const result = OCPPServiceUtils.isMessageTriggerSupported( + station, + OCPP16MessageTrigger.Heartbeat as MessageTrigger + ) + + expect(result).toBe(true) + }) + + await it('should return false when trigger is explicitly disabled', () => { + const station = makeStationMock({ + messageTriggerSupport: { + [OCPP16MessageTrigger.Heartbeat]: false, + }, + }) + + const result = OCPPServiceUtils.isMessageTriggerSupported( + station, + OCPP16MessageTrigger.Heartbeat as MessageTrigger + ) + + expect(result).toBe(false) + }) + + await it('should return true when messageTriggerSupport is undefined', () => { + const station = makeStationMock({}) + + const result = OCPPServiceUtils.isMessageTriggerSupported( + station, + OCPP16MessageTrigger.Heartbeat as MessageTrigger + ) + + expect(result).toBe(true) + }) + + await it('should return true when messageTriggerSupport is empty', () => { + const station = makeStationMock({ + messageTriggerSupport: null, + }) + + const result = OCPPServiceUtils.isMessageTriggerSupported( + station, + OCPP16MessageTrigger.Heartbeat as MessageTrigger + ) + + expect(result).toBe(true) + }) + }) +})