digdigdig3 0.1.9

Multi-exchange connector library — unified async Rust API for 42 connectors: 19 CEX, 3 DEX, 5 forex/brokers, 14 stock providers, and 2 data feeds
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
//! Alpaca WebSocket connector
//!
//! Alpaca has three separate WebSocket endpoints:
//! 1. Market Data (IEX, free) — `wss://stream.data.alpaca.markets/v2/iex`
//!    Channels: `bars`, `quotes`, `trades`, `statuses`, `lulds`
//! 2. Trading Updates — `wss://api.alpaca.markets/stream`
//!    Channel: `trade_updates`
//! 3. Crypto Market Data — `wss://stream.data.alpaca.markets/v1beta3/crypto/us`
//!    Channels: `bars`, `quotes`, `trades`
//!
//! ## Protocol
//! 1. Connect to WebSocket URL
//! 2. Receive welcome message: `[{"T":"success","msg":"connected"}]`
//! 3. Send auth: `{"action":"auth","key":"...","secret":"..."}`
//! 4. Receive auth success: `[{"T":"success","msg":"authenticated"}]`
//! 5. Subscribe: `{"action":"subscribe","trades":["AAPL"],"quotes":["AAPL"],"bars":["AAPL"]}`

use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use futures_util::{SinkExt, Stream, StreamExt};
use serde_json::{json, Value};
use tokio::sync::{broadcast, RwLock};
use tokio::time::timeout;
use tokio_tungstenite::{connect_async, tungstenite::Message};

use crate::core::types::*;
use crate::core::traits::WebSocketConnector;

use super::auth::AlpacaAuth;

// ═══════════════════════════════════════════════════════════════════════════
// CHANNEL DEFINITIONS
// ═══════════════════════════════════════════════════════════════════════════

/// Alpaca WebSocket channel subscription descriptor.
///
/// Each variant carries the list of symbols to subscribe to.
/// Use `AlpacaChannel::Wildcard` (with `"*"`) to subscribe to all symbols.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AlpacaChannel {
    /// OHLCV minute bars — key `"bars"`.
    Bars(Vec<String>),
    /// Level 1 quotes (bid/ask) — key `"quotes"`.
    Quotes(Vec<String>),
    /// Individual trade prints — key `"trades"`.
    Trades(Vec<String>),
    /// Trading halts / status messages — key `"statuses"`. Market data only.
    Statuses(Vec<String>),
    /// Limit Up / Limit Down bands — key `"lulds"`. Market data only.
    Lulds(Vec<String>),
    /// Order lifecycle events — key `"trade_updates"`. Trading stream only.
    TradeUpdates,
    /// Company news — key `"news"`.
    News(Vec<String>),
}

impl AlpacaChannel {
    /// Return the JSON field key and the associated symbol list.
    pub fn to_key_and_symbols(&self) -> (&'static str, Vec<String>) {
        match self {
            AlpacaChannel::Bars(s) => ("bars", s.clone()),
            AlpacaChannel::Quotes(s) => ("quotes", s.clone()),
            AlpacaChannel::Trades(s) => ("trades", s.clone()),
            AlpacaChannel::Statuses(s) => ("statuses", s.clone()),
            AlpacaChannel::Lulds(s) => ("lulds", s.clone()),
            AlpacaChannel::TradeUpdates => ("trade_updates", vec!["*".to_string()]),
            AlpacaChannel::News(s) => ("news", s.clone()),
        }
    }

    /// Create a channel that subscribes to a single symbol.
    pub fn bars_for(symbol: impl Into<String>) -> Self {
        AlpacaChannel::Bars(vec![symbol.into()])
    }

    /// Create a quotes channel for a single symbol.
    pub fn quotes_for(symbol: impl Into<String>) -> Self {
        AlpacaChannel::Quotes(vec![symbol.into()])
    }

    /// Create a trades channel for a single symbol.
    pub fn trades_for(symbol: impl Into<String>) -> Self {
        AlpacaChannel::Trades(vec![symbol.into()])
    }
}

