test: add some tests for charging station helpers
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 26 Jul 2024 15:39:58 +0000 (17:39 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 26 Jul 2024 15:39:58 +0000 (17:39 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/Helpers.ts
tests/charging-station/Helpers.test.ts [new file with mode: 0644]

index 02419444ba6c6b2e7bf4c30f0b603a35eb37864c..8f442f46dfd62fb6f9150d310a1d683a2b467aea 100644 (file)
@@ -202,10 +202,12 @@ export class AutomaticTransactionGenerator {
       }
       const wait = secondsToMilliseconds(
         randomInt(
-          this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
-            ?.minDelayBetweenTwoTransactions,
-          this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
-            ?.maxDelayBetweenTwoTransactions
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!
+            .minDelayBetweenTwoTransactions,
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!
+            .maxDelayBetweenTwoTransactions
         )
       )
       logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`)
@@ -224,8 +226,10 @@ export class AutomaticTransactionGenerator {
           // Wait until end of transaction
           const waitTrxEnd = secondsToMilliseconds(
             randomInt(
-              this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration,
-              this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration
+              // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+              this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.minDuration,
+              // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+              this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.maxDuration
             )
           )
           logger.info(
index e8ef83c43f23741f025cfc96a99eb48e9435ecde..9b2d83700a2cb260eb04975baff7ce4375ec50d4 100644 (file)
@@ -176,46 +176,56 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat
 }
 
 export const validateStationInfo = (chargingStation: ChargingStation): void => {
-  if (isEmpty(chargingStation.stationInfo)) {
+  if (chargingStation.stationInfo == null || isEmpty(chargingStation.stationInfo)) {
     throw new BaseError('Missing charging station information')
   }
-  if (isEmpty(chargingStation.stationInfo?.chargingStationId?.trim())) {
+  if (
+    chargingStation.stationInfo.chargingStationId == null ||
+    isEmpty(chargingStation.stationInfo.chargingStationId.trim())
+  ) {
     throw new BaseError('Missing chargingStationId in stationInfo properties')
   }
-  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-  const chargingStationId: string = chargingStation.stationInfo!.chargingStationId!
-  if (isEmpty(chargingStation.stationInfo?.hashId.trim())) {
+  const chargingStationId = chargingStation.stationInfo.chargingStationId
+  if (
+    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+    chargingStation.stationInfo.hashId == null ||
+    isEmpty(chargingStation.stationInfo.hashId.trim())
+  ) {
     throw new BaseError(`${chargingStationId}: Missing hashId in stationInfo properties`)
   }
-  if (isEmpty(chargingStation.stationInfo?.templateIndex)) {
+  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+  if (chargingStation.stationInfo.templateIndex == null) {
     throw new BaseError(`${chargingStationId}: Missing templateIndex in stationInfo properties`)
   }
-  if (isEmpty(chargingStation.stationInfo?.templateName.trim())) {
+  if (chargingStation.stationInfo.templateIndex <= 0) {
+    throw new BaseError(
+      `${chargingStationId}: Invalid templateIndex value in stationInfo properties`
+    )
+  }
+  if (
+    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+    chargingStation.stationInfo.templateName == null ||
+    isEmpty(chargingStation.stationInfo.templateName.trim())
+  ) {
     throw new BaseError(`${chargingStationId}: Missing templateName in stationInfo properties`)
   }
-  if (isEmpty(chargingStation.stationInfo?.maximumPower)) {
+  if (chargingStation.stationInfo.maximumPower == null) {
     throw new BaseError(`${chargingStationId}: Missing maximumPower in stationInfo properties`)
   }
-  if (
-    chargingStation.stationInfo?.maximumPower != null &&
-    chargingStation.stationInfo.maximumPower <= 0
-  ) {
+  if (chargingStation.stationInfo.maximumPower <= 0) {
     throw new RangeError(
       `${chargingStationId}: Invalid maximumPower value in stationInfo properties`
     )
   }
-  if (isEmpty(chargingStation.stationInfo?.maximumAmperage)) {
+  if (chargingStation.stationInfo.maximumAmperage == null) {
     throw new BaseError(`${chargingStationId}: Missing maximumAmperage in stationInfo properties`)
   }
-  if (
-    chargingStation.stationInfo?.maximumAmperage != null &&
-    chargingStation.stationInfo.maximumAmperage <= 0
-  ) {
+  if (chargingStation.stationInfo.maximumAmperage <= 0) {
     throw new RangeError(
       `${chargingStationId}: Invalid maximumAmperage value in stationInfo properties`
     )
   }
-  switch (chargingStation.stationInfo?.ocppVersion) {
+  switch (chargingStation.stationInfo.ocppVersion) {
     case OCPPVersion.VERSION_20:
     case OCPPVersion.VERSION_201:
       if (chargingStation.evses.size === 0) {
diff --git a/tests/charging-station/Helpers.test.ts b/tests/charging-station/Helpers.test.ts
new file mode 100644 (file)
index 0000000..2a3c799
--- /dev/null
@@ -0,0 +1,114 @@
+import { describe, it } from 'node:test'
+
+import { expect } from 'expect'
+
+import {
+  getChargingStationId,
+  getHashId,
+  validateStationInfo,
+} from '../../src/charging-station/Helpers.js'
+import type { ChargingStation } from '../../src/charging-station/index.js'
+import { BaseError } from '../../src/exception/index.js'
+import {
+  type ChargingStationInfo,
+  type ChargingStationTemplate,
+  type EvseStatus,
+  OCPPVersion,
+} from '../../src/types/index.js'
+
+await describe('Helpers test suite', async () => {
+  const baseName = 'CS-TEST'
+  const chargingStationTemplate = {
+    baseName,
+  } as ChargingStationTemplate
+  const chargingStation = {} as ChargingStation
+
+  await it('Verify getChargingStationId()', t => {
+    expect(getChargingStationId(1, chargingStationTemplate)).toBe(`${baseName}-00001`)
+  })
+
+  await it('Verify getHashId()', t => {
+    expect(getHashId(1, chargingStationTemplate)).toBe(
+      'b4b1e8ec4fca79091d99ea9a7ea5901548010e6c0e98be9296f604b9d68734444dfdae73d7d406b6124b42815214d088'
+    )
+  })
+
+  await it('Verify validateStationInfo()', t => {
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError('Missing charging station information'))
+    chargingStation.stationInfo = {} as ChargingStationInfo
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError('Missing charging station information'))
+    chargingStation.stationInfo.baseName = baseName
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties'))
+    chargingStation.stationInfo.chargingStationId = ''
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties'))
+    chargingStation.stationInfo.chargingStationId = getChargingStationId(1, chargingStationTemplate)
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`))
+    chargingStation.stationInfo.hashId = ''
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`))
+    chargingStation.stationInfo.hashId = getHashId(1, chargingStationTemplate)
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError(`${baseName}-00001: Missing templateIndex in stationInfo properties`))
+    chargingStation.stationInfo.templateIndex = 0
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(
+      new BaseError(`${baseName}-00001: Invalid templateIndex value in stationInfo properties`)
+    )
+    chargingStation.stationInfo.templateIndex = 1
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`))
+    chargingStation.stationInfo.templateName = ''
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`))
+    chargingStation.stationInfo.templateName = 'test-template.json'
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(new BaseError(`${baseName}-00001: Missing maximumPower in stationInfo properties`))
+    chargingStation.stationInfo.maximumPower = 0
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(
+      new BaseError(`${baseName}-00001: Invalid maximumPower value in stationInfo properties`)
+    )
+    chargingStation.stationInfo.maximumPower = 12000
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(
+      new BaseError(`${baseName}-00001: Missing maximumAmperage in stationInfo properties`)
+    )
+    chargingStation.stationInfo.maximumAmperage = 0
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(
+      new BaseError(`${baseName}-00001: Invalid maximumAmperage value in stationInfo properties`)
+    )
+    chargingStation.stationInfo.maximumAmperage = 16
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).not.toThrow()
+    chargingStation.evses = new Map<number, EvseStatus>()
+    chargingStation.stationInfo.ocppVersion = OCPPVersion.VERSION_201
+    expect(() => {
+      validateStationInfo(chargingStation)
+    }).toThrow(
+      new BaseError(
+        `${baseName}-00001: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration`
+      )
+    )
+  })
+})