/** * WebSocket Transport Implementation Tests * * Tests WebSocket-specific implementation details, error scenarios, and mock factory behavior. * Generic transport behaviors are covered by contract tests. */ import { describe, it, expect } from '@codeforbreakfast/bun-test-effect'; import { Duration, Effect, pipe, Stream, Layer, Option } from 'effect'; import * as Socket from '@effect/platform/Socket'; import { ConnectionError, makeTransportMessage } from '@codeforbreakfast/eventsourcing-transport'; import { WebSocketConnector } from './websocket-transport'; // ============================================================================= // Test Socket Implementation // ============================================================================= interface TestSocketConfig { readonly shouldFailConnection?: boolean; readonly shouldFailBeforeOpen?: boolean; readonly shouldCloseBeforeOpen?: boolean; readonly openDelay?: number; readonly preloadedMessages?: readonly Uint8Array[]; readonly shouldFailOnWrite?: boolean; } // WebSocket state constants const WebSocketState = { CONNECTING: 0, OPEN: 1, CLOSING: 2, CLOSED: 3, } as const; // Mock WebSocket factory that behaves like a real WebSocket for Socket.makeWebSocket const createMockWebSocket = (url: string, _protocols?: string | readonly string[]) => (config: TestSocketConfig = {}): Readonly => { // Mutable state for the mock (contained within closure) let readyState: number = WebSocketState.CONNECTING; let closedEarly = false; const eventListeners = new Map void)[]>(); const dispatchEvent = (event: unknown): boolean => { const listeners = eventListeners.get((event as { readonly type: string }).type); if (listeners) { listeners.forEach((listener) => { try { listener(event); } catch { // Silently ignore listener errors in test mock to prevent one failing listener // from breaking others. Real WebSocket implementations handle this internally. } }); } return true; }; const simulateConnection = () => { if (config.shouldFailConnection || config.shouldFailBeforeOpen) { dispatchEvent({ type: 'error', error: new Error('Connection failed') }); readyState = WebSocketState.CLOSED; } else if (config.shouldCloseBeforeOpen) { dispatchEvent({ type: 'close', code: 1006, reason: 'Connection refused' }); readyState = WebSocketState.CLOSED; } else { const openDelay = config.openDelay ?? 0; pipe(openDelay, Duration.millis, Effect.sleep, Effect.runPromise).then(() => { if (!closedEarly) { readyState = WebSocketState.OPEN; dispatchEvent({ type: 'open' }); if (config.preloadedMessages) { pipe(100, Duration.millis, Effect.sleep, Effect.runPromise).then(() => { if (!closedEarly) { config.preloadedMessages!.forEach((message) => { dispatchEvent({ type: 'message', data: message }); }); } }); } } }); } }; // Start connection simulation with delay to allow 'connecting' state to be observed pipe(50, Duration.millis, Effect.sleep, Effect.runPromise).then(() => { if (!closedEarly) { simulateConnection(); } }); // Return WebSocket-compatible object return { url, protocol: '', get readyState() { return readyState; }, bufferedAmount: 0, extensions: '', binaryType: 'blob' as 'blob' | 'arraybuffer', // WebSocket constants CONNECTING: WebSocketState.CONNECTING, OPEN: WebSocketState.OPEN, CLOSING: WebSocketState.CLOSING, CLOSED: WebSocketState.CLOSED, addEventListener(type: string, listener: (event: unknown) => void) { const currentListeners = eventListeners.get(type) ?? []; eventListeners.set(type, [...currentListeners, listener]); }, removeEventListener(type: string, listener: (event: unknown) => void) { const listeners = eventListeners.get(type); if (listeners) { const filteredListeners = listeners.filter((l) => l !== listener); eventListeners.set(type, filteredListeners); } }, dispatchEvent, send(_data: string | ArrayBuffer | Blob | ArrayBufferView) { if (readyState !== WebSocketState.OPEN) { throw new Error('WebSocket is not open'); } if (config.shouldFailOnWrite) { throw new Error('Send failed'); } // In a real implementation, this would send data to the server }, close(code?: number, reason?: string) { closedEarly = true; readyState = WebSocketState.CLOSED; dispatchEvent({ type: 'close', code: code ?? 1000, reason: reason ?? '' }); }, // Event handler properties onopen: null, onclose: null, onmessage: null, onerror: null, } as globalThis.WebSocket; }; const applyConfigToMockWebSocket = (config: TestSocketConfig) => (mockFactory: (config: TestSocketConfig) => Readonly) => mockFactory(config); const createTestWebSocketConstructor = (config: TestSocketConfig) => (url: string, protocols?: string | readonly string[]): Readonly => pipe(createMockWebSocket(url, protocols), applyConfigToMockWebSocket(config)); const createTestSocketLayer = (config: TestSocketConfig = {}) => Layer.succeed(Socket.WebSocketConstructor, createTestWebSocketConstructor(config)); // ============================================================================= // Mock WebSocket Factory Tests // ============================================================================= const waitForConnectedState = (transport: { readonly connectionState: Stream.Stream; }) => pipe( transport.connectionState, Stream.filter((state) => state === 'connected'), Stream.take(1), Stream.runDrain ); describe('WebSocket Transport - Mock Factory', () => { it.scoped('should create mock WebSocket with proper state transitions', () => pipe( 'ws://test.example.com', WebSocketConnector.connect, Effect.provide(createTestSocketLayer({ openDelay: 100 })), Effect.flatMap(waitForConnectedState) ) ); }); // ============================================================================= // Error Scenario Tests // ============================================================================= describe('WebSocket Transport - Error Scenarios', () => { it.scoped('should fail when WebSocket connection fails', () => pipe( 'ws://test.example.com', WebSocketConnector.connect, Effect.provide(createTestSocketLayer({ shouldFailConnection: true })), Effect.flip, Effect.map((error) => { expect(error).toBeInstanceOf(ConnectionError); expect(error.message).toContain('WebSocket connection failed'); }) ) ); it.scoped('should handle WebSocket error before open', () => pipe( 'ws://test.example.com', WebSocketConnector.connect, Effect.provide(createTestSocketLayer({ shouldFailBeforeOpen: true })), Effect.flip, Effect.map((error) => { expect(error).toBeInstanceOf(ConnectionError); expect(error.message).toContain('WebSocket connection failed'); }) ) ); it.scoped('should handle WebSocket close before open (connection refused)', () => pipe( 'ws://test.example.com', WebSocketConnector.connect, Effect.provide(createTestSocketLayer({ shouldCloseBeforeOpen: true })), Effect.flip, Effect.map((error) => { expect(error).toBeInstanceOf(ConnectionError); expect(error.message).toContain('WebSocket connection failed'); }) ) ); it.scoped('should handle connection with very long delay', () => pipe( 'ws://test.example.com', WebSocketConnector.connect, Effect.provide(createTestSocketLayer({ openDelay: 2000 })), Effect.flatMap(waitForConnectedState) ) ); }); // ============================================================================= // WebSocket-Specific Message Handling Tests // ============================================================================= const extractMessageId = (msg: unknown): string => typeof msg === 'object' && msg !== null && 'id' in msg && typeof msg.id === 'string' ? msg.id : (() => { throw new Error('Invalid message structure'); })(); const takeFirstMessage = (subscription: Stream.Stream) => pipe( subscription, Stream.take(1), Stream.runHead, Effect.map((message) => { expect(Option.isSome(message)).toBe(true); if (Option.isSome(message)) { expect(extractMessageId(message.value)).toBe('valid-id'); } }) ); const subscribeAndTakeFirst = (transport: { readonly subscribe: () => Effect.Effect, E, R>; }) => pipe(transport, (t) => t.subscribe(), Effect.flatMap(takeFirstMessage)); describe('WebSocket Transport - WebSocket-Specific Behavior', () => { it.scoped('should handle malformed JSON messages (WebSocket binary data)', () => pipe( 'ws://test.example.com', WebSocketConnector.connect, Effect.provide( createTestSocketLayer({ preloadedMessages: [ new TextEncoder().encode('invalid json'), new TextEncoder().encode( JSON.stringify(makeTransportMessage('valid-id', 'test-type', '{"data":"test"}')) ), ], }) ), Effect.flatMap(subscribeAndTakeFirst) ) ); }); // ============================================================================= // Edge Case Tests // ============================================================================= describe('WebSocket Transport - Edge Cases', () => { it.scoped('should handle connection to non-existent server', () => pipe( // Try to connect to a port that's very unlikely to be in use 'ws://localhost:59999', WebSocketConnector.connect, Effect.map((transport) => { // With Socket abstraction, connection might succeed initially // but fail when actually trying to use the connection // This is valid according to the Transport interface expect(transport).toBeDefined(); expect(transport.connectionState).toBeDefined(); expect(transport.publish).toBeDefined(); expect(transport.subscribe).toBeDefined(); }), Effect.catchAll((error) => { // Or it might fail immediately - both are valid expect(error.message).toContain('failed'); return Effect.void; }) ) ); it.skip('should handle invalid protocol URL - skipped due to test runner timeout issues', () => { // Invalid URL - Socket abstraction times out after 3 seconds // but test runner times out before that can complete // This test is skipped due to timeout issues with the test runner // When enabled, would test: WebSocketConnector.connect('not-a-websocket-url') }); });