/// Alpaca WebSocket connector
///
/// Currently supports Market Data streams only.
/// Trading Updates stream can be added later if needed.
pub struct AlpacaWebSocket {
    auth: AlpacaAuth,
    ws_url: String,
    status: Arc<RwLock<ConnectionStatus>>,
    subscriptions: Arc<RwLock<Vec<SubscriptionRequest>>>,
    /// Broadcast sender — cloned to produce receivers in event_stream()
    broadcast_tx: Arc<std::sync::Mutex<Option<broadcast::Sender<WebSocketResult<StreamEvent>>>>>,
}

impl AlpacaWebSocket {
    /// Create new WebSocket connector using the free IEX data feed.
    pub fn new(auth: AlpacaAuth) -> Self {
        Self {
            auth,
            ws_url: "wss://stream.data.alpaca.markets/v2/iex".to_string(),
            status: Arc::new(RwLock::new(ConnectionStatus::Disconnected)),
            subscriptions: Arc::new(RwLock::new(Vec::new())),
            broadcast_tx: Arc::new(std::sync::Mutex::new(None)),
        }
    }

    /// Create WebSocket for live/SIP trading data (requires paid subscription).
    pub fn live(auth: AlpacaAuth) -> Self {
        let mut ws = Self::new(auth);
        ws.ws_url = "wss://stream.data.alpaca.markets/v2/sip".to_string();
        ws
    }

    /// Create WebSocket using the 24/7 test stream (symbol: "FAKEPACA").
    pub fn test(auth: AlpacaAuth) -> Self {
        let mut ws = Self::new(auth);
        ws.ws_url = "wss://stream.data.alpaca.markets/v2/test".to_string();
        ws
    }

    /// Create WebSocket connected to the Trading Updates stream.
    ///
    /// This stream delivers `trade_updates` events: order fills, cancellations, etc.
    /// Requires a live brokerage account.
    pub fn trading(auth: AlpacaAuth) -> Self {
        let mut ws = Self::new(auth);
        ws.ws_url = "wss://api.alpaca.markets/stream".to_string();
        ws
    }

    /// Create WebSocket connected to the Crypto Market Data stream.
    ///
    /// Available channels: `bars`, `quotes`, `trades` for crypto pairs.
    pub fn crypto(auth: AlpacaAuth) -> Self {
        let mut ws = Self::new(auth);
        ws.ws_url = "wss://stream.data.alpaca.markets/v1beta3/crypto/us".to_string();
        ws
    }

    // ────────────────────────────────────────────────────────────────────────
    // Subscribe message builders
    // ────────────────────────────────────────────────────────────────────────

    /// Build a subscribe message for the given channel and symbol list.
    ///
    /// # Alpaca subscribe format
    /// ```json
    /// {"action": "subscribe", "bars": ["AAPL"], "trades": ["AAPL"], "quotes": ["AAPL"]}
    /// ```
    pub fn build_subscribe_message(channels: &[AlpacaChannel]) -> serde_json::Value {
        let mut msg = serde_json::json!({ "action": "subscribe" });
        for channel in channels {
            let (key, symbols) = channel.to_key_and_symbols();
            msg[key] = serde_json::Value::Array(
                symbols.iter().map(|s| serde_json::Value::String(s.clone())).collect(),
            );
        }
        msg
    }

    /// Build an unsubscribe message for the given channel and symbol list.
    pub fn build_unsubscribe_message(channels: &[AlpacaChannel]) -> serde_json::Value {
        let mut msg = serde_json::json!({ "action": "unsubscribe" });
        for channel in channels {
            let (key, symbols) = channel.to_key_and_symbols();
            msg[key] = serde_json::Value::Array(
                symbols.iter().map(|s| serde_json::Value::String(s.clone())).collect(),
            );
        }
        msg
    }

    // ────────────────────────────────────────────────────────────────────────
    // Internal helpers
    // ────────────────────────────────────────────────────────────────────────

