rustrade-data 0.2.0

High performance & normalised WebSocket intergration for leading cryptocurrency exchanges - batteries included.
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! WebSocket live streaming client for Massive real-time market data.
//!
//! Provides real-time market data streaming via [`MassiveLive`].
//!
//! # Architecture
//!
//! - **One connection per market**: Each `MassiveLive` instance connects to one market
//!   (stocks, crypto, forex, options). For multiple markets, create separate instances.
//! - **Multiple symbols per connection**: Subscribe to multiple symbols before calling `start()`.
//! - **No auto-reconnect**: Returns [`MassiveError::Disconnected`] on connection drop; consumer owns
//!   reconnection policy (backoff, credential refresh, dedup/fill-recovery).
//!
//! # Connection Limits
//!
//! Massive limits concurrent WebSocket connections per subscription tier:
//! - **Individual plan**: 1 connection per purchased product
//! - **Business plan**: 3 connections per purchased product
//!
//! See <https://massive.com/knowledge-base/article/how-many-massive-websocket-connections-can-i-use-at-one-time>
//!
//! # Symbol Formats (WebSocket vs REST)
//!
//! **Important**: WebSocket uses different symbol formats than REST API:
//!
//! | Market | REST Format | WebSocket Format |
//! |--------|-------------|------------------|
//! | Crypto | `X:BTCUSD` | `BTC-USD` |
//! | Forex | `C:EURUSD` | `EUR-USD` |
//! | Stocks | `AAPL` | `AAPL` |
//! | Options | `O:AAPL251219C00150000` | `O:AAPL251219C00150000` |
//!
//! # Keepalive
//!
//! The client sends ping frames every 20 seconds and checks for pong responses at each ping tick.
//! If no pong has been received since the last ping, the connection is considered dead and
//! `MassiveError::Disconnected` is returned. Effective timeout is up to 40 seconds (two ping intervals).
//!
//! # Example
//!
//! ```ignore
//! use rustrade_data::exchange::massive::{MassiveLive, Market, ChannelType};
//! use rustrade_instrument::exchange::ExchangeId;
//! use std::collections::HashMap;
//! use futures::StreamExt;
//!
//! // Map WebSocket symbols to your instrument keys
//! let instruments: HashMap<String, String> = [
//!     ("BTC-USD".to_string(), "btc-usd".to_string()),
//!     ("ETH-USD".to_string(), "eth-usd".to_string()),
//! ].into_iter().collect();
//!
//! let mut client = MassiveLive::from_env(
//!     Market::Crypto,
//!     ExchangeId::Massive,
//!     instruments,
//! )?;
//!
//! // Subscribe to channels (can call multiple times before start)
//! client.subscribe(&["BTC-USD", "ETH-USD"], ChannelType::Trade);
//! client.subscribe(&["BTC-USD"], ChannelType::Quote);
//!
//! // Start streaming (consumes client)
//! let mut stream = client.start().await?;
//!
//! while let Some(event) = stream.next().await {
//!     match event {
//!         Ok(market_event) => println!("{:?}", market_event),
//!         Err(e) => {
//!             eprintln!("Error: {}", e);
//!             // Consumer decides: reconnect, backoff, or exit
//!             break;
//!         }
//!     }
//! }
//! ```

use super::error::MassiveError;
use super::transformer::{WsMessage, parse_ws_message};
use crate::event::{DataKind, MarketEvent};
use chrono::{DateTime, Utc};
use futures::{SinkExt, Stream, StreamExt};
use rustrade_instrument::exchange::ExchangeId;
use serde::Serialize;
use std::collections::HashMap;
use std::env;
use std::time::Duration;
use tokio::net::TcpStream;
use tokio::sync::mpsc;
use tokio::time::{Instant, interval_at};
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async, tungstenite::Message};
use tracing::{debug, trace, warn};

const ENV_API_KEY: &str = "MASSIVE_API_KEY";

const PING_INTERVAL: Duration = Duration::from_secs(20);
const PONG_TIMEOUT: Duration = Duration::from_secs(19);

/// Market type for WebSocket connection.
///
/// Each market requires a separate WebSocket connection to a different endpoint.
/// Massive allows one simultaneous connection per cluster (market type).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Market {
    /// US equities (NYSE, NASDAQ, etc.)
    Stocks,
    /// Cryptocurrency pairs (BTC-USD, ETH-USD, etc.)
    Crypto,
    /// Foreign exchange pairs (EUR-USD, GBP-USD, etc.)
    Forex,
    /// US equity options
    Options,
}

