use axum::{body::{to_bytes, Body}, http::{Request, StatusCode}}; use hermes_backend::{app_state::AppState, build_router, config::AppConfig}; use serde_json as json; use tower::ServiceExt; #[tokio::test] async fn health_returns_ok() { let app = build_router(AppState::new(AppConfig::default(), None, None)); let response = app .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); } #[tokio::test] async fn session_start_and_me_work() { let app = build_router(AppState::new(AppConfig::default(), None, None)); let response = app .clone() .oneshot( Request::builder() .method("POST") .uri("/api/v1/session/start") .header("content-type", "application/json") .body(Body::from("{}")) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::CREATED); let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); let json: json::Value = json::from_slice(&body).unwrap(); assert_eq!(json["locale_code"], "en"); let response = app .oneshot(Request::builder().uri("/api/v1/session/me").body(Body::empty()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); } #[tokio::test] async fn participant_ref_reuses_the_same_user() { let app = build_router(AppState::new(AppConfig::default(), None, None)); let body = json::json!({ "external_ref": "participant-001", "locale_code": "sv" }) .to_string(); let first = app .clone() .oneshot( Request::builder() .method("POST") .uri("/api/v1/session/start") .header("content-type", "application/json") .body(Body::from(body.clone())) .unwrap(), ) .await .unwrap(); let first_body = to_bytes(first.into_body(), usize::MAX).await.unwrap(); let first_json: json::Value = json::from_slice(&first_body).unwrap(); let second = app .oneshot( Request::builder() .method("POST") .uri("/api/v1/session/start") .header("content-type", "application/json") .body(Body::from(body)) .unwrap(), ) .await .unwrap(); let second_body = to_bytes(second.into_body(), usize::MAX).await.unwrap(); let second_json: json::Value = json::from_slice(&second_body).unwrap(); assert_eq!(first_json["user_id"], second_json["user_id"]); assert_eq!(first_json["locale_code"], "sv"); } #[tokio::test] async fn feed_next_returns_a_manifestable_event() { let app = build_router(AppState::new(AppConfig::default(), None, None)); let response = app .clone() .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); let event: json::Value = json::from_slice(&body).unwrap(); let event_id = event["id"].as_str().unwrap().to_string(); assert_eq!(event["status"], "prefetch_ready"); let response = app .clone() .oneshot( Request::builder() .uri(format!("/api/v1/events/{event_id}/manifest")) .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); let manifest: json::Value = json::from_slice(&body).unwrap(); assert_eq!(manifest["event"]["id"], event_id); assert!(manifest["media"].as_array().unwrap().len() >= 1); assert!(manifest["markets"].as_array().unwrap().len() >= 1); } #[tokio::test] async fn event_markets_and_current_odds_work() { let app = build_router(AppState::new(AppConfig::default(), None, None)); let event_response = app .clone() .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap()) .await .unwrap(); let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap(); let event: json::Value = json::from_slice(&event_body).unwrap(); let event_id = event["id"].as_str().unwrap().to_string(); let markets_response = app .clone() .oneshot( Request::builder() .uri(format!("/api/v1/events/{event_id}/markets")) .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(markets_response.status(), StatusCode::OK); let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap(); let markets: json::Value = json::from_slice(&markets_body).unwrap(); let market_id = markets[0]["id"].as_str().unwrap().to_string(); assert_eq!(markets[0]["outcomes"].as_array().unwrap().len(), 2); let odds_response = app .oneshot( Request::builder() .uri(format!("/api/v1/markets/{market_id}/odds/current")) .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(odds_response.status(), StatusCode::OK); let odds_body = to_bytes(odds_response.into_body(), usize::MAX).await.unwrap(); let odds: json::Value = json::from_slice(&odds_body).unwrap(); assert_eq!(odds["market_id"], market_id); assert_eq!(odds["is_current"], true); assert_eq!(odds["odds"].as_array().unwrap().len(), 2); }