From 934ba3a9c5cff727e0cecfba0c1877f0ea34ad31 Mon Sep 17 00:00:00 2001 From: damocles Date: Sat, 7 Nov 2026 07:56:50 +0100 Subject: [PATCH] hive-agent: test the state status computation --- hive-agent/src/web_ui/state.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/hive-agent/src/web_ui/state.rs b/hive-agent/src/web_ui/state.rs index d646970e..bbf47591 100644 --- a/hive-agent/src/web_ui/state.rs +++ b/hive-agent/src/web_ui/state.rs @@ -13,11 +13,7 @@ pub(super) async fn api_state(State(state): State) -> axum::Json snapshot.seq are post-snapshot, never missed." let seq = state.bus.current_seq(); let login = *state.login.lock().unwrap(); - let status = match login { - LoginState::Online if state.bus.is_rate_limited() => "rate_limited", - LoginState::Online => "online", - LoginState::NeedsLogin => "needs_login_idle", - }; + let status = status_of(login, state.bus.is_rate_limited()); let dashboard_port = std::env::var("HIVE_DASHBOARD_PORT") .ok() .and_then(|s| s.parse::().ok()) @@ -430,11 +426,33 @@ fn offered_shown(model: String, effort: String, offered: hive_runtime::SessionCh } } +/// The `status` string of the state snapshot: credentials missing beats +/// everything, then a rate limit, else online. +fn status_of(login: LoginState, rate_limited: bool) -> &'static str { + match login { + LoginState::Online if rate_limited => "rate_limited", + LoginState::Online => "online", + LoginState::NeedsLogin => "needs_login_idle", + } +} + #[cfg(test)] mod tests { use hive_runtime::{Choice, SessionChoices}; - use super::{Shown, offered_shown}; + use super::{LoginState, Shown, offered_shown, status_of}; + + #[test] + fn status_follows_the_credentials_then_the_rate_limit() { + assert_eq!(status_of(LoginState::Online, false), "online"); + assert_eq!(status_of(LoginState::Online, true), "rate_limited"); + assert_eq!(status_of(LoginState::NeedsLogin, false), "needs_login_idle"); + assert_eq!( + status_of(LoginState::NeedsLogin, true), + "needs_login_idle", + "missing credentials outrank a rate limit" + ); + } fn choice(current: &str, values: &[&str]) -> Choice { Choice {