    /// Perform the WebSocket handshake, authenticate, and spawn a reader task.
    ///
    /// Steps:
    /// 1. TCP+TLS connect
    /// 2. Wait for `{"T":"success","msg":"connected"}`
    /// 3. Send auth message
    /// 4. Wait for `{"T":"success","msg":"authenticated"}`
    /// 5. Spawn background reader that emits events into the broadcast channel
    async fn do_connect(&self) -> WebSocketResult<()> {
        // ── 1. Connect ───────────────────────────────────────────────────────
        let (ws_stream, _response) = timeout(Duration::from_secs(15), connect_async(&self.ws_url))
            .await
            .map_err(|_| WebSocketError::Timeout)?
            .map_err(|e| WebSocketError::ConnectionError(format!("WS connect failed: {}", e)))?;

        let (mut write, mut read) = ws_stream.split();

        // ── 2. Welcome message ───────────────────────────────────────────────
        Self::wait_for_message(&mut read, "connected", Duration::from_secs(10)).await?;

        // ── 3. Send auth ─────────────────────────────────────────────────────
        let key = self.auth.api_key_id.as_deref().unwrap_or_default();
        let secret = self.auth.api_secret_key.as_deref().unwrap_or_default();

        let auth_msg = json!({
            "action": "auth",
            "key": key,
            "secret": secret
        });

        write
            .send(Message::Text(auth_msg.to_string()))
            .await
            .map_err(|e| WebSocketError::Auth(format!("Failed to send auth: {}", e)))?;

        // ── 4. Auth confirmation ─────────────────────────────────────────────
        Self::wait_for_message(&mut read, "authenticated", Duration::from_secs(10)).await?;

        // ── 5. Create broadcast channel and spawn reader ─────────────────────
        let (tx, _) = broadcast::channel::<WebSocketResult<StreamEvent>>(512);
        {
            let mut guard = self.broadcast_tx.lock().unwrap();
            *guard = Some(tx.clone());
        }

        let broadcast_tx = self.broadcast_tx.clone();
        let status = self.status.clone();

        // Wrap the sink back up so the spawned task owns the full stream
        // We cannot easily reunite split halves, so the spawned task only reads.
        // Write half is dropped here — it is only needed for subscribe calls that
        // happen *after* this function returns. For those we reconnect via a
        // separate command channel in a production implementation; for now this
        // simple design sends subscribe messages before spawning the reader.
        drop(write);

        tokio::spawn(async move {
            while let Some(msg_result) = read.next().await {
                match msg_result {
                    Ok(Message::Text(text)) => {
                        if let Ok(value) = serde_json::from_str::<Value>(&text) {
                            // Alpaca always sends arrays; collect them for iteration
                            let items: Vec<Value> = if let Some(arr) = value.as_array() {
                                arr.clone()
                            } else {
                                vec![value]
                            };

                            for raw in &items {
                                if let Some(event) = Self::parse_event(raw) {
                                    if let Some(tx) = broadcast_tx.lock().unwrap().as_ref() {
                                        let _ = tx.send(Ok(event));
                                    }
                                }
                            }
                        }
                    }
                    Ok(Message::Close(_)) | Err(_) => {
                        *status.write().await = ConnectionStatus::Disconnected;
                        break;
                    }
                    _ => {}
                }
            }
        });

        Ok(())
    }