impl Market {
    /// Get the WebSocket endpoint URL for this market.
    ///
    /// Massive operates on Polygon.io infrastructure; endpoints are unchanged from Polygon.
    fn ws_url(&self) -> &'static str {
        match self {
            Market::Stocks => "wss://socket.polygon.io/stocks",
            Market::Crypto => "wss://socket.polygon.io/crypto",
            Market::Forex => "wss://socket.polygon.io/forex",
            Market::Options => "wss://socket.polygon.io/options",
        }
    }
}

/// Channel types for WebSocket subscriptions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ChannelType {
    /// Tick-level trades
    /// - Stocks: `T.AAPL`
    /// - Crypto: `XT.BTC-USD`
    /// - Forex: Not available (use Quote instead)
    Trade,
    /// Quote updates / BBO
    /// - Stocks: `Q.AAPL`
    /// - Crypto: `XQ.BTC-USD`
    /// - Forex: `C.EUR-USD`
    Quote,
    /// Per-second aggregates
    /// - Stocks: `A.AAPL`
    /// - Crypto: `XA.BTC-USD`
    /// - Forex: `CA.EUR-USD`
    AggregateSecond,
    /// Per-minute aggregates
    /// - Stocks: `AM.AAPL`
    /// - Crypto: `XAM.BTC-USD`
    /// - Forex: `CAM.EUR-USD`
    AggregateMinute,
}

impl ChannelType {
    /// Get the channel prefix for a given market.
    ///
    /// Returns `None` if the channel type is not supported for the market
    /// (e.g., Trade for Forex).
    fn prefix(self, market: Market) -> Option<&'static str> {
        match (self, market) {
            // Stocks
            (ChannelType::Trade, Market::Stocks) => Some("T"),
            (ChannelType::Quote, Market::Stocks) => Some("Q"),
            (ChannelType::AggregateSecond, Market::Stocks) => Some("A"),
            (ChannelType::AggregateMinute, Market::Stocks) => Some("AM"),
            // Crypto
            (ChannelType::Trade, Market::Crypto) => Some("XT"),
            (ChannelType::Quote, Market::Crypto) => Some("XQ"),
            (ChannelType::AggregateSecond, Market::Crypto) => Some("XA"),
            (ChannelType::AggregateMinute, Market::Crypto) => Some("XAM"),
            // Forex (no trades channel)
            (ChannelType::Trade, Market::Forex) => None,
            (ChannelType::Quote, Market::Forex) => Some("C"),
            (ChannelType::AggregateSecond, Market::Forex) => Some("CA"),
            (ChannelType::AggregateMinute, Market::Forex) => Some("CAM"),
            // Options
            (ChannelType::Trade, Market::Options) => Some("T"),
            (ChannelType::Quote, Market::Options) => Some("Q"),
            (ChannelType::AggregateSecond, Market::Options) => Some("A"),
            (ChannelType::AggregateMinute, Market::Options) => Some("AM"),
        }
    }

    /// Build channel string for a symbol.
    ///
    /// Returns `None` if this channel type is not supported for the market.
    fn channel_for(self, market: Market, symbol: &str) -> Option<String> {
        let prefix = self.prefix(market)?;
        Some(format!("{}.{}", prefix, symbol))
    }
}

/// WebSocket message for authentication.
#[derive(Serialize)]
struct AuthMessage<'a> {
    action: &'static str,
    params: &'a str,
}

/// WebSocket message for subscription.
#[derive(Serialize)]
struct SubscribeMessage {
    action: &'static str,
    params: String,
}

/// Live streaming client for Massive WebSocket API.
///
/// Connects to Massive's WebSocket endpoint for real-time market data.
/// Each instance connects to one market (stocks, crypto, forex, options).
///
/// # Connection Limits
///
/// See module docs and <https://massive.com/knowledge-base/article/how-many-massive-websocket-connections-can-i-use-at-one-time>
pub struct MassiveLive<K> {
    api_key: String,
    market: Market,
    instruments: HashMap<String, K>,
    exchange: ExchangeId,
    subscriptions: Vec<String>,
    ws_url: String,
}

