Nessuna descrizione

Helpers.test.ts 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* eslint-disable @typescript-eslint/no-unsafe-member-access */
  2. import { expect } from '@std/expect'
  3. import { describe, it } from 'node:test'
  4. import type { ChargingStation } from '../../src/charging-station/index.js'
  5. import {
  6. checkChargingStationState,
  7. checkConfiguration,
  8. checkStationInfoConnectorStatus,
  9. checkTemplate,
  10. getChargingStationId,
  11. getHashId,
  12. getMaxNumberOfEvses,
  13. getPhaseRotationValue,
  14. validateStationInfo,
  15. } from '../../src/charging-station/Helpers.js'
  16. import { BaseError } from '../../src/exception/index.js'
  17. import {
  18. type ChargingStationConfiguration,
  19. type ChargingStationInfo,
  20. type ChargingStationTemplate,
  21. type ConnectorStatus,
  22. ConnectorStatusEnum,
  23. type EvseStatus,
  24. OCPPVersion,
  25. } from '../../src/types/index.js'
  26. import { logger } from '../../src/utils/Logger.js'
  27. await describe('Helpers test suite', async () => {
  28. const baseName = 'CS-TEST'
  29. const chargingStationTemplate = {
  30. baseName,
  31. } as ChargingStationTemplate
  32. const chargingStation = {
  33. connectors: new Map<number, ConnectorStatus>(),
  34. evses: new Map<number, EvseStatus>(),
  35. logPrefix: () => `${baseName} |`,
  36. started: false,
  37. } as ChargingStation
  38. await it('Verify getChargingStationId()', () => {
  39. expect(getChargingStationId(1, chargingStationTemplate)).toBe(`${baseName}-00001`)
  40. })
  41. await it('Verify getHashId()', () => {
  42. expect(getHashId(1, chargingStationTemplate)).toBe(
  43. 'b4b1e8ec4fca79091d99ea9a7ea5901548010e6c0e98be9296f604b9d68734444dfdae73d7d406b6124b42815214d088'
  44. )
  45. })
  46. await it('Verify validateStationInfo()', () => {
  47. expect(() => {
  48. validateStationInfo(chargingStation)
  49. }).toThrow(new BaseError('Missing charging station information'))
  50. chargingStation.stationInfo = {} as ChargingStationInfo
  51. expect(() => {
  52. validateStationInfo(chargingStation)
  53. }).toThrow(new BaseError('Missing charging station information'))
  54. chargingStation.stationInfo.baseName = baseName
  55. expect(() => {
  56. validateStationInfo(chargingStation)
  57. }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties'))
  58. chargingStation.stationInfo.chargingStationId = ''
  59. expect(() => {
  60. validateStationInfo(chargingStation)
  61. }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties'))
  62. chargingStation.stationInfo.chargingStationId = getChargingStationId(1, chargingStationTemplate)
  63. expect(() => {
  64. validateStationInfo(chargingStation)
  65. }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`))
  66. chargingStation.stationInfo.hashId = ''
  67. expect(() => {
  68. validateStationInfo(chargingStation)
  69. }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`))
  70. chargingStation.stationInfo.hashId = getHashId(1, chargingStationTemplate)
  71. expect(() => {
  72. validateStationInfo(chargingStation)
  73. }).toThrow(new BaseError(`${baseName}-00001: Missing templateIndex in stationInfo properties`))
  74. chargingStation.stationInfo.templateIndex = 0
  75. expect(() => {
  76. validateStationInfo(chargingStation)
  77. }).toThrow(
  78. new BaseError(`${baseName}-00001: Invalid templateIndex value in stationInfo properties`)
  79. )
  80. chargingStation.stationInfo.templateIndex = 1
  81. expect(() => {
  82. validateStationInfo(chargingStation)
  83. }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`))
  84. chargingStation.stationInfo.templateName = ''
  85. expect(() => {
  86. validateStationInfo(chargingStation)
  87. }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`))
  88. chargingStation.stationInfo.templateName = 'test-template.json'
  89. expect(() => {
  90. validateStationInfo(chargingStation)
  91. }).toThrow(new BaseError(`${baseName}-00001: Missing maximumPower in stationInfo properties`))
  92. chargingStation.stationInfo.maximumPower = 0
  93. expect(() => {
  94. validateStationInfo(chargingStation)
  95. }).toThrow(
  96. new RangeError(`${baseName}-00001: Invalid maximumPower value in stationInfo properties`)
  97. )
  98. chargingStation.stationInfo.maximumPower = 12000
  99. expect(() => {
  100. validateStationInfo(chargingStation)
  101. }).toThrow(
  102. new BaseError(`${baseName}-00001: Missing maximumAmperage in stationInfo properties`)
  103. )
  104. chargingStation.stationInfo.maximumAmperage = 0
  105. expect(() => {
  106. validateStationInfo(chargingStation)
  107. }).toThrow(
  108. new RangeError(`${baseName}-00001: Invalid maximumAmperage value in stationInfo properties`)
  109. )
  110. chargingStation.stationInfo.maximumAmperage = 16
  111. expect(() => {
  112. validateStationInfo(chargingStation)
  113. }).not.toThrow()
  114. chargingStation.stationInfo.ocppVersion = OCPPVersion.VERSION_20
  115. expect(() => {
  116. validateStationInfo(chargingStation)
  117. }).toThrow(
  118. new BaseError(
  119. `${baseName}-00001: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration`
  120. )
  121. )
  122. chargingStation.stationInfo.ocppVersion = OCPPVersion.VERSION_201
  123. expect(() => {
  124. validateStationInfo(chargingStation)
  125. }).toThrow(
  126. new BaseError(
  127. `${baseName}-00001: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration`
  128. )
  129. )
  130. })
  131. await it('Verify checkChargingStationState()', t => {
  132. t.mock.method(logger, 'warn')
  133. expect(checkChargingStationState(chargingStation, 'log prefix |')).toBe(false)
  134. expect(logger.warn.mock.calls.length).toBe(1)
  135. chargingStation.starting = true
  136. expect(checkChargingStationState(chargingStation, 'log prefix |')).toBe(true)
  137. expect(logger.warn.mock.calls.length).toBe(1)
  138. chargingStation.started = true
  139. expect(checkChargingStationState(chargingStation, 'log prefix |')).toBe(true)
  140. expect(logger.warn.mock.calls.length).toBe(1)
  141. })
  142. await it('Verify getPhaseRotationValue()', () => {
  143. expect(getPhaseRotationValue(0, 0)).toBe('0.RST')
  144. expect(getPhaseRotationValue(1, 0)).toBe('1.NotApplicable')
  145. expect(getPhaseRotationValue(2, 0)).toBe('2.NotApplicable')
  146. expect(getPhaseRotationValue(0, 1)).toBe('0.NotApplicable')
  147. expect(getPhaseRotationValue(1, 1)).toBe('1.NotApplicable')
  148. expect(getPhaseRotationValue(2, 1)).toBe('2.NotApplicable')
  149. expect(getPhaseRotationValue(0, 2)).toBeUndefined()
  150. expect(getPhaseRotationValue(1, 2)).toBeUndefined()
  151. expect(getPhaseRotationValue(2, 2)).toBeUndefined()
  152. expect(getPhaseRotationValue(0, 3)).toBe('0.RST')
  153. expect(getPhaseRotationValue(1, 3)).toBe('1.RST')
  154. expect(getPhaseRotationValue(2, 3)).toBe('2.RST')
  155. })
  156. await it('Verify getMaxNumberOfEvses()', () => {
  157. expect(getMaxNumberOfEvses(undefined)).toBe(-1)
  158. expect(getMaxNumberOfEvses({})).toBe(0)
  159. })
  160. await it('Verify checkTemplate()', t => {
  161. t.mock.method(logger, 'warn')
  162. t.mock.method(logger, 'error')
  163. expect(() => {
  164. checkTemplate(undefined, 'log prefix |', 'test-template.json')
  165. }).toThrow(new BaseError('Failed to read charging station template file test-template.json'))
  166. expect(logger.error.mock.calls.length).toBe(1)
  167. expect(() => {
  168. checkTemplate({} as ChargingStationTemplate, 'log prefix |', 'test-template.json')
  169. }).toThrow(
  170. new BaseError('Empty charging station information from template file test-template.json')
  171. )
  172. expect(logger.error.mock.calls.length).toBe(2)
  173. checkTemplate(chargingStationTemplate, 'log prefix |', 'test-template.json')
  174. expect(logger.warn.mock.calls.length).toBe(1)
  175. })
  176. await it('Verify checkConfiguration()', t => {
  177. t.mock.method(logger, 'error')
  178. expect(() => {
  179. checkConfiguration(undefined, 'log prefix |', 'configuration.json')
  180. }).toThrow(
  181. new BaseError('Failed to read charging station configuration file configuration.json')
  182. )
  183. expect(logger.error.mock.calls.length).toBe(1)
  184. expect(() => {
  185. checkConfiguration({} as ChargingStationConfiguration, 'log prefix |', 'configuration.json')
  186. }).toThrow(new BaseError('Empty charging station configuration from file configuration.json'))
  187. expect(logger.error.mock.calls.length).toBe(2)
  188. })
  189. await it('Verify checkStationInfoConnectorStatus()', t => {
  190. t.mock.method(logger, 'warn')
  191. checkStationInfoConnectorStatus(1, {} as ConnectorStatus, 'log prefix |', 'test-template.json')
  192. expect(logger.warn.mock.calls.length).toBe(0)
  193. const connectorStatus = { status: ConnectorStatusEnum.Available } as ConnectorStatus
  194. checkStationInfoConnectorStatus(1, connectorStatus, 'log prefix |', 'test-template.json')
  195. expect(logger.warn.mock.calls.length).toBe(1)
  196. expect(connectorStatus.status).toBeUndefined()
  197. })
  198. })