| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use axum::Router; |
| 22 |
use axum::extract::Path; |
| 23 |
use axum::routing::get; |
| 24 |
use std::time::Duration; |
| 25 |
|
| 26 |
|
| 27 |
pub(super) struct MtStub { |
| 28 |
|
| 29 |
base_url: String, |
| 30 |
server: tokio::task::JoinHandle<()>, |
| 31 |
} |
| 32 |
|
| 33 |
impl MtStub { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
pub(super) async fn start(latency: Duration, memberships: usize) -> Self { |
| 40 |
let app = Router::new().route( |
| 41 |
"/api/user/{user_id}/summary", |
| 42 |
get(move |Path(_user_id): Path<String>| async move { |
| 43 |
if !latency.is_zero() { |
| 44 |
tokio::time::sleep(latency).await; |
| 45 |
} |
| 46 |
axum::Json(summary(memberships)) |
| 47 |
}), |
| 48 |
); |
| 49 |
|
| 50 |
let listener = tokio::net::TcpListener::bind("127.0.0.1:0") |
| 51 |
.await |
| 52 |
.expect("MT stub could not bind"); |
| 53 |
let addr = listener.local_addr().expect("MT stub has no address"); |
| 54 |
|
| 55 |
let server = tokio::spawn(async move { |
| 56 |
|
| 57 |
|
| 58 |
let _ = axum::serve(listener, app).await; |
| 59 |
}); |
| 60 |
|
| 61 |
MtStub { |
| 62 |
base_url: format!("http://{addr}"), |
| 63 |
server, |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub(super) fn base_url(&self) -> String { |
| 68 |
self.base_url.clone() |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl Drop for MtStub { |
| 73 |
fn drop(&mut self) { |
| 74 |
self.server.abort(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
fn summary(memberships: usize) -> serde_json::Value { |
| 82 |
let rows: Vec<serde_json::Value> = (0..memberships) |
| 83 |
.map(|i| { |
| 84 |
serde_json::json!({ |
| 85 |
"community_slug": format!("community-{i}"), |
| 86 |
"community_name": format!("Community {i}"), |
| 87 |
"role": if i == 0 { "Moderator" } else { "Member" }, |
| 88 |
"post_count": 40 + i as i64, |
| 89 |
"joined_at": "2026-03-14T09:00:00Z", |
| 90 |
}) |
| 91 |
}) |
| 92 |
.collect(); |
| 93 |
serde_json::json!({ "memberships": rows }) |
| 94 |
} |
| 95 |
|