drafftink-core 0.1.0

Core data structures and logic for DrafftInk whiteboard
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! WebSocket client for collaboration.
//!
//! Provides a platform-agnostic WebSocket client interface for connecting
//! to the relay server.

use serde::{Deserialize, Serialize};

/// Messages sent to the server
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ClientMessage {
    /// Join a room
    Join { room: String },
    /// Leave current room
    Leave,
    /// Sync CRDT data (base64 encoded Loro bytes)
    Sync { data: String },
    /// Awareness update (cursor position, selection, etc.)
    Awareness {
        peer_id: u64,
        #[serde(flatten)]
        state: AwarenessState,
    },
}

/// Messages received from the server
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerMessage {
    /// Confirm room join with current state
    Joined {
        room: String,
        peer_count: usize,
        /// Initial sync data (if room has history)
        #[serde(skip_serializing_if = "Option::is_none")]
        initial_sync: Option<String>,
    },
    /// Peer joined the room
    PeerJoined { peer_id: String },
    /// Peer left the room
    PeerLeft { peer_id: String },
    /// Sync data from another peer
    Sync { from: String, data: String },
    /// Awareness update from another peer
    Awareness {
        from: String,
        peer_id: u64,
        #[serde(flatten)]
        state: AwarenessState,
    },
    /// Error message
    Error { message: String },
}

/// Awareness state for a peer
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AwarenessState {
    /// Cursor position (if any)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<CursorPosition>,
    /// User name/color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<UserInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CursorPosition {
    pub x: f64,
    pub y: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInfo {
    pub name: String,
    pub color: String,
}

/// Connection state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
    Disconnected,
    Connecting,
    Connected,
    Error,
}

/// Events from the WebSocket client
#[derive(Debug, Clone)]
pub enum SyncEvent {
    /// Connected to server
    Connected,
    /// Disconnected from server
    Disconnected,
    /// Joined a room
    JoinedRoom { room: String, peer_count: usize, initial_sync: Option<Vec<u8>> },
    /// A peer joined the room
    PeerJoined { peer_id: String },
    /// A peer left the room
    PeerLeft { peer_id: String },
    /// Received sync data from a peer
    SyncReceived { from: String, data: Vec<u8> },
    /// Received awareness update from a peer
    AwarenessReceived { from: String, peer_id: u64, state: AwarenessState },
    /// Error occurred
    Error { message: String },
}

/// Base64 decoding
pub fn base64_decode(input: &str) -> Option<Vec<u8>> {
    const DECODE_TABLE: [i8; 128] = [
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
        -1,  0,  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, -1, -1, -1, -1, -1,
        -1, 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, -1, -1, -1, -1, -1,
    ];

    let input = input.trim_end_matches('=');
    let mut result = Vec::with_capacity(input.len() * 3 / 4);
    let mut buf = 0u32;
    let mut bits = 0;

    for c in input.bytes() {
        if c >= 128 {
            return None;
        }
        let val = DECODE_TABLE[c as usize];
        if val < 0 {
            return None;
        }
        buf = (buf << 6) | (val as u32);
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            result.push((buf >> bits) as u8);
            buf &= (1 << bits) - 1;
        }
    }

    Some(result)
}

/// Base64 encoding
pub fn base64_encode(data: &[u8]) -> String {
    const B64_CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    let mut result = String::with_capacity((data.len() + 2) / 3 * 4);
    
    for chunk in data.chunks(3) {
        let b0 = chunk[0];
        let b1 = chunk.get(1).copied().unwrap_or(0);
        let b2 = chunk.get(2).copied().unwrap_or(0);
        
        result.push(B64_CHARS[(b0 >> 2) as usize] as char);
        result.push(B64_CHARS[(((b0 & 0x03) << 4) | (b1 >> 4)) as usize] as char);
        
        if chunk.len() > 1 {
            result.push(B64_CHARS[(((b1 & 0x0f) << 2) | (b2 >> 6)) as usize] as char);
        } else {
            result.push('=');
        }
        
        if chunk.len() > 2 {
            result.push(B64_CHARS[(b2 & 0x3f) as usize] as char);
        } else {
            result.push('=');
        }
    }
    
    result
}

