use std::time::Duration; async fn service_available(base: &str) -> bool { let client = match reqwest::Client::builder().timeout(Duration::from_millis(500)).build() { Ok(c) => c, Err(_) => return false, }; let url = format!("{}/health", base); match client.get(url).send().await { Ok(resp) => resp.status().is_success(), Err(_) => false, } } #[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()); if !service_available(&base).await { eprintln!("sdk_relay indisponible, test /relays ignoré"); return; } 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()); if !service_available(&base).await { eprintln!("sdk_relay indisponible, test /sync/status ignoré"); return; } 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"]}); if !service_available(&base).await { eprintln!("sdk_relay indisponible, test /sync/force ignoré"); return; } 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"); }