From: Jérôme Benoit Date: Thu, 16 Apr 2026 20:22:56 +0000 (+0200) Subject: refactor(ui-common): derive ClientConfig and AuthenticationConfig from Zod schemas X-Git-Tag: v4.5~61 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2d81b7ea4c293c833f87d56e05e24023d46efbd1;p=e-mobility-charging-stations-simulator.git refactor(ui-common): derive ClientConfig and AuthenticationConfig from Zod schemas Replace two hand-written interfaces in client/types.ts with type aliases derived from the Zod-inferred UIServerConfigurationSection: - AuthenticationConfig = NonNullable - ClientConfig = Omit 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. --- diff --git a/ui/common/src/client/types.ts b/ui/common/src/client/types.ts index d1b49d8c..d37cd5e4 100644 --- a/ui/common/src/client/types.ts +++ b/ui/common/src/client/types.ts @@ -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 -export interface ClientConfig { - authentication?: AuthenticationConfig - host: string - port: number - protocol: string - secure?: boolean - version: string -} +export type ClientConfig = Omit export interface ResponseHandler { reject: (reason?: unknown) => void diff --git a/ui/common/tests/WebSocketClient.test.ts b/ui/common/tests/WebSocketClient.test.ts index a0233929..ddfa3bac 100644 --- a/ui/common/tests/WebSocketClient.test.ts +++ b/ui/common/tests/WebSocketClient.test.ts @@ -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()