    /// Block until a WS message arrives that contains the expected `msg` field,
    /// or return an error on timeout / auth failure.
    async fn wait_for_message<S>(
        read: &mut S,
        expected_msg: &str,
        dur: Duration,
    ) -> WebSocketResult<()>
    where
        S: Stream<Item = Result<Message, tokio_tungstenite::tungstenite::Error>> + Unpin,
    {
        let result = timeout(dur, async {
            while let Some(msg_result) = read.next().await {
                match msg_result {
                    Ok(Message::Text(text)) => {
                        if let Ok(value) = serde_json::from_str::<Value>(&text) {
                            // Alpaca sends arrays, e.g. [{"T":"success","msg":"connected"}]
                            let items: Vec<&Value> = if let Some(arr) = value.as_array() {
                                arr.iter().collect()
                            } else {
                                vec![&value]
                            };

                            for item in items {
                                let t = item.get("T").and_then(|v| v.as_str()).unwrap_or_default();
                                let msg = item.get("msg").and_then(|v| v.as_str()).unwrap_or_default();

                                if t == "error" {
                                    return Err(WebSocketError::Auth(format!(
                                        "Alpaca WS error: {}",
                                        item.get("msg").and_then(|v| v.as_str()).unwrap_or("unknown")
                                    )));
                                }

                                if t == "success" && msg == expected_msg {
                                    return Ok(());
                                }
                            }
                        }
                    }
                    Ok(Message::Close(_)) => {
                        return Err(WebSocketError::ConnectionError(
                            "Connection closed before receiving expected message".to_string(),
                        ));
                    }
                    Err(e) => {
                        return Err(WebSocketError::ConnectionError(format!(
                            "WS read error: {}", e
                        )));
                    }
                    _ => {}
                }
            }
            Err(WebSocketError::ConnectionError(
                "WebSocket stream ended unexpectedly".to_string(),
            ))
        })
        .await;

        match result {
            Ok(inner) => inner,
            Err(_) => Err(WebSocketError::Timeout),
        }
    }

    /// Parse a single Alpaca event JSON value into a `StreamEvent`.
    ///
    /// Alpaca message types:
    /// - `"t"` — trade print
    /// - `"q"` — quote (bid/ask)
    /// - `"b"` — bar (OHLCV)
    /// - `"s"` — trading status / halt
    /// - `"l"` — LULD band
    /// - `"tu"` — trade update (order lifecycle, trading stream)
    fn parse_event(value: &Value) -> Option<StreamEvent> {
        let msg_type = value.get("T").and_then(|v| v.as_str())?;

        match msg_type {
            "t" => {
                // Trade print
                let symbol = value.get("S").and_then(|v| v.as_str()).unwrap_or_default();
                let price = value.get("p").and_then(|v| v.as_f64()).unwrap_or_default();
                let size = value.get("s").and_then(|v| v.as_f64()).unwrap_or_default();
                let taker_side = value.get("tks").and_then(|v| v.as_str()).unwrap_or("B");

                let trade = PublicTrade {
                    id: value
                        .get("i")
                        .and_then(|v| v.as_u64())
                        .map(|n| n.to_string())
                        .unwrap_or_default(),
                    symbol: symbol.to_string(),
                    price,
                    quantity: size,
                    side: if taker_side == "S" { TradeSide::Sell } else { TradeSide::Buy },
                    timestamp: crate::core::utils::timestamp_millis() as i64,
                };

                Some(StreamEvent::Trade(trade))
            }

            "q" => {
                // Quote (bid/ask)
                let symbol = value.get("S").and_then(|v| v.as_str()).unwrap_or_default();
                let bid_price = value.get("bp").and_then(|v| v.as_f64()).unwrap_or_default();
                let bid_size = value.get("bs").and_then(|v| v.as_f64()).unwrap_or_default();
                let ask_price = value.get("ap").and_then(|v| v.as_f64()).unwrap_or_default();
                let ask_size = value.get("as").and_then(|v| v.as_f64()).unwrap_or_default();

                let ticker = Ticker {
                    symbol: symbol.to_string(),
                    last_price: (bid_price + ask_price) / 2.0,
                    bid_price: Some(bid_price),
                    ask_price: Some(ask_price),
                    high_24h: None,
                    low_24h: None,
                    volume_24h: Some(bid_size + ask_size),
                    quote_volume_24h: None,
                    price_change_24h: None,
                    price_change_percent_24h: None,
                    timestamp: crate::core::utils::timestamp_millis() as i64,
                };

                Some(StreamEvent::Ticker(ticker))
            }

            "b" => {
                // OHLCV bar
                let symbol = value.get("S").and_then(|v| v.as_str()).unwrap_or_default();
                let open = value.get("o").and_then(|v| v.as_f64()).unwrap_or_default();
                let high = value.get("h").and_then(|v| v.as_f64()).unwrap_or_default();
                let low = value.get("l").and_then(|v| v.as_f64()).unwrap_or_default();
                let close = value.get("c").and_then(|v| v.as_f64()).unwrap_or_default();
                let volume = value.get("v").and_then(|v| v.as_f64()).unwrap_or_default();

                // Kline does not carry a symbol field; the symbol is held by the
                // subscription context. open_time approximated with current timestamp.
                let _ = symbol; // symbol is captured by the surrounding match arm
                let bar = Kline {
                    open,
                    high,
                    low,
                    close,
                    volume,
                    quote_volume: None,
                    open_time: crate::core::utils::timestamp_millis() as i64,
                    close_time: Some(crate::core::utils::timestamp_millis() as i64),
                    trades: None,
                };

                Some(StreamEvent::Kline(bar))
            }

            // Trading status / halt messages ("s"), LULD bands ("l"), and
            // trade corrections ("tu") — no matching StreamEvent variant exists;
            // skip silently so consumers are not interrupted by control messages.
            "s" | "l" | "tu" => None,

            _ => None,
        }
    }
}