// ============================================================================
// WASM WebSocket Client
// ============================================================================

#[cfg(target_arch = "wasm32")]
mod wasm_client {
    use super::*;
    use std::cell::RefCell;
    use std::rc::Rc;
    use wasm_bindgen::prelude::*;
    use wasm_bindgen::JsCast;
    use web_sys::{MessageEvent, WebSocket, CloseEvent, ErrorEvent};

    /// WebSocket client for WASM.
    /// 
    /// Events are collected and must be polled via `poll_events()`.
    pub struct WasmWebSocket {
        ws: Option<WebSocket>,
        state: ConnectionState,
        events: Rc<RefCell<Vec<SyncEvent>>>,
        // Store closures to prevent them from being dropped
        _on_open: Option<Closure<dyn Fn()>>,
        _on_message: Option<Closure<dyn Fn(MessageEvent)>>,
        _on_close: Option<Closure<dyn Fn(CloseEvent)>>,
        _on_error: Option<Closure<dyn Fn(ErrorEvent)>>,
    }

    impl WasmWebSocket {
        /// Create a new disconnected WebSocket client.
        pub fn new() -> Self {
            Self {
                ws: None,
                state: ConnectionState::Disconnected,
                events: Rc::new(RefCell::new(Vec::new())),
                _on_open: None,
                _on_message: None,
                _on_close: None,
                _on_error: None,
            }
        }

        /// Connect to a WebSocket server.
        pub fn connect(&mut self, url: &str) -> Result<(), String> {
            if self.ws.is_some() {
                return Err("Already connected".to_string());
            }

            let ws = WebSocket::new(url).map_err(|e| format!("Failed to create WebSocket: {:?}", e))?;
            ws.set_binary_type(web_sys::BinaryType::Arraybuffer);

            self.state = ConnectionState::Connecting;
            let events = self.events.clone();

            // onopen
            let events_open = events.clone();
            let on_open = Closure::wrap(Box::new(move || {
                events_open.borrow_mut().push(SyncEvent::Connected);
            }) as Box<dyn Fn()>);
            ws.set_onopen(Some(on_open.as_ref().unchecked_ref()));

            // onmessage
            let events_msg = events.clone();
            let on_message = Closure::wrap(Box::new(move |e: MessageEvent| {
                if let Ok(txt) = e.data().dyn_into::<js_sys::JsString>() {
                    let s: String = txt.into();
                    // Parse and convert to SyncEvent
                    if let Ok(server_msg) = serde_json::from_str::<ServerMessage>(&s) {
                        let event = match server_msg {
                            ServerMessage::Joined { room, peer_count, initial_sync } => {
                                let data = initial_sync.and_then(|s| super::base64_decode(&s));
                                SyncEvent::JoinedRoom { room, peer_count, initial_sync: data }
                            }
                            ServerMessage::PeerJoined { peer_id } => SyncEvent::PeerJoined { peer_id },
                            ServerMessage::PeerLeft { peer_id } => SyncEvent::PeerLeft { peer_id },
                            ServerMessage::Sync { from, data } => {
                                if let Some(bytes) = super::base64_decode(&data) {
                                    SyncEvent::SyncReceived { from, data: bytes }
                                } else {
                                    return;
                                }
                            }
                            ServerMessage::Awareness { from, peer_id, state } => {
                                SyncEvent::AwarenessReceived { from, peer_id, state }
                            }
                            ServerMessage::Error { message } => SyncEvent::Error { message },
                        };
                        events_msg.borrow_mut().push(event);
                    }
                }
            }) as Box<dyn Fn(MessageEvent)>);
            ws.set_onmessage(Some(on_message.as_ref().unchecked_ref()));

            // onclose
            let events_close = events.clone();
            let on_close = Closure::wrap(Box::new(move |_e: CloseEvent| {
                events_close.borrow_mut().push(SyncEvent::Disconnected);
            }) as Box<dyn Fn(CloseEvent)>);
            ws.set_onclose(Some(on_close.as_ref().unchecked_ref()));

            // onerror
            let events_err = events;
            let on_error = Closure::wrap(Box::new(move |_e: ErrorEvent| {
                events_err.borrow_mut().push(SyncEvent::Error {
                    message: "WebSocket error".to_string(),
                });
            }) as Box<dyn Fn(ErrorEvent)>);
            ws.set_onerror(Some(on_error.as_ref().unchecked_ref()));

            self.ws = Some(ws);
            self._on_open = Some(on_open);
            self._on_message = Some(on_message);
            self._on_close = Some(on_close);
            self._on_error = Some(on_error);

            Ok(())
        }