impl<K: std::fmt::Debug> std::fmt::Debug for MassiveLive<K> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MassiveLive")
            .field("api_key", &"[REDACTED]")
            .field("market", &self.market)
            .field("instruments", &self.instruments)
            .field("exchange", &self.exchange)
            .field("subscriptions", &self.subscriptions)
            .field("ws_url", &self.ws_url)
            .finish()
    }
}

impl<K> MassiveLive<K> {
    /// Create a new live client with explicit API key.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Massive API key from <https://massive.com/dashboard/api-keys>
    /// * `market` - Market type to connect to
    /// * `exchange` - ExchangeId to tag events with
    /// * `instruments` - Map from WebSocket symbol strings to user's instrument keys
    ///
    /// # Symbol Formats
    ///
    /// Use WebSocket-native formats (different from REST API):
    /// - Crypto: `BTC-USD`, `ETH-USD` (hyphenated)
    /// - Forex: `EUR-USD`, `GBP-USD` (hyphenated)
    /// - Stocks: `AAPL`, `MSFT` (plain)
    /// - Options: `O:AAPL251219C00150000`
    pub fn new(
        api_key: impl Into<String>,
        market: Market,
        exchange: ExchangeId,
        instruments: HashMap<String, K>,
    ) -> Self {
        Self {
            api_key: api_key.into(),
            market,
            instruments,
            exchange,
            subscriptions: Vec::new(),
            ws_url: market.ws_url().to_string(),
        }
    }

    /// Create a new live client from `MASSIVE_API_KEY` environment variable.
    ///
    /// # Errors
    ///
    /// Returns error if `MASSIVE_API_KEY` is not set.
    pub fn from_env(
        market: Market,
        exchange: ExchangeId,
        instruments: HashMap<String, K>,
    ) -> Result<Self, MassiveError> {
        let api_key =
            env::var(ENV_API_KEY).map_err(|_| MassiveError::EnvVar { var: ENV_API_KEY })?;
        Ok(Self::new(api_key, market, exchange, instruments))
    }

    /// Override the WebSocket URL (useful for testing).
    #[must_use]
    pub fn with_ws_url(mut self, url: impl Into<String>) -> Self {
        self.ws_url = url.into();
        self
    }

    /// Get the market type for this client.
    pub fn market(&self) -> Market {
        self.market
    }

    /// Subscribe to channels for the given symbols.
    ///
    /// Can be called multiple times before `start()` to add more subscriptions.
    /// Subscriptions are batched and sent after authentication.
    ///
    /// # Arguments
    ///
    /// * `symbols` - Symbol identifiers in WebSocket format:
    ///   - Crypto: `BTC-USD`, `ETH-USD` (hyphenated)
    ///   - Forex: `EUR-USD`, `GBP-USD` (hyphenated)
    ///   - Stocks: `AAPL`, `MSFT` (plain)
    ///   - Options: `O:AAPL251219C00150000`
    /// * `channel_type` - Type of data to subscribe to
    ///
    /// # Notes
    ///
    /// If `channel_type` is not supported for this market (e.g., `Trade` for `Forex`),
    /// a warning is logged and the subscription is skipped.
    pub fn subscribe(&mut self, symbols: &[&str], channel_type: ChannelType) {
        for symbol in symbols {
            if let Some(channel) = channel_type.channel_for(self.market, symbol) {
                debug!(channel = %channel, "Adding subscription");
                self.subscriptions.push(channel);
            } else {
                warn!(
                    symbol = %symbol,
                    channel_type = ?channel_type,
                    market = ?self.market,
                    "Channel type not supported for this market, skipping"
                );
            }
        }
    }

    /// Get the list of pending subscriptions.
    pub fn subscriptions(&self) -> &[String] {
        &self.subscriptions
    }
}