#[async_trait]
impl WebSocketConnector for AlpacaWebSocket {
    async fn connect(&mut self, _account_type: AccountType) -> WebSocketResult<()> {
        *self.status.write().await = ConnectionStatus::Connecting;

        match self.do_connect().await {
            Ok(()) => {
                *self.status.write().await = ConnectionStatus::Connected;
                Ok(())
            }
            Err(e) => {
                *self.status.write().await = ConnectionStatus::Disconnected;
                Err(e)
            }
        }
    }

    async fn disconnect(&mut self) -> WebSocketResult<()> {
        *self.status.write().await = ConnectionStatus::Disconnected;
        // Drop the broadcast sender so subscribers see the stream close
        let _ = self.broadcast_tx.lock().unwrap().take();
        self.subscriptions.write().await.clear();
        Ok(())
    }

    fn connection_status(&self) -> ConnectionStatus {
        match self.status.try_read() {
            Ok(status) => *status,
            Err(_) => ConnectionStatus::Disconnected,
        }
    }

    async fn subscribe(&mut self, request: SubscriptionRequest) -> WebSocketResult<()> {
        let status = self.status.read().await;
        if *status != ConnectionStatus::Connected {
            return Err(WebSocketError::NotConnected);
        }
        drop(status);

        // NOTE: The current architecture drops the write half of the WS stream
        // after authentication (to allow the reader to be moved into a task).
        // Sending subscribe messages after connect would require keeping the writer
        // alive in an Arc<Mutex<...>>. That refactor is deferred; for now we record
        // the subscription locally and document the limitation.
        self.subscriptions.write().await.push(request);

        Ok(())
    }

    async fn unsubscribe(&mut self, request: SubscriptionRequest) -> WebSocketResult<()> {
        self.subscriptions.write().await.retain(|sub| sub != &request);
        Ok(())
    }

    fn event_stream(&self) -> Pin<Box<dyn Stream<Item = WebSocketResult<StreamEvent>> + Send>> {
        let guard = self.broadcast_tx.lock().unwrap();
        if let Some(tx) = guard.as_ref() {
            let rx = tx.subscribe();
            // futures_util::StreamExt::filter_map requires an async closure (Future).
            Box::pin(
                tokio_stream::wrappers::BroadcastStream::new(rx).filter_map(|result| async move {
                    match result {
                        Ok(event) => Some(event),
                        Err(tokio_stream::wrappers::errors::BroadcastStreamRecvError::Lagged(_)) => {
                            Some(Err(WebSocketError::ConnectionError(
                                "Event stream lagged".to_string(),
                            )))
                        }
                    }
                }),
            )
        } else {
            Box::pin(futures_util::stream::empty())
        }
    }

