use polymarket_api::websocket::{PolymarketWebSocket, SubscriptionMessage};
#[test]
fn test_subscription_message_serialization() {
let msg = SubscriptionMessage {
auth: None,
markets: None,
assets_ids: Some(vec!["test_token_1".to_string(), "test_token_2".to_string()]),
channel_type: "MARKET".to_string(),
custom_feature_enabled: None,
};
let json = serde_json::to_string(&msg).expect("Should serialize");
assert!(json.contains("\"type\":\"MARKET\""));
assert!(json.contains("test_token_1"));
assert!(json.contains("test_token_2"));
assert!(!json.contains("auth")); assert!(!json.contains("markets")); }
#[test]
fn test_websocket_client_creation() {
let asset_ids = vec!["token1".to_string(), "token2".to_string()];
let _client = PolymarketWebSocket::new(asset_ids.clone());
}
#[tokio::test]
async fn test_websocket_connection_with_subscription() {
use {
futures_util::{SinkExt, StreamExt},
tokio_tungstenite::{connect_async, tungstenite::Message},
};
let (ws_stream, _) =
match connect_async("wss://ws-subscriptions-clob.polymarket.com/ws/market").await {
Ok(stream) => stream,
Err(e) => {
eprintln!("WebSocket connection test skipped: {}", e);
return;
},
};
let (mut write, mut read) = ws_stream.split();
let subscribe_msg = serde_json::json!({
"type": "MARKET",
"assets_ids": ["test_token"]
});
if write
.send(Message::Text(subscribe_msg.to_string()))
.await
.is_ok()
{
use tokio::time::{Duration, timeout};
if let Ok(Some(Ok(Message::Text(_)))) = timeout(Duration::from_secs(2), read.next()).await {
}
}
}