mobhub
Decentralized model and agent weights distribution network
Brought by drakestapleton
mobhub / tests / integration_test.rs
| 1 | //! Integration tests for MobHub native suite. |
| 2 | |
| 3 | use axum::body::Body; |
| 4 | use axum::http::{Request, StatusCode}; |
| 5 | use mobhub::gym::{SessionParams, StatName}; |
| 6 | use mobhub::payout::{MemberInput, PayoutModel, PayoutOptions, PayoutPolicy}; |
| 7 | use mobhub::reconcile::{GiveForMatch, RegisterRowForMatch}; |
| 8 | use mobhub::server::{create_router, AppState}; |
| 9 | use mobhub::storage::Database; |
| 10 | use mobhub::torn::TornClient; |
| 11 | use serde_json::json; |
| 12 | use tower::ServiceExt; |
| 13 | |
| 14 | #[tokio::test] |
| 15 | async fn test_full_router_integration() { |
| 16 | let storage = Database::open_in_memory().unwrap(); |
| 17 | let torn = TornClient::new_mock(); |
| 18 | let state = AppState { |
| 19 | storage, |
| 20 | torn, |
| 21 | faction_id: 50645, |
| 22 | }; |
| 23 | let app = create_router(state); |
| 24 | |
| 25 | // 1. Health check |
| 26 | let res = app |
| 27 | .clone() |
| 28 | .oneshot( |
| 29 | Request::builder() |
| 30 | .uri("/health") |
| 31 | .body(Body::empty()) |
| 32 | .unwrap(), |
| 33 | ) |
| 34 | .await |
| 35 | .unwrap(); |
| 36 | assert_eq!(res.status(), StatusCode::OK); |
| 37 | |
| 38 | // 2. Status check |
| 39 | let res = app |
| 40 | .clone() |
| 41 | .oneshot( |
| 42 | Request::builder() |
| 43 | .uri("/status") |
| 44 | .body(Body::empty()) |
| 45 | .unwrap(), |
| 46 | ) |
| 47 | .await |
| 48 | .unwrap(); |
| 49 | assert_eq!(res.status(), StatusCode::OK); |
| 50 | |
| 51 | // 3. Payout compute endpoint |
| 52 | let policy = PayoutPolicy { |
| 53 | model: PayoutModel::FlatRate, |
| 54 | rate_per_respect: 180284.0, |
| 55 | rate_per_outside_hit: 500000.0, |
| 56 | rate_per_assist: 250000.0, |
| 57 | weight_war_hit: 1.0, |
| 58 | weight_assist: 0.5, |
| 59 | weight_outside_hit: 0.5, |
| 60 | split_members_pct: 0.8, |
| 61 | split_faction_pct: 0.2, |
| 62 | split_leadership_pct: 0.0, |
| 63 | leadership_recipient_torn_id: None, |
| 64 | use_fixed_respect_rate: None, |
| 65 | leadership_also_gets_member_pay: None, |
| 66 | }; |
| 67 | let members = vec![ |
| 68 | MemberInput { |
| 69 | torn_id: 2532285, |
| 70 | name: Some("MrRazorlove".to_string()), |
| 71 | flat_respect: 413.12, |
| 72 | war_hits: 55, |
| 73 | assists: 0, |
| 74 | outside_hits: 1, |
| 75 | is_leadership: None, |
| 76 | }, |
| 77 | MemberInput { |
| 78 | torn_id: 4187899, |
| 79 | name: Some("BabyBubbles".to_string()), |
| 80 | flat_respect: 180.5, |
| 81 | war_hits: 26, |
| 82 | assists: 0, |
| 83 | outside_hits: 0, |
| 84 | is_leadership: None, |
| 85 | }, |
| 86 | ]; |
| 87 | let payload = json!({ |
| 88 | "cache_value": 349499999i64, |
| 89 | "policy": policy, |
| 90 | "members": members, |
| 91 | "options": PayoutOptions::default() |
| 92 | }); |
| 93 | |
| 94 | let res = app |
| 95 | .clone() |
| 96 | .oneshot( |
| 97 | Request::builder() |
| 98 | .method("POST") |
| 99 | .uri("/api/payout/compute") |
| 100 | .header("Content-Type", "application/json") |
| 101 | .body(Body::from(payload.to_string())) |
| 102 | .unwrap(), |
| 103 | ) |
| 104 | .await |
| 105 | .unwrap(); |
| 106 | |
| 107 | assert_eq!(res.status(), StatusCode::OK); |
| 108 | let bytes = axum::body::to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 109 | let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); |
| 110 | assert_eq!(resp_json["faction_fund"], 69900000); |
| 111 | assert_eq!(resp_json["members"].as_array().unwrap().len(), 2); |
| 112 | |
| 113 | // 4. Valuation compute endpoint |
| 114 | let val_payload = json!({ |
| 115 | "respect": 100000.0, |
| 116 | "respect_rate": 180284.0, |
| 117 | "armory_value": 50000000.0, |
| 118 | "faction_cash": 25000000.0 |
| 119 | }); |
| 120 | let res = app |
| 121 | .clone() |
| 122 | .oneshot( |
| 123 | Request::builder() |
| 124 | .method("POST") |
| 125 | .uri("/api/valuation/compute") |
| 126 | .header("Content-Type", "application/json") |
| 127 | .body(Body::from(val_payload.to_string())) |
| 128 | .unwrap(), |
| 129 | ) |
| 130 | .await |
| 131 | .unwrap(); |
| 132 | assert_eq!(res.status(), StatusCode::OK); |
| 133 | let bytes = axum::body::to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 134 | let val_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); |
| 135 | assert_eq!(val_json["total_value"], 18028400000i64 + 50000000 + 25000000); |
| 136 | |
| 137 | // 5. Gym simulate endpoint |
| 138 | let gym_payload = json!(SessionParams { |
| 139 | stat: StatName::Strength, |
| 140 | start_stat: 1_000_000.0, |
| 141 | start_happy: 5000.0, |
| 142 | energy_available: 50.0, |
| 143 | energy_per_train: 5.0, |
| 144 | gym_dots: 7.3, |
| 145 | perk_bonuses: vec![], |
| 146 | happy_loss_per_train: Some(2.5), |
| 147 | min_happy: Some(0.0), |
| 148 | }); |
| 149 | let res = app |
| 150 | .clone() |
| 151 | .oneshot( |
| 152 | Request::builder() |
| 153 | .method("POST") |
| 154 | .uri("/api/gym/simulate") |
| 155 | .header("Content-Type", "application/json") |
| 156 | .body(Body::from(gym_payload.to_string())) |
| 157 | .unwrap(), |
| 158 | ) |
| 159 | .await |
| 160 | .unwrap(); |
| 161 | assert_eq!(res.status(), StatusCode::OK); |
| 162 | let bytes = axum::body::to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 163 | let gym_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); |
| 164 | assert_eq!(gym_json["trains"], 10); |
| 165 | |
| 166 | // 6. Reconcile parse endpoint |
| 167 | let news_payload = json!({ |
| 168 | "news": [{ |
| 169 | "id": "1001", |
| 170 | "timestamp": 1720000000, |
| 171 | "text": "<a href=\"profiles.php?XID=100\">Banker</a> increased <a href=\"profiles.php?XID=2532285\">MrRazorlove</a>'s money balance by $74,978,810 from $0 to $74,978,810." |
| 172 | }] |
| 173 | }); |
| 174 | let res = app |
| 175 | .clone() |
| 176 | .oneshot( |
| 177 | Request::builder() |
| 178 | .method("POST") |
| 179 | .uri("/api/reconcile/parse") |
| 180 | .header("Content-Type", "application/json") |
| 181 | .body(Body::from(news_payload.to_string())) |
| 182 | .unwrap(), |
| 183 | ) |
| 184 | .await |
| 185 | .unwrap(); |
| 186 | assert_eq!(res.status(), StatusCode::OK); |
| 187 | let bytes = axum::body::to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 188 | let parsed_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); |
| 189 | assert_eq!(parsed_json.as_array().unwrap().len(), 1); |
| 190 | assert_eq!(parsed_json[0]["amount"], 74978810); |
| 191 | |
| 192 | // 7. Reconcile match endpoint |
| 193 | let match_payload = json!({ |
| 194 | "gives": [ |
| 195 | GiveForMatch { news_id: "1001".to_string(), recipient_torn_id: 2532285, amount: 74978810, occurred_at: None } |
| 196 | ], |
| 197 | "register": [ |
| 198 | RegisterRowForMatch { id: 1, torn_id: 2532285, amount_owed: 74978810, paid: false } |
| 199 | ] |
| 200 | }); |
| 201 | let res = app |
| 202 | .clone() |
| 203 | .oneshot( |
| 204 | Request::builder() |
| 205 | .method("POST") |
| 206 | .uri("/api/reconcile/match") |
| 207 | .header("Content-Type", "application/json") |
| 208 | .body(Body::from(match_payload.to_string())) |
| 209 | .unwrap(), |
| 210 | ) |
| 211 | .await |
| 212 | .unwrap(); |
| 213 | assert_eq!(res.status(), StatusCode::OK); |
| 214 | let bytes = axum::body::to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 215 | let match_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); |
| 216 | assert_eq!(match_json["matched"], 1); |
| 217 | assert_eq!(match_json["decisions"][0]["matched_register_id"], 1); |
| 218 | } |