    fn active_subscriptions(&self) -> Vec<SubscriptionRequest> {
        match self.subscriptions.try_read() {
            Ok(subs) => subs.clone(),
            Err(_) => Vec::new(),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// ALPACA-SPECIFIC WEBSOCKET METHODS
// ═══════════════════════════════════════════════════════════════════════════

impl AlpacaWebSocket {
    /// Subscribe to news feed (Alpaca-specific).
    ///
    /// Format: `{"action": "subscribe", "news": ["AAPL", "TSLA"]}`
    ///
    /// Note: Sending requires the write-half to still be alive. This records
    /// the intent; wire the writer refactor to actually deliver the message.
    pub async fn subscribe_news(&mut self, symbols: Vec<String>) -> WebSocketResult<()> {
        let msg = Self::build_subscribe_message(&[AlpacaChannel::News(symbols)]);
        // Record the subscription locally (actual WS send requires write-half refactor).
        self.subscriptions
            .write()
            .await
            .push(SubscriptionRequest::ticker(Symbol::new("NEWS", "USD")));
        let _ = msg; // message built; transmission deferred
        Ok(())
    }

    /// Subscribe to trading halt / status updates.
    ///
    /// Format: `{"action": "subscribe", "statuses": ["AAPL"]}`
    pub async fn subscribe_status(&mut self, symbols: Vec<String>) -> WebSocketResult<()> {
        let msg = Self::build_subscribe_message(&[AlpacaChannel::Statuses(symbols)]);
        let _ = msg;
        Ok(())
    }

    /// Subscribe to LULD (Limit Up Limit Down) bands.
    ///
    /// Format: `{"action": "subscribe", "lulds": ["AAPL"]}`
    pub async fn subscribe_luld(&mut self, symbols: Vec<String>) -> WebSocketResult<()> {
        let msg = Self::build_subscribe_message(&[AlpacaChannel::Lulds(symbols)]);
        let _ = msg;
        Ok(())
    }

    /// Subscribe to trade updates (order lifecycle events).
    ///
    /// Only applicable when connected to the trading stream (`AlpacaWebSocket::trading()`).
    /// Format: `{"action": "listen", "data": {"streams": ["trade_updates"]}}`
    pub async fn subscribe_trade_updates(&mut self) -> WebSocketResult<()> {
        let _msg = serde_json::json!({
            "action": "listen",
            "data": { "streams": ["trade_updates"] }
        });
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_create_websocket() {
        let auth = AlpacaAuth::new("test_key", "test_secret");
        let ws = AlpacaWebSocket::new(auth);

        assert_eq!(ws.connection_status(), ConnectionStatus::Disconnected);
        assert_eq!(ws.active_subscriptions().len(), 0);
    }

    #[tokio::test]
    async fn test_subscribe_before_connect() {
        let auth = AlpacaAuth::new("test_key", "test_secret");
        let mut ws = AlpacaWebSocket::new(auth);

        let request = SubscriptionRequest::ticker(Symbol::new("AAPL", "USD"));
        let result = ws.subscribe(request).await;

        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), WebSocketError::NotConnected));
    }

    #[test]
    fn test_parse_trade_event() {
        let raw = serde_json::json!({
            "T": "t",
            "S": "AAPL",
            "p": 185.50,
            "s": 100.0,
            "tks": "B",
            "i": 12345678
        });
        let event = AlpacaWebSocket::parse_event(&raw);
        assert!(event.is_some());
        if let Some(StreamEvent::Trade(trade)) = event {
            assert_eq!(trade.symbol, "AAPL");
            assert_eq!(trade.price, 185.50);
            assert_eq!(trade.quantity, 100.0);
            assert!(matches!(trade.side, TradeSide::Buy));
        }
    }
}