impl<K: Clone + Send + 'static> MassiveLive<K> {
    /// Connect, authenticate, subscribe, and start streaming.
    ///
    /// Consumes `self` because the WebSocket connection requires exclusive access.
    /// The stream will emit events until the connection closes or an error occurs.
    ///
    /// # Returns
    ///
    /// A stream of `MarketEvent<K, DataKind>` where `DataKind` depends on the
    /// subscribed channels (Trade, OrderBookL1, or Candle).
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - WebSocket connection fails
    /// - Authentication fails
    /// - Subscription fails
    ///
    /// Stream items may contain `MassiveError::Disconnected` if:
    /// - Connection drops unexpectedly
    /// - Pong not received within one ping interval (checked at each 20s tick)
    pub async fn start(
        self,
    ) -> Result<impl Stream<Item = Result<MarketEvent<K, DataKind>, MassiveError>>, MassiveError>
    {
        debug!(url = %self.ws_url, market = ?self.market, "Connecting to Massive WebSocket");

        // Connect
        let (ws_stream, _response) = connect_async(&self.ws_url).await?;
        let (mut write, mut read) = ws_stream.split();

        // Consume the initial "connected" status frame before sending auth
        Self::consume_connected_frame(&mut read).await?;

        // Authenticate
        debug!("Sending auth message");
        let auth_msg = AuthMessage {
            action: "auth",
            params: &self.api_key,
        };
        // AuthMessage contains only &'static str and &str — serialization cannot fail
        #[allow(clippy::expect_used)]
        let auth_json = serde_json::to_string(&auth_msg).expect("infallible");
        write.send(Message::Text(auth_json.into())).await?;

        // Wait for auth response
        let auth_response = tokio::time::timeout(Duration::from_secs(10), read.next())
            .await
            .map_err(|_| MassiveError::Auth {
                message: "Auth response timeout".into(),
            })?
            .ok_or_else(|| MassiveError::Disconnected {
                reason: "Connection closed before auth response".into(),
            })??;

        Self::verify_auth_response(&auth_response)?;
        debug!("Authentication successful");

        // Subscribe to channels
        if self.subscriptions.is_empty() {
            warn!("start() called with no subscriptions; stream will receive no market data");
        }
        if !self.subscriptions.is_empty() {
            let params = self.subscriptions.join(",");
            debug!(channels = %params, "Subscribing to channels");

            let sub_msg = SubscribeMessage {
                action: "subscribe",
                params,
            };
            // SubscribeMessage contains only &'static str and String — serialization cannot fail
            #[allow(clippy::expect_used)]
            let sub_json = serde_json::to_string(&sub_msg).expect("infallible");
            write.send(Message::Text(sub_json.into())).await?;

            // Wait for subscription confirmation
            let sub_response = tokio::time::timeout(Duration::from_secs(10), read.next())
                .await
                .map_err(|_| MassiveError::Disconnected {
                    reason: "Subscription response timeout".into(),
                })?
                .ok_or_else(|| MassiveError::Disconnected {
                    reason: "Connection closed before subscription response".into(),
                })??;

            Self::verify_subscription_response(&sub_response)?;
            debug!("Subscription successful");
        }

        // Reunite the stream for the read loop
        let ws_stream = write
            .reunite(read)
            .map_err(|e| MassiveError::Disconnected {
                reason: format!("Failed to reunite WebSocket stream: {}", e),
            })?;

        // Create the event stream with ping/pong handling
        Ok(Self::create_event_stream(
            ws_stream,
            self.instruments,
            self.exchange,
        ))
    }

    /// Consume the initial "connected" status frame sent by the server on connect.
    ///
    /// Polygon/Massive sends `[{"ev":"status","status":"connected",...}]` immediately
    /// when a WebSocket connection is established, before the client sends auth.
    async fn consume_connected_frame(
        read: &mut futures::stream::SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
    ) -> Result<(), MassiveError> {
        let msg = tokio::time::timeout(Duration::from_secs(10), read.next())
            .await
            .map_err(|_| MassiveError::Disconnected {
                reason: "Timeout waiting for connected frame".into(),
            })?
            .ok_or_else(|| MassiveError::Disconnected {
                reason: "Connection closed before connected frame".into(),
            })??;

        match &msg {
            Message::Text(text) => {
                // Verify it's a "connected" status message
                if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(text)
                    && let Some(arr) = parsed.as_array()
                {
                    for m in arr {
                        if m.get("ev").and_then(|v| v.as_str()) == Some("status")
                            && m.get("status").and_then(|v| v.as_str()) == Some("connected")
                        {
                            debug!("Received connected frame");
                            return Ok(());
                        }
                    }
                }
                // Not a connected frame - unexpected but continue anyway
                debug!(msg = %text, "Unexpected initial frame (expected connected status)");
                Ok(())
            }
            Message::Close(frame) => Err(MassiveError::Disconnected {
                reason: frame
                    .as_ref()
                    .map(|f| f.reason.to_string())
                    .unwrap_or_else(|| "Connection closed".into()),
            }),
            _ => {
                // Intentional: unexpected non-text/non-close frames (e.g., Ping) are ignored;
                // the server drove an unusual handshake but we continue to auth
                debug!(?msg, "Unexpected message type before auth");
                Ok(())
            }
        }
    }

    /// Verify the authentication response.
    fn verify_auth_response(msg: &Message) -> Result<(), MassiveError> {
        match msg {
            Message::Text(text) => {
                // Parse status message: [{"ev":"status","status":"auth_success",...}]
                let parsed: serde_json::Value =
                    serde_json::from_str(text).map_err(|e| MassiveError::Auth {
                        message: format!("Failed to parse auth response: {}", e),
                    })?;

                // Response is an array of status messages
                let messages = parsed.as_array().ok_or_else(|| MassiveError::Auth {
                    message: "Auth response is not an array".into(),
                })?;

                for msg in messages {
                    if msg.get("ev").and_then(|v| v.as_str()) == Some("status") {
                        let status = msg.get("status").and_then(|v| v.as_str());
                        match status {
                            Some("auth_success") => return Ok(()),
                            Some("auth_failed") => {
                                let message = msg
                                    .get("message")
                                    .and_then(|v| v.as_str())
                                    .unwrap_or("Authentication failed");
                                return Err(MassiveError::Auth {
                                    message: message.into(),
                                });
                            }
                            _ => {}
                        }
                    }
                }

                Err(MassiveError::Auth {
                    message: format!("Unexpected auth response: {}", text),
                })
            }
            Message::Close(frame) => Err(MassiveError::Disconnected {
                reason: frame
                    .as_ref()
                    .map(|f| f.reason.to_string())
                    .unwrap_or_else(|| "Connection closed".into()),
            }),
            _ => Err(MassiveError::Auth {
                message: format!("Unexpected message type during auth: {:?}", msg),
            }),
        }
    }

    /// Verify the subscription response.
    fn verify_subscription_response(msg: &Message) -> Result<(), MassiveError> {
        match msg {
            Message::Text(text) => {
                // Parse status message: [{"ev":"status","status":"success","message":"subscribed to: ..."}]
                let parsed: serde_json::Value =
                    serde_json::from_str(text).map_err(|e| MassiveError::Disconnected {
                        reason: format!("Failed to parse subscription response: {}", e),
                    })?;

                let messages = parsed
                    .as_array()
                    .ok_or_else(|| MassiveError::Disconnected {
                        reason: "Subscription response is not an array".into(),
                    })?;

                for msg in messages {
                    if msg.get("ev").and_then(|v| v.as_str()) == Some("status") {
                        let status = msg.get("status").and_then(|v| v.as_str());
                        if status == Some("success") {
                            return Ok(());
                        }
                    }
                }

                // No success status found — observable failure over silent one
                Err(MassiveError::Disconnected {
                    reason: format!("Subscription failed: {}", text),
                })
            }
            Message::Close(frame) => Err(MassiveError::Disconnected {
                reason: frame
                    .as_ref()
                    .map(|f| f.reason.to_string())
                    .unwrap_or_else(|| "Connection closed".into()),
            }),
            _ => {
                // Observable failure over silent one: unexpected message types should error
                Err(MassiveError::Disconnected {
                    reason: format!("Unexpected message type during subscription: {:?}", msg),
                })
            }
        }
    }

    /// Create the event stream with ping/pong handling.
    ///
    /// # Note
    ///
    /// If the internal streaming task panics, the stream ends without emitting an error item.
    /// Panics in the streaming task are not expected under normal operation.
    fn create_event_stream(
        ws_stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
        instruments: HashMap<String, K>,
        exchange: ExchangeId,
    ) -> impl Stream<Item = Result<MarketEvent<K, DataKind>, MassiveError>> {
        let (mut write, mut read) = ws_stream.split();

        // Channel for passing events from the read task
        let (tx, rx) = mpsc::unbounded_channel();

        // Spawn the read/ping task
        tokio::spawn(async move {
            // Skip the immediate first tick; first ping sent after PING_INTERVAL
            let mut ping_interval = interval_at(Instant::now() + PING_INTERVAL, PING_INTERVAL);
            let mut ping_sent_at: Option<Instant> = None;

            loop {
                tokio::select! {
                    // Send ping at regular intervals
                    _ = ping_interval.tick() => {
                        // Check if we're still waiting for a pong from the last ping
                        if let Some(sent_at) = ping_sent_at
                            && sent_at.elapsed() > PONG_TIMEOUT
                        {
                            let _ = tx.send(Err(MassiveError::Disconnected {
                                reason: "Pong timeout".into(),
                            }));
                            break;
                        }

                        trace!("Sending ping");
                        if let Err(e) = write.send(Message::Ping(vec![].into())).await {
                            let _ = tx.send(Err(MassiveError::Disconnected {
                                reason: format!("Failed to send ping: {}", e),
                            }));
                            break;
                        }
                        ping_sent_at = Some(Instant::now());
                    }

                    // Read incoming messages
                    msg = read.next() => {
                        match msg {
                            Some(Ok(Message::Text(text))) => {
                                // Sample time once per frame for all events
                                let time_received = Utc::now();
                                // Parse and emit market events
                                match parse_ws_message(&text) {
                                    Ok(messages) => {
                                        for ws_msg in messages {
                                            if let Some(event) = Self::ws_message_to_event(
                                                ws_msg,
                                                &instruments,
                                                exchange,
                                                time_received,
                                            ) && tx.send(Ok(event)).is_err() {
                                                // Receiver dropped
                                                return;
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        warn!(error = %e, "Failed to parse WebSocket message");
                                    }
                                }
                            }
                            Some(Ok(Message::Pong(_))) => {
                                trace!("Received pong");
                                ping_sent_at = None;
                            }
                            Some(Ok(Message::Ping(data))) => {
                                trace!("Received ping, sending pong");
                                if let Err(e) = write.send(Message::Pong(data)).await {
                                    let _ = tx.send(Err(MassiveError::Disconnected {
                                        reason: format!("Failed to send pong: {}", e),
                                    }));
                                    break;
                                }
                            }
                            Some(Ok(Message::Close(frame))) => {
                                let reason = frame
                                    .as_ref()
                                    .map(|f| f.reason.to_string())
                                    .unwrap_or_else(|| "Connection closed".into());
                                let _ = tx.send(Err(MassiveError::Disconnected { reason }));
                                break;
                            }
                            Some(Ok(Message::Binary(_))) => {
                                // Unexpected binary message, ignore
                                trace!("Received unexpected binary message");
                            }
                            Some(Ok(Message::Frame(_))) => {
                                // Raw frame, ignore
                            }
                            Some(Err(e)) => {
                                let _ = tx.send(Err(MassiveError::Disconnected {
                                    reason: e.to_string(),
                                }));
                                break;
                            }
                            None => {
                                let _ = tx.send(Err(MassiveError::Disconnected {
                                    reason: "WebSocket stream ended".into(),
                                }));
                                break;
                            }
                        }
                    }
                }
            }
        });

        // Convert the channel receiver into a stream
        futures::stream::unfold(rx, |mut rx| async move {
            rx.recv().await.map(|item| (item, rx))
        })
    }

    /// Convert a WebSocket message to a MarketEvent.
    fn ws_message_to_event(
        msg: WsMessage,
        instruments: &HashMap<String, K>,
        exchange: ExchangeId,
        time_received: DateTime<Utc>,
    ) -> Option<MarketEvent<K, DataKind>> {
        match msg {
            WsMessage::TradeStocks(trade) | WsMessage::TradeCrypto(trade) => {
                let instrument = instruments.get(&trade.symbol)?.clone();
                let (time_exchange, public_trade) = trade.into_public_trade();

                Some(MarketEvent {
                    time_exchange,
                    time_received,
                    exchange,
                    instrument,
                    kind: DataKind::Trade(public_trade),
                })
            }
            WsMessage::QuoteStocks(quote)
            | WsMessage::QuoteCrypto(quote)
            | WsMessage::QuoteForex(quote) => {
                let instrument = instruments.get(&quote.symbol)?.clone();
                let (time_exchange, l1) = quote.into_order_book_l1();

                Some(MarketEvent {
                    time_exchange,
                    time_received,
                    exchange,
                    instrument,
                    kind: DataKind::OrderBookL1(l1),
                })
            }
            WsMessage::AggSecondStocks(agg)
            | WsMessage::AggMinuteStocks(agg)
            | WsMessage::AggSecondCrypto(agg)
            | WsMessage::AggMinuteCrypto(agg)
            | WsMessage::AggSecondForex(agg)
            | WsMessage::AggMinuteForex(agg) => {
                let instrument = instruments.get(&agg.symbol)?.clone();
                let (time_exchange, candle) = agg.into_candle();

                Some(MarketEvent {
                    time_exchange,
                    time_received,
                    exchange,
                    instrument,
                    kind: DataKind::Candle(candle),
                })
            }
            WsMessage::Status(_) => {
                // Status messages are handled during auth/subscribe, not emitted as events
                None
            }
        }
    }
}

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

    #[test]
    fn test_market_ws_url() {
        assert_eq!(Market::Stocks.ws_url(), "wss://socket.polygon.io/stocks");
        assert_eq!(Market::Crypto.ws_url(), "wss://socket.polygon.io/crypto");
        assert_eq!(Market::Forex.ws_url(), "wss://socket.polygon.io/forex");
        assert_eq!(Market::Options.ws_url(), "wss://socket.polygon.io/options");
    }

    #[test]
    fn test_channel_type_prefix_stocks() {
        assert_eq!(ChannelType::Trade.prefix(Market::Stocks), Some("T"));
        assert_eq!(ChannelType::Quote.prefix(Market::Stocks), Some("Q"));
        assert_eq!(
            ChannelType::AggregateSecond.prefix(Market::Stocks),
            Some("A")
        );
        assert_eq!(
            ChannelType::AggregateMinute.prefix(Market::Stocks),
            Some("AM")
        );
    }

    #[test]
    fn test_channel_type_prefix_crypto() {
        assert_eq!(ChannelType::Trade.prefix(Market::Crypto), Some("XT"));
        assert_eq!(ChannelType::Quote.prefix(Market::Crypto), Some("XQ"));
        assert_eq!(
            ChannelType::AggregateSecond.prefix(Market::Crypto),
            Some("XA")
        );
        assert_eq!(
            ChannelType::AggregateMinute.prefix(Market::Crypto),
            Some("XAM")
        );
    }

    #[test]
    fn test_channel_type_prefix_forex() {
        // Forex has no trades channel
        assert_eq!(ChannelType::Trade.prefix(Market::Forex), None);
        assert_eq!(ChannelType::Quote.prefix(Market::Forex), Some("C"));
        assert_eq!(
            ChannelType::AggregateSecond.prefix(Market::Forex),
            Some("CA")
        );
        assert_eq!(
            ChannelType::AggregateMinute.prefix(Market::Forex),
            Some("CAM")
        );
    }

    #[test]
    fn test_channel_for() {
        // Crypto
        assert_eq!(
            ChannelType::Trade.channel_for(Market::Crypto, "BTC-USD"),
            Some("XT.BTC-USD".to_string())
        );
        assert_eq!(
            ChannelType::Quote.channel_for(Market::Crypto, "BTC-USD"),
            Some("XQ.BTC-USD".to_string())
        );
        assert_eq!(
            ChannelType::AggregateMinute.channel_for(Market::Crypto, "BTC-USD"),
            Some("XAM.BTC-USD".to_string())
        );

        // Stocks
        assert_eq!(
            ChannelType::Trade.channel_for(Market::Stocks, "AAPL"),
            Some("T.AAPL".to_string())
        );
        assert_eq!(
            ChannelType::Quote.channel_for(Market::Stocks, "AAPL"),
            Some("Q.AAPL".to_string())
        );
        assert_eq!(
            ChannelType::AggregateMinute.channel_for(Market::Stocks, "AAPL"),
            Some("AM.AAPL".to_string())
        );

        // Forex (no trades)
        assert_eq!(
            ChannelType::Trade.channel_for(Market::Forex, "EUR-USD"),
            None
        );
        assert_eq!(
            ChannelType::Quote.channel_for(Market::Forex, "EUR-USD"),
            Some("C.EUR-USD".to_string())
        );
    }

    #[test]
    fn test_subscribe_accumulates() {
        let instruments: HashMap<String, String> = HashMap::new();
        let mut client =
            MassiveLive::new("test_key", Market::Crypto, ExchangeId::Massive, instruments);

        client.subscribe(&["BTC-USD", "ETH-USD"], ChannelType::Trade);
        client.subscribe(&["BTC-USD"], ChannelType::Quote);

        assert_eq!(client.subscriptions().len(), 3);
        assert!(client.subscriptions().contains(&"XT.BTC-USD".to_string()));
        assert!(client.subscriptions().contains(&"XT.ETH-USD".to_string()));
        assert!(client.subscriptions().contains(&"XQ.BTC-USD".to_string()));
    }

    #[test]
    fn test_subscribe_skips_unsupported() {
        let instruments: HashMap<String, String> = HashMap::new();
        let mut client =
            MassiveLive::new("test_key", Market::Forex, ExchangeId::Massive, instruments);

        // Forex doesn't support trades
        client.subscribe(&["EUR-USD"], ChannelType::Trade);
        client.subscribe(&["EUR-USD"], ChannelType::Quote);

        // Only quote subscription should be added
        assert_eq!(client.subscriptions().len(), 1);
        assert!(client.subscriptions().contains(&"C.EUR-USD".to_string()));
    }

    #[test]
    fn test_from_env_missing() {
        temp_env::with_var_unset(ENV_API_KEY, || {
            let result: Result<MassiveLive<String>, _> =
                MassiveLive::from_env(Market::Crypto, ExchangeId::Massive, HashMap::new());
            assert!(matches!(result, Err(MassiveError::EnvVar { .. })));
        });
    }

    #[test]
    fn test_with_ws_url_override() {
        let instruments: HashMap<String, String> = HashMap::new();
        let client = MassiveLive::new("test_key", Market::Crypto, ExchangeId::Massive, instruments)
            .with_ws_url("wss://test.example.com/crypto");

        assert_eq!(client.ws_url, "wss://test.example.com/crypto");
    }

    // ========================================================================
    // Auth/Subscription Response Tests
    // ========================================================================

    #[test]
    fn test_verify_auth_response_success() {
        let msg = Message::Text(
            r#"[{"ev":"status","status":"auth_success","message":"authenticated"}]"#.into(),
        );
        assert!(MassiveLive::<String>::verify_auth_response(&msg).is_ok());
    }

    #[test]
    fn test_verify_auth_response_failed() {
        let msg = Message::Text(
            r#"[{"ev":"status","status":"auth_failed","message":"invalid api key"}]"#.into(),
        );
        let result = MassiveLive::<String>::verify_auth_response(&msg);
        assert!(matches!(result, Err(MassiveError::Auth { .. })));
    }

    #[test]
    fn test_verify_auth_response_close_frame() {
        let msg = Message::Close(Some(tokio_tungstenite::tungstenite::protocol::CloseFrame {
            code: tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode::Normal,
            reason: "server shutdown".into(),
        }));
        let result = MassiveLive::<String>::verify_auth_response(&msg);
        assert!(matches!(result, Err(MassiveError::Disconnected { .. })));
    }

    #[test]
    fn test_verify_auth_response_connected_status_is_unexpected() {
        // "connected" status should be consumed before auth is sent,
        // so if we see it here, it's unexpected and should error
        let msg = Message::Text(
            r#"[{"ev":"status","status":"connected","message":"Connected Successfully"}]"#.into(),
        );
        let result = MassiveLive::<String>::verify_auth_response(&msg);
        assert!(matches!(result, Err(MassiveError::Auth { .. })));
    }

    #[test]
    fn test_verify_subscription_response_success() {
        let msg = Message::Text(
            r#"[{"ev":"status","status":"success","message":"subscribed to: XT.BTC-USD"}]"#.into(),
        );
        assert!(MassiveLive::<String>::verify_subscription_response(&msg).is_ok());
    }

    #[test]
    fn test_verify_subscription_response_failure() {
        // No "success" status in response should now return error
        let msg = Message::Text(
            r#"[{"ev":"status","status":"error","message":"invalid symbol"}]"#.into(),
        );
        let result = MassiveLive::<String>::verify_subscription_response(&msg);
        assert!(matches!(result, Err(MassiveError::Disconnected { .. })));
    }

    #[test]
    fn test_verify_subscription_response_close_frame() {
        let msg = Message::Close(None);
        let result = MassiveLive::<String>::verify_subscription_response(&msg);
        assert!(matches!(result, Err(MassiveError::Disconnected { .. })));
    }
}