From 8d8fcb59cf8b9a05d721e973981157e5f2f0161f Mon Sep 17 00:00:00 2001 From: atlas Date: Sat, 28 Nov 2026 00:00:26 +0100 Subject: [PATCH] swarm-controller: one test constructor for AppState fixtures --- .../src/accounts/forge_account.rs | 29 +------ .../src/accounts/github_account.rs | 29 +------ .../src/accounts/gitlab_account.rs | 29 +------ .../src/accounts/linked_accounts.rs | 29 +------ .../src/accounts/matrix_account.rs | 29 +------ swarm-controller/src/agents/agent_limits.rs | 28 +------ swarm-controller/src/routes/mod.rs | 82 ++++++++++--------- swarm-controller/src/secrets/backend_env.rs | 29 +------ 8 files changed, 50 insertions(+), 234 deletions(-) diff --git a/swarm-controller/src/accounts/forge_account.rs b/swarm-controller/src/accounts/forge_account.rs index 4e2441d53..7a88418c9 100644 --- a/swarm-controller/src/accounts/forge_account.rs +++ b/swarm-controller/src/accounts/forge_account.rs @@ -153,35 +153,8 @@ mod tests { /// Bare-minimum `AppState`, as `matrix_account`'s tests build it. fn state(store: &Arc) -> crate::routes::AppState { crate::routes::AppState { - hives: std::sync::Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), accounts: Accounts::Fake(Arc::clone(store)), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } } diff --git a/swarm-controller/src/accounts/github_account.rs b/swarm-controller/src/accounts/github_account.rs index f27e7e041..b7aaeb0a0 100644 --- a/swarm-controller/src/accounts/github_account.rs +++ b/swarm-controller/src/accounts/github_account.rs @@ -115,35 +115,8 @@ mod tests { /// Bare-minimum `AppState`, as `forge_account`'s tests build it. fn state(store: &Arc) -> crate::routes::AppState { crate::routes::AppState { - hives: std::sync::Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), accounts: Accounts::Fake(Arc::clone(store)), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } } diff --git a/swarm-controller/src/accounts/gitlab_account.rs b/swarm-controller/src/accounts/gitlab_account.rs index 54dea717f..2f29ca7b7 100644 --- a/swarm-controller/src/accounts/gitlab_account.rs +++ b/swarm-controller/src/accounts/gitlab_account.rs @@ -206,35 +206,8 @@ mod tests { /// Bare-minimum `AppState`, as `forge_account`'s tests build it. fn state(store: &Arc) -> crate::routes::AppState { crate::routes::AppState { - hives: std::sync::Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), accounts: Accounts::Fake(Arc::clone(store)), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } } diff --git a/swarm-controller/src/accounts/linked_accounts.rs b/swarm-controller/src/accounts/linked_accounts.rs index 47d497f74..a59cc7b84 100644 --- a/swarm-controller/src/accounts/linked_accounts.rs +++ b/swarm-controller/src/accounts/linked_accounts.rs @@ -1231,35 +1231,8 @@ pub(crate) mod tests { /// Bare-minimum `AppState`, as `matrix_account`'s tests build it. fn state(store: &Arc) -> crate::routes::AppState { crate::routes::AppState { - hives: std::sync::Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), accounts: super::Accounts::Fake(Arc::clone(store)), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } } diff --git a/swarm-controller/src/accounts/matrix_account.rs b/swarm-controller/src/accounts/matrix_account.rs index c0f756c2a..ea2d3a8d9 100644 --- a/swarm-controller/src/accounts/matrix_account.rs +++ b/swarm-controller/src/accounts/matrix_account.rs @@ -455,35 +455,8 @@ mod tests { /// never touches, and `store` as the account store. fn state(store: &Arc) -> crate::routes::AppState { crate::routes::AppState { - hives: std::sync::Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), accounts: Accounts::Fake(Arc::clone(store)), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } } diff --git a/swarm-controller/src/agents/agent_limits.rs b/swarm-controller/src/agents/agent_limits.rs index 9902c038e..d3c3c427e 100644 --- a/swarm-controller/src/agents/agent_limits.rs +++ b/swarm-controller/src/agents/agent_limits.rs @@ -452,38 +452,12 @@ pub(crate) mod tests { /// One hive, `pr1ma`, on `client`. pub(crate) fn state(client: &async_nats::Client) -> crate::routes::AppState { crate::routes::AppState { - hives: Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: Arc::new(Vec::new()), status: Some(Arc::new(crate::status::StatusReader::new( client.clone(), std::time::Duration::from_mins(1), ))), wanted: Some(Arc::new(WantedWriter::new(client.clone()))), - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: Arc::default(), - swarm_name: None, - auth: None, - forge: Arc::default(), - create_gate: Arc::default(), - accounts: crate::linked_accounts::Accounts::Fake(Arc::default()), - reviews: Arc::default(), - try_outs: crate::review_classifier::TryOuts::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } } diff --git a/swarm-controller/src/routes/mod.rs b/swarm-controller/src/routes/mod.rs index 79d972d4d..b7afa6a05 100644 --- a/swarm-controller/src/routes/mod.rs +++ b/swarm-controller/src/routes/mod.rs @@ -198,6 +198,47 @@ pub(crate) struct AppState { pub(crate) alerts: Option>, } +#[cfg(test)] +impl AppState { + /// The state every handler test starts from: one hive, `pr1ma`, no queue + /// behind any reader, an empty in-memory job graph, and an in-memory + /// account store. A test that needs more sets those fields with + /// struct-update syntax (`..AppState::for_test()`). + pub(crate) fn for_test() -> Self { + Self { + hives: Arc::new(vec![HiveEntry { + name: "pr1ma".to_owned(), + domain: "pr1ma.example".to_owned(), + dashboard: "pr1ma.hives.example".to_owned(), + }]), + links: Arc::new(Vec::new()), + status: None, + wanted: None, + agent_status: None, + agent_icons: None, + agents_md: None, + reported_inference: None, + agent_login: None, + jobq: Arc::new(Mutex::new(hive_jobq::scheduler::Scheduler::new( + hive_jobq::Graph::new(), + hive_jobq::resources::ResourceTable::new(), + ))), + webhook_secret: Arc::default(), + config_prs: Arc::default(), + swarm_name: None, + auth: None, + forge: Arc::default(), + create_gate: Arc::default(), + accounts: linked_accounts::Accounts::Fake(Arc::default()), + try_outs: review_classifier::TryOuts::default(), + reviews: Arc::default(), + runs: review_classifier::runs::Runs::Live(None), + builds: builder::runs::Builds::Live(None), + alerts: None, + } + } +} + /// Why the status route answers 503 rather than an empty list. /// /// "I cannot reach the store" and "every hive is silent" are different @@ -456,45 +497,8 @@ pub(crate) mod tests { >; pub(crate) fn state_with_roster() -> (super::AppState, SharedSched) { - let sched = - std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))); - let state = super::AppState { - hives: std::sync::Arc::new(vec![HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - // No queue, for the same reason as `status`: these tests build - // the creation graph and read its shape, and never run the node - // that would write a declaration. - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::clone(&sched), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - // No bridge: these tests drive agent *creation*, which queues a - // job and consults one only for a rule-breaking name. The roster - // read is the verb that needs it, and it has its own test below. - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), - accounts: crate::linked_accounts::Accounts::Fake(std::sync::Arc::default()), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, - }; + let state = super::AppState::for_test(); + let sched = std::sync::Arc::clone(&state.jobq); (state, sched) } diff --git a/swarm-controller/src/secrets/backend_env.rs b/swarm-controller/src/secrets/backend_env.rs index de414148b..625849e9d 100644 --- a/swarm-controller/src/secrets/backend_env.rs +++ b/swarm-controller/src/secrets/backend_env.rs @@ -202,35 +202,8 @@ pub(crate) mod tests { /// Bare-minimum `AppState`, as `github_account`'s tests build it. pub(crate) fn state(store: &Arc) -> crate::routes::AppState { crate::routes::AppState { - hives: std::sync::Arc::new(vec![crate::routes::hives::HiveEntry { - name: "pr1ma".to_owned(), - domain: "pr1ma.example".to_owned(), - dashboard: "pr1ma.hives.example".to_owned(), - }]), - links: std::sync::Arc::new(Vec::new()), - status: None, - wanted: None, - agent_status: None, - agent_icons: None, - agents_md: None, - reported_inference: None, - agent_login: None, - jobq: std::sync::Arc::new(std::sync::Mutex::new(hive_jobq::scheduler::Scheduler::new( - hive_jobq::Graph::new(), - hive_jobq::resources::ResourceTable::new(), - ))), - webhook_secret: std::sync::Arc::default(), - config_prs: std::sync::Arc::default(), - swarm_name: None, - auth: None, - forge: std::sync::Arc::default(), - create_gate: std::sync::Arc::default(), accounts: Accounts::Fake(Arc::clone(store)), - try_outs: crate::review_classifier::TryOuts::default(), - reviews: std::sync::Arc::default(), - runs: crate::review_classifier::runs::Runs::Live(None), - builds: crate::builder::runs::Builds::Live(None), - alerts: None, + ..crate::routes::AppState::for_test() } }