        /// Disconnect from the server.
        pub fn disconnect(&mut self) {
            if let Some(ws) = self.ws.take() {
                let _ = ws.close();
            }
            self.state = ConnectionState::Disconnected;
            self._on_open = None;
            self._on_message = None;
            self._on_close = None;
            self._on_error = None;
        }

        /// Send a text message.
        pub fn send(&self, msg: &str) -> Result<(), String> {
            if let Some(ref ws) = self.ws {
                ws.send_with_str(msg)
                    .map_err(|e| format!("Send failed: {:?}", e))
            } else {
                Err("Not connected".to_string())
            }
        }

        /// Poll for pending events (non-blocking).
        pub fn poll_events(&mut self) -> Vec<SyncEvent> {
            let mut events = self.events.borrow_mut();
            
            // Update state based on events
            for event in events.iter() {
                match event {
                    SyncEvent::Connected => self.state = ConnectionState::Connected,
                    SyncEvent::Disconnected => self.state = ConnectionState::Disconnected,
                    SyncEvent::Error { .. } => self.state = ConnectionState::Error,
                    _ => {}
                }
            }
            
            std::mem::take(&mut *events)
        }

        /// Get current connection state.
        pub fn state(&self) -> ConnectionState {
            self.state
        }

        /// Check if connected.
        pub fn is_connected(&self) -> bool {
            self.state == ConnectionState::Connected
        }
    }

    impl Default for WasmWebSocket {
        fn default() -> Self {
            Self::new()
        }
    }
}

#[cfg(target_arch = "wasm32")]
pub use wasm_client::WasmWebSocket;

// ============================================================================
// Native WebSocket Client
// ============================================================================

#[cfg(not(target_arch = "wasm32"))]
mod native_client {
    use super::*;
    use std::sync::mpsc::{channel, Receiver, Sender, TryRecvError};
    use std::thread::{self, JoinHandle};
    use std::time::Duration;
    use tungstenite::{connect, Message};
    use url::Url;

    /// Commands sent to the WebSocket thread.
    enum WsCommand {
        Send(String),
        Close,
    }

    /// WebSocket client for native platforms.
    /// 
    /// Uses a background thread for non-blocking operation.
    pub struct NativeWebSocket {
        state: ConnectionState,
        events: Vec<SyncEvent>,
        /// Channel to send commands to the WebSocket thread.
        cmd_tx: Option<Sender<WsCommand>>,
        /// Channel to receive events from the WebSocket thread.
        event_rx: Option<Receiver<SyncEvent>>,
        /// Handle to the WebSocket thread.
        _thread: Option<JoinHandle<()>>,
    }

    impl NativeWebSocket {
        /// Create a new disconnected WebSocket client.
        pub fn new() -> Self {
            Self {
                state: ConnectionState::Disconnected,
                events: Vec::new(),
                cmd_tx: None,
                event_rx: None,
                _thread: None,
            }
        }

