]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(tests): move mock creation from it() to beforeEach()
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 28 Feb 2026 17:05:27 +0000 (18:05 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 28 Feb 2026 17:05:27 +0000 (18:05 +0100)
- Refactor OCPPAuthServiceImpl.test.ts: declare mocks at describe level, initialize in beforeEach()
- Refactor OCPPAuthServiceFactory.test.ts: same pattern for consistency
- Refactor InMemoryAuthCache.test.ts: move common mockResult to beforeEach()

Aligns with TEST_STYLE_GUIDE.md recommendation for test isolation pattern.

tests/charging-station/ocpp/auth/cache/InMemoryAuthCache.test.ts
tests/charging-station/ocpp/auth/services/OCPPAuthServiceFactory.test.ts
tests/charging-station/ocpp/auth/services/OCPPAuthServiceImpl.test.ts

index 54ca9e4bcfa9e57b2fbf1e544b57df03e0725bdb..3b03ac3f2183ad1c0853d37dc4411cb6df8b70ea 100644 (file)
@@ -5,6 +5,8 @@
 import { expect } from '@std/expect'
 import { afterEach, beforeEach, describe, it, mock } from 'node:test'
 
+import type { AuthorizationResult } from '../../../../../src/charging-station/ocpp/auth/types/AuthTypes.js'
+
 import { InMemoryAuthCache } from '../../../../../src/charging-station/ocpp/auth/cache/InMemoryAuthCache.js'
 import {
   AuthenticationMethod,
@@ -43,11 +45,16 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.001 - Cache Hit Behavior', async () => {
-    await it('should return cached result on cache hit', async () => {
-      const identifier = 'test-token-001'
-      const mockResult = createMockAuthorizationResult({
+    let mockResult: AuthorizationResult
+
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult({
         status: AuthorizationStatus.ACCEPTED,
       })
+    })
+
+    await it('should return cached result on cache hit', async () => {
+      const identifier = 'test-token-001'
 
       // Cache the result
       await cache.set(identifier, mockResult, 60)
@@ -62,7 +69,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
 
     await it('should track cache hits in statistics', async () => {
       const identifier = 'test-token-002'
-      const mockResult = createMockAuthorizationResult()
 
       await cache.set(identifier, mockResult)
       await cache.get(identifier)
@@ -81,7 +87,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
         maxEntries: 3,
         rateLimit: { enabled: false },
       })
-      const mockResult = createMockAuthorizationResult()
 
       // Fill cache to capacity
       await lruCache.set('token-1', mockResult)
@@ -107,6 +112,12 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.002 - Cache Miss Behavior', async () => {
+    let mockResult: AuthorizationResult
+
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
+
     await it('should return undefined on cache miss', async () => {
       const result = await cache.get('non-existent-token')
 
@@ -125,8 +136,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should calculate hit rate correctly with mixed hits/misses', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // 2 sets
       await cache.set('token-1', mockResult)
       await cache.set('token-2', mockResult)
@@ -148,9 +157,14 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.003 - Cache Expiration (TTL)', async () => {
+    let mockResult: AuthorizationResult
+
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
+
     await it('should expire entries after TTL', async () => {
       const identifier = 'expiring-token'
-      const mockResult = createMockAuthorizationResult()
 
       // Set with 1ms TTL (will expire immediately)
       await cache.set(identifier, mockResult, 0.001)
@@ -164,8 +178,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should track expired entries in statistics', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // Set with very short TTL
       await cache.set('token-1', mockResult, 0.001)
       await cache.set('token-2', mockResult, 0.001)
@@ -186,7 +198,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
         defaultTtl: 0.001, // 1ms default
       })
 
-      const mockResult = createMockAuthorizationResult()
       await cacheWithShortTTL.set('token', mockResult) // No TTL specified
 
       // Wait for expiration
@@ -198,7 +209,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
 
     await it('should not expire entries before TTL', async () => {
       const identifier = 'long-lived-token'
-      const mockResult = createMockAuthorizationResult()
 
       // Set with 60 second TTL
       await cache.set(identifier, mockResult, 60)
@@ -212,9 +222,14 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.004 - Cache Invalidation', async () => {
+    let mockResult: AuthorizationResult
+
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
+
     await it('should remove entry on invalidation', async () => {
       const identifier = 'token-to-remove'
-      const mockResult = createMockAuthorizationResult()
 
       await cache.set(identifier, mockResult)
 
@@ -231,8 +246,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should clear all entries', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       await cache.set('token-1', mockResult)
       await cache.set('token-2', mockResult)
       await cache.set('token-3', mockResult)
@@ -247,8 +260,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should reset statistics on clear', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       await cache.set('token', mockResult)
       await cache.get('token')
       await cache.get('miss')
@@ -265,9 +276,14 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.005 - Rate Limiting (Security)', async () => {
+    let mockResult: AuthorizationResult
+
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
+
     await it('should block requests exceeding rate limit', async () => {
       const identifier = 'rate-limited-token'
-      const mockResult = createMockAuthorizationResult()
 
       // Make 3 requests (at limit)
       await cache.set(identifier, mockResult)
@@ -284,7 +300,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
 
     await it('should track rate limit statistics', async () => {
       const identifier = 'token'
-      const mockResult = createMockAuthorizationResult()
 
       // Exceed rate limit
       await cache.set(identifier, mockResult)
@@ -299,7 +314,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
 
     await it('should reset rate limit after window expires', async () => {
       const identifier = 'windowed-token'
-      const mockResult = createMockAuthorizationResult()
 
       // Fill rate limit
       await cache.set(identifier, mockResult)
@@ -315,8 +329,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should rate limit per identifier independently', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // Fill rate limit for token-1
       await cache.set('token-1', mockResult)
       await cache.get('token-1')
@@ -334,8 +346,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
         rateLimit: { enabled: false },
       })
 
-      const mockResult = createMockAuthorizationResult()
-
       // Make many requests without blocking
       for (let i = 0; i < 20; i++) {
         await unratedCache.set('token', mockResult)
@@ -350,9 +360,13 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.006 - LRU Eviction', async () => {
-    await it('should evict least recently used entry when full', async () => {
-      const mockResult = createMockAuthorizationResult()
+    let mockResult: AuthorizationResult
 
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
+
+    await it('should evict least recently used entry when full', async () => {
       // Fill cache to capacity (5 entries)
       await cache.set('token-1', mockResult)
       await cache.set('token-2', mockResult)
@@ -376,8 +390,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should track eviction count in statistics', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // Trigger multiple evictions
       for (let i = 1; i <= 10; i++) {
         await cache.set(`token-${String(i)}`, mockResult)
@@ -391,9 +403,13 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.007 - Statistics & Monitoring', async () => {
-    await it('should provide accurate cache statistics', async () => {
-      const mockResult = createMockAuthorizationResult()
+    let mockResult: AuthorizationResult
+
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
 
+    await it('should provide accurate cache statistics', async () => {
       await cache.set('token-1', mockResult)
       await cache.set('token-2', mockResult)
       await cache.get('token-1') // hit
@@ -409,8 +425,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should track memory usage estimate', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       const statsBefore = await cache.getStats()
       const memoryBefore = statsBefore.memoryUsage
 
@@ -426,8 +440,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should provide rate limit statistics', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // Make some rate-limited requests
       await cache.set('token', mockResult)
       await cache.set('token', mockResult)
@@ -444,9 +456,13 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
   })
 
   await describe('G03.FR.01.008 - Edge Cases', async () => {
-    await it('should handle empty identifier gracefully', async () => {
-      const mockResult = createMockAuthorizationResult()
+    let mockResult: AuthorizationResult
 
+    beforeEach(() => {
+      mockResult = createMockAuthorizationResult()
+    })
+
+    await it('should handle empty identifier gracefully', async () => {
       await cache.set('', mockResult)
       const result = await cache.get('')
 
@@ -455,7 +471,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
 
     await it('should handle very long identifier strings', async () => {
       const longIdentifier = 'x'.repeat(1000)
-      const mockResult = createMockAuthorizationResult()
 
       await cache.set(longIdentifier, mockResult)
       const result = await cache.get(longIdentifier)
@@ -464,8 +479,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should handle concurrent operations', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // Concurrent sets
       await Promise.all([
         cache.set('token-1', mockResult),
@@ -486,8 +499,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should handle zero TTL (immediate expiration)', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       await cache.set('token', mockResult, 0)
 
       // Should be immediately expired
@@ -496,8 +507,6 @@ await describe('InMemoryAuthCache - G03.FR.01 Conformance', async () => {
     })
 
     await it('should handle very large TTL values', async () => {
-      const mockResult = createMockAuthorizationResult()
-
       // 1 year TTL
       await cache.set('token', mockResult, 31536000)
 
index 0e75d64da90a3a89bd60e8873349e69bf326c540..1ffbd928f9856b324199f7fd804723caf0802faf 100644 (file)
@@ -3,7 +3,7 @@
  * @description Unit tests for OCPP authentication service factory
  */
 import { expect } from '@std/expect'
-import { afterEach, describe, it } from 'node:test'
+import { afterEach, beforeEach, describe, it } from 'node:test'
 
 import type { ChargingStation } from '../../../../../src/charging-station/ChargingStation.js'
 
@@ -18,10 +18,16 @@ await describe('OCPPAuthServiceFactory', async () => {
   })
 
   await describe('getInstance', async () => {
-    await it('should create a new instance for a charging station', async () => {
-      const mockStation = createMockAuthServiceTestStation('001')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService = await OCPPAuthServiceFactory.getInstance(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('getInstance-16')
+      mockStation20 = createMockAuthServiceTestStation('getInstance-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should create a new instance for a charging station', async () => {
+      const authService = await OCPPAuthServiceFactory.getInstance(mockStation16)
 
       expect(authService).toBeDefined()
       expect(typeof authService.authorize).toBe('function')
@@ -29,20 +35,15 @@ await describe('OCPPAuthServiceFactory', async () => {
     })
 
     await it('should return cached instance for same charging station', async () => {
-      const mockStation = createMockAuthServiceTestStation('002', OCPPVersion.VERSION_20)
-
-      const authService1 = await OCPPAuthServiceFactory.getInstance(mockStation)
-      const authService2 = await OCPPAuthServiceFactory.getInstance(mockStation)
+      const authService1 = await OCPPAuthServiceFactory.getInstance(mockStation20)
+      const authService2 = await OCPPAuthServiceFactory.getInstance(mockStation20)
 
       expect(authService1).toBe(authService2)
     })
 
     await it('should create different instances for different charging stations', async () => {
-      const mockStation1 = createMockAuthServiceTestStation('003')
-      const mockStation2 = createMockAuthServiceTestStation('004', OCPPVersion.VERSION_20)
-
-      const authService1 = await OCPPAuthServiceFactory.getInstance(mockStation1)
-      const authService2 = await OCPPAuthServiceFactory.getInstance(mockStation2)
+      const authService1 = await OCPPAuthServiceFactory.getInstance(mockStation16)
+      const authService2 = await OCPPAuthServiceFactory.getInstance(mockStation20)
 
       expect(authService1).not.toBe(authService2)
     })
@@ -65,11 +66,17 @@ await describe('OCPPAuthServiceFactory', async () => {
   })
 
   await describe('createInstance', async () => {
-    await it('should create a new uncached instance', async () => {
-      const mockStation = createMockAuthServiceTestStation('005')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService1 = await OCPPAuthServiceFactory.createInstance(mockStation)
-      const authService2 = await OCPPAuthServiceFactory.createInstance(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('createInstance-16')
+      mockStation20 = createMockAuthServiceTestStation('createInstance-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should create a new uncached instance', async () => {
+      const authService1 = await OCPPAuthServiceFactory.createInstance(mockStation16)
+      const authService2 = await OCPPAuthServiceFactory.createInstance(mockStation16)
 
       expect(authService1).toBeDefined()
       expect(authService2).toBeDefined()
@@ -77,10 +84,8 @@ await describe('OCPPAuthServiceFactory', async () => {
     })
 
     await it('should not cache created instances', async () => {
-      const mockStation = createMockAuthServiceTestStation('006', OCPPVersion.VERSION_20)
-
       const initialCount = OCPPAuthServiceFactory.getCachedInstanceCount()
-      await OCPPAuthServiceFactory.createInstance(mockStation)
+      await OCPPAuthServiceFactory.createInstance(mockStation20)
       const finalCount = OCPPAuthServiceFactory.getCachedInstanceCount()
 
       expect(finalCount).toBe(initialCount)
@@ -88,38 +93,47 @@ await describe('OCPPAuthServiceFactory', async () => {
   })
 
   await describe('clearInstance', async () => {
-    await it('should clear cached instance for a charging station', async () => {
-      const mockStation = createMockAuthServiceTestStation('007')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('clearInstance-16')
+      mockStation20 = createMockAuthServiceTestStation('clearInstance-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should clear cached instance for a charging station', async () => {
       // Create and cache instance
-      const authService1 = await OCPPAuthServiceFactory.getInstance(mockStation)
+      const authService1 = await OCPPAuthServiceFactory.getInstance(mockStation16)
 
       // Clear the cache
-      OCPPAuthServiceFactory.clearInstance(mockStation)
+      OCPPAuthServiceFactory.clearInstance(mockStation16)
 
       // Get instance again - should be a new instance
-      const authService2 = await OCPPAuthServiceFactory.getInstance(mockStation)
+      const authService2 = await OCPPAuthServiceFactory.getInstance(mockStation16)
 
       expect(authService1).not.toBe(authService2)
     })
 
     await it('should not throw when clearing non-existent instance', () => {
-      const mockStation = createMockAuthServiceTestStation('008', OCPPVersion.VERSION_20)
-
       expect(() => {
-        OCPPAuthServiceFactory.clearInstance(mockStation)
+        OCPPAuthServiceFactory.clearInstance(mockStation20)
       }).not.toThrow()
     })
   })
 
   await describe('clearAllInstances', async () => {
-    await it('should clear all cached instances', async () => {
-      const mockStation1 = createMockAuthServiceTestStation('009')
-      const mockStation2 = createMockAuthServiceTestStation('010', OCPPVersion.VERSION_20)
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('clearAll-16')
+      mockStation20 = createMockAuthServiceTestStation('clearAll-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should clear all cached instances', async () => {
       // Create multiple instances
-      await OCPPAuthServiceFactory.getInstance(mockStation1)
-      await OCPPAuthServiceFactory.getInstance(mockStation2)
+      await OCPPAuthServiceFactory.getInstance(mockStation16)
+      await OCPPAuthServiceFactory.getInstance(mockStation20)
 
       // Clear all
       OCPPAuthServiceFactory.clearAllInstances()
@@ -131,43 +145,51 @@ await describe('OCPPAuthServiceFactory', async () => {
   })
 
   await describe('getCachedInstanceCount', async () => {
-    await it('should return the number of cached instances', async () => {
-      OCPPAuthServiceFactory.clearAllInstances()
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const mockStation1 = createMockAuthServiceTestStation('011')
-      const mockStation2 = createMockAuthServiceTestStation('012', OCPPVersion.VERSION_20)
+    beforeEach(() => {
+      OCPPAuthServiceFactory.clearAllInstances()
+      mockStation16 = createMockAuthServiceTestStation('count-16')
+      mockStation20 = createMockAuthServiceTestStation('count-20', OCPPVersion.VERSION_20)
+    })
 
+    await it('should return the number of cached instances', async () => {
       expect(OCPPAuthServiceFactory.getCachedInstanceCount()).toBe(0)
 
-      await OCPPAuthServiceFactory.getInstance(mockStation1)
+      await OCPPAuthServiceFactory.getInstance(mockStation16)
       expect(OCPPAuthServiceFactory.getCachedInstanceCount()).toBe(1)
 
-      await OCPPAuthServiceFactory.getInstance(mockStation2)
+      await OCPPAuthServiceFactory.getInstance(mockStation20)
       expect(OCPPAuthServiceFactory.getCachedInstanceCount()).toBe(2)
 
       // Getting same instance should not increase count
-      await OCPPAuthServiceFactory.getInstance(mockStation1)
+      await OCPPAuthServiceFactory.getInstance(mockStation16)
       expect(OCPPAuthServiceFactory.getCachedInstanceCount()).toBe(2)
     })
   })
 
   await describe('getStatistics', async () => {
-    await it('should return factory statistics', async () => {
-      OCPPAuthServiceFactory.clearAllInstances()
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const mockStation1 = createMockAuthServiceTestStation('013')
-      const mockStation2 = createMockAuthServiceTestStation('014', OCPPVersion.VERSION_20)
+    beforeEach(() => {
+      OCPPAuthServiceFactory.clearAllInstances()
+      mockStation16 = createMockAuthServiceTestStation('stats-16')
+      mockStation20 = createMockAuthServiceTestStation('stats-20', OCPPVersion.VERSION_20)
+    })
 
-      await OCPPAuthServiceFactory.getInstance(mockStation1)
-      await OCPPAuthServiceFactory.getInstance(mockStation2)
+    await it('should return factory statistics', async () => {
+      await OCPPAuthServiceFactory.getInstance(mockStation16)
+      await OCPPAuthServiceFactory.getInstance(mockStation20)
 
       const stats = OCPPAuthServiceFactory.getStatistics()
 
       expect(stats).toBeDefined()
       expect(stats.cachedInstances).toBe(2)
       expect(stats.stationIds).toHaveLength(2)
-      expect(stats.stationIds).toContain('TEST-CS-013')
-      expect(stats.stationIds).toContain('TEST-CS-014')
+      expect(stats.stationIds).toContain('TEST-CS-stats-16')
+      expect(stats.stationIds).toContain('TEST-CS-stats-20')
     })
 
     await it('should return empty statistics when no instances cached', () => {
@@ -181,10 +203,16 @@ await describe('OCPPAuthServiceFactory', async () => {
   })
 
   await describe('OCPP version handling', async () => {
-    await it('should create service for OCPP 1.6 station', async () => {
-      const mockStation = createMockAuthServiceTestStation('015')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService = await OCPPAuthServiceFactory.getInstance(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('version-16')
+      mockStation20 = createMockAuthServiceTestStation('version-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should create service for OCPP 1.6 station', async () => {
+      const authService = await OCPPAuthServiceFactory.getInstance(mockStation16)
 
       expect(authService).toBeDefined()
       expect(typeof authService.authorize).toBe('function')
@@ -192,9 +220,7 @@ await describe('OCPPAuthServiceFactory', async () => {
     })
 
     await it('should create service for OCPP 2.0 station', async () => {
-      const mockStation = createMockAuthServiceTestStation('016', OCPPVersion.VERSION_20)
-
-      const authService = await OCPPAuthServiceFactory.getInstance(mockStation)
+      const authService = await OCPPAuthServiceFactory.getInstance(mockStation20)
 
       expect(authService).toBeDefined()
       expect(typeof authService.authorize).toBe('function')
@@ -203,16 +229,19 @@ await describe('OCPPAuthServiceFactory', async () => {
   })
 
   await describe('memory management', async () => {
-    await it('should properly manage instance lifecycle', async () => {
-      OCPPAuthServiceFactory.clearAllInstances()
+    let mockStations: ChargingStation[]
 
-      const mockStations = Array.from({ length: 5 }, (_, i) =>
+    beforeEach(() => {
+      OCPPAuthServiceFactory.clearAllInstances()
+      mockStations = Array.from({ length: 5 }, (_, i) =>
         createMockAuthServiceTestStation(
-          String(100 + i),
+          `memory-${String(i)}`,
           i % 2 === 0 ? OCPPVersion.VERSION_16 : OCPPVersion.VERSION_20
         )
       )
+    })
 
+    await it('should properly manage instance lifecycle', async () => {
       // Create instances
       for (const station of mockStations) {
         await OCPPAuthServiceFactory.getInstance(station)
index 48b1e4e2d9570700d67b493f4e884cc9ac914e41..25808b54ec6d77bed1c25c2144fd7398fe21b57e 100644 (file)
@@ -3,8 +3,9 @@
  * @description Unit tests for OCPP authentication service implementation
  */
 import { expect } from '@std/expect'
-import { afterEach, describe, it } from 'node:test'
+import { afterEach, beforeEach, describe, it } from 'node:test'
 
+import type { ChargingStation } from '../../../../../src/charging-station/ChargingStation.js'
 import type { OCPPAuthService } from '../../../../../src/charging-station/ocpp/auth/interfaces/OCPPAuthService.js'
 
 import { OCPPAuthServiceImpl } from '../../../../../src/charging-station/ocpp/auth/services/OCPPAuthServiceImpl.js'
@@ -24,10 +25,16 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('constructor', async () => {
-    await it('should initialize with OCPP 1.6 charging station', () => {
-      const mockStation = createMockAuthServiceTestStation('001')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService: OCPPAuthService = new OCPPAuthServiceImpl(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('constructor-16')
+      mockStation20 = createMockAuthServiceTestStation('constructor-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should initialize with OCPP 1.6 charging station', () => {
+      const authService: OCPPAuthService = new OCPPAuthServiceImpl(mockStation16)
 
       expect(authService).toBeDefined()
       expect(typeof authService.authorize).toBe('function')
@@ -35,18 +42,20 @@ await describe('OCPPAuthServiceImpl', async () => {
     })
 
     await it('should initialize with OCPP 2.0 charging station', () => {
-      const mockStation = createMockAuthServiceTestStation('002', OCPPVersion.VERSION_20)
-
-      const authService = new OCPPAuthServiceImpl(mockStation)
+      const authService = new OCPPAuthServiceImpl(mockStation20)
 
       expect(authService).toBeDefined()
     })
   })
 
   await describe('getConfiguration', async () => {
-    await it('should return default configuration', () => {
-      const mockStation = createMockAuthServiceTestStation('003')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('getConfig')
+    })
 
+    await it('should return default configuration', () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
       const config = authService.getConfiguration()
 
@@ -58,9 +67,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('updateConfiguration', async () => {
-    await it('should update configuration', async () => {
-      const mockStation = createMockAuthServiceTestStation('004')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('updateConfig')
+    })
 
+    await it('should update configuration', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       await authService.updateConfiguration({
@@ -75,10 +88,16 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('isSupported', async () => {
-    await it('should check if identifier type is supported for OCPP 1.6', async () => {
-      const mockStation = createMockAuthServiceTestStation('005')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService = new OCPPAuthServiceImpl(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('isSupported-16')
+      mockStation20 = createMockAuthServiceTestStation('isSupported-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should check if identifier type is supported for OCPP 1.6', async () => {
+      const authService = new OCPPAuthServiceImpl(mockStation16)
       await authService.initialize()
 
       const idTagIdentifier: UnifiedIdentifier = {
@@ -91,9 +110,7 @@ await describe('OCPPAuthServiceImpl', async () => {
     })
 
     await it('should check if identifier type is supported for OCPP 2.0', async () => {
-      const mockStation = createMockAuthServiceTestStation('006', OCPPVersion.VERSION_20)
-
-      const authService = new OCPPAuthServiceImpl(mockStation)
+      const authService = new OCPPAuthServiceImpl(mockStation20)
       await authService.initialize()
 
       const centralIdentifier: UnifiedIdentifier = {
@@ -107,9 +124,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('testConnectivity', async () => {
-    await it('should test remote connectivity', async () => {
-      const mockStation = createMockAuthServiceTestStation('007')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('connectivity')
+    })
 
+    await it('should test remote connectivity', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
       const isConnected = await authService.testConnectivity()
 
@@ -118,9 +139,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('clearCache', async () => {
-    await it('should clear authorization cache', async () => {
-      const mockStation = createMockAuthServiceTestStation('008')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('clearCache')
+    })
 
+    await it('should clear authorization cache', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       await expect(authService.clearCache()).resolves.toBeUndefined()
@@ -128,9 +153,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('invalidateCache', async () => {
-    await it('should invalidate cache for specific identifier', async () => {
-      const mockStation = createMockAuthServiceTestStation('009')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('invalidateCache')
+    })
 
+    await it('should invalidate cache for specific identifier', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       const identifier: UnifiedIdentifier = {
@@ -144,9 +173,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('getStats', async () => {
-    await it('should return authentication statistics', async () => {
-      const mockStation = createMockAuthServiceTestStation('010')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('getStats')
+    })
 
+    await it('should return authentication statistics', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
       const stats = await authService.getStats()
 
@@ -159,9 +192,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('authorize', async () => {
-    await it('should authorize identifier using strategy chain', async () => {
-      const mockStation = createMockAuthServiceTestStation('011')
+    let mockStation: ChargingStation
 
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('authorize')
+    })
+
+    await it('should authorize identifier using strategy chain', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       const identifier: UnifiedIdentifier = {
@@ -184,8 +221,6 @@ await describe('OCPPAuthServiceImpl', async () => {
     })
 
     await it('should return INVALID status when all strategies fail', async () => {
-      const mockStation = createMockAuthServiceTestStation('012')
-
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       const identifier: UnifiedIdentifier = {
@@ -208,9 +243,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('isLocallyAuthorized', async () => {
-    await it('should check local authorization', async () => {
-      const mockStation = createMockAuthServiceTestStation('013')
+    let mockStation: ChargingStation
+
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('localAuth')
+    })
 
+    await it('should check local authorization', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       const identifier: UnifiedIdentifier = {
@@ -227,10 +266,16 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('OCPP version specific behavior', async () => {
-    await it('should handle OCPP 1.6 specific identifiers', async () => {
-      const mockStation = createMockAuthServiceTestStation('014')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService = new OCPPAuthServiceImpl(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('version-16')
+      mockStation20 = createMockAuthServiceTestStation('version-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should handle OCPP 1.6 specific identifiers', async () => {
+      const authService = new OCPPAuthServiceImpl(mockStation16)
 
       const identifier: UnifiedIdentifier = {
         ocppVersion: OCPPVersion.VERSION_16,
@@ -250,9 +295,7 @@ await describe('OCPPAuthServiceImpl', async () => {
     })
 
     await it('should handle OCPP 2.0 specific identifiers', async () => {
-      const mockStation = createMockAuthServiceTestStation('015', OCPPVersion.VERSION_20)
-
-      const authService = new OCPPAuthServiceImpl(mockStation)
+      const authService = new OCPPAuthServiceImpl(mockStation20)
 
       const identifier: UnifiedIdentifier = {
         ocppVersion: OCPPVersion.VERSION_20,
@@ -273,9 +316,13 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('error handling', async () => {
-    await it('should handle invalid identifier gracefully', async () => {
-      const mockStation = createMockAuthServiceTestStation('016')
+    let mockStation: ChargingStation
 
+    beforeEach(() => {
+      mockStation = createMockAuthServiceTestStation('errorHandling')
+    })
+
+    await it('should handle invalid identifier gracefully', async () => {
       const authService = new OCPPAuthServiceImpl(mockStation)
 
       const identifier: UnifiedIdentifier = {
@@ -297,10 +344,16 @@ await describe('OCPPAuthServiceImpl', async () => {
   })
 
   await describe('authentication contexts', async () => {
-    await it('should handle TRANSACTION_START context', async () => {
-      const mockStation = createMockAuthServiceTestStation('017')
+    let mockStation16: ChargingStation
+    let mockStation20: ChargingStation
 
-      const authService = new OCPPAuthServiceImpl(mockStation)
+    beforeEach(() => {
+      mockStation16 = createMockAuthServiceTestStation('context-16')
+      mockStation20 = createMockAuthServiceTestStation('context-20', OCPPVersion.VERSION_20)
+    })
+
+    await it('should handle TRANSACTION_START context', async () => {
+      const authService = new OCPPAuthServiceImpl(mockStation16)
 
       const identifier: UnifiedIdentifier = {
         ocppVersion: OCPPVersion.VERSION_16,
@@ -321,9 +374,7 @@ await describe('OCPPAuthServiceImpl', async () => {
     })
 
     await it('should handle TRANSACTION_STOP context', async () => {
-      const mockStation = createMockAuthServiceTestStation('018')
-
-      const authService = new OCPPAuthServiceImpl(mockStation)
+      const authService = new OCPPAuthServiceImpl(mockStation16)
 
       const identifier: UnifiedIdentifier = {
         ocppVersion: OCPPVersion.VERSION_16,
@@ -344,9 +395,7 @@ await describe('OCPPAuthServiceImpl', async () => {
     })
 
     await it('should handle REMOTE_START context', async () => {
-      const mockStation = createMockAuthServiceTestStation('019', OCPPVersion.VERSION_20)
-
-      const authService = new OCPPAuthServiceImpl(mockStation)
+      const authService = new OCPPAuthServiceImpl(mockStation20)
 
       const identifier: UnifiedIdentifier = {
         ocppVersion: OCPPVersion.VERSION_20,