]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(ui-common): derive ClientConfig and AuthenticationConfig from Zod schemas
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 16 Apr 2026 20:22:56 +0000 (22:22 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 16 Apr 2026 20:22:56 +0000 (22:22 +0200)
Replace two hand-written interfaces in client/types.ts with type aliases
derived from the Zod-inferred UIServerConfigurationSection:
- AuthenticationConfig = NonNullable<UIServerConfigurationSection['authentication']>
- ClientConfig = Omit<UIServerConfigurationSection, 'name'>

This eliminates duplication: protocol/version now carry enum types (Protocol,
ProtocolVersion) instead of loose strings. Update WebSocketClient.test.ts to
use Protocol.UI and ProtocolVersion['0.0.1'] enum values accordingly.

ui/common/src/client/types.ts
ui/common/tests/WebSocketClient.test.ts

index d1b49d8c27457bee7741a6c481688b86876a9744..d37cd5e46b6372d2130e8d13823641bc07b5c4d3 100644 (file)
@@ -1,4 +1,5 @@
-import type { AuthenticationType, ResponsePayload } from '../types/UIProtocol.js'
+import type { UIServerConfigurationSection } from '../config/schema.js'
+import type { ResponsePayload } from '../types/UIProtocol.js'
 
 export const enum WebSocketReadyState {
   CONNECTING = 0,
@@ -7,21 +8,9 @@ export const enum WebSocketReadyState {
   CLOSED = 3,
 }
 
-export interface AuthenticationConfig {
-  enabled: boolean
-  password?: string
-  type: AuthenticationType
-  username?: string
-}
+export type AuthenticationConfig = NonNullable<UIServerConfigurationSection['authentication']>
 
-export interface ClientConfig {
-  authentication?: AuthenticationConfig
-  host: string
-  port: number
-  protocol: string
-  secure?: boolean
-  version: string
-}
+export type ClientConfig = Omit<UIServerConfigurationSection, 'name'>
 
 export interface ResponseHandler {
   reject: (reason?: unknown) => void
index a02339294a4f998b491a815bcd3944cdd4a90718..ddfa3bacb29545587cbb58266a3a60b5ec82ae6b 100644 (file)
@@ -7,7 +7,13 @@ import type { WebSocketFactory, WebSocketLike } from '../src/client/types.js'
 import type { ResponsePayload } from '../src/types/UIProtocol.js'
 
 import { ServerFailureError, WebSocketClient } from '../src/client/WebSocketClient.js'
-import { AuthenticationType, ProcedureName, ResponseStatus } from '../src/types/UIProtocol.js'
+import {
+  AuthenticationType,
+  ProcedureName,
+  Protocol,
+  ProtocolVersion,
+  ResponseStatus,
+} from '../src/types/UIProtocol.js'
 
 /**
  * @returns Mock WebSocket with trigger methods for testing.
@@ -85,8 +91,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -109,8 +115,8 @@ await describe('WebSocketClient', async () => {
       },
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -126,8 +132,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -154,8 +160,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -184,8 +190,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -228,8 +234,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerError('Connection refused')
@@ -247,8 +253,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -267,8 +273,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -293,9 +299,9 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'example.com',
       port: 443,
-      protocol: 'ui',
+      protocol: Protocol.UI,
       secure: true,
-      version: '0.0.1',
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -309,8 +315,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -328,8 +334,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -350,8 +356,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     // Close without opening — simulates handshake rejection
@@ -370,8 +376,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()
@@ -404,8 +410,8 @@ await describe('WebSocketClient', async () => {
       {
         host: 'localhost',
         port: 8080,
-        protocol: 'ui',
-        version: '0.0.1',
+        protocol: Protocol.UI,
+        version: ProtocolVersion['0.0.1'],
       },
       5000
     )
@@ -433,8 +439,8 @@ await describe('WebSocketClient', async () => {
       {
         host: 'localhost',
         port: 8080,
-        protocol: 'ui',
-        version: '0.0.1',
+        protocol: Protocol.UI,
+        version: ProtocolVersion['0.0.1'],
       },
       5000
     )
@@ -461,8 +467,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     setTimeout(() => {
       mockWs.triggerOpen()
@@ -485,8 +491,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     setTimeout(() => {
       mockWs.triggerOpen()
@@ -509,8 +515,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(factory, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     setTimeout(() => {
       mockWs.triggerOpen()
@@ -531,7 +537,7 @@ await describe('WebSocketClient', async () => {
     const mockWs = createMockWs()
     const client = new WebSocketClient(
       () => mockWs,
-      { host: 'localhost', port: 8080, protocol: 'ui', version: '0.0.1' },
+      { host: 'localhost', port: 8080, protocol: Protocol.UI, version: ProtocolVersion['0.0.1'] },
       undefined,
       notification => {
         notifications.push(notification)
@@ -554,7 +560,7 @@ await describe('WebSocketClient', async () => {
     const factory: WebSocketFactory = () => mockWs
     const client = new WebSocketClient(
       factory,
-      { host: 'localhost', port: 8080, protocol: 'ui', version: '0.0.1' },
+      { host: 'localhost', port: 8080, protocol: Protocol.UI, version: ProtocolVersion['0.0.1'] },
       undefined,
       notification => {
         notifications.push(notification)
@@ -578,8 +584,8 @@ await describe('WebSocketClient', async () => {
     const client = new WebSocketClient(() => mockWs, {
       host: 'localhost',
       port: 8080,
-      protocol: 'ui',
-      version: '0.0.1',
+      protocol: Protocol.UI,
+      version: ProtocolVersion['0.0.1'],
     })
     const connectPromise = client.connect()
     mockWs.triggerOpen()