From e0b37fde6381504077d905eec9b35e310ec60b40 Mon Sep 17 00:00:00 2001 From: Nicolas Cantu Date: Mon, 25 Aug 2025 15:41:01 +0200 Subject: [PATCH] test(sdk_relay): tests fonctionnels sync (/relays, /sync/status, /sync/force) et WS (ping, subscribe) --- tests/functional_sync.rs | 44 ++++++++++++++++++++++++++++++++++++++++ tests/functional_ws.rs | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/functional_sync.rs create mode 100644 tests/functional_ws.rs diff --git a/tests/functional_sync.rs b/tests/functional_sync.rs new file mode 100644 index 0000000..d2f37a7 --- /dev/null +++ b/tests/functional_sync.rs @@ -0,0 +1,44 @@ +use std::time::Duration; + +#[tokio::test] +async fn relays_listing_should_return_array() { + let client = reqwest::Client::builder() + .timeout(Duration::from_secs(5)) + .build() + .expect("client"); + let base = std::env::var("SDK_RELAY_HTTP").unwrap_or_else(|_| "http://localhost:8091".to_string()); + let res = client.get(format!("{}/relays", base)).send().await.expect("/relays call"); + assert!(res.status().is_success()); + let json: serde_json::Value = res.json().await.expect("json"); + assert!(json.get("relays").and_then(|v| v.as_array()).is_some(), "relays should be array"); +} + +#[tokio::test] +async fn sync_status_should_contain_sync_types() { + let client = reqwest::Client::builder() + .timeout(Duration::from_secs(5)) + .build() + .expect("client"); + let base = std::env::var("SDK_RELAY_HTTP").unwrap_or_else(|_| "http://localhost:8091".to_string()); + let res = client.get(format!("{}/sync/status", base)).send().await.expect("/sync/status call"); + assert!(res.status().is_success()); + let json: serde_json::Value = res.json().await.expect("json"); + assert!(json.get("sync_types").and_then(|v| v.as_array()).is_some(), "sync_types should be array"); +} + +#[tokio::test] +async fn forcing_sync_should_return_sync_triggered() { + let client = reqwest::Client::builder() + .timeout(Duration::from_secs(5)) + .build() + .expect("client"); + let base = std::env::var("SDK_RELAY_HTTP").unwrap_or_else(|_| "http://localhost:8091".to_string()); + let body = serde_json::json!({"sync_types":["StateSync"]}); + let res = client.post(format!("{}/sync/force", base)) + .json(&body) + .send().await.expect("/sync/force call"); + assert!(res.status().is_success()); + let json: serde_json::Value = res.json().await.expect("json"); + let status = json.get("status").and_then(|v| v.as_str()).unwrap_or(""); + assert_eq!(status, "sync_triggered"); +} diff --git a/tests/functional_ws.rs b/tests/functional_ws.rs new file mode 100644 index 0000000..7b2b228 --- /dev/null +++ b/tests/functional_ws.rs @@ -0,0 +1,42 @@ +use futures_util::{SinkExt, StreamExt}; +use serde_json::json; +use tokio_tungstenite::connect_async; + +#[tokio::test] +async fn websocket_ping_pong_should_work() { + let url = std::env::var("SDK_RELAY_WS").unwrap_or_else(|_| "ws://localhost:8090".to_string()); + let (mut ws, _) = connect_async(url).await.expect("connect ws"); + + let ping = json!({"type":"ping","client_id":"functional-test","timestamp":1703001600u64}).to_string(); + ws.send(tokio_tungstenite::tungstenite::Message::Text(ping)) + .await + .expect("send ping"); + + let msg = ws.next().await.expect("no response").expect("ws err"); + let txt = msg.into_text().expect("not text"); + let json: serde_json::Value = serde_json::from_str(&txt).expect("invalid json"); + assert_eq!(json.get("type").and_then(|v| v.as_str()).unwrap_or(""), "pong"); +} + +#[tokio::test] +async fn websocket_subscribe_should_ack() { + let url = std::env::var("SDK_RELAY_WS").unwrap_or_else(|_| "ws://localhost:8090".to_string()); + let (mut ws, _) = connect_async(url).await.expect("connect ws"); + + let subscribe = json!({ + "type":"subscribe", + "subscriptions":["notifications","health","metrics"], + "client_id":"functional-test", + "timestamp":1703001600u64 + }).to_string(); + + ws.send(tokio_tungstenite::tungstenite::Message::Text(subscribe)) + .await + .expect("send subscribe"); + + let msg = ws.next().await.expect("no response").expect("ws err"); + let txt = msg.into_text().expect("not text"); + let json: serde_json::Value = serde_json::from_str(&txt).expect("invalid json"); + assert_eq!(json.get("type").and_then(|v| v.as_str()).unwrap_or(""), "subscribe_response"); + assert_eq!(json.get("status").and_then(|v| v.as_str()).unwrap_or(""), "subscribed"); +}