Skip to content

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

test(hosted): add SDK-backed integration tests for billing and team operations

Steven Enamakel4462f29be7ef
diff --git a/crates/openhuman-tinyhumans/src/hosted/billing/ops_tests.rs b/crates/openhuman-tinyhumans/src/hosted/billing/ops_tests.rs
index 3d727ed45d..0c1ad62375 100644
--- a/crates/openhuman-tinyhumans/src/hosted/billing/ops_tests.rs
+++ b/crates/openhuman-tinyhumans/src/hosted/billing/ops_tests.rs
@@ -140,3 +140,136 @@ async fn top_up_rejects_invalid_gateway_after_amount_passes() {
.unwrap_err();
assert_eq!(err, "gateway must be one of: stripe, coinbase");
}
+
+// --- SDK-backed calls against a mock backend -----------------------------
+
+use crate::hosted::test_support;
+use tempfile::TempDir;
+use wiremock::matchers::{body_json, method, path, query_param};
+use wiremock::{Mock, MockServer, ResponseTemplate};
+
+#[test]
+fn parse_enum_names_field_and_value() {
+ assert_eq!(
+ parse_enum::<BillingPlan>("plan", "PRO_MONTHLY").unwrap(),
+ BillingPlan::ProMonthly
+ );
+ assert_eq!(
+ parse_enum::<BillingPlan>("plan", "GOLD").unwrap_err(),
+ "unsupported plan: GOLD"
+ );
+}
+
+#[tokio::test]
+async fn purchase_plan_rejects_unknown_plan_before_any_request() {
+ let err = purchase_plan(&cfg(), "GOLD").await.unwrap_err();
+ assert_eq!(err, "unsupported plan: GOLD");
+}
+
+#[tokio::test]
+async fn billing_calls_send_schema_current_requests() {
+ let server = MockServer::start().await;
+ let ok = || ResponseTemplate::new(200).set_body_json(json!({"success": true, "data": {"ok": true}}));
+ Mock::given(method("GET"))
+ .and(path("/payments/credits/transactions"))
+ .and(query_param("limit", "20"))
+ .and(query_param("offset", "0"))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/payments/credits/top-up"))
+ .and(body_json(json!({"amountUsd": 5.0, "gateway": "coinbase"})))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/payments/stripe/purchasePlan"))
+ .and(body_json(json!({"plan": "BASIC_YEARLY"})))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/payments/coinbase/charge"))
+ .and(body_json(json!({"plan": "PRO", "interval": "annual"})))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("PATCH"))
+ .and(path("/payments/credits/auto-recharge"))
+ .and(body_json(json!({"enabled": true, "weeklyLimitUsd": 50})))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("PATCH"))
+ .and(path("/payments/credits/auto-recharge/cards/pm_1"))
+ .and(body_json(json!({"isDefault": true})))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/coupons/redeem"))
+ .and(body_json(json!({"code": "FREE"})))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ for (verb, route) in [
+ ("GET", "/payments/stripe/currentPlan"),
+ ("GET", "/payments/summary"),
+ ("GET", "/payments/credits/balance"),
+ ("GET", "/payments/credits/auto-recharge"),
+ ("GET", "/payments/credits/auto-recharge/cards"),
+ ("POST", "/payments/credits/auto-recharge/cards/setup-intent"),
+ ("DELETE", "/payments/credits/auto-recharge/cards/pm_1"),
+ ("POST", "/payments/stripe/portal"),
+ ("GET", "/coupons/me"),
+ ] {
+ Mock::given(method(verb))
+ .and(path(route))
+ .respond_with(ok())
+ .expect(1)
+ .mount(&server)
+ .await;
+ }
+
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::signed_in(&tmp, &server.uri());
+ get_transactions(&config, None, None).await.unwrap();
+ top_up_credits(&config, 5.0, Some(" Coinbase ".into()))
+ .await
+ .unwrap();
+ purchase_plan(&config, "BASIC_YEARLY").await.unwrap();
+ create_coinbase_charge(&config, "PRO", None).await.unwrap();
+ update_auto_recharge(&config, json!({"enabled": true, "weeklyLimitUsd": 50}))
+ .await
+ .unwrap();
+ update_card(&config, "pm_1", json!({"isDefault": true}))
+ .await
+ .unwrap();
+ redeem_coupon(&config, " FREE ").await.unwrap();
+ let out = get_current_plan(&config).await.unwrap();
+ assert_eq!(out.value, json!({"ok": true}));
+ get_summary(&config).await.unwrap();
+ get_balance(&config).await.unwrap();
+ get_auto_recharge(&config).await.unwrap();
+ get_cards(&config).await.unwrap();
+ create_setup_intent(&config).await.unwrap();
+ delete_card(&config, "pm_1").await.unwrap();
+ create_portal_session(&config).await.unwrap();
+ get_user_coupons(&config).await.unwrap();
+}
+
+#[tokio::test]
+async fn update_card_rejects_non_object_payload() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::signed_in(&tmp, "http://127.0.0.1:9");
+ let err = update_card(&config, "pm_1", json!([1])).await.unwrap_err();
+ assert_eq!(err, "card update payload must be an object");
+}
diff --git a/crates/openhuman-tinyhumans/src/hosted/mod.rs b/crates/openhuman-tinyhumans/src/hosted/mod.rs
index 42439914b9..08bbff3875 100644
--- a/crates/openhuman-tinyhumans/src/hosted/mod.rs
+++ b/crates/openhuman-tinyhumans/src/hosted/mod.rs
@@ -20,6 +20,9 @@ pub mod billing;
pub mod referral;
pub mod team;
+#[cfg(test)]
+pub(crate) mod test_support;
+
use openhuman_core::core::all::{ControllerExtension, DomainGroup};
/// Namespace descriptions the core's `namespace_description` serves for the
diff --git a/crates/openhuman-tinyhumans/src/hosted/team/ops_tests.rs b/crates/openhuman-tinyhumans/src/hosted/team/ops_tests.rs
index 6460d25d03..62c215eeff 100644
--- a/crates/openhuman-tinyhumans/src/hosted/team/ops_tests.rs
+++ b/crates/openhuman-tinyhumans/src/hosted/team/ops_tests.rs
@@ -158,3 +158,99 @@ async fn revoke_invite_rejects_empty_invite_id() {
let err = revoke_invite(&cfg(), "t1", "").await.unwrap_err();
assert_eq!(err, "inviteId is required");
}
+
+// --- SDK-backed calls against a mock backend -----------------------------
+
+use crate::hosted::test_support;
+use openhuman_core::core::observability::expected_error_kind;
+use serde_json::json;
+use tempfile::TempDir;
+use wiremock::matchers::{body_json, method, path};
+use wiremock::{Mock, MockServer, ResponseTemplate};
+
+#[test]
+fn clamp_u32_rejects_out_of_range() {
+ assert_eq!(clamp_u32(None, "maxUses").unwrap(), None);
+ assert_eq!(clamp_u32(Some(5), "maxUses").unwrap(), Some(5));
+ assert_eq!(
+ clamp_u32(Some(u64::MAX), "maxUses").unwrap_err(),
+ "maxUses is out of range"
+ );
+}
+
+#[tokio::test]
+async fn get_usage_for_local_session_is_backend_unavailable_without_a_request() {
+ // Sentry 36649: the offline local user must get the demoted sentinel.
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::local_session(&tmp);
+ let err = get_usage(&config).await.unwrap_err();
+ assert!(err.starts_with("BACKEND_UNAVAILABLE:"), "{err}");
+ assert!(expected_error_kind(&err).is_some());
+}
+
+#[tokio::test]
+async fn get_usage_unwraps_the_usage_document() {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/teams/me/usage"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "success": true,
+ "data": {"remainingUsd": 3.0}
+ })))
+ .mount(&server)
+ .await;
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::signed_in(&tmp, &server.uri());
+ let out = get_usage(&config).await.unwrap();
+ assert_eq!(out.value, json!({"remainingUsd": 3.0}));
+}
+
+#[tokio::test]
+async fn team_calls_encode_ids_and_send_typed_bodies() {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/teams/t%2F1/members"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"success": true, "data": []})))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/teams/t1/invites"))
+ .and(body_json(json!({"maxUses": 3})))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"success": true, "data": {"code": "c"}})))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("PUT"))
+ .and(path("/teams/t1/members/u1/role"))
+ .and(body_json(json!({"role": "admin"})))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"success": true, "data": {}})))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/teams/join"))
+ .and(body_json(json!({"code": "JOIN"})))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"success": true, "data": {}})))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("GET"))
+ .and(path("/teams"))
+ .respond_with(ResponseTemplate::new(401).set_body_json(json!({"error": "Invalid token"})))
+ .expect(1)
+ .mount(&server)
+ .await;
+
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::signed_in(&tmp, &server.uri());
+ assert_eq!(list_members(&config, " t/1 ").await.unwrap().value, json!([]));
+ assert_eq!(
+ create_invite(&config, "t1", Some(3), None).await.unwrap().value,
+ json!({"code": "c"})
+ );
+ change_member_role(&config, "t1", "u1", "admin").await.unwrap();
+ join_team(&config, " JOIN ").await.unwrap();
+ let err = list_teams(&config).await.unwrap_err();
+ assert!(err.starts_with("SESSION_EXPIRED:"), "{err}");
+}
diff --git a/crates/openhuman-tinyhumans/src/hosted/test_support.rs b/crates/openhuman-tinyhumans/src/hosted/test_support.rs
new file mode 100644
index 0000000000..8842de72b1
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/hosted/test_support.rs
@@ -0,0 +1,44 @@
+//! Test fixtures shared by the hosted domains' `*_tests.rs`.
+
+use openhuman_core::config::Config;
+use openhuman_core::security::credentials::{AuthService, APP_SESSION_PROVIDER};
+use tempfile::TempDir;
+
+/// A config rooted in `tmp` whose backend is `api_url`.
+pub fn config(tmp: &TempDir, api_url: &str) -> Config {
+ Config {
+ workspace_dir: tmp.path().join("workspace"),
+ action_dir: tmp.path().join("workspace"),
+ config_path: tmp.path().join("config.toml"),
+ api_url: Some(api_url.to_string()),
+ ..Config::default()
+ }
+}
+
+/// Store `token` as the app-session credential for `config`.
+pub fn store_session(config: &Config, token: &str) {
+ AuthService::from_config(config)
+ .store_provider_token(
+ APP_SESSION_PROVIDER,
+ "default",
+ token,
+ std::collections::HashMap::new(),
+ true,
+ )
+ .expect("store session token");
+}
+
+/// A signed-in config against `api_url` (session token `jwt.test`).
+pub fn signed_in(tmp: &TempDir, api_url: &str) -> Config {
+ let config = config(tmp, api_url);
+ store_session(&config, "jwt.test");
+ config
+}
+
+/// A config signed in with the offline local credential ("Continue locally").
+pub fn local_session(tmp: &TempDir) -> Config {
+ // Unroutable: any request would fail loudly instead of returning the sentinel.
+ let config = config(tmp, "http://127.0.0.1:9");
+ store_session(&config, "desktop.test.local");
+ config
+}