Skip to content

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

test(composio): add unit tests for file, identity, and profile stores

Steven Enamakel58f84a0fed77
diff --git a/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs b/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs
index ccd6b54cb1..706f15387e 100644
--- a/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs
@@ -1,20 +1,20 @@
use super::*;
+use crate::config::Config;
use crate::storage::{MemoryStorage, Scope, StorageBackend};
use std::collections::BTreeMap;
-use std::path::PathBuf;
fn docs_in(storage: &MemoryStorage, scope: &str) -> Docs {
Docs::over(&storage.for_scope(&Scope::new(scope).unwrap()).unwrap())
}
-fn path() -> PathBuf {
- PathBuf::from("/ws/integrations/composio_identities.json")
+fn path() -> &'static str {
+ "composio_identities.json"
}
#[test]
fn a_missing_file_loads_the_default() {
let loaded: BTreeMap<String, u32> = docs_in(&MemoryStorage::new(), "local")
- .load(&path())
+ .load(path())
.unwrap();
assert!(loaded.is_empty());
}
@@ -24,15 +24,10 @@ fn save_then_load_round_trips_by_file_name() {
let docs = docs_in(&MemoryStorage::new(), "local");
let mut value = BTreeMap::new();
value.insert("gmail".to_string(), 3u32);
- docs.save(&path(), &value).unwrap();
- // The workspace part of the path is irrelevant; the file name is the key.
- let loaded: BTreeMap<String, u32> = docs
- .load(&PathBuf::from("/elsewhere/composio_identities.json"))
- .unwrap();
+ docs.save(path(), &value).unwrap();
+ let loaded: BTreeMap<String, u32> = docs.load("composio_identities.json").unwrap();
assert_eq!(loaded, value);
- let other: BTreeMap<String, u32> = docs
- .load(&PathBuf::from("/ws/integrations/composio_user_scopes.json"))
- .unwrap();
+ let other: BTreeMap<String, u32> = docs.load("composio_user_scopes.json").unwrap();
assert!(other.is_empty());
}
@@ -41,8 +36,8 @@ fn scopes_do_not_see_each_others_state() {
let storage = MemoryStorage::new();
let mut value = BTreeMap::new();
value.insert("gmail".to_string(), 1u32);
- docs_in(&storage, "alice").save(&path(), &value).unwrap();
- let bob: BTreeMap<String, u32> = docs_in(&storage, "bob").load(&path()).unwrap();
+ docs_in(&storage, "alice").save(path(), &value).unwrap();
+ let bob: BTreeMap<String, u32> = docs_in(&storage, "bob").load(path()).unwrap();
assert!(bob.is_empty());
}
@@ -51,43 +46,41 @@ async fn the_file_store_dispatches_to_documents_when_a_backend_is_pinned() {
let storage = MemoryStorage::new();
let docs = docs_in(&storage, "local");
let dir = tempfile::tempdir().unwrap();
- let file = dir.path().join("composio_identities.json");
+ let config = Config {
+ workspace_dir: dir.path().to_path_buf(),
+ ..Config::default()
+ };
let mut value = BTreeMap::new();
value.insert("gmail".to_string(), 7u32);
// Pin on this thread for the awaits below (a current-thread runtime).
super::TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = Some(docs.clone()));
- super::super::file_store::save(&file, &value).await.unwrap();
- let loaded: BTreeMap<String, u32> = super::super::file_store::load(&file).await.unwrap();
+ super::super::file_store::save(&config, "composio_identities.json", &value)
+ .await
+ .unwrap();
+ let loaded: BTreeMap<String, u32> =
+ super::super::file_store::load(&config, "composio_identities.json")
+ .await
+ .unwrap();
super::TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = None);
assert_eq!(loaded, value);
- assert!(!file.exists());
-}
-
-#[tokio::test]
-async fn the_file_path_still_works_with_no_backend() {
- let dir = tempfile::tempdir().unwrap();
- let path = dir.path().join("state.json");
- let mut value = BTreeMap::new();
- value.insert("gmail".to_string(), 3u32);
- super::super::file_store::save(&path, &value).await.unwrap();
- assert!(path.exists());
+ assert!(!dir.path().join("storage").exists());
}
#[test]
fn update_creates_applies_and_skips_unchanged() {
let docs = docs_in(&MemoryStorage::new(), "local");
let added = docs
- .update(&path(), |v: &mut BTreeMap<String, u32>| {
+ .update(path(), |v: &mut BTreeMap<String, u32>| {
v.insert("gmail".into(), 1);
(v.len(), true)
})
.unwrap();
assert_eq!(added, 1);
let unchanged = docs
- .update(&path(), |v: &mut BTreeMap<String, u32>| (v.len(), false))
+ .update(path(), |v: &mut BTreeMap<String, u32>| (v.len(), false))
.unwrap();
assert_eq!(unchanged, 1);
- let loaded: BTreeMap<String, u32> = docs.load(&path()).unwrap();
+ let loaded: BTreeMap<String, u32> = docs.load(path()).unwrap();
assert_eq!(loaded.get("gmail"), Some(&1));
}
@@ -98,7 +91,7 @@ fn concurrent_updates_do_not_lose_each_other() {
.map(|n| {
let docs = docs.clone();
std::thread::spawn(move || {
- docs.update(&path(), move |v: &mut BTreeMap<String, u32>| {
+ docs.update(path(), move |v: &mut BTreeMap<String, u32>| {
v.insert(format!("k{n}"), n);
((), true)
})
@@ -109,32 +102,6 @@ fn concurrent_updates_do_not_lose_each_other() {
for handle in handles {
handle.join().unwrap();
}
- let loaded: BTreeMap<String, u32> = docs.load(&path()).unwrap();
+ let loaded: BTreeMap<String, u32> = docs.load(path()).unwrap();
assert_eq!(loaded.len(), 8);
}
-
-#[tokio::test]
-async fn file_store_update_uses_the_file_with_no_backend_and_documents_when_pinned() {
- let dir = tempfile::tempdir().unwrap();
- let file = dir.path().join("state.json");
- let bump = |v: &mut BTreeMap<String, u32>| {
- *v.entry("n".into()).or_default() += 1;
- ((), true)
- };
- super::super::file_store::update(&file, bump).await.unwrap();
- super::super::file_store::update(&file, bump).await.unwrap();
- let loaded: BTreeMap<String, u32> = super::super::file_store::load(&file).await.unwrap();
- assert_eq!(loaded["n"], 2);
- assert!(file.exists());
-
- let docs = docs_in(&MemoryStorage::new(), "local");
- let pinned = dir.path().join("pinned.json");
- super::TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = Some(docs.clone()));
- super::super::file_store::update(&pinned, bump)
- .await
- .unwrap();
- super::TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = None);
- let stored: BTreeMap<String, u32> = docs.load(&pinned).unwrap();
- assert_eq!(stored["n"], 1);
- assert!(!pinned.exists());
-}
diff --git a/crates/openhuman-core/src/integrations/composio/file_store_tests.rs b/crates/openhuman-core/src/integrations/composio/file_store_tests.rs
index 30a6f4e406..e228f4f295 100644
--- a/crates/openhuman-core/src/integrations/composio/file_store_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/file_store_tests.rs
@@ -1,46 +1,46 @@
use super::*;
use std::collections::BTreeMap;
+fn config_in(dir: &tempfile::TempDir) -> Config {
+ Config {
+ workspace_dir: dir.path().to_path_buf(),
+ ..Config::default()
+ }
+}
+
#[tokio::test]
-async fn missing_file_loads_the_default() {
+async fn missing_value_loads_the_default() {
let dir = tempfile::tempdir().unwrap();
- let loaded: BTreeMap<String, u32> = load(&dir.path().join("absent.json")).await.unwrap();
+ let loaded: BTreeMap<String, u32> = load(&config_in(&dir), IDENTITIES_FILE).await.unwrap();
assert!(loaded.is_empty());
}
#[tokio::test]
-async fn save_then_load_round_trips_and_creates_the_directory() {
+async fn save_then_load_round_trips_in_the_workspace_default_backend() {
let dir = tempfile::tempdir().unwrap();
- let path = dir.path().join("nested").join("state.json");
+ let config = config_in(&dir);
let mut value = BTreeMap::new();
value.insert("gmail".to_string(), 3u32);
- save(&path, &value).await.unwrap();
- let loaded: BTreeMap<String, u32> = load(&path).await.unwrap();
+ save(&config, USER_SCOPES_FILE, &value).await.unwrap();
+ let loaded: BTreeMap<String, u32> = load(&config, USER_SCOPES_FILE).await.unwrap();
assert_eq!(loaded, value);
- assert!(!path.with_extension("json.tmp").exists());
+ // The other value is untouched, and nothing was written as a JSON file.
+ let other: BTreeMap<String, u32> = load(&config, IDENTITIES_FILE).await.unwrap();
+ assert!(other.is_empty());
+ assert!(!dir.path().join("integrations").exists());
+ assert!(dir.path().join("storage").exists());
}
#[tokio::test]
-async fn corrupt_file_is_an_error_not_a_default() {
+async fn update_applies_and_persists_a_change() {
let dir = tempfile::tempdir().unwrap();
- let path = dir.path().join("state.json");
- std::fs::write(&path, b"{not json").unwrap();
- let loaded: Result<BTreeMap<String, u32>, String> = load(&path).await;
- assert!(loaded.is_err());
-}
-
-#[test]
-fn paths_live_under_the_workspace_integrations_dir() {
- let config = Config {
- workspace_dir: std::path::PathBuf::from("/ws"),
- ..Config::default()
+ let config = config_in(&dir);
+ let bump = |v: &mut BTreeMap<String, u32>| {
+ *v.entry("n".into()).or_default() += 1;
+ ((), true)
};
- assert_eq!(
- path(&config, IDENTITIES_FILE),
- std::path::PathBuf::from("/ws/integrations/composio_identities.json")
- );
- assert_eq!(
- path(&config, USER_SCOPES_FILE),
- std::path::PathBuf::from("/ws/integrations/composio_user_scopes.json")
- );
+ update(&config, IDENTITIES_FILE, bump).await.unwrap();
+ update(&config, IDENTITIES_FILE, bump).await.unwrap();
+ let loaded: BTreeMap<String, u32> = load(&config, IDENTITIES_FILE).await.unwrap();
+ assert_eq!(loaded["n"], 2);
}
diff --git a/crates/openhuman-core/src/integrations/composio/identity_store_tests.rs b/crates/openhuman-core/src/integrations/composio/identity_store_tests.rs
index 5b594e5266..0962ec1e30 100644
--- a/crates/openhuman-core/src/integrations/composio/identity_store_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/identity_store_tests.rs
@@ -37,7 +37,6 @@ async fn persist_then_load_groups_fields_per_connection() {
assert_eq!(identity.user_id.as_deref(), Some("U123ABC"));
assert!(identity.email.is_some());
assert!(identity.handle.is_some());
- assert!(file_store::path(&config, IDENTITIES_FILE).exists());
}
#[tokio::test]
@@ -71,7 +70,8 @@ async fn empty_profile_writes_nothing() {
..ProviderUserProfile::default()
};
assert_eq!(persist_provider_profile(&config, &empty).await.unwrap(), 0);
- assert!(!file_store::path(&config, IDENTITIES_FILE).exists());
+ let stored: Vec<ConnectedIdentity> = file_store::load(&config, IDENTITIES_FILE).await.unwrap();
+ assert!(stored.is_empty());
assert!(load_connected_identities(&config).await.unwrap().is_empty());
}
diff --git a/crates/openhuman-core/src/integrations/composio/ops_profile_tools_execute_mock_tests.rs b/crates/openhuman-core/src/integrations/composio/ops_profile_tools_execute_mock_tests.rs
index 4b0dd63069..df39368f2a 100644
--- a/crates/openhuman-core/src/integrations/composio/ops_profile_tools_execute_mock_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops_profile_tools_execute_mock_tests.rs
@@ -183,6 +183,131 @@ async fn composio_execute_via_mock_propagates_backend_error() {
assert!(err.contains("rate limited"), "got: {err}");
}
+#[tokio::test]
+async fn composio_list_tools_honours_a_preference_still_in_the_legacy_file() {
+ // A workspace upgraded from before the preference moved into the module
+ // still has `composio_user_scopes.json`. The module filters the listing by
+ // the preference it holds, so that file must be migrated before the call;
+ // otherwise its default (`write` on) outranks the user's stored `write=false`.
+ let _serialised = module_guard().await;
+ let app = Router::new().route(
+ "/agent-integrations/composio/tools",
+ get(|| async {
+ Json(json!({
+ "success": true,
+ "data": {"tools": [
+ {"type":"function","function":{"name":"GMAIL_FETCH_EMAILS"}},
+ {"type":"function","function":{"name":"GMAIL_SEND_EMAIL"}}
+ ]}
+ }))
+ }),
+ );
+ let base = start_mock_backend(app).await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_with_backend(&tmp, base);
+ let legacy: std::collections::BTreeMap<String, crate::integrations::composio::contract::UserScopePref> =
+ serde_json::from_value(json!({"gmail": {"read": true, "write": false, "admin": false}}))
+ .unwrap();
+ crate::integrations::composio::file_store::save(
+ &config,
+ crate::integrations::composio::file_store::USER_SCOPES_FILE,
+ &legacy,
+ )
+ .await
+ .unwrap();
+
+ let outcome = composio_list_tools(&config, Some(vec!["gmail".into()]), None)
+ .await
+ .unwrap();
+ assert_eq!(outcome.value.tools.len(), 2);
+}
+
+#[tokio::test]
+async fn composio_list_tools_lets_the_module_apply_the_scope_preference_and_catalog() {
+ // The host no longer opts out of the module's scope filter
+ // (`apply_user_scopes` defaults on): an admin-scoped action, and one the
+ // gmail catalog does not name, never reach the caller.
+ let _serialised = module_guard().await;
+ let app = Router::new().route(
+ "/agent-integrations/composio/tools",
+ get(|| async {
+ Json(json!({
+ "success": true,
+ "data": {"tools": [
+ {"type":"function","function":{"name":"GMAIL_FETCH_EMAILS"}},
+ {"type":"function","function":{"name":"GMAIL_DELETE_MESSAGE"}},
+ {"type":"function","function":{"name":"GMAIL_NOT_IN_THE_CATALOG"}}
+ ]}
+ }))
+ }),
+ );
+ let base = start_mock_backend(app).await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_with_backend(&tmp, base);
+ let outcome = composio_list_tools(&config, Some(vec!["gmail".into()]), None)
+ .await
+ .unwrap();
+ let names: Vec<&str> = outcome
+ .value
+ .tools
+ .iter()
+ .map(|tool| tool.function.name.as_str())
+ .collect();
+ assert_eq!(names, ["GMAIL_FETCH_EMAILS"]);
+}
+
+#[tokio::test]
+async fn composio_execute_via_mock_succeeds_and_logs_elapsed() {
+ let _serialised = module_guard().await;
+ let app = Router::new().route(
+ "/agent-integrations/composio/execute",
+ post(|Json(b): Json<Value>| async move {
+ Json(json!({
+ "success": true,
+ "data": {
+ "data": {"echo": b["tool"]},
+ "successful": true,
+ "error": null,
+ "costUsd": 0.001
+ }
+ }))
+ }),
+ );
+ let base = start_mock_backend(app).await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_with_backend(&tmp, base);
+ let outcome = composio_execute(&config, "GMAIL_SEND", Some(json!({"to": "a"})), None)
+ .await
+ .unwrap();
+ assert!(outcome.value.successful);
+ assert!(outcome
+ .logs
+ .iter()
+ .any(|l| l.contains("executed GMAIL_SEND")));
+}
+
+#[tokio::test]
+async fn composio_execute_via_mock_propagates_backend_error() {
+ let _serialised = module_guard().await;
+ let app = Router::new().route(
+ "/agent-integrations/composio/execute",
+ post(|| async { Json(json!({"success": false, "error": "rate limited"})) }),
+ );
+ let base = start_mock_backend(app).await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_with_backend(&tmp, base);
+ let err = composio_execute(&config, "ANY_TOOL", None, None)
+ .await
+ .unwrap_err();
+ // The dispatcher (`execute_composio_action`) classifies transport
+ // failures and prefixes them with `[composio:error:<class>] …`; ops.rs
+ // preserves that prefix so the frontend formatter can parse the class.
+ // For an unrecognised tool slug and a 502-shaped envelope the only
+ // signal we get is the backend error text, so assert on its contents.
+ assert!(err.starts_with("[composio:error:"), "got: {err}");
+ assert!(err.contains("rate limited"), "got: {err}");
+}
+
#[tokio::test]
async fn composio_list_tools_honours_a_preference_still_in_the_legacy_file() {
// A workspace upgraded from before the preference moved into the module