Skip to content

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

test(embedding, memory): add tests for API-key-only cloud embedding and memory host authentication

Steven Enamakelcf417f68834e
diff --git a/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs b/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs
index 14ba397f44..06d2136d6e 100644
--- a/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs
+++ b/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs
@@ -97,3 +97,74 @@ fn env_workspace_scope_is_the_config_dir_not_the_raw_workspace() {
"returning the raw workspace dir is the regression this guards against"
);
}
+
+/// A stored TinyHumans API key authenticates cloud embeddings on its own (no
+/// session), sent as `Authorization: Bearer <key>` like managed inference.
+#[tokio::test]
+async fn api_key_alone_authenticates_cloud_embeddings() {
+ use axum::{http::HeaderMap, routing::post, Json, Router};
+ use std::sync::{Arc, Mutex};
+
+ let seen: Arc<Mutex<Option<String>>> = Arc::default();
+ let app = Router::new().route(
+ "/openai/v1/embeddings",
+ post({
+ let seen = Arc::clone(&seen);
+ move |headers: HeaderMap| async move {
+ *seen.lock().unwrap() = headers
+ .get("authorization")
+ .and_then(|v| v.to_str().ok())
+ .map(str::to_owned);
+ Json(serde_json::json!({
+ "object": "list",
+ "data": [{ "object": "embedding", "index": 0, "embedding": [0.5, 0.25] }],
+ "model": "m"
+ }))
+ }
+ }),
+ );
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
+
+ let state = tempfile::tempdir().unwrap();
+ crate::security::credentials::api_key::store_api_key_in(state.path(), false, "tiny_test_emb")
+ .unwrap();
+ let provider = OpenHumanCloudEmbeddingModel::new(
+ Some(format!("http://{addr}")),
+ Some(state.path().to_path_buf()),
+ false,
+ "m",
+ 2,
+ );
+
+ let vectors = provider
+ .embed(&["hello".to_string()])
+ .await
+ .expect("an API key alone embeds");
+ assert_eq!(vectors.len(), 1);
+ assert_eq!(
+ seen.lock().unwrap().as_deref(),
+ Some("Bearer tiny_test_emb")
+ );
+}
+
+/// The key is never put on a plaintext non-loopback wire.
+#[tokio::test]
+async fn api_key_is_refused_over_plaintext_non_loopback() {
+ let state = tempfile::tempdir().unwrap();
+ crate::security::credentials::api_key::store_api_key_in(state.path(), false, "tiny_test_emb")
+ .unwrap();
+ let provider = OpenHumanCloudEmbeddingModel::new(
+ Some("http://embeddings.example.test".into()),
+ Some(state.path().to_path_buf()),
+ false,
+ "m",
+ 2,
+ );
+ let err = provider.embed(&["hello".to_string()]).await.unwrap_err();
+ assert!(
+ err.to_string().contains("refusing to send the TinyHumans API key"),
+ "{err}"
+ );
+}
diff --git a/crates/openhuman-core/src/modules/memory_host_tests.rs b/crates/openhuman-core/src/modules/memory_host_tests.rs
index c2298b5f99..77132980be 100644
--- a/crates/openhuman-core/src/modules/memory_host_tests.rs
+++ b/crates/openhuman-core/src/modules/memory_host_tests.rs
@@ -397,3 +397,35 @@ fn non_summarization_roles_keep_the_role_factory() {
super::resolve_chat_model("chat", &config)
.expect("a non-summarization role resolves through the factory (test override)");
}
+
+/// With only a TinyHumans API key stored (no session), the host lends the key
+/// as the proxied-Composio bearer and reports Composio available — the backend
+/// accepts a key as `Authorization: Bearer`.
+#[tokio::test]
+async fn session_bearer_and_availability_come_from_the_api_key() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let config = scoped_config(dir.path());
+ crate::security::credentials::api_key::store_api_key(config.as_ref(), "tiny_test_memory")
+ .expect("store api key");
+ let callbacks = ComposioCallbacks(Arc::clone(&config));
+
+ let bearer = call(&callbacks, "SessionBearer", json!([]))
+ .await
+ .expect("SessionBearer never fails");
+ assert_eq!(bearer, json!("tiny_test_memory"));
+
+ let available = call(&callbacks, "IsAvailable", json!([]))
+ .await
+ .expect("IsAvailable never fails");
+ assert_eq!(available, json!(true));
+}
+
+#[tokio::test]
+async fn session_bearer_is_absent_without_any_credential() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let callbacks = ComposioCallbacks(scoped_config(dir.path()));
+ let bearer = call(&callbacks, "SessionBearer", json!([]))
+ .await
+ .expect("SessionBearer never fails");
+ assert_eq!(bearer, json!(null));
+}