Skip to content

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

docs(hosted): add README and test files for channel_link module

Steven Enamakel3da2ea8825d7
diff --git a/crates/openhuman-tinyhumans/src/hosted/channel_link/README.md b/crates/openhuman-tinyhumans/src/hosted/channel_link/README.md
new file mode 100644
index 0000000000..a8c5ad3299
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/hosted/channel_link/README.md
@@ -0,0 +1,20 @@
+# channel_link (hosted)
+
+Links the managed TinyHumans Telegram and Discord bots to the user's
+TinyHumans account, on the SDK's typed `auth()` client.
+
+| Method | Backend call |
+| --- | --- |
+| `auth_create_channel_link_token` (`channel`) | `POST /auth/channels/{channel}/link-token` |
+| `channels_telegram_login_start` | link token, then a `t.me/<bot>?start=<token>` deep link |
+| `channels_telegram_login_check` (`linkToken`) | `GET /auth/me`, looks for `telegramId`; stores `channel:telegram:managed_dm` |
+| `channels_discord_link_start` | link token, then `!start <token>` instructions |
+| `channels_discord_link_check` (`linkToken`) | `GET /auth/me`, looks for `discordId`; stores `channel:discord:managed_dm` |
+
+The `channels_*` schemas come from the `tinychannels-bus` contract, converted
+by the core (`channels::controllers::controller_schema`), and exist only with
+the `channels` feature. They share the `channels` / `auth` namespaces with the
+core, which keeps the rest of the channel controllers (connect, status, bot-token
+Discord discovery, messaging) and the credential controllers. The core's
+`OpenHumanChannelBackend` still implements the four contract methods, but only
+to answer `BACKEND_UNAVAILABLE:`, since nothing in the core dispatches them any more.
diff --git a/crates/openhuman-tinyhumans/src/hosted/channel_link/managed_tests.rs b/crates/openhuman-tinyhumans/src/hosted/channel_link/managed_tests.rs
new file mode 100644
index 0000000000..30bca5145c
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/hosted/channel_link/managed_tests.rs
@@ -0,0 +1,113 @@
+use super::*;
+use crate::hosted::test_support;
+use serde_json::json;
+use tempfile::TempDir;
+use wiremock::matchers::{method, path};
+use wiremock::{Mock, MockServer, ResponseTemplate};
+
+#[test]
+fn extract_link_token_accepts_both_shapes_and_rejects_blank() {
+ assert_eq!(
+ extract_link_token(&json!({"linkToken": " a "})).unwrap(),
+ "a"
+ );
+ assert_eq!(extract_link_token(&json!({"token": "b"})).unwrap(), "b");
+ assert!(extract_link_token(&json!({}))
+ .unwrap_err()
+ .contains("missing linkToken"));
+ assert_eq!(
+ extract_link_token(&json!({"linkToken": " "})).unwrap_err(),
+ "backend returned empty link token"
+ );
+}
+
+#[test]
+fn profile_id_reads_the_first_non_empty_key() {
+ let profile = json!({"telegramId": "", "telegram_id": "42"});
+ assert_eq!(
+ profile_id(&profile, &["telegramId", "telegram_id"]),
+ Some("42")
+ );
+ assert_eq!(profile_id(&json!({}), &["discordId"]), None);
+}
+
+#[tokio::test]
+async fn start_flows_mint_link_tokens() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/auth/channels/telegram/link-token"))
+ .respond_with(
+ ResponseTemplate::new(200)
+ .set_body_json(json!({"success": true, "data": {"linkToken": "tg-1"}})),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/auth/channels/discord/link-token"))
+ .respond_with(
+ ResponseTemplate::new(200)
+ .set_body_json(json!({"success": true, "data": {"token": "dc-1"}})),
+ )
+ .mount(&server)
+ .await;
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::signed_in(&tmp, &server.uri());
+
+ let tg = telegram_login_start(&config).await.unwrap().value;
+ assert_eq!(tg.link_token, "tg-1");
+ assert!(tg.telegram_url.ends_with("?start=tg-1"));
+ assert!(tg.telegram_url.contains(&tg.bot_username));
+
+ let dc = discord_link_start(&config).await.unwrap().value;
+ assert_eq!(dc.link_token, "dc-1");
+ assert!(dc.instructions.contains("!start dc-1"));
+}
+
+#[tokio::test]
+async fn check_flows_read_auth_me_and_store_the_marker_when_linked() {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/auth/me"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "success": true,
+ "user": {"id": "u1", "telegramId": "tg-42"}
+ })))
+ .mount(&server)
+ .await;
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::signed_in(&tmp, &server.uri());
+
+ let tg = telegram_login_check(&config, "lt").await.unwrap().value;
+ assert!(tg.linked);
+ assert_eq!(tg.details.unwrap()["telegramId"], json!("tg-42"));
+ let stored = openhuman_core::security::credentials::ops::list_provider_credentials(
+ &config,
+ Some("channel:telegram:managed_dm".to_string()),
+ )
+ .await
+ .unwrap();
+ assert!(
+ serde_json::to_string(&stored.value)
+ .unwrap()
+ .contains("channel:telegram:managed_dm"),
+ "managed marker must be stored"
+ );
+
+ let dc = discord_link_check(&config, "lt").await.unwrap().value;
+ assert!(!dc.linked, "no discordId on the profile yet");
+ assert!(dc.details.is_none());
+}
+
+#[tokio::test]
+async fn local_session_is_backend_unavailable_for_every_flow() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::local_session(&tmp);
+ for err in [
+ telegram_login_start(&config).await.err().unwrap(),
+ telegram_login_check(&config, "x").await.err().unwrap(),
+ discord_link_start(&config).await.err().unwrap(),
+ discord_link_check(&config, "x").await.err().unwrap(),
+ ] {
+ assert!(err.starts_with("BACKEND_UNAVAILABLE:"), "{err}");
+ }
+}
diff --git a/crates/openhuman-tinyhumans/src/hosted/channel_link/ops_tests.rs b/crates/openhuman-tinyhumans/src/hosted/channel_link/ops_tests.rs
new file mode 100644
index 0000000000..b0911b940e
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/hosted/channel_link/ops_tests.rs
@@ -0,0 +1,60 @@
+use super::*;
+use crate::hosted::test_support;
+use openhuman_core::core::observability::is_session_expired_message;
+use serde_json::json;
+use tempfile::TempDir;
+use wiremock::matchers::{method, path};
+use wiremock::{Mock, MockServer, ResponseTemplate};
+
+#[test]
+fn normalize_channel_validates_and_lowercases() {
+ assert_eq!(normalize_channel(" Telegram ").unwrap(), "telegram");
+ assert_eq!(normalize_channel("discord").unwrap(), "discord");
+ assert_eq!(normalize_channel(" ").unwrap_err(), "channel is required");
+ assert_eq!(
+ normalize_channel("Slack").unwrap_err(),
+ "unsupported channel: slack"
+ );
+}
+
+#[tokio::test]
+async fn link_token_validation_runs_before_the_credential() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_support::config(&tmp, "http://127.0.0.1:9");
+ assert_eq!(
+ auth_create_channel_link_token(&config, "matrix")
+ .await
+ .unwrap_err(),
+ "unsupported channel: matrix"
+ );
+}
+
+#[tokio::test]
+async fn link_token_round_trip_and_401_classification() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/auth/channels/telegram/link-token"))
+ .respond_with(
+ ResponseTemplate::new(200)
+ .set_body_json(json!({"success": true, "data": {"linkToken": "lt"}})),
+ )
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/auth/channels/discord/link-token"))
+ .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());
+ let out = auth_create_channel_link_token(&config, "TELEGRAM")
+ .await
+ .unwrap();
+ assert_eq!(out.value, json!({"linkToken": "lt"}));
+ let err = auth_create_channel_link_token(&config, "discord")
+ .await
+ .unwrap_err();
+ assert!(is_session_expired_message(&err), "{err}");
+}
diff --git a/crates/openhuman-tinyhumans/src/hosted/channel_link/schemas_tests.rs b/crates/openhuman-tinyhumans/src/hosted/channel_link/schemas_tests.rs
new file mode 100644
index 0000000000..127527754e
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/hosted/channel_link/schemas_tests.rs
@@ -0,0 +1,28 @@
+use super::*;
+
+#[test]
+fn catalog_keeps_the_wire_names() {
+ let names: Vec<String> = all_channel_link_controller_schemas()
+ .iter()
+ .map(|s| format!("{}.{}", s.namespace, s.function))
+ .collect();
+ assert_eq!(names[0], "auth.create_channel_link_token");
+ #[cfg(feature = "channels")]
+ assert_eq!(
+ names[1..],
+ [
+ "channels.telegram_login_start",
+ "channels.telegram_login_check",
+ "channels.discord_link_start",
+ "channels.discord_link_check",
+ ]
+ );
+ assert_eq!(channel_link_schemas("nope").function, "unknown");
+}
+
+#[test]
+fn link_token_requires_channel() {
+ let s = channel_link_schemas("auth_create_channel_link_token");
+ assert!(s.inputs.iter().any(|f| f.name == "channel" && f.required));
+ assert!(deserialize_params::<AuthCreateChannelLinkTokenParams>(Map::new()).is_err());
+}