tighten backend regression coverage
Add targeted tests for rejection paths, settlement overrides, and key analytics flows, and align localization/research docs with the shipped contract.
This commit is contained in:
@@ -761,3 +761,477 @@ async fn event_markets_and_current_odds_work() {
|
||||
assert_eq!(odds["is_current"], true);
|
||||
assert_eq!(odds["odds"].as_array().unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bets_reject_after_market_lock() {
|
||||
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_id = Uuid::new_v4();
|
||||
let market_id = Uuid::new_v4();
|
||||
let home_outcome_id = Uuid::new_v4();
|
||||
let away_outcome_id = Uuid::new_v4();
|
||||
|
||||
let event_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/admin/events")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"event": {
|
||||
"id": event_id,
|
||||
"sport_type": "football",
|
||||
"source_ref": "locked-event-001",
|
||||
"title_en": "Locked event",
|
||||
"title_sv": "Låst händelse",
|
||||
"status": "scheduled",
|
||||
"preview_start_ms": 0,
|
||||
"preview_end_ms": 1000,
|
||||
"reveal_start_ms": 2000,
|
||||
"reveal_end_ms": 3000,
|
||||
"lock_at": "2000-01-01T00:00:00Z",
|
||||
"settle_at": "2000-01-01T00:15:00Z"
|
||||
},
|
||||
"media": [],
|
||||
"markets": []
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(event_response.status(), StatusCode::CREATED);
|
||||
|
||||
let market_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/admin/markets")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"id": market_id,
|
||||
"event_id": event_id,
|
||||
"question_key": "market.locked.winner",
|
||||
"market_type": "winner",
|
||||
"status": "locked",
|
||||
"lock_at": "2000-01-01T00:00:00Z",
|
||||
"settlement_rule_key": "settle_on_match_winner",
|
||||
"outcomes": [
|
||||
{
|
||||
"id": home_outcome_id,
|
||||
"market_id": market_id,
|
||||
"outcome_code": "home",
|
||||
"label_key": "outcome.home",
|
||||
"sort_order": 1
|
||||
},
|
||||
{
|
||||
"id": away_outcome_id,
|
||||
"market_id": market_id,
|
||||
"outcome_code": "away",
|
||||
"label_key": "outcome.away",
|
||||
"sort_order": 2
|
||||
}
|
||||
]
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(market_response.status(), StatusCode::CREATED);
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/bets/intent")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"session_id": session_id,
|
||||
"event_id": event_id,
|
||||
"market_id": market_id,
|
||||
"outcome_id": home_outcome_id,
|
||||
"idempotency_key": "late-bet-001",
|
||||
"client_sent_at": Utc::now().to_rfc3339(),
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::CREATED);
|
||||
|
||||
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
let bet_json: json::Value = json::from_slice(&body).unwrap();
|
||||
|
||||
assert_eq!(bet_json["accepted"], false);
|
||||
assert_eq!(bet_json["acceptance_code"], "rejected_too_late");
|
||||
assert!(bet_json["accepted_odds_version_id"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bets_reject_with_invalid_session() {
|
||||
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();
|
||||
|
||||
assert_eq!(session_response.status(), StatusCode::CREATED);
|
||||
|
||||
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 response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/bets/intent")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"session_id": Uuid::new_v4(),
|
||||
"event_id": event_id,
|
||||
"market_id": market_id,
|
||||
"outcome_id": outcome_id,
|
||||
"idempotency_key": "invalid-session-001",
|
||||
"client_sent_at": Utc::now().to_rfc3339(),
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::CREATED);
|
||||
|
||||
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
let bet_json: json::Value = json::from_slice(&body).unwrap();
|
||||
|
||||
assert_eq!(bet_json["accepted"], false);
|
||||
assert_eq!(bet_json["acceptance_code"], "rejected_invalid_session");
|
||||
assert!(bet_json["accepted_odds_version_id"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bets_reject_with_invalid_market() {
|
||||
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();
|
||||
|
||||
assert_eq!(session_response.status(), StatusCode::CREATED);
|
||||
|
||||
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 outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/bets/intent")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"session_id": session_id,
|
||||
"event_id": event_id,
|
||||
"market_id": Uuid::new_v4(),
|
||||
"outcome_id": outcome_id,
|
||||
"idempotency_key": "invalid-market-001",
|
||||
"client_sent_at": Utc::now().to_rfc3339(),
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::CREATED);
|
||||
|
||||
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
let bet_json: json::Value = json::from_slice(&body).unwrap();
|
||||
|
||||
assert_eq!(bet_json["accepted"], false);
|
||||
assert_eq!(bet_json["acceptance_code"], "rejected_invalid_market");
|
||||
assert!(bet_json["accepted_odds_version_id"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn settlement_override_is_reflected_in_results() {
|
||||
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 home_outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();
|
||||
let away_outcome_id = markets_json[0]["outcomes"][1]["id"].as_str().unwrap().to_string();
|
||||
|
||||
let first_settlement_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/admin/settlements")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"id": Uuid::new_v4(),
|
||||
"market_id": market_id,
|
||||
"settled_at": "2099-01-01T01:25:00Z",
|
||||
"winning_outcome_id": home_outcome_id
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(first_settlement_response.status(), StatusCode::CREATED);
|
||||
|
||||
let second_settlement_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/admin/settlements")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
json::json!({
|
||||
"id": Uuid::new_v4(),
|
||||
"market_id": market_id,
|
||||
"settled_at": "2099-01-01T01:30:00Z",
|
||||
"winning_outcome_id": away_outcome_id
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(second_settlement_response.status(), StatusCode::CREATED);
|
||||
|
||||
let result_response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/api/v1/events/{event_id}/result"))
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result_response.status(), StatusCode::OK);
|
||||
|
||||
let result_body = to_bytes(result_response.into_body(), usize::MAX).await.unwrap();
|
||||
let result_json: json::Value = json::from_slice(&result_body).unwrap();
|
||||
|
||||
assert_eq!(result_json["market_id"], market_id);
|
||||
assert_eq!(result_json["winning_outcome_id"], away_outcome_id);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn analytics_records_key_user_flow_events() {
|
||||
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": "session_started",
|
||||
"occurred_at": Utc::now().to_rfc3339(),
|
||||
"attributes": [
|
||||
{"key": "locale_code", "value": "en"},
|
||||
{"key": "experiment_variant", "value": "modern"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"event_name": "feed_viewed",
|
||||
"occurred_at": Utc::now().to_rfc3339(),
|
||||
"attributes": [
|
||||
{"key": "screen_name", "value": "feed"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"event_name": "round_loaded",
|
||||
"occurred_at": Utc::now().to_rfc3339(),
|
||||
"attributes": [
|
||||
{"key": "event_id", "value": Uuid::new_v4().to_string()}
|
||||
]
|
||||
},
|
||||
{
|
||||
"event_name": "outcome_selected",
|
||||
"occurred_at": Utc::now().to_rfc3339(),
|
||||
"attributes": [
|
||||
{"key": "market_id", "value": Uuid::new_v4().to_string()},
|
||||
{"key": "outcome_id", "value": Uuid::new_v4().to_string()}
|
||||
]
|
||||
},
|
||||
{
|
||||
"event_name": "selection_submitted",
|
||||
"occurred_at": Utc::now().to_rfc3339()
|
||||
},
|
||||
{
|
||||
"event_name": "result_viewed",
|
||||
"occurred_at": Utc::now().to_rfc3339(),
|
||||
"attributes": [
|
||||
{"key": "screen_name", "value": "result"}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::ACCEPTED);
|
||||
|
||||
let (event_count, attribute_count) = state.analytics_counts().await;
|
||||
assert_eq!(event_count, 6);
|
||||
assert_eq!(attribute_count, 7);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user