        /// Connect to a WebSocket server.
        pub fn connect(&mut self, url: &str) -> Result<(), String> {
            if self.cmd_tx.is_some() {
                return Err("Already connected".to_string());
            }

            // Validate URL
            let parsed_url = Url::parse(url).map_err(|e| format!("Invalid URL: {}", e))?;
            if parsed_url.scheme() != "ws" && parsed_url.scheme() != "wss" {
                return Err(format!("Invalid WebSocket URL scheme: {}", parsed_url.scheme()));
            }

            self.state = ConnectionState::Connecting;
            
            let (cmd_tx, cmd_rx) = channel::<WsCommand>();
            let (event_tx, event_rx) = channel::<SyncEvent>();
            
            let url = url.to_string();
            
            let handle = thread::spawn(move || {
                log::info!("WebSocket thread: connecting to {}", url);
                
                // Connect to WebSocket with timeout
                let ws_result = connect(&url);
                
                match ws_result {
                    Ok((mut socket, response)) => {
                        log::info!("WebSocket connected, status: {}", response.status());
                        let _ = event_tx.send(SyncEvent::Connected);
                        
                        // Set read timeout on the underlying TCP stream for non-blocking behavior
                        // This is more reliable for tunneled/forwarded connections
                        {
                            let stream = socket.get_mut();
                            match stream {
                                tungstenite::stream::MaybeTlsStream::Plain(tcp) => {
                                    let _ = tcp.set_read_timeout(Some(Duration::from_millis(50)));
                                    let _ = tcp.set_write_timeout(Some(Duration::from_secs(5)));
                                }
                                #[allow(unreachable_patterns)]
                                _ => {
                                    // For TLS streams, we'll rely on WouldBlock/TimedOut errors
                                    log::debug!("TLS or other stream - using default timeout handling");
                                }
                            }
                        }
                        
                        loop {
                            // Check for commands (non-blocking)
                            match cmd_rx.try_recv() {
                                Ok(WsCommand::Send(msg)) => {
                                    log::debug!("WebSocket sending: {}", &msg[..msg.len().min(100)]);
                                    if let Err(e) = socket.send(Message::Text(msg)) {
                                        log::error!("WebSocket send error: {}", e);
                                        break;
                                    }
                                }
                                Ok(WsCommand::Close) => {
                                    log::info!("WebSocket close requested");
                                    let _ = socket.close(None);
                                    break;
                                }
                                Err(TryRecvError::Disconnected) => {
                                    log::info!("WebSocket command channel disconnected");
                                    break;
                                }
                                Err(TryRecvError::Empty) => {}
                            }
                            
                            // Check for incoming messages (with timeout)
                            match socket.read() {
                                Ok(Message::Text(txt)) => {
                                    log::debug!("WebSocket received: {}", &txt[..txt.len().min(100)]);
                                    if let Ok(server_msg) = serde_json::from_str::<ServerMessage>(&txt) {
                                        let event = match server_msg {
                                            ServerMessage::Joined { room, peer_count, initial_sync } => {
                                                let data = initial_sync.and_then(|s| super::base64_decode(&s));
                                                SyncEvent::JoinedRoom { room, peer_count, initial_sync: data }
                                            }
                                            ServerMessage::PeerJoined { peer_id } => SyncEvent::PeerJoined { peer_id },
                                            ServerMessage::PeerLeft { peer_id } => SyncEvent::PeerLeft { peer_id },
                                            ServerMessage::Sync { from, data } => {
                                                if let Some(bytes) = super::base64_decode(&data) {
                                                    SyncEvent::SyncReceived { from, data: bytes }
                                                } else {
                                                    continue;
                                                }
                                            }
                                            ServerMessage::Awareness { from, peer_id, state } => {
                                                SyncEvent::AwarenessReceived { from, peer_id, state }
                                            }
                                            ServerMessage::Error { message } => SyncEvent::Error { message },
                                        };
                                        let _ = event_tx.send(event);
                                    } else {
                                        log::warn!("Failed to parse server message: {}", txt);
                                    }
                                }
                                Ok(Message::Ping(data)) => {
                                    // Respond to ping with pong
                                    let _ = socket.send(Message::Pong(data));
                                }
                                Ok(Message::Close(_)) => {
                                    log::info!("WebSocket received close frame");
                                    break;
                                }
                                Ok(_) => {} // Ignore binary, pong
                                Err(tungstenite::Error::Io(ref e)) 
                                    if e.kind() == std::io::ErrorKind::WouldBlock 
                                    || e.kind() == std::io::ErrorKind::TimedOut => {
                                    // Timeout on read, continue loop
                                    continue;
                                }
                                Err(e) => {
                                    log::error!("WebSocket read error: {}", e);
                                    break;
                                }
                            }
                        }
                        
                        log::info!("WebSocket thread exiting");
                        let _ = event_tx.send(SyncEvent::Disconnected);
                    }
                    Err(e) => {
                        log::error!("WebSocket connection failed: {}", e);
                        let _ = event_tx.send(SyncEvent::Error {
                            message: format!("Connection failed: {}", e),
                        });
                    }
                }
            });
            
            self.cmd_tx = Some(cmd_tx);
            self.event_rx = Some(event_rx);
            self._thread = Some(handle);
            
            Ok(())
        }

