leptos-sync-core 0.9.0

Core synchronization library for Leptos applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Test-Driven Development tests for real WebSocket integration with leptos-ws-pro
//! 
//! These tests define the expected behavior for actual WebSocket functionality
//! using leptos-ws-pro APIs before implementation.

use super::{SyncTransport, TransportError};
use crate::transport::leptos_ws_pro_transport::{LeptosWsProTransport, LeptosWsProConfig};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tokio::time::timeout;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct RealTimeMessage {
    id: String,
    content: String,
    timestamp: u64,
    message_type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct SyncOperation {
    operation: String,
    data: serde_json::Value,
    client_id: String,
    sequence: u64,
}

/// Test configuration for real WebSocket integration
#[derive(Debug, Clone)]
pub struct RealWebSocketTestConfig {
    pub url: String,
    pub timeout: Duration,
    pub max_reconnect_attempts: usize,
    pub heartbeat_interval: Duration,
    pub connection_timeout: Duration,
    pub retry_delay: Duration,
    pub message_timeout: Duration,
}

impl Default for RealWebSocketTestConfig {
    fn default() -> Self {
        Self {
            url: "ws://localhost:8080".to_string(),
            timeout: Duration::from_secs(30),
            max_reconnect_attempts: 5,
            heartbeat_interval: Duration::from_secs(30),
            connection_timeout: Duration::from_secs(10),
            retry_delay: Duration::from_millis(1000),
            message_timeout: Duration::from_secs(5),
        }
    }
}

impl From<RealWebSocketTestConfig> for LeptosWsProConfig {
    fn from(config: RealWebSocketTestConfig) -> Self {
        Self {
            url: config.url,
            timeout: config.timeout,
            max_reconnect_attempts: config.max_reconnect_attempts,
            heartbeat_interval: config.heartbeat_interval,
            connection_timeout: config.connection_timeout,
            retry_delay: config.retry_delay,
        }
    }
}

#[cfg(test)]
mod real_websocket_integration_tests {
    use super::*;

    /// Test 1: Real WebSocket Connection Establishment
    /// 
    /// This test verifies that the transport can establish a real WebSocket
    /// connection using leptos-ws-pro APIs.
    #[tokio::test]
    async fn test_real_websocket_connection() {
        let config = RealWebSocketTestConfig::default();
        let transport = LeptosWsProTransport::new(config.into());
        
        // Initially disconnected
        assert!(!transport.is_connected());
        
        // Attempt to connect to a real WebSocket server
        let result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        match result {
            Ok(Ok(())) => {
                // Connection succeeded
                assert!(transport.is_connected());
                
                // Should be able to disconnect
                let disconnect_result = transport.disconnect().await;
                assert!(disconnect_result.is_ok());
                assert!(!transport.is_connected());
            }
            Ok(Err(e)) => {
                // Connection failed (expected if no server running)
                assert!(!transport.is_connected());
                println!("Connection failed as expected: {}", e);
            }
            Err(_) => {
                // Timeout occurred
                assert!(!transport.is_connected());
                println!("Connection timeout as expected");
            }
        }
    }

    /// Test 2: Real Message Sending and Receiving
    /// 
    /// This test verifies that the transport can send and receive real messages
    /// through a WebSocket connection.
    #[tokio::test]
    async fn test_real_message_sending_receiving() {
        let config = RealWebSocketTestConfig::default();
        let transport = LeptosWsProTransport::new(config.into());
        
        // Connect to server
        let connect_result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        if let Ok(Ok(())) = connect_result {
            // Connection successful, test message sending
            let test_message = RealTimeMessage {
                id: "test_001".to_string(),
                content: "Hello, Real WebSocket!".to_string(),
                timestamp: chrono::Utc::now().timestamp() as u64,
                message_type: "test".to_string(),
            };
            
            let serialized = serde_json::to_vec(&test_message).unwrap();
            
            // Send message
            let send_result = transport.send(&serialized).await;
            assert!(send_result.is_ok(), "Failed to send message: {:?}", send_result);
            
            // Wait for potential response
            let receive_result = timeout(
                Duration::from_secs(2), 
                transport.receive()
            ).await;
            
            match receive_result {
                Ok(Ok(messages)) => {
                    // Received messages successfully
                    println!("Received {} messages", messages.len());
                    for (i, message) in messages.iter().enumerate() {
                        println!("Message {}: {} bytes", i, message.len());
                    }
                }
                Ok(Err(e)) => {
                    println!("Receive error: {}", e);
                }
                Err(_) => {
                    println!("Receive timeout (no messages received)");
                }
            }
            
            // Disconnect
            let _ = transport.disconnect().await;
        } else {
            println!("Skipping message test - no server connection available");
        }
    }

    /// Test 3: Real Sync Operations
    /// 
    /// This test verifies that the transport can handle real sync operations
    /// with proper message formatting and handling.
    #[tokio::test]
    async fn test_real_sync_operations() {
        let config = RealWebSocketTestConfig::default();
        let transport = LeptosWsProTransport::new(config.into());
        
        // Connect to server
        let connect_result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        if let Ok(Ok(())) = connect_result {
            // Test sync operation
            let sync_op = SyncOperation {
                operation: "insert".to_string(),
                data: serde_json::json!({
                    "id": "item_123",
                    "content": "New item",
                    "position": 0
                }),
                client_id: "client_001".to_string(),
                sequence: 1,
            };
            
            let serialized = serde_json::to_vec(&sync_op).unwrap();
            
            // Send sync operation
            let send_result = transport.send(&serialized).await;
            assert!(send_result.is_ok(), "Failed to send sync operation: {:?}", send_result);
            
            // Test multiple sync operations
            for i in 2..=5 {
                let sync_op = SyncOperation {
                    operation: "update".to_string(),
                    data: serde_json::json!({
                        "id": "item_123",
                        "content": format!("Updated item {}", i),
                        "version": i
                    }),
                    client_id: "client_001".to_string(),
                    sequence: i,
                };
                
                let serialized = serde_json::to_vec(&sync_op).unwrap();
                let send_result = transport.send(&serialized).await;
                assert!(send_result.is_ok(), "Failed to send sync operation {}: {:?}", i, send_result);
            }
            
            // Disconnect
            let _ = transport.disconnect().await;
        } else {
            println!("Skipping sync operations test - no server connection available");
        }
    }

    /// Test 4: Real Reconnection with Server Restart
    /// 
    /// This test verifies that the transport can handle server disconnections
    /// and reconnections properly.
    #[tokio::test]
    async fn test_real_reconnection_behavior() {
        let config = RealWebSocketTestConfig {
            max_reconnect_attempts: 3,
            retry_delay: Duration::from_millis(500),
            ..Default::default()
        };
        let transport = LeptosWsProTransport::new(config.into());
        
        // Attempt initial connection
        let connect_result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        if let Ok(Ok(())) = connect_result {
            println!("Initial connection successful");
            
            // Send a test message
            let test_message = RealTimeMessage {
                id: "reconnect_test".to_string(),
                content: "Testing reconnection".to_string(),
                timestamp: chrono::Utc::now().timestamp() as u64,
                message_type: "reconnect_test".to_string(),
            };
            
            let serialized = serde_json::to_vec(&test_message).unwrap();
            let send_result = transport.send(&serialized).await;
            assert!(send_result.is_ok(), "Failed to send test message: {:?}", send_result);
            
            // Simulate connection loss by disconnecting
            let disconnect_result = transport.disconnect().await;
            assert!(disconnect_result.is_ok());
            assert!(!transport.is_connected());
            
            // Attempt reconnection
            let reconnect_result = timeout(Duration::from_secs(10), transport.connect()).await;
            
            match reconnect_result {
                Ok(Ok(())) => {
                    println!("Reconnection successful");
                    assert!(transport.is_connected());
                    
                    // Send another message after reconnection
                    let test_message = RealTimeMessage {
                        id: "after_reconnect".to_string(),
                        content: "Message after reconnection".to_string(),
                        timestamp: chrono::Utc::now().timestamp() as u64,
                        message_type: "after_reconnect".to_string(),
                    };
                    
                    let serialized = serde_json::to_vec(&test_message).unwrap();
                    let send_result = transport.send(&serialized).await;
                    assert!(send_result.is_ok(), "Failed to send message after reconnection: {:?}", send_result);
                }
                Ok(Err(e)) => {
                    println!("Reconnection failed: {}", e);
                }
                Err(_) => {
                    println!("Reconnection timeout");
                }
            }
            
            // Final disconnect
            let _ = transport.disconnect().await;
        } else {
            println!("Skipping reconnection test - no server connection available");
        }
    }

    /// Test 5: Real Heartbeat Mechanism
    /// 
    /// This test verifies that the transport implements proper heartbeat
    /// functionality to keep connections alive.
    #[tokio::test]
    async fn test_real_heartbeat_mechanism() {
        let config = RealWebSocketTestConfig {
            heartbeat_interval: Duration::from_millis(1000), // 1 second for testing
            ..Default::default()
        };
        let transport = LeptosWsProTransport::new(config.into());
        
        // Connect to server
        let connect_result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        if let Ok(Ok(())) = connect_result {
            println!("Connected, testing heartbeat mechanism");
            
            // Wait for heartbeat to be sent
            tokio::time::sleep(Duration::from_millis(1500)).await;
            
            // Connection should still be alive
            assert!(transport.is_connected(), "Connection should still be alive after heartbeat");
            
            // Send a regular message to verify connection is working
            let test_message = RealTimeMessage {
                id: "heartbeat_test".to_string(),
                content: "Testing heartbeat".to_string(),
                timestamp: chrono::Utc::now().timestamp() as u64,
                message_type: "heartbeat_test".to_string(),
            };
            
            let serialized = serde_json::to_vec(&test_message).unwrap();
            let send_result = transport.send(&serialized).await;
            assert!(send_result.is_ok(), "Failed to send message after heartbeat: {:?}", send_result);
            
            // Disconnect
            let _ = transport.disconnect().await;
        } else {
            println!("Skipping heartbeat test - no server connection available");
        }
    }

    /// Test 6: Real Concurrent Operations
    /// 
    /// This test verifies that the transport can handle concurrent send/receive
    /// operations safely.
    #[tokio::test]
    async fn test_real_concurrent_operations() {
        let config = RealWebSocketTestConfig::default();
        let transport = LeptosWsProTransport::new(config.into());
        
        // Connect to server
        let connect_result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        if let Ok(Ok(())) = connect_result {
            println!("Connected, testing concurrent operations");
            
            // Spawn multiple concurrent send operations
            let mut send_handles = Vec::new();
            for i in 0..10 {
                let transport_clone = transport.clone();
                let handle = tokio::spawn(async move {
                    let test_message = RealTimeMessage {
                        id: format!("concurrent_{}", i),
                        content: format!("Concurrent message {}", i),
                        timestamp: chrono::Utc::now().timestamp() as u64,
                        message_type: "concurrent".to_string(),
                    };
                    
                    let serialized = serde_json::to_vec(&test_message).unwrap();
                    transport_clone.send(&serialized).await
                });
                send_handles.push(handle);
            }
            
            // Spawn concurrent receive operations
            let mut receive_handles = Vec::new();
            for _ in 0..5 {
                let transport_clone = transport.clone();
                let handle = tokio::spawn(async move {
                    timeout(Duration::from_secs(2), transport_clone.receive()).await
                });
                receive_handles.push(handle);
            }
            
            // Wait for all send operations to complete
            for (i, handle) in send_handles.into_iter().enumerate() {
                let result = handle.await.unwrap();
                assert!(result.is_ok(), "Concurrent send operation {} failed: {:?}", i, result);
            }
            
            // Wait for all receive operations to complete
            for (i, handle) in receive_handles.into_iter().enumerate() {
                let result = handle.await.unwrap();
                match result {
                    Ok(Ok(messages)) => {
                        println!("Concurrent receive {} got {} messages", i, messages.len());
                    }
                    Ok(Err(e)) => {
                        println!("Concurrent receive {} error: {}", i, e);
                    }
                    Err(_) => {
                        println!("Concurrent receive {} timeout", i);
                    }
                }
            }
            
            // Connection should still be alive
            assert!(transport.is_connected(), "Connection should still be alive after concurrent operations");
            
            // Disconnect
            let _ = transport.disconnect().await;
        } else {
            println!("Skipping concurrent operations test - no server connection available");
        }
    }

    /// Test 7: Real Error Handling and Recovery
    /// 
    /// This test verifies that the transport handles various error conditions
    /// gracefully and recovers appropriately.
    #[tokio::test]
    async fn test_real_error_handling_recovery() {
        let config = RealWebSocketTestConfig::default();
        let transport = LeptosWsProTransport::new(config.into());
        
        // Test 1: Connection to invalid URL
        let invalid_config = RealWebSocketTestConfig {
            url: "ws://invalid-url-that-does-not-exist:9999".to_string(),
            ..Default::default()
        };
        let invalid_transport = LeptosWsProTransport::new(invalid_config.into());
        
        let connect_result = timeout(Duration::from_secs(5), invalid_transport.connect()).await;
        match connect_result {
            Ok(Ok(())) => {
                // Unexpected success
                assert!(invalid_transport.is_connected());
                let _ = invalid_transport.disconnect().await;
            }
            Ok(Err(e)) => {
                // Expected failure
                assert!(!invalid_transport.is_connected());
                println!("Expected connection failure: {}", e);
            }
            Err(_) => {
                // Timeout
                assert!(!invalid_transport.is_connected());
                println!("Expected connection timeout");
            }
        }
        
        // Test 2: Send to disconnected transport
        let send_result = invalid_transport.send(b"test message").await;
        assert!(send_result.is_err(), "Send to disconnected transport should fail");
        
        // Test 3: Receive from disconnected transport
        let receive_result = invalid_transport.receive().await;
        assert!(receive_result.is_ok(), "Receive from disconnected transport should return empty");
        assert!(receive_result.unwrap().is_empty(), "Receive from disconnected transport should return empty messages");
    }

    /// Test 8: Real Performance Characteristics
    /// 
    /// This test verifies that the transport meets performance requirements
    /// for real-time applications.
    #[tokio::test]
    async fn test_real_performance_characteristics() {
        let config = RealWebSocketTestConfig::default();
        let transport = LeptosWsProTransport::new(config.into());
        
        // Connect to server
        let connect_result = timeout(Duration::from_secs(5), transport.connect()).await;
        
        if let Ok(Ok(())) = connect_result {
            println!("Connected, testing performance characteristics");
            
            // Test message throughput
            let start_time = std::time::Instant::now();
            let message_count = 100;
            
            for i in 0..message_count {
                let test_message = RealTimeMessage {
                    id: format!("perf_{}", i),
                    content: format!("Performance test message {}", i),
                    timestamp: chrono::Utc::now().timestamp() as u64,
                    message_type: "performance".to_string(),
                };
                
                let serialized = serde_json::to_vec(&test_message).unwrap();
                let send_result = transport.send(&serialized).await;
                assert!(send_result.is_ok(), "Failed to send performance test message {}: {:?}", i, send_result);
            }
            
            let elapsed = start_time.elapsed();
            let messages_per_second = message_count as f64 / elapsed.as_secs_f64();
            
            println!("Sent {} messages in {:?} ({:.2} msg/s)", message_count, elapsed, messages_per_second);
            
            // Performance should be reasonable (at least 10 messages per second)
            assert!(messages_per_second > 10.0, "Performance too low: {:.2} msg/s", messages_per_second);
            
            // Test large message handling
            let large_content = "x".repeat(10000); // 10KB message
            let large_message = RealTimeMessage {
                id: "large_message".to_string(),
                content: large_content,
                timestamp: chrono::Utc::now().timestamp() as u64,
                message_type: "large".to_string(),
            };
            
            let serialized = serde_json::to_vec(&large_message).unwrap();
            let large_send_start = std::time::Instant::now();
            let send_result = transport.send(&serialized).await;
            let large_send_elapsed = large_send_start.elapsed();
            
            assert!(send_result.is_ok(), "Failed to send large message: {:?}", send_result);
            assert!(large_send_elapsed < Duration::from_millis(100), "Large message send too slow: {:?}", large_send_elapsed);
            
            println!("Large message ({} bytes) sent in {:?}", serialized.len(), large_send_elapsed);
            
            // Disconnect
            let _ = transport.disconnect().await;
        } else {
            println!("Skipping performance test - no server connection available");
        }
    }
}

/// Integration tests that require a running WebSocket server
#[cfg(test)]
mod server_integration_tests {
    use super::*;

    /// Test 9: Full Integration with Real Server
    /// 
    /// This test requires a running WebSocket server and tests the complete
    /// integration with leptos-ws-pro.
    #[tokio::test]
    #[ignore] // Ignored by default, run with: cargo test -- --ignored
    async fn test_full_integration_with_real_server() {
        // This test requires a real WebSocket server running
        // It will be enabled when we implement the server integration
        println!("Full integration test with real server is disabled - requires server implementation");
    }

    /// Test 10: Server Message Protocol Compatibility
    /// 
    /// This test verifies compatibility with the existing server message protocol.
    #[tokio::test]
    #[ignore] // Ignored by default, run with: cargo test -- --ignored
    async fn test_server_message_protocol_compatibility() {
        // This test will verify compatibility with the existing server protocol
        println!("Server message protocol compatibility test is disabled - requires server implementation");
    }
}