diff --git a/swarm-controller/src/git_forge/config_pr.rs b/swarm-controller/src/git_forge/config_pr.rs index e44082e40..39c1c19c9 100644 --- a/swarm-controller/src/git_forge/config_pr.rs +++ b/swarm-controller/src/git_forge/config_pr.rs @@ -173,7 +173,7 @@ const POLL_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5); pub struct ConfigPrCache(Mutex>); impl ConfigPrCache { - fn new() -> Self { + pub(crate) fn new() -> Self { Self(Mutex::new(HashMap::new())) } diff --git a/swarm-controller/src/routes/config_prs.rs b/swarm-controller/src/routes/config_prs.rs index b0ac4004e..747fa6f00 100644 --- a/swarm-controller/src/routes/config_prs.rs +++ b/swarm-controller/src/routes/config_prs.rs @@ -1,14 +1,36 @@ //! Agents' open config PRs. +use std::sync::Arc; + use anyhow::Result; +use axum::extract::FromRef; use axum::{ Json, extract::{Path, State}, }; +use crate::config_pr::ConfigPrCache; use crate::forge; +use crate::jobq::Late; use crate::routes::{AppState, StatusUnavailable}; +/// What the config-PR routes may reach of [`AppState`]: the config-PR cache. +/// +/// Derived per request from [`AppState`] (`FromRef`), so the field is a clone +/// of the same `Arc` and nothing is held twice. +#[derive(Clone)] +pub(crate) struct ConfigPrsState { + pub(crate) config_prs: Late>, +} + +impl FromRef for ConfigPrsState { + fn from_ref(state: &AppState) -> Self { + Self { + config_prs: Arc::clone(&state.config_prs), + } + } +} + /// `agent`'s open config PR, from [`ConfigPrCache`](crate::config_pr::ConfigPrCache) — kept /// current by a webhook nudge (near-real-time) backstopped by a periodic /// scan (worst case: up to one `config_pr::POLL_INTERVAL` stale if a @@ -32,7 +54,7 @@ use crate::routes::{AppState, StatusUnavailable}; tag = "agents" )] pub(crate) async fn get_agent_config_pr( - State(state): State, + State(state): State, Path(name): Path, ) -> Result>, StatusUnavailable> { let Some(cache) = state.config_prs.get() else { @@ -70,7 +92,7 @@ pub(crate) async fn get_agent_config_pr( tag = "agents" )] pub(crate) async fn get_config_prs( - State(state): State, + State(state): State, ) -> Result>, StatusUnavailable> { let Some(cache) = state.config_prs.get() else { return Err(StatusUnavailable( @@ -79,3 +101,43 @@ pub(crate) async fn get_config_prs( }; Ok(Json(cache.snapshot())) } + +#[cfg(test)] +mod tests { + use axum::body::Body; + use axum::http::{Request, StatusCode}; + use tower::ServiceExt as _; + + use super::*; + + async fn get_config_prs_status(cache: Option) -> StatusCode { + let (state, _) = crate::routes::tests::state_with_roster(); + if let Some(cache) = cache { + assert!(state.config_prs.set(Arc::new(cache)).is_ok()); + } + let req = Request::get("/api/config-prs") + .body(Body::empty()) + .expect("a request"); + crate::startup::build_app(state) + .oneshot(req) + .await + .expect("infallible") + .status() + } + + #[tokio::test] + async fn serves_the_cache_the_app_state_holds() { + assert_eq!( + get_config_prs_status(Some(ConfigPrCache::new())).await, + StatusCode::OK + ); + } + + #[tokio::test] + async fn is_unavailable_without_a_cache() { + assert_eq!( + get_config_prs_status(None).await, + StatusCode::SERVICE_UNAVAILABLE + ); + } +}