Skip to content

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

test(skills): add catalog store tests and oauth flow coverage

Steven Enamakel42df101c8ec8
diff --git a/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_bearer_token_tests.rs b/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_bearer_token_tests.rs
index fcc4ead507..0af8d75e11 100644
--- a/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_bearer_token_tests.rs
+++ b/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_bearer_token_tests.rs
@@ -203,6 +203,6 @@ fn disconnect_openai_oauth_clears_profile() {
store.upsert_profile(profile, true).unwrap();
assert!(openai_oauth_status(&config).unwrap().connected);
- disconnect_openai_oauth(&config).unwrap();
+ runtime().block_on(disconnect_openai_oauth(&config)).unwrap();
assert!(!openai_oauth_status(&config).unwrap().connected);
}
diff --git a/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_lifecycle_tests.rs b/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_lifecycle_tests.rs
index 9e5bcb9c68..9cc44d7746 100644
--- a/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_lifecycle_tests.rs
+++ b/crates/openhuman-core/src/security/credentials/openai_oauth/flow_tests_lifecycle_tests.rs
@@ -5,7 +5,7 @@ fn start_openai_oauth_returns_authorize_url() {
let tmp = tempdir().unwrap();
let config = test_config(&tmp);
- let start = start_openai_oauth(&config).unwrap();
+ let start = runtime().block_on(start_openai_oauth(&config)).unwrap();
assert!(start.auth_url.contains("auth.openai.com"));
assert!(start.auth_url.contains("code_challenge="));
assert_eq!(start.redirect_uri, "http://127.0.0.1:1455/auth/callback");
@@ -96,35 +96,90 @@ fn complete_openai_oauth_rejects_missing_pending_session() {
#[test]
fn complete_openai_oauth_rejects_expired_pending_session() {
+ use tinystoragedrivers::secrets::SecretStore as _;
+
let tmp = tempdir().unwrap();
let config = test_config(&tmp);
- std::fs::write(
- tmp.path().join("openai-oauth-pending.json"),
- serde_json::json!({
- "state": "state",
- "verifier": "verifier",
- "redirect_uri": "http://127.0.0.1:1455/auth/callback",
- "created_at": 1_u64,
- })
- .to_string(),
- )
- .unwrap();
+ let pending = pending::store(&config).unwrap();
+ runtime().block_on(async {
+ pending
+ .set(
+ "openai-oauth-pending",
+ serde_json::json!({
+ "state": "state",
+ "verifier": "verifier",
+ "redirect_uri": "http://127.0.0.1:1455/auth/callback",
+ "created_at": 1_u64,
+ })
+ .to_string()
+ .as_bytes(),
+ )
+ .await
+ .unwrap();
+ let err = complete_openai_oauth(
+ &config,
+ "http://127.0.0.1:1455/auth/callback?code=fake&state=state",
+ )
+ .await
+ .unwrap_err();
+ assert!(err.contains("no pending OAuth session"));
+ assert!(pending.get("openai-oauth-pending").await.unwrap().is_none());
+ });
+}
+
+#[test]
+fn start_openai_oauth_keeps_the_pending_session_in_the_secret_store_not_a_file() {
+ use tinystoragedrivers::secrets::SecretStore as _;
+ let tmp = tempdir().unwrap();
+ let config = test_config(&tmp);
+ let start = runtime().block_on(start_openai_oauth(&config)).unwrap();
+
+ assert!(!tmp.path().join("openai-oauth-pending.json").exists());
+ let pending = pending::store(&config).unwrap();
+ let stored = runtime()
+ .block_on(pending.get("openai-oauth-pending"))
+ .unwrap()
+ .expect("the session is stored");
+ let doc: serde_json::Value = serde_json::from_slice(&stored).unwrap();
+ assert_eq!(doc["state"], start.state);
+}
+
+#[test]
+fn a_legacy_pending_file_started_before_the_upgrade_still_completes_its_flow() {
+ let tmp = tempdir().unwrap();
+ let config = test_config(&tmp);
+ let legacy = tmp.path().join("openai-oauth-pending.json");
+ let created_at = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_secs();
+ let bytes = serde_json::json!({
+ "state": "legacy-state",
+ "verifier": "legacy-verifier",
+ "redirect_uri": "http://127.0.0.1:1455/auth/callback",
+ "created_at": created_at,
+ })
+ .to_string();
+ std::fs::write(&legacy, &bytes).unwrap();
+
+ // A wrong state is rejected as a mismatch, which only happens when the
+ // legacy session was found.
let err = runtime()
.block_on(complete_openai_oauth(
&config,
- "http://127.0.0.1:1455/auth/callback?code=fake&state=state",
+ "http://127.0.0.1:1455/auth/callback?code=fake&state=wrong",
))
.unwrap_err();
- assert!(err.contains("no pending OAuth session"));
- assert!(!tmp.path().join("openai-oauth-pending.json").exists());
+ assert!(err.contains("state mismatch"), "{err}");
+ assert_eq!(std::fs::read_to_string(&legacy).unwrap(), bytes);
}
#[test]
fn complete_openai_oauth_rejects_state_mismatch() {
let tmp = tempdir().unwrap();
let config = test_config(&tmp);
- let start = start_openai_oauth(&config).unwrap();
+ let start = runtime().block_on(start_openai_oauth(&config)).unwrap();
let callback = format!(
"http://127.0.0.1:1455/auth/callback?code=fake&state=not-{}",
start.state
diff --git a/crates/openhuman-core/src/security/credentials/openai_oauth/pending_tests.rs b/crates/openhuman-core/src/security/credentials/openai_oauth/pending_tests.rs
new file mode 100644
index 0000000000..5867e58eeb
--- /dev/null
+++ b/crates/openhuman-core/src/security/credentials/openai_oauth/pending_tests.rs
@@ -0,0 +1,114 @@
+use super::*;
+
+fn config_in(tmp: &tempfile::TempDir) -> Config {
+ Config {
+ config_path: tmp.path().join("config.toml"),
+ ..Config::default()
+ }
+}
+
+fn runtime() -> tokio::runtime::Runtime {
+ tokio::runtime::Runtime::new().unwrap()
+}
+
+fn session(created_at: u64) -> Vec<u8> {
+ serde_json::json!({
+ "state": "legacy-state",
+ "verifier": "legacy-verifier",
+ "redirect_uri": "http://127.0.0.1:1455/auth/callback",
+ "created_at": created_at,
+ })
+ .to_string()
+ .into_bytes()
+}
+
+#[test]
+fn the_fallback_store_is_shared_per_state_dir_and_not_across_dirs() {
+ let (a, b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let first = store(&config_in(&a)).unwrap();
+ let again = store(&config_in(&a)).unwrap();
+ let other = store(&config_in(&b)).unwrap();
+
+ runtime().block_on(async {
+ first.set(PENDING_SECRET, b"one").await.unwrap();
+ assert_eq!(
+ again.get(PENDING_SECRET).await.unwrap().unwrap().as_slice(),
+ b"one"
+ );
+ assert!(other.get(PENDING_SECRET).await.unwrap().is_none());
+ });
+}
+
+#[test]
+fn a_fresh_legacy_session_is_adopted_once_and_the_file_is_left_alone() {
+ let tmp = tempfile::tempdir().unwrap();
+ let legacy = tmp.path().join(LEGACY_FILE);
+ let bytes = session(now_secs());
+ std::fs::write(&legacy, &bytes).unwrap();
+ let secrets = store(&config_in(&tmp)).unwrap();
+
+ runtime().block_on(async {
+ adopt_legacy(tmp.path(), &secrets).await;
+ assert_eq!(
+ secrets.get(PENDING_SECRET).await.unwrap().unwrap().as_slice(),
+ bytes.as_slice()
+ );
+
+ // Consumed by a completed flow, it is not resurrected from the file.
+ secrets.delete(PENDING_SECRET).await.unwrap();
+ adopt_legacy(tmp.path(), &secrets).await;
+ assert!(secrets.get(PENDING_SECRET).await.unwrap().is_none());
+ });
+ assert_eq!(std::fs::read(&legacy).unwrap(), bytes);
+ forget_for_tests(tmp.path());
+}
+
+#[test]
+fn an_expired_or_malformed_legacy_session_is_ignored() {
+ let tmp = tempfile::tempdir().unwrap();
+ let secrets = store(&config_in(&tmp)).unwrap();
+
+ runtime().block_on(async {
+ std::fs::write(tmp.path().join(LEGACY_FILE), session(1)).unwrap();
+ adopt_legacy(tmp.path(), &secrets).await;
+ assert!(secrets.get(PENDING_SECRET).await.unwrap().is_none());
+
+ forget_for_tests(tmp.path());
+ let secrets = store(&config_in(&tmp)).unwrap();
+ std::fs::write(tmp.path().join(LEGACY_FILE), b"not json").unwrap();
+ adopt_legacy(tmp.path(), &secrets).await;
+ assert!(secrets.get(PENDING_SECRET).await.unwrap().is_none());
+ });
+}
+
+#[test]
+fn a_session_already_in_the_store_wins_over_the_legacy_file() {
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::write(tmp.path().join(LEGACY_FILE), session(now_secs())).unwrap();
+ let secrets = store(&config_in(&tmp)).unwrap();
+
+ runtime().block_on(async {
+ secrets.set(PENDING_SECRET, b"newer").await.unwrap();
+ adopt_legacy(tmp.path(), &secrets).await;
+ assert_eq!(
+ secrets.get(PENDING_SECRET).await.unwrap().unwrap().as_slice(),
+ b"newer"
+ );
+ });
+}
+
+#[test]
+fn an_oversized_legacy_file_is_ignored() {
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::write(
+ tmp.path().join(LEGACY_FILE),
+ vec![b' '; (MAX_LEGACY_BYTES + 1) as usize],
+ )
+ .unwrap();
+ let secrets = store(&config_in(&tmp)).unwrap();
+
+ runtime().block_on(async {
+ adopt_legacy(tmp.path(), &secrets).await;
+ assert!(secrets.get(PENDING_SECRET).await.unwrap().is_none());
+ });
+}
diff --git a/crates/openhuman-core/src/skills/catalog/mod.rs b/crates/openhuman-core/src/skills/catalog/mod.rs
index dd139ad4d5..917b0ef24a 100644
--- a/crates/openhuman-core/src/skills/catalog/mod.rs
+++ b/crates/openhuman-core/src/skills/catalog/mod.rs
@@ -19,6 +19,8 @@ pub mod ops;
mod registry;
#[cfg(feature = "skills")]
pub mod schemas;
+#[cfg(feature = "skills")]
+mod store;
#[cfg(all(test, feature = "skills"))]
pub(crate) mod test_fixtures;
#[cfg(feature = "skills")]
diff --git a/crates/openhuman-core/src/skills/catalog/store.rs b/crates/openhuman-core/src/skills/catalog/store.rs
index 60dfd92488..470f38450f 100644
--- a/crates/openhuman-core/src/skills/catalog/store.rs
+++ b/crates/openhuman-core/src/skills/catalog/store.rs
@@ -86,7 +86,9 @@ impl HostCatalogStore {
}
// A refresh saved a catalog between the read and the import: that
// one is newer than the file, so it wins.
- Err(error) if error.kind() == ErrorKind::AlreadyExists => store.load(registry).await,
+ Err(error) if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {
+ store.load(registry).await
+ }
Err(error) => Err(StoreError::Io(error.to_string())),
}
}
diff --git a/crates/openhuman-core/src/skills/catalog/store_tests.rs b/crates/openhuman-core/src/skills/catalog/store_tests.rs
new file mode 100644
index 0000000000..b2a304f521
--- /dev/null
+++ b/crates/openhuman-core/src/skills/catalog/store_tests.rs
@@ -0,0 +1,107 @@
+use super::*;
+use tinyskills::{FileCatalogStore, Validators};
+
+fn catalog(fetched_at: u64) -> StoredCatalog {
+ StoredCatalog::new(Vec::new(), fetched_at, Validators::default(), 0)
+}
+
+#[tokio::test]
+async fn a_saved_catalog_is_read_back_without_writing_a_catalog_file() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = HostCatalogStore::new(tmp.path());
+
+ assert!(store.load("hermes").await.unwrap().is_none());
+ store.save("hermes", &catalog(7)).await.unwrap();
+
+ let loaded = store.load("hermes").await.unwrap().unwrap();
+ assert_eq!(loaded.fetched_at, 7);
+ assert!(
+ !tmp.path().join("hermes.json").exists(),
+ "the catalog lives in the storage backend, not in a file"
+ );
+}
+
+#[tokio::test]
+async fn a_legacy_catalog_file_is_imported_once_and_left_in_place() {
+ let tmp = tempfile::tempdir().unwrap();
+ FileCatalogStore::new(tmp.path())
+ .save("hermes", &catalog(11))
+ .await
+ .unwrap();
+ let legacy = tmp.path().join("hermes.json");
+ let before = std::fs::read(&legacy).unwrap();
+ let store = HostCatalogStore::new(tmp.path());
+
+ assert_eq!(store.load("hermes").await.unwrap().unwrap().fetched_at, 11);
+ assert_eq!(std::fs::read(&legacy).unwrap(), before, "never modified");
+
+ // A refresh replaces the document; the older file is not read again.
+ store.save("hermes", &catalog(99)).await.unwrap();
+ assert_eq!(store.load("hermes").await.unwrap().unwrap().fetched_at, 99);
+ assert_eq!(std::fs::read(&legacy).unwrap(), before);
+}
+
+#[tokio::test]
+async fn an_unusable_legacy_file_reads_as_an_empty_cache() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = HostCatalogStore::new(tmp.path());
+
+ std::fs::write(tmp.path().join("hermes.json"), b"{ not json").unwrap();
+ assert!(store.load("hermes").await.unwrap().is_none());
+
+ std::fs::write(
+ tmp.path().join("other.json"),
+ serde_json::json!({ "format": 999, "entries": [] }).to_string(),
+ )
+ .unwrap();
+ assert!(store.load("other").await.unwrap().is_none());
+}
+
+#[cfg(unix)]
+#[tokio::test]
+async fn a_symlinked_legacy_file_is_not_followed() {
+ let tmp = tempfile::tempdir().unwrap();
+ let elsewhere = tempfile::tempdir().unwrap();
+ FileCatalogStore::new(elsewhere.path())
+ .save("hermes", &catalog(5))
+ .await
+ .unwrap();
+ std::os::unix::fs::symlink(
+ elsewhere.path().join("hermes.json"),
+ tmp.path().join("hermes.json"),
+ )
+ .unwrap();
+
+ let store = HostCatalogStore::new(tmp.path());
+ assert!(store.load("hermes").await.unwrap().is_none());
+}
+
+#[tokio::test]
+async fn a_registry_id_that_is_not_a_path_segment_is_refused() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = HostCatalogStore::new(tmp.path());
+
+ assert!(matches!(
+ store.load("../escape").await,
+ Err(StoreError::InvalidId(_))
+ ));
+ assert!(matches!(
+ store.save("../escape", &catalog(1)).await,
+ Err(StoreError::InvalidId(_))
+ ));
+}
+
+#[tokio::test]
+async fn catalogs_of_different_cache_directories_do_not_mix() {
+ let (a, b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ HostCatalogStore::new(a.path())
+ .save("hermes", &catalog(1))
+ .await
+ .unwrap();
+
+ assert!(HostCatalogStore::new(b.path())
+ .load("hermes")
+ .await
+ .unwrap()
+ .is_none());
+}