465 lines
15 KiB
Rust
465 lines
15 KiB
Rust
use axum::{body::{to_bytes, Body}, http::{Request, StatusCode}};
|
|
use chrono::Utc;
|
|
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 bet_intent_accepts_and_is_idempotent() {
|
|
let app = build_router(AppState::new(AppConfig::default(), None, None));
|
|
|
|
let session_response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/session/start")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from("{}"))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let session_body = to_bytes(session_response.into_body(), usize::MAX).await.unwrap();
|
|
let session_json: json::Value = json::from_slice(&session_body).unwrap();
|
|
let session_id = session_json["session_id"].as_str().unwrap().to_string();
|
|
|
|
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: json::Value = json::from_slice(&event_body).unwrap();
|
|
let event_id = event_json["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();
|
|
|
|
let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
|
|
let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
|
|
let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
|
|
let outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();
|
|
|
|
let request = json::json!({
|
|
"session_id": session_id,
|
|
"event_id": event_id,
|
|
"market_id": market_id,
|
|
"outcome_id": outcome_id,
|
|
"idempotency_key": "bet-001",
|
|
"client_sent_at": Utc::now().to_rfc3339(),
|
|
})
|
|
.to_string();
|
|
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/bets/intent")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from(request.clone()))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::CREATED);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let first_json: json::Value = json::from_slice(&body).unwrap();
|
|
let bet_id = first_json["id"].as_str().unwrap().to_string();
|
|
assert_eq!(first_json["accepted"], true);
|
|
assert_eq!(first_json["acceptance_code"], "accepted");
|
|
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri(format!("/api/v1/bets/{bet_id}"))
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let lookup_json: json::Value = json::from_slice(&body).unwrap();
|
|
assert_eq!(lookup_json["id"], bet_id);
|
|
assert_eq!(lookup_json["accepted"], true);
|
|
|
|
let response = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/bets/intent")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from(request))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::CREATED);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let duplicate_json: json::Value = json::from_slice(&body).unwrap();
|
|
assert_eq!(duplicate_json["id"], bet_id);
|
|
assert_eq!(duplicate_json["accepted"], true);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn event_result_returns_settlement() {
|
|
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: json::Value = json::from_slice(&event_body).unwrap();
|
|
let event_id = event_json["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();
|
|
|
|
let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
|
|
let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
|
|
let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
|
|
let winning_outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();
|
|
|
|
let response = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri(format!("/api/v1/events/{event_id}/result"))
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let result_json: json::Value = json::from_slice(&body).unwrap();
|
|
|
|
assert_eq!(result_json["market_id"], market_id);
|
|
assert_eq!(result_json["winning_outcome_id"], winning_outcome_id);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn experiments_and_localization_work() {
|
|
let app = build_router(AppState::new(AppConfig::default(), None, None));
|
|
|
|
let session_response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/session/start")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from(
|
|
json::json!({
|
|
"experiment_variant": "modern"
|
|
})
|
|
.to_string(),
|
|
))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(session_response.status(), StatusCode::CREATED);
|
|
|
|
let config_response = app
|
|
.clone()
|
|
.oneshot(Request::builder().uri("/api/v1/experiments/config").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(config_response.status(), StatusCode::OK);
|
|
|
|
let config_body = to_bytes(config_response.into_body(), usize::MAX).await.unwrap();
|
|
let config_json: json::Value = json::from_slice(&config_body).unwrap();
|
|
assert_eq!(config_json["variant"], "modern");
|
|
assert_eq!(config_json["feature_flags"]["modern_mode"], true);
|
|
|
|
let localization_response = app
|
|
.clone()
|
|
.oneshot(Request::builder().uri("/api/v1/localization/sv").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(localization_response.status(), StatusCode::OK);
|
|
|
|
let localization_body = to_bytes(localization_response.into_body(), usize::MAX).await.unwrap();
|
|
let localization_json: json::Value = json::from_slice(&localization_body).unwrap();
|
|
assert_eq!(localization_json["locale_code"], "sv");
|
|
assert_eq!(localization_json["values"]["common.continue"], "Fortsätt");
|
|
|
|
let localization_en_response = app
|
|
.oneshot(Request::builder().uri("/api/v1/localization/en").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(localization_en_response.status(), StatusCode::OK);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn analytics_batch_is_recorded() {
|
|
let state = AppState::new(AppConfig::default(), None, None);
|
|
let app = build_router(state.clone());
|
|
|
|
let session_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!(session_response.status(), StatusCode::CREATED);
|
|
|
|
let response = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/analytics/batch")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from(
|
|
json::json!({
|
|
"events": [
|
|
{
|
|
"event_name": "screen_viewed",
|
|
"occurred_at": Utc::now().to_rfc3339(),
|
|
"attributes": [
|
|
{"key": "screen_name", "value": "feed"}
|
|
]
|
|
},
|
|
{
|
|
"event_name": "cta_pressed",
|
|
"occurred_at": Utc::now().to_rfc3339()
|
|
}
|
|
]
|
|
})
|
|
.to_string(),
|
|
))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::ACCEPTED);
|
|
|
|
let (event_count, attribute_count) = state.analytics_counts().await;
|
|
assert_eq!(event_count, 2);
|
|
assert_eq!(attribute_count, 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);
|
|
}
|