]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(tests): cleanup unused exports and consolidate testable classes
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 28 Feb 2026 21:39:32 +0000 (22:39 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 28 Feb 2026 21:39:56 +0000 (22:39 +0100)
- Remove unused exports from UIServerTestUtils
- Remove unused ping()/pong() from MockWebSocket
- Remove waitForCondition from StationHelpers
- Consolidate TestableUIWebSocketServer into UIServerTestUtils
- Remove createMockOCPP20TransactionTestStation wrapper
- Add _brand to MockSharedLRUCache to fix static-only class lint

tests/charging-station/ChargingStationTestUtils.ts
tests/charging-station/helpers/StationHelpers.ts
tests/charging-station/mocks/MockCaches.ts
tests/charging-station/mocks/MockWebSocket.ts
tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts
tests/charging-station/ocpp/2.0/OCPP20TestUtils.ts
tests/charging-station/ui-server/UIServerTestUtils.ts
tests/charging-station/ui-server/UIWebSocketServer.test.ts
tests/charging-station/ui-server/ui-services/AbstractUIService.test.ts

index 4d27ced11438d6763694c63d86c12fc844045271..bfff81aef610195dd0e09986b3b12492857d884c 100644 (file)
@@ -35,7 +35,6 @@ export {
   createMockChargingStation,
   createMockChargingStationTemplate,
   resetChargingStationState,
-  waitForCondition,
 } from './helpers/StationHelpers.js'
 
 export { MockIdTagsCache, MockSharedLRUCache } from './mocks/MockCaches.js'
index e0f047b8f5e19db88df464637fdfba1d7411f55b..065439c348befe0153b571e1c7b87e371fb212ad 100644 (file)
@@ -918,26 +918,6 @@ export function resetChargingStationState (station: ChargingStation): void {
   }
 }
 
-/**
- * Wait for a condition to be true with timeout
- * @param condition - Function that returns true when condition is met
- * @param timeout - Maximum time to wait in milliseconds
- * @param interval - Check interval in milliseconds
- */
-export async function waitForCondition (
-  condition: () => boolean,
-  timeout = 1000,
-  interval = 10
-): Promise<void> {
-  const startTime = Date.now()
-  while (!condition()) {
-    if (Date.now() - startTime > timeout) {
-      throw new Error('Timeout waiting for condition')
-    }
-    await new Promise(resolve => setTimeout(resolve, interval))
-  }
-}
-
 /**
  * Determines whether EVSEs should be used based on configuration
  * @param options - Configuration options to check
index b21ed2ace52cf4718f5ad8bb2d06faa0cdbbf22f..19bf0f6f14404e40bd1933489f55a7ac70521b36 100644 (file)
@@ -1,18 +1,14 @@
 /**
  * Mock cache implementations for testing
  *
- * Provides in-memory caching without requiring Bootstrap initialization.
+ * Provides minimal singleton mock caches for test isolation.
+ * These mocks implement only getInstance/resetInstance for singleton management.
  */
 
