Watch
0
0
Fork
You've already forked hyperhive
0

tests for the forge objects pass: team, rule, repo and mirror writes

adds routed_forge, writes and sent to the forge.rs test module: a stand-in forge
that answers by path. it adds an x-total-count header to list answers, which the
forgejo client reads to page; test code only, no production file changes.
This commit is contained in:
atlas 2026-11-16 20:39:37 +01:00
commit c1234973be

View file

@ -2819,6 +2819,80 @@ mod tests {
(Client { api, url }, seen)
}
/// A stand-in forge on a loopback port answering each request by
/// `route(method, path, body)` with a status and JSON body, and
/// recording every request like [`stub_forge`]. For the tests that need
/// a different answer per path.
pub(super) async fn routed_forge(
route: impl Fn(&str, &str, &serde_json::Value) -> (u16, String) + Send + Sync + 'static,
) -> (Client, Seen) {
use axum::http::{Method, StatusCode, Uri, header::CONTENT_TYPE};
let seen = Seen::default();
let recorded = seen.clone();
let route = std::sync::Arc::new(route);
let app = axum::Router::new().fallback(
move |method: Method, uri: Uri, body: axum::body::Bytes| {
let (recorded, route) = (recorded.clone(), std::sync::Arc::clone(&route));
async move {
let json = serde_json::from_slice(&body).unwrap_or_default();
let (status, answer) = route(method.as_str(), uri.path(), &json);
recorded.lock().unwrap().push((
method.to_string(),
uri.path().to_owned(),
json,
));
let status = StatusCode::from_u16(status).unwrap();
// A list answer carries its length, which the forge client
// reads to know it has seen every page.
let total = serde_json::from_str::<Vec<serde_json::Value>>(&answer)
.map_or(0, |list| list.len());
(
status,
[
(CONTENT_TYPE, "application/json".to_owned()),
(axum::http::HeaderName::from_static("x-total-count"), total.to_string()),
],
answer,
)
}
},
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
tokio::spawn(async move { axum::serve(listener, app).await });
let api = crate::forge::api::tests::fixed(url.clone(), "stub-token");
(Client { api, url }, seen)
}
/// The requests in `seen` that are not reads, as `METHOD path`.
pub(super) fn writes(seen: &Seen) -> Vec<String> {
seen.lock()
.unwrap()
.iter()
.filter(|(method, _, _)| method != "GET")
.map(|(method, path, _)| format!("{method} {path}"))
.collect()
}
/// The body of the first `method path` request in `seen`, with unset
/// (`null`) fields dropped: what the forge acts on.
pub(super) fn sent(seen: &Seen, method: &str, path: &str) -> Option<serde_json::Value> {
let body = seen
.lock()
.unwrap()
.iter()
.find(|(m, p, _)| m == method && p == path)
.map(|(_, _, body)| body.clone())?;
Some(serde_json::Value::Object(
body.as_object()?
.iter()
.filter(|(_, v)| !v.is_null())
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
))
}
/// The `PATCH` [`Client::ensure_agent_user`] sent, if any.
pub(super) fn lockdown_patch(seen: &Seen, agent: &str) -> Option<serde_json::Value> {
let path = format!("/api/v1/admin/users/{agent}");