test(sdk_relay): tests fonctionnels sync (/relays, /sync/status, /sync/force) et WS (ping, subscribe)

This commit is contained in:
Nicolas Cantu 2025-08-25 15:41:01 +02:00
parent 636061f064
commit 238f200e1c
2 changed files with 86 additions and 0 deletions

44
tests/functional_sync.rs Normal file
View File

@ -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");
}

42
tests/functional_ws.rs Normal file
View File

@ -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");
}