-import type {
-  ChargingStationConfiguration,
-  ChargingStationTemplate,
-} from '../../../src/types/index.js'
-
 /**
  * Mock IdTagsCache for testing
  *
- * Provides mock RFID tag management without file system access.
+ * Minimal singleton mock for RFID tag cache.
  */
 export class MockIdTagsCache {
   private static instance: MockIdTagsCache | null = null
@@ -27,18 +23,6 @@ export class MockIdTagsCache {
     MockIdTagsCache.instance = null
   }
 
-  public clear (): void {
-    this.idTagsMap.clear()
-  }
-
-  public deleteIdTags (file: string): boolean {
-    return this.idTagsMap.delete(file)
-  }
-
-  public getIdTag (): string {
-    return 'TEST-TAG-001'
-  }
-
   public getIdTags (file: string): string[] | undefined {
     return this.idTagsMap.get(file)
   }
@@ -51,12 +35,11 @@ export class MockIdTagsCache {
 /**
  * Mock SharedLRUCache for testing
  *
- * Provides in-memory caching without requiring Bootstrap initialization.
+ * Minimal singleton mock for shared LRU cache.
  */
 export class MockSharedLRUCache {
   private static instance: MockSharedLRUCache | null = null
-  private readonly configurations = new Map<string, ChargingStationConfiguration>()
-  private readonly templates = new Map<string, ChargingStationTemplate>()
+  private readonly _brand = 'MockSharedLRUCache' as const
 
   public static getInstance (): MockSharedLRUCache {
     MockSharedLRUCache.instance ??= new MockSharedLRUCache()
@@ -66,45 +49,4 @@ export class MockSharedLRUCache {
   public static resetInstance (): void {
     MockSharedLRUCache.instance = null
   }
-
-  public clear (): void {
-    this.templates.clear()
-    this.configurations.clear()
-  }
-
-  public deleteChargingStationConfiguration (hash: string): void {
-    this.configurations.delete(hash)
-  }
-
-  public deleteChargingStationTemplate (hash: string): void {
-    this.templates.delete(hash)
-  }
-
-  public getChargingStationConfiguration (hash: string): ChargingStationConfiguration | undefined {
-    return this.configurations.get(hash)
-  }
-
-  public getChargingStationTemplate (hash: string): ChargingStationTemplate | undefined {
-    return this.templates.get(hash)
-  }
-
-  public hasChargingStationConfiguration (hash: string): boolean {
-    return this.configurations.has(hash)
-  }
-
-  public hasChargingStationTemplate (hash: string): boolean {
-    return this.templates.has(hash)
-  }
-
-  public setChargingStationConfiguration (config: ChargingStationConfiguration): void {
-    if (config.configurationHash != null) {
-      this.configurations.set(config.configurationHash, config)
-    }
-  }
-
-  public setChargingStationTemplate (template: ChargingStationTemplate): void {
-    if (template.templateHash != null) {
-      this.templates.set(template.templateHash, template)
-    }
-  }
 }
index 04e75bc74317a673eec2b8f7768f8c41f2e3720e..2420023bd6677b72cc61bc5c631b52c5b31d1175 100644 (file)
@@ -98,20 +98,6 @@ export class MockWebSocket extends EventEmitter {
     return this.sentMessages.map(msg => JSON.parse(msg) as unknown)
   }
 
-  /**
-   * Ping the server (no-op in mock)
-   */
-  public ping (): void {
-    // No-op for tests
-  }
-
-  /**
-   * Pong response (no-op in mock)
-   */
-  public pong (): void {
-    // No-op for tests
-  }
-
   /**
    * Send a message through the WebSocket
    * @param data - Message to send
index fa3fa3f67fafcccb71d33602a6208152054cb85c..58c7acda09102509baf04c1b8815f34d6fa31716 100644 (file)
@@ -36,14 +36,12 @@ import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConsta
 import { createMockChargingStation } from '../../ChargingStationTestUtils.js'
 import {
   type CapturedOCPPRequest,
-  createMockOCPP20TransactionTestStation,
   createMockStationWithRequestTracking,
   type MockStationWithTracking,
   resetConnectorTransactionState,
   resetLimits,
   TransactionContextFixtures,
 } from './OCPP20TestUtils.js'
-
 // ============================================================================
 // Transaction Flow Patterns for Parameterized Testing
 // ============================================================================
@@ -80,10 +78,24 @@ const TRANSACTION_FLOWS = [
 ] as const
 
 await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () => {
-  let mockChargingStation: ReturnType<typeof createMockOCPP20TransactionTestStation>
+  let mockChargingStation: ChargingStation
 
   beforeEach(() => {
-    mockChargingStation = createMockOCPP20TransactionTestStation()
+    const { station } = createMockChargingStation({
+      baseName: TEST_CHARGING_STATION_BASE_NAME,
+      connectorsCount: 3,
+      evseConfiguration: { evsesCount: 3 },
+      heartbeatInterval: Constants.DEFAULT_HEARTBEAT_INTERVAL,
+      ocppRequestService: {
+        requestHandler: async () => Promise.resolve({} as EmptyObject),
+      },
+      stationInfo: {
+        ocppStrictCompliance: true,
+        ocppVersion: OCPPVersion.VERSION_201,
+      },
+      websocketPingInterval: Constants.DEFAULT_WEBSOCKET_PING_INTERVAL,
+    })
+    mockChargingStation = station
     resetLimits(mockChargingStation)
   })
 
@@ -91,7 +103,6 @@ await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async ()
   afterEach(() => {
     standardCleanup()
   })
-
   // FR: E01.FR.01 - TransactionEventRequest structure validation
   await describe('buildTransactionEvent', async () => {
     await it('should build valid TransactionEvent Started with sequence number 0', () => {
index a2654cf6cc94ec883bcf49460801f4ef7cbc32fa..9ce6b990c179c1d0137afb83dc15f95ed8304aff 100644 (file)
@@ -72,29 +72,6 @@ export interface TestableOCPP20RequestService {
   ) => JsonType
 }
 
-/**
- * Create a mock ChargingStation for OCPP 2.0 transaction event testing.
- * Provides standard configuration used across all transaction event test files.
- * @returns ChargingStation configured for OCPP 2.0 transaction testing
- */
-export function createMockOCPP20TransactionTestStation (): ChargingStation {
-  const { station } = createMockChargingStation({
-    baseName: TEST_CHARGING_STATION_BASE_NAME,
-    connectorsCount: 3,
-    evseConfiguration: { evsesCount: 3 },
-    heartbeatInterval: Constants.DEFAULT_HEARTBEAT_INTERVAL,
-    ocppRequestService: {
-      requestHandler: async () => Promise.resolve({} as EmptyObject),
-    },
-    stationInfo: {
-      ocppStrictCompliance: true,
-      ocppVersion: OCPPVersion.VERSION_201,
-    },
-    websocketPingInterval: Constants.DEFAULT_WEBSOCKET_PING_INTERVAL,
-  })
-  return station
-}
-
 /**
  * Create a mock ChargingStation with request tracking for testing OCPP request flows.
  * This is useful for tests that need to verify what requests were sent.
index f22dddf6890aa6fc276fe4431d5521ca1331bf01..bbe82d0fe657fa8599dad40b3d227cff0159150d 100644 (file)
@@ -13,15 +13,62 @@ import type {
   UUIDv4,
 } from '../../../src/types/index.js'
 
+import { UIWebSocketServer } from '../../../src/charging-station/ui-server/UIWebSocketServer.js'
 import {
   ApplicationProtocol,
   ApplicationProtocolVersion,
   AuthenticationType,
   ProcedureName,
+  ProtocolVersion,
   ResponseStatus,
 } from '../../../src/types/index.js'
 import { MockWebSocket } from '../mocks/MockWebSocket.js'
 
+/**
+ * Testable UIWebSocketServer that exposes protected members for testing.
+ * Consolidates TestableUIWebSocketServer from UIWebSocketServer.test.ts and AbstractUIService.test.ts.
+ */
+export class TestableUIWebSocketServer extends UIWebSocketServer {
+  /**
+   * Add a response handler for testing
+   * @param uuid
+   * @param ws
+   */
+  public addResponseHandler (uuid: UUIDv4, ws: MockWebSocket): void {
+    this.responseHandlers.set(uuid, ws as never)
+  }
+
+  /** Get the size of response handlers map */
+  public getResponseHandlersSize (): number {
+    return this.responseHandlers.size
+  }
+
+  /**
+   * Get UI service by version
+   * @param version
+   */
+  public getUIService (version: ProtocolVersion) {
+    return this.uiServices.get(version)
+  }
+
+  /**
+   * Register a mock UI service for testing
+   * @param version
+   * @param service
+   */
+  public registerMockUIService (version: string, service: unknown): void {
+    this.uiServices.set(version as never, service as never)
+  }
+
+  /**
+   * Test helper to register protocol version UI service
+   * @param version
+   */
+  public testRegisterProtocolVersionUIService (version: ProtocolVersion): void {
+    this.registerProtocolVersionUIService(version)
+  }
+}
+
 /**
  * Create a MockWebSocket configured for UI protocol testing.
  * @param protocol - UI protocol version (default: 'ui0.0.1')
@@ -142,35 +189,6 @@ export const createProtocolRequest = (
   return [uuid, procedureName, payload]
 }
 
-export const createValidAuthorizeRequest = (uuid: UUIDv4, hashId: string): string => {
-  return JSON.stringify(
-    createProtocolRequest(uuid, ProcedureName.AUTHORIZE, {
-      hashIds: [hashId],
-      idTag: 'test-id-tag',
-    })
-  )
-}
-
-export const createValidListRequest = (uuid: UUIDv4): string => {
-  return JSON.stringify(createProtocolRequest(uuid, ProcedureName.LIST_CHARGING_STATIONS, {}))
-}
-
-export const createInvalidRequest = (): string => {
-  return '{"invalid": "json"'
-}
-
-export const createMalformedRequest = (): string => {
-  return JSON.stringify({ not: 'an array' })
-}
-
-export const createMockBroadcastResponse = (
-  uuid: string,
-  hashId: string,
-  status: ResponseStatus = ResponseStatus.SUCCESS
-): [string, { hashId: string; status: ResponseStatus }] => {
-  return [uuid, { hashId, status }]
-}
-
 /**
  * Mock UI service behavior mode for testing different request handler scenarios.
  */
index 05d71e6b7b3c949be9e158a11daf12de73233907..a0afa68d3c3139919c6dde68ae560ab828431baf 100644 (file)
@@ -9,31 +9,16 @@ import { afterEach, describe, it, mock } from 'node:test'
 
 import type { UUIDv4 } from '../../../src/types/index.js'
 
-import { UIWebSocketServer } from '../../../src/charging-station/ui-server/UIWebSocketServer.js'
 import { ProcedureName, ResponseStatus } from '../../../src/types/index.js'
-import { MockWebSocket } from '../mocks/MockWebSocket.js'
 import { TEST_UUID } from './UIServerTestConstants.js'
 import {
   createMockUIServerConfiguration,
   createMockUIService,
   createMockUIWebSocket,
   MockUIServiceMode,
+  TestableUIWebSocketServer,
 } from './UIServerTestUtils.js'
 
-class TestableUIWebSocketServer extends UIWebSocketServer {
-  public addResponseHandler (uuid: UUIDv4, ws: MockWebSocket): void {
-    this.responseHandlers.set(uuid, ws as never)
-  }
-
-  public getResponseHandlersSize (): number {
-    return this.responseHandlers.size
-  }
-
-  public registerMockUIService (version: string, service: unknown): void {
-    this.uiServices.set(version as never, service as never)
-  }
-}
-
 await describe('UIWebSocketServer test suite', async () => {
   afterEach(() => {
     mock.restoreAll()
@@ -181,7 +166,7 @@ await describe('UIWebSocketServer test suite', async () => {
 
   await it('should create server with valid WebSocket configuration', () => {
     const config = createMockUIServerConfiguration()
-    const server = new UIWebSocketServer(config)
+    const server = new TestableUIWebSocketServer(config)
 
     expect(server).toBeDefined()
   })
@@ -194,7 +179,7 @@ await describe('UIWebSocketServer test suite', async () => {
       },
     })
 
-    const server = new UIWebSocketServer(config)
+    const server = new TestableUIWebSocketServer(config)
     expect(server).toBeDefined()
   })
 })
index a1eadcd02f84af8565c162e60651d1d38fc978a1..5531054b0ec4500aaaeb6525fe0f9f5750ee8d25 100644 (file)
@@ -7,25 +7,15 @@
 import { expect } from '@std/expect'
 import { afterEach, describe, it, mock } from 'node:test'
 
-import { UIWebSocketServer } from '../../../../src/charging-station/ui-server/UIWebSocketServer.js'
 import { ProcedureName, ProtocolVersion, ResponseStatus } from '../../../../src/types/index.js'
 import { TEST_HASH_ID, TEST_UUID } from '../UIServerTestConstants.js'
 import {
   createMockChargingStationData,
   createMockUIServerConfiguration,
   createProtocolRequest,
+  TestableUIWebSocketServer,
 } from '../UIServerTestUtils.js'
 
-class TestableUIWebSocketServer extends UIWebSocketServer {
-  public getUIService (version: ProtocolVersion) {
-    return this.uiServices.get(version)
-  }
-
-  public testRegisterProtocolVersionUIService (version: ProtocolVersion): void {
-    this.registerProtocolVersionUIService(version)
-  }
-}
-
 await describe('AbstractUIService test suite', async () => {
   afterEach(() => {
     mock.restoreAll()