add study flow endpoints
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
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;
|
||||
@@ -130,6 +131,285 @@ async fn feed_next_returns_a_manifestable_event() {
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user