Skip to content

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

test(backend): add test module for headers and fix doc comment

Steven Enamakel1aba1e478c28
diff --git a/crates/openhuman-tinyhumans/src/backend/headers.rs b/crates/openhuman-tinyhumans/src/backend/headers.rs
index f48922974c..a4fd69df75 100644
--- a/crates/openhuman-tinyhumans/src/backend/headers.rs
+++ b/crates/openhuman-tinyhumans/src/backend/headers.rs
@@ -100,3 +100,7 @@ pub fn build_backend_client(profile: TransportProfile) -> Result<reqwest::Client
.build()
.map_err(|e| anyhow::anyhow!("failed to build HTTP client: {e}"))
}
+
+#[cfg(test)]
+#[path = "headers_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-tinyhumans/src/backend/headers_tests.rs b/crates/openhuman-tinyhumans/src/backend/headers_tests.rs
new file mode 100644
index 0000000000..92c20f4646
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/backend/headers_tests.rs
@@ -0,0 +1,91 @@
+use super::*;
+use crate::backend::product::{
+ product_identity_test_lock, reset_product_identity_for_test, set_product_identity,
+ ProductIdentity, DEFAULT_PRODUCT_IDENTITY, PRODUCT_IDENTITY_HEADER,
+};
+use wiremock::matchers::{header, method, path};
+use wiremock::{Mock, MockServer, ResponseTemplate};
+
+#[test]
+fn sanitize_client_version_strips_invalid_chars_and_clamps_length() {
+ let raw = format!(" 1.2.3 (desktop)+build!?{} ", "a".repeat(80));
+ let sanitized = sanitize_client_version(&raw).unwrap();
+ assert_eq!(sanitized, format!("1.2.3desktop+build{}", "a".repeat(46)));
+ assert_eq!(sanitized.len(), CLIENT_VERSION_HEADER_MAX_LEN);
+ assert_eq!(sanitize_client_version(" ?! "), None);
+}
+
+#[test]
+fn attribution_headers_carry_the_core_version_and_default_identity() {
+ let _guard = product_identity_test_lock();
+ reset_product_identity_for_test();
+ let headers = attribution_headers().unwrap();
+ assert_eq!(
+ headers.get("x-core-version").and_then(|v| v.to_str().ok()),
+ sanitize_client_version(env!("CARGO_PKG_VERSION")).as_deref()
+ );
+ assert_eq!(
+ headers.get(PRODUCT_IDENTITY_HEADER).and_then(|v| v.to_str().ok()),
+ Some(DEFAULT_PRODUCT_IDENTITY)
+ );
+}
+
+#[tokio::test]
+async fn the_profile_client_stamps_versions_and_identity_on_raw_requests() {
+ // `http_client` traffic (multipart STT upload, the Langfuse push) bypasses
+ // the SDK, so the attribution has to ride the client's default headers.
+ let _guard = product_identity_test_lock();
+ reset_product_identity_for_test();
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/probe"))
+ .and(header(PRODUCT_IDENTITY_HEADER, DEFAULT_PRODUCT_IDENTITY))
+ .and(header(
+ "x-core-version",
+ sanitize_client_version(env!("CARGO_PKG_VERSION"))
+ .unwrap()
+ .as_str(),
+ ))
+ .respond_with(ResponseTemplate::new(200))
+ .expect(1)
+ .mount(&server)
+ .await;
+
+ let client = build_backend_client(TransportProfile::Api).unwrap();
+ let response = client
+ .get(format!("{}/probe", server.uri()))
+ .send()
+ .await
+ .unwrap();
+ assert!(response.status().is_success());
+}
+
+#[tokio::test]
+async fn the_shell_version_rides_along_when_exported() {
+ static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+ let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ std::env::set_var(TAURI_VERSION_ENV_VAR, "9.8.7-shell+test");
+ let headers = attribution_headers();
+ std::env::remove_var(TAURI_VERSION_ENV_VAR);
+ let headers = headers.unwrap();
+ assert_eq!(
+ headers.get("x-tauri-version").and_then(|v| v.to_str().ok()),
+ Some("9.8.7-shell+test")
+ );
+ assert!(headers.get("x-core-version").is_some());
+}
+
+#[test]
+fn an_embedding_product_can_override_the_identity() {
+ let _guard = product_identity_test_lock();
+ set_product_identity(ProductIdentity::new("opencompany").unwrap());
+ let headers = attribution_headers();
+ reset_product_identity_for_test();
+ assert_eq!(
+ headers
+ .unwrap()
+ .get(PRODUCT_IDENTITY_HEADER)
+ .and_then(|v| v.to_str().ok()),
+ Some("opencompany")
+ );
+}
diff --git a/crates/openhuman-tinyhumans/src/backend/product.rs b/crates/openhuman-tinyhumans/src/backend/product.rs
index c37125cc67..094a60b439 100644
--- a/crates/openhuman-tinyhumans/src/backend/product.rs
+++ b/crates/openhuman-tinyhumans/src/backend/product.rs
@@ -152,7 +152,7 @@ pub fn product_identity_headers() -> HeaderMap {
/// process-wide product identity.
///
/// The identity is process state, so a module-local lock cannot prevent
-/// cross-module races: `api::product`, `api::rest` and
+/// cross-module races: `backend::product`, `backend::headers` and
/// `openhuman_core::integrations` tests all touch it from parallel test threads, and
/// a test asserting the `openhuman` default would flake against a test that has
/// installed an override. Every test that touches the identity must take THIS
diff --git a/crates/openhuman-tinyhumans/src/transport/transport_tests.rs b/crates/openhuman-tinyhumans/src/transport/transport_tests.rs
index 7a3fa61507..65fe352b78 100644
--- a/crates/openhuman-tinyhumans/src/transport/transport_tests.rs
+++ b/crates/openhuman-tinyhumans/src/transport/transport_tests.rs
@@ -284,3 +284,45 @@ async fn install_registers_hosted_controllers_that_dispatch_through_the_transpor
// A second install is a no-op for the registry, not a collision.
crate::install(crate::InstallOptions::default()).unwrap();
}
+
+/// The core asks the installed transport where the backend is: control-plane
+/// calls skip an `api_url` that is really an inference endpoint, managed
+/// inference honours it.
+#[test]
+fn base_url_applies_the_inference_endpoint_guard_per_purpose() {
+ let transport = SdkBackendTransport::new().unwrap();
+ let ollama = Some("http://127.0.0.1:11434/v1");
+ let control = transport.base_url(ollama, BaseUrlPurpose::ControlPlane);
+ assert_ne!(control, "http://127.0.0.1:11434/v1");
+ assert!(!control.contains("11434"), "{control}");
+ assert_eq!(
+ transport.base_url(ollama, BaseUrlPurpose::Inference),
+ "http://127.0.0.1:11434/v1"
+ );
+ assert_eq!(
+ transport.base_url(
+ Some("https://api.tinyhumans.ai/openai/v1/chat/completions"),
+ BaseUrlPurpose::ControlPlane
+ ),
+ "https://api.tinyhumans.ai"
+ );
+}
+
+#[test]
+fn product_identity_and_attribution_come_from_this_crate() {
+ let _guard = crate::backend::product::product_identity_test_lock();
+ crate::backend::product::reset_product_identity_for_test();
+ let transport = SdkBackendTransport::new().unwrap();
+ assert_eq!(
+ transport.product_identity(),
+ crate::backend::DEFAULT_PRODUCT_IDENTITY
+ );
+ let headers = transport.attribution_headers();
+ assert_eq!(
+ headers
+ .get(crate::backend::PRODUCT_IDENTITY_HEADER)
+ .and_then(|v| v.to_str().ok()),
+ Some(crate::backend::DEFAULT_PRODUCT_IDENTITY)
+ );
+ assert!(headers.get("x-core-version").is_some());
+}