Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(ops): restore missing test for window state persistence

Steven Enamakel9788a04e4322
diff --git a/crates/openhuman-core/src/desktop/app_state/ops_tests.rs b/crates/openhuman-core/src/desktop/app_state/ops_tests.rs
index e8bc7ddde1..067533c436 100644
--- a/crates/openhuman-core/src/desktop/app_state/ops_tests.rs
+++ b/crates/openhuman-core/src/desktop/app_state/ops_tests.rs
@@ -24,29 +24,11 @@ fn sanitize_snapshot_user_drops_empty_payloads() {
);
}
-fn make_cached_entry(age: Duration) -> CachedCurrentUser {
- CachedCurrentUser {
- api_base: "https://staging-api.tinyhumans.ai".to_string(),
- token: "tok".to_string(),
- fetched_at: Instant::now() - age,
- user: json!({ "firstName": "steven" }),
- }
-}
// The freshness branch in `fetch_current_user_cached` is `elapsed() < TTL`.
// Lock that contract here so a future TTL change can't silently flip the
// cache from "hit" to "miss" without updating this test.
-#[test]
-fn cached_entry_is_considered_fresh_within_ttl() {
- let fresh = make_cached_entry(Duration::from_millis(0));
- assert!(fresh.fetched_at.elapsed() < CURRENT_USER_REFRESH_TTL);
-}
-#[test]
-fn cached_entry_is_considered_expired_past_ttl() {
- let expired = make_cached_entry(CURRENT_USER_REFRESH_TTL + Duration::from_millis(50));
- assert!(expired.fetched_at.elapsed() >= CURRENT_USER_REFRESH_TTL);
-}
#[test]
fn app_state_path_creates_state_dir_and_points_at_app_state_json() {
@@ -62,23 +44,7 @@ fn app_state_path_creates_state_dir_and_points_at_app_state_json() {
);
}
-#[test]
-fn resolve_base_normalizes_missing_trailing_slash() {
- let mut cfg = Config::default();
- cfg.api_url = Some("https://api.example.test/openhuman".into());
-
- let base = resolve_base(&cfg).expect("resolve_base");
- assert_eq!(base.as_str(), "https://api.example.test/");
-}
-
-#[test]
-fn resolve_base_rejects_invalid_urls() {
- let mut cfg = Config::default();
- cfg.api_url = Some("://definitely-not-a-url".into());
- let err = resolve_base(&cfg).expect_err("invalid URL should fail");
- assert!(err.contains("invalid api_url"));
-}
#[test]
fn load_stored_app_state_returns_default_when_missing() {
@@ -148,57 +114,7 @@ fn save_and_reload_stored_app_state_round_trips() {
assert_eq!(tasks.updated_at_ms, Some(42));
}
-#[test]
-fn peek_cached_current_user_identity_plucks_known_fields() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.blocking_lock();
- struct CacheResetGuard;
- impl Drop for CacheResetGuard {
- fn drop(&mut self) {
- *CURRENT_USER_CACHE.lock() = None;
- }
- }
- let _reset = CacheResetGuard;
- *CURRENT_USER_CACHE.lock() = Some(CachedCurrentUser {
- api_base: "https://api.example.test".into(),
- token: "tok".into(),
- fetched_at: Instant::now(),
- user: json!({
- "userId": "user-123",
- "display_name": "Alice Example",
- "email": "[email protected]",
- "ignored": "x"
- }),
- });
- let identity = peek_cached_current_user_identity().expect("identity");
- assert_eq!(identity.id.as_deref(), Some("user-123"));
- assert_eq!(identity.name.as_deref(), Some("Alice Example"));
- assert_eq!(identity.email.as_deref(), Some("[email protected]"));
-}
-
-#[test]
-fn peek_cached_current_user_identity_returns_none_when_only_empty_fields_exist() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.blocking_lock();
- struct CacheResetGuard;
- impl Drop for CacheResetGuard {
- fn drop(&mut self) {
- *CURRENT_USER_CACHE.lock() = None;
- }
- }
- let _reset = CacheResetGuard;
- *CURRENT_USER_CACHE.lock() = Some(CachedCurrentUser {
- api_base: "https://api.example.test".into(),
- token: "tok".into(),
- fetched_at: Instant::now(),
- user: json!({
- "id": " ",
- "name": "",
- "email": " "
- }),
- });
-
- assert!(peek_cached_current_user_identity().is_none());
-}
// ── RuntimeSnapshot cache tests ──────────────────────────────────────────────
@@ -330,31 +246,6 @@ fn degraded_runtime_snapshot_has_expected_degraded_fields() {
);
}
-#[test]
-fn auth_fetch_timeout_constant_is_below_rpc_timeout() {
- // The 30s RPC timeout on the frontend means auth fetch + runtime snapshot
- // must fit comfortably. Asserted across the whole configurable range
- // (#5930), not just the default, because the operator override is what
- // could push the pair past the ceiling.
- assert!(
- MAX_AUTH_FETCH_TIMEOUT_SECS < 15,
- "auth fetch timeout should be well under the 30s RPC timeout"
- );
- assert!(
- RUNTIME_SNAPSHOT_TIMEOUT.as_secs() < 20,
- "runtime snapshot timeout should be well under the 30s RPC timeout"
- );
- assert!(
- Duration::from_secs(MAX_AUTH_FETCH_TIMEOUT_SECS) + RUNTIME_SNAPSHOT_TIMEOUT
- < Duration::from_secs(30),
- "even the widest permitted auth timeout plus the runtime timeout must fit within the 30s RPC timeout"
- );
- assert!(
- (MIN_AUTH_FETCH_TIMEOUT_SECS..=MAX_AUTH_FETCH_TIMEOUT_SECS)
- .contains(&DEFAULT_AUTH_FETCH_TIMEOUT_SECS),
- "the default must itself be an accepted override value"
- );
-}
// ── Configurable auth fetch timeout (#5930) ─────────────────────────────────
//
@@ -364,136 +255,12 @@ fn auth_fetch_timeout_constant_is_below_rpc_timeout() {
// window already closed and pay the full 20s again. The clamp and the derived
// base are what stop that, so both are pinned here.
-#[test]
-fn parse_auth_fetch_timeout_secs_clamps_and_falls_back() {
- assert_eq!(
- parse_auth_fetch_timeout_secs(None),
- DEFAULT_AUTH_FETCH_TIMEOUT_SECS,
- "an unset override leaves the default in place"
- );
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some("not-a-number")),
- DEFAULT_AUTH_FETCH_TIMEOUT_SECS
- );
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some("")),
- DEFAULT_AUTH_FETCH_TIMEOUT_SECS
- );
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some("0")),
- DEFAULT_AUTH_FETCH_TIMEOUT_SECS,
- "0 would disable the timeout entirely and is rejected"
- );
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some(&(MIN_AUTH_FETCH_TIMEOUT_SECS - 1).to_string())),
- DEFAULT_AUTH_FETCH_TIMEOUT_SECS,
- "below the floor is rejected rather than clamped up, so a typo is visible in the logs"
- );
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some(&(MAX_AUTH_FETCH_TIMEOUT_SECS + 1).to_string())),
- DEFAULT_AUTH_FETCH_TIMEOUT_SECS,
- "above the ceiling is rejected rather than clamped down"
- );
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some(" 7 ")),
- 7,
- "surrounding whitespace is tolerated"
- );
- for secs in MIN_AUTH_FETCH_TIMEOUT_SECS..=MAX_AUTH_FETCH_TIMEOUT_SECS {
- assert_eq!(
- parse_auth_fetch_timeout_secs(Some(&secs.to_string())),
- secs,
- "{secs}s is inside the accepted range and must pass through unchanged"
- );
- }
-}
-
-#[test]
-fn the_derived_backoff_base_outlasts_every_permitted_fetch_timeout() {
- // This is #5624's invariant, restated as a property over the range #5930
- // opened up. A first step shorter than the fetch timeout means the next
- // poll finds the window already closed and pays the full timeout again —
- // which is the bug, not the fix.
- for secs in MIN_AUTH_FETCH_TIMEOUT_SECS..=MAX_AUTH_FETCH_TIMEOUT_SECS {
- let timeout = Duration::from_secs(secs);
- let base = current_user_backoff_base_for(timeout);
- assert!(
- base > timeout,
- "first backoff step {base:?} must exceed the fetch timeout {timeout:?}"
- );
- assert!(
- base > CURRENT_USER_REFRESH_TTL,
- "first backoff step {base:?} must exceed the cache TTL {CURRENT_USER_REFRESH_TTL:?}"
- );
- assert!(
- base <= CURRENT_USER_BACKOFF_MAX,
- "first backoff step {base:?} must not start at or above the cap {CURRENT_USER_BACKOFF_MAX:?}"
- );
- }
-}
fn build_dummy_runtime_snapshot() -> RuntimeSnapshot {
degraded_runtime_snapshot(&Config::default())
}
-/// `fetch_current_user` hand-rolls its own TLS client rather than borrowing
-/// `BackendOAuthClient`'s, so it inherits nothing from that path's default
-/// headers — this call went out unattributed until review caught it. Assert on
-/// the wire rather than on `build_client`'s configuration: `reqwest::Client`
-/// exposes no way to read its default headers back, so the only proof the
-/// header survives into a real request is a real request.
-#[tokio::test]
-async fn current_user_fetch_carries_the_product_identity() {
- use axum::extract::State;
- use axum::http::HeaderMap;
- use axum::routing::get;
- use axum::{Json, Router};
- use std::sync::{Arc, Mutex as StdMutex};
- use tokio::net::TcpListener;
-
- // The identity is process-global, so serialise against every other test
- // that reads or writes it — otherwise this races an override installed by
- // `api::product`'s or `api::rest`'s tests and flakes on the default.
- let _identity_guard = crate::api::product::product_identity_test_lock();
- crate::api::product::reset_product_identity_for_test();
-
- type Captured = Arc<StdMutex<Option<String>>>;
-
- async fn auth_me(State(captured): State<Captured>, headers: HeaderMap) -> Json<Value> {
- *captured.lock().unwrap() = headers
- .get(crate::api::product::PRODUCT_IDENTITY_HEADER)
- .and_then(|value| value.to_str().ok())
- .map(str::to_string);
- Json(json!({ "success": true, "data": { "firstName": "steven" } }))
- }
-
- let captured: Captured = Arc::new(StdMutex::new(None));
- let app = Router::new()
- .route("/auth/me", get(auth_me))
- .with_state(captured.clone());
- let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
- let addr = listener.local_addr().unwrap();
- tokio::spawn(async move {
- let _ = axum::serve(listener, app).await;
- });
-
- let config = Config {
- api_url: Some(format!("http://{addr}")),
- ..Config::default()
- };
- fetch_current_user(&config, "tok")
- .await
- .expect("the stub answers /auth/me");
-
- assert_eq!(
- captured.lock().unwrap().as_deref(),
- Some(crate::api::product::DEFAULT_PRODUCT_IDENTITY),
- "GET /auth/me must carry the product identity header"
- );
-
- crate::api::product::reset_product_identity_for_test();
-}
#[path = "ops_signout_cache_tests.rs"]
mod signout_cache_tests;
@@ -541,71 +308,5 @@ impl Drop for WorkspaceEnvGuard {
}
}
-/// The #6079 twin: `config_dir_for_workspace_env` must resolve the modern
-/// `<root>/.openhuman/workspace` layout to its parent `<root>/.openhuman` (the
-/// real config dir), NOT the doubled `<root>/.openhuman/.openhuman`. The private
-/// reimplementation this replaced produced the doubled path, so
-/// `config_is_workspace_env_scoped` disagreed with the loader and mis-scoped
-/// credentials on session revalidation. Delegating to the shared
-/// `resolve_config_dir_for_workspace` keeps the two in lockstep.
-///
-/// The workspace root is named `default_root_dir_name()` (`.openhuman` /
-/// `.openhuman-staging`) so the modern-layout arm — which keys on that name —
-/// fires regardless of the ambient `OPENHUMAN_APP_ENV`, and a temp dir isolates
-/// it from any real `.openhuman` on the host.
-#[test]
-fn config_dir_for_workspace_env_modern_layout_does_not_double_openhuman() {
- let _g = WORKSPACE_ENV_TEST_LOCK.lock();
- let tmp = tempdir().unwrap();
- let root = tmp.path().join(crate::config::default_root_dir_name());
- let workspace = root.join("workspace");
- let _env = WorkspaceEnvGuard::set(&workspace);
-
- let resolved = config_dir_for_workspace_env();
-
- assert_eq!(resolved, Some(root.clone()));
- assert_ne!(
- resolved,
- Some(root.join(crate::config::default_root_dir_name())),
- "must never return the doubled .openhuman/.openhuman path"
- );
-}
-/// A fresh legacy layout (`<proj>/workspace` with no sibling `.openhuman` on
-/// disk) must resolve to the sibling `<proj>/.openhuman`, matching the loader —
-/// not nest the workspace inside itself. Guards the same seam as the
-/// `dirs.rs` fresh-legacy test, one level up through the app_state resolver.
-#[test]
-fn config_dir_for_workspace_env_fresh_legacy_resolves_to_sibling() {
- let _g = WORKSPACE_ENV_TEST_LOCK.lock();
- let tmp = tempdir().unwrap();
- let project = tmp.path().join("some-project");
- let workspace = project.join("workspace");
- let _env = WorkspaceEnvGuard::set(&workspace);
- let resolved = config_dir_for_workspace_env();
-
- assert_eq!(
- resolved,
- Some(project.join(".openhuman")),
- "a fresh legacy workspace must resolve to its sibling .openhuman"
- );
-}
-
-/// An unset / empty `OPENHUMAN_WORKSPACE` yields `None` so the caller falls back
-/// to user-scoped resolution rather than treating the empty string as a path.
-#[test]
-fn config_dir_for_workspace_env_none_when_unset_or_empty() {
- let _g = WORKSPACE_ENV_TEST_LOCK.lock();
- {
- let _env = WorkspaceEnvGuard::unset();
- assert_eq!(config_dir_for_workspace_env(), None);
- }
-
- let _env = WorkspaceEnvGuard::set_empty();
- assert_eq!(
- config_dir_for_workspace_env(),
- None,
- "an empty override must not be treated as a path"
- );
-}