|
| 1 | +use super::{ |
| 2 | + tests::{call, config, fixture}, |
| 3 | + *, |
| 4 | +}; |
| 5 | +use axum::http::StatusCode; |
| 6 | +use serde_json::{json, Value}; |
| 7 | + |
| 8 | +type TestResult = Result<(), ServerError>; |
| 9 | + |
| 10 | +async fn batch(app: &Application, operations: Value) -> Result<Value, ServerError> { |
| 11 | + let (status, body) = call( |
| 12 | + &app.router, |
| 13 | + "POST", |
| 14 | + "/v1/batch/step", |
| 15 | + json!({"operations": operations}), |
| 16 | + None, |
| 17 | + ) |
| 18 | + .await?; |
| 19 | + assert_eq!(status, StatusCode::OK, "{body}"); |
| 20 | + Ok(body) |
| 21 | +} |
| 22 | + |
| 23 | +#[tokio::test] |
| 24 | +async fn training_batches_create_step_reset_branch_and_never_persist() -> TestResult { |
| 25 | + let dir = tempfile::tempdir()?; |
| 26 | + let app = fixture(&config(&dir)).await?; |
| 27 | + let initial = batch( |
| 28 | + &app, |
| 29 | + json!([ |
| 30 | + {"op":"create","game_id":"tictactoe","seed":42,"seat":0}, |
| 31 | + {"op":"create","game_id":"connect4","seed":43,"seat":1}, |
| 32 | + {"op":"create","game_id":"missing","seed":0,"seat":0} |
| 33 | + ]), |
| 34 | + ) |
| 35 | + .await?; |
| 36 | + assert_eq!(initial["results"][0]["status"], "ok"); |
| 37 | + assert_eq!( |
| 38 | + initial["results"][1]["frame"]["action_mask"], |
| 39 | + json!(vec![false; 7]) |
| 40 | + ); |
| 41 | + assert_eq!(initial["results"][2]["error"]["code"], "UNKNOWN_GAME"); |
| 42 | + let checkpoint = initial["results"][0]["checkpoint"].clone(); |
| 43 | + let request = json!([ |
| 44 | + {"op":"step","checkpoint":checkpoint,"seat":0,"action":4}, |
| 45 | + {"op":"step","checkpoint":checkpoint,"seat":0,"action":0}, |
| 46 | + {"op":"step","checkpoint":checkpoint,"seat":1,"action":4} |
| 47 | + ]); |
| 48 | + let first = batch(&app, request.clone()).await?; |
| 49 | + assert_eq!(first, batch(&app, request).await?); |
| 50 | + assert_eq!(first["results"][0]["frame"]["to_act"], json!([1])); |
| 51 | + assert_eq!(first["results"][0]["frame"]["rewards"], json!([0.0, 0.0])); |
| 52 | + assert_ne!( |
| 53 | + first["results"][0]["checkpoint"], |
| 54 | + first["results"][1]["checkpoint"] |
| 55 | + ); |
| 56 | + assert_eq!(first["results"][2]["status"], "error"); |
| 57 | + let moved = &first["results"][0]["checkpoint"]; |
| 58 | + let frames = batch( |
| 59 | + &app, |
| 60 | + json!([ |
| 61 | + {"op":"observe","checkpoint":moved,"seat":1}, |
| 62 | + {"op":"observe","checkpoint":moved,"seat":9}, |
| 63 | + {"op":"reset","checkpoint":moved,"seed":42,"seat":0}, |
| 64 | + {"op":"observe","checkpoint":moved} |
| 65 | + ]), |
| 66 | + ) |
| 67 | + .await?; |
| 68 | + assert_eq!( |
| 69 | + frames["results"][0]["frame"]["legal_actions"] |
| 70 | + .as_array() |
| 71 | + .map(Vec::len), |
| 72 | + Some(8) |
| 73 | + ); |
| 74 | + assert_eq!(frames["results"][1]["status"], "error"); |
| 75 | + assert_eq!(frames["results"][2]["checkpoint"], checkpoint); |
| 76 | + assert!(frames["results"][3]["frame"]["seat"].is_null()); |
| 77 | + assert_eq!(frames["results"][3]["frame"]["legal_actions"], json!([])); |
| 78 | + assert_eq!( |
| 79 | + frames["results"][3]["frame"]["action_mask"], |
| 80 | + json!(vec![false; 9]) |
| 81 | + ); |
| 82 | + assert!(frames["results"][0]["frame"].get("checkpoint").is_none()); |
| 83 | + let (_, history) = call(&app.router, "GET", "/v1/matches", Value::Null, None).await?; |
| 84 | + assert_eq!(history["matches"], json!([])); |
| 85 | + app.store.close().await; |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +#[tokio::test] |
| 90 | +async fn batch_rewards_endings_and_invalid_checkpoints_match_native_rules() -> TestResult { |
| 91 | + let dir = tempfile::tempdir()?; |
| 92 | + let app = fixture(&config(&dir)).await?; |
| 93 | + let created = batch( |
| 94 | + &app, |
| 95 | + json!([ |
| 96 | + {"op":"create","game_id":"tictactoe","seed":0,"seat":0}, |
| 97 | + {"op":"create","game_id":"chess","config":{"max_plies":1},"seed":0,"seat":0} |
| 98 | + ]), |
| 99 | + ) |
| 100 | + .await?; |
| 101 | + let mut checkpoint = created["results"][0]["checkpoint"].clone(); |
| 102 | + for (turn, action) in [0, 3, 1, 4, 2].into_iter().enumerate() { |
| 103 | + let result = batch( |
| 104 | + &app, |
| 105 | + json!([ |
| 106 | + {"op":"step","checkpoint":checkpoint,"seat":turn%2,"action":action} |
| 107 | + ]), |
| 108 | + ) |
| 109 | + .await?; |
| 110 | + assert_eq!(result["results"][0]["status"], "ok"); |
| 111 | + if turn == 4 { |
| 112 | + assert_eq!(result["results"][0]["frame"]["terminated"], true); |
| 113 | + assert_eq!(result["results"][0]["frame"]["rewards"], json!([1.0, -1.0])); |
| 114 | + } |
| 115 | + checkpoint = result["results"][0]["checkpoint"].clone(); |
| 116 | + } |
| 117 | + let ended = batch( |
| 118 | + &app, |
| 119 | + json!([ |
| 120 | + {"op":"step","checkpoint":checkpoint,"seat":1,"action":8} |
| 121 | + ]), |
| 122 | + ) |
| 123 | + .await?; |
| 124 | + assert_eq!(ended["results"][0]["error"]["code"], "MATCH_FINISHED"); |
| 125 | + let chess = &created["results"][1]; |
| 126 | + let capped = batch( |
| 127 | + &app, |
| 128 | + json!([{ |
| 129 | + "op":"step","checkpoint":chess["checkpoint"],"seat":0, |
| 130 | + "action":chess["frame"]["legal_actions"][0]["index"] |
| 131 | + }]), |
| 132 | + ) |
| 133 | + .await?; |
| 134 | + assert_eq!(capped["results"][0]["frame"]["truncated"], true); |
| 135 | + assert_eq!(capped["results"][0]["frame"]["terminated"], false); |
| 136 | + checkpoint["engine_version"] = json!("incompatible"); |
| 137 | + let bad = batch( |
| 138 | + &app, |
| 139 | + json!([ |
| 140 | + {"op":"observe","checkpoint":checkpoint,"seat":0} |
| 141 | + ]), |
| 142 | + ) |
| 143 | + .await?; |
| 144 | + assert_eq!(bad["results"][0]["error"]["code"], "INVALID_POSITION"); |
| 145 | + app.store.close().await; |
| 146 | + Ok(()) |
| 147 | +} |
| 148 | + |
| 149 | +#[tokio::test] |
| 150 | +async fn batch_limits_and_route_body_override_are_enforced() -> TestResult { |
| 151 | + let dir = tempfile::tempdir()?; |
| 152 | + let app = fixture(&config(&dir)).await?; |
| 153 | + let create = json!({"op":"create","game_id":"tictactoe","seed":0,"seat":0}); |
| 154 | + for operations in [vec![], vec![create.clone(); 65]] { |
| 155 | + let (status, body) = call( |
| 156 | + &app.router, |
| 157 | + "POST", |
| 158 | + "/v1/batch/step", |
| 159 | + json!({"operations":operations}), |
| 160 | + None, |
| 161 | + ) |
| 162 | + .await?; |
| 163 | + assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY); |
| 164 | + assert_eq!(body["error"]["code"], "BATCH_LIMIT"); |
| 165 | + } |
| 166 | + // Training accepts more than the standard route's 64 KiB. |
| 167 | + let result = batch( |
| 168 | + &app, |
| 169 | + json!([{ |
| 170 | + "op":"create","game_id":"tictactoe","seed":0,"seat":0, |
| 171 | + "config":{"unknown": "x".repeat(70000)} |
| 172 | + }]), |
| 173 | + ) |
| 174 | + .await?; |
| 175 | + assert_eq!(result["results"][0]["error"]["code"], "INVALID_CONFIG"); |
| 176 | + let (status, _) = call( |
| 177 | + &app.router, |
| 178 | + "POST", |
| 179 | + "/v1/batch/step", |
| 180 | + json!({ |
| 181 | + "operations":[{"op":"create","game_id":"tictactoe","seed":0,"seat":0, |
| 182 | + "config":{"unknown":"x".repeat(4 * 1024 * 1024)}}] |
| 183 | + }), |
| 184 | + None, |
| 185 | + ) |
| 186 | + .await?; |
| 187 | + assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE); |
| 188 | + let (status, _) = call(&app.router, "POST", "/v1/batch/step", json!({ |
| 189 | + "operations":[{"op":"create","game_id":"tictactoe","seed":0,"seat":0,"match_id":"private"}] |
| 190 | + }), None).await?; |
| 191 | + assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY); |
| 192 | + |
| 193 | + // Direct trusted service calls also bound output even without HTTP input limits. |
| 194 | + let request = serde_json::from_value(json!({"operations":vec![json!({ |
| 195 | + "op":"create","game_id":"x".repeat(200000),"seed":0,"seat":0 |
| 196 | + });64]}))?; |
| 197 | + let result = app.service.training_batch(request)?; |
| 198 | + assert_eq!(result.results.len(), 64); |
| 199 | + assert!(serde_json::to_vec(&result)?.len() <= 8 * 1024 * 1024); |
| 200 | + assert!(serde_json::to_value(result)?["results"] |
| 201 | + .as_array() |
| 202 | + .ok_or("results")? |
| 203 | + .iter() |
| 204 | + .any(|row| row["error"]["code"] == "BATCH_LIMIT")); |
| 205 | + app.store.close().await; |
| 206 | + Ok(()) |
| 207 | +} |
0 commit comments