        /// Disconnect from the server.
        pub fn disconnect(&mut self) {
            if let Some(tx) = self.cmd_tx.take() {
                let _ = tx.send(WsCommand::Close);
            }
            self.event_rx = None;
            self._thread = None;
            self.state = ConnectionState::Disconnected;
        }

        /// Send a text message.
        pub fn send(&self, msg: &str) -> Result<(), String> {
            if let Some(ref tx) = self.cmd_tx {
                tx.send(WsCommand::Send(msg.to_string()))
                    .map_err(|e| format!("Send failed: {}", e))
            } else {
                Err("Not connected".to_string())
            }
        }

        /// Poll for pending events (non-blocking).
        pub fn poll_events(&mut self) -> Vec<SyncEvent> {
            // Drain events from channel
            if let Some(ref rx) = self.event_rx {
                while let Ok(event) = rx.try_recv() {
                    // Update state based on event
                    match &event {
                        SyncEvent::Connected => self.state = ConnectionState::Connected,
                        SyncEvent::Disconnected => self.state = ConnectionState::Disconnected,
                        SyncEvent::Error { .. } => self.state = ConnectionState::Error,
                        _ => {}
                    }
                    self.events.push(event);
                }
            }
            
            std::mem::take(&mut self.events)
        }

        /// Get current connection state.
        pub fn state(&self) -> ConnectionState {
            self.state
        }

        /// Check if connected.
        pub fn is_connected(&self) -> bool {
            self.state == ConnectionState::Connected
        }
    }

    impl Default for NativeWebSocket {
        fn default() -> Self {
            Self::new()
        }
    }
    
    impl Drop for NativeWebSocket {
        fn drop(&mut self) {
            self.disconnect();
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub use native_client::NativeWebSocket;

// ============================================================================
// Platform type alias
// ============================================================================

/// Platform-specific WebSocket client type.
#[cfg(target_arch = "wasm32")]
pub type PlatformWebSocket = WasmWebSocket;

#[cfg(not(target_arch = "wasm32"))]
pub type PlatformWebSocket = NativeWebSocket;

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

    #[test]
    fn test_base64_roundtrip() {
        let data = b"Hello, World!";
        let encoded = base64_encode(data);
        let decoded = base64_decode(&encoded).unwrap();
        assert_eq!(data.to_vec(), decoded);
    }

    #[test]
    fn test_base64_empty() {
        let data = b"";
        let encoded = base64_encode(data);
        let decoded = base64_decode(&encoded).unwrap();
        assert_eq!(data.to_vec(), decoded);
    }

    #[test]
    fn test_base64_padding() {
        // 1 byte -> 2 chars + 2 padding
        assert_eq!(base64_encode(b"a"), "YQ==");
        // 2 bytes -> 3 chars + 1 padding
        assert_eq!(base64_encode(b"ab"), "YWI=");
        // 3 bytes -> 4 chars, no padding
        assert_eq!(base64_encode(b"abc"), "YWJj");
    }

    #[test]
    fn test_client_message_serialize() {
        let msg = ClientMessage::Join { room: "test-room".to_string() };
        let json = serde_json::to_string(&msg).unwrap();
        assert!(json.contains("join"));
        assert!(json.contains("test-room"));
    }

    #[test]
    fn test_server_message_deserialize() {
        let json = r#"{"type":"joined","room":"test","peer_count":2}"#;
        let msg: ServerMessage = serde_json::from_str(json).unwrap();
        match msg {
            ServerMessage::Joined { room, peer_count, .. } => {
                assert_eq!(room, "test");
                assert_eq!(peer_count, 2);
            }
            _ => panic!("Wrong message type"),
        }
    }
}