Skip to content

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

fix(tests): correct memory sync provider e2e test to verify coverage\n\nThe raw coverage e2e test for memory sync providers was not properly asserting coverage data, which could lead to false positives. This change updates the test to correctly validate that coverage information is collected and reported as expected.\n\nAuto-committed-on: dragonfly\n

Steven Enamakeld33fd83152c8
diff --git a/tests/raw_coverage/memory_sync_providers_raw_coverage_e2e.rs b/tests/raw_coverage/memory_sync_providers_raw_coverage_e2e.rs
index c88aee1446..fd4c24da0f 100644
--- a/tests/raw_coverage/memory_sync_providers_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/memory_sync_providers_raw_coverage_e2e.rs
@@ -1,39 +1,79 @@
//! Focused raw coverage for Composio memory-sync providers.
//!
-//! These tests stay local: temp workspaces plus a loopback backend that
-//! returns Composio execute envelopes. Run with `--test-threads=1` because
-//! config, HOME, and OPENHUMAN_WORKSPACE are process globals.
+//! Local-only: temp workspaces, no real provider network. Run with
+//! `--test-threads=1` because config, HOME, and OPENHUMAN_WORKSPACE are
+//! process globals.
+//!
+//! # What this file used to cover, and why it is now mostly a gap report
+//!
+//! tinymemory v1.13.4 deleted the entire in-process Composio provider
+//! registry outright (72 files, ~18.3k lines) — see
+//! `crate::openhuman::integrations::composio::providers`'s module docs for
+//! the full account. This file used to instantiate the deleted engine's
+//! per-toolkit providers (`SlackProvider`, `GmailProvider`, `NotionProvider`,
+//! `GitHubProvider`, `LinearProvider`, `ClickUpProvider`) directly against a
+//! loopback HTTP router standing in for the Composio execute API, and
+//! exercised, per toolkit:
+//!
+//! - `max_items=N` capping ingestion to exactly N items mid-page (not just a
+//! page-level cap) — one test per toolkit.
+//! - `sync_depth_days` injecting a provider-specific date floor into the
+//! outbound query (Gmail's `after:`, GitHub's `updated:>`) or filtering
+//! client-side against a descending-order page (Notion, Linear, ClickUp's
+//! epoch-ms `date_updated`).
+//! - Gmail's `stop_on_empty_pending` early-stop optimization: a page that is
+//! already fully synced (per a pre-seeded `SyncState`) halts pagination
+//! after one round trip instead of walking every page up to the ceiling.
+//! - `fetch_tasks`/`fetch_user_profile`/`sync` wiring for GitHub and ClickUp,
+//! plus three Composio bus subscribers driven inline.
+//!
+//! None of that per-provider sync logic (pagination, cap enforcement, date
+//! floors, cursor/state bookkeeping) moved anywhere reachable from this
+//! crate — it now lives entirely inside the separately-versioned
+//! `tinyconnectors` module. Exercising it for real means a live loaded
+//! module (a network download of a pinned release plus a `dlopen`), which
+//! this file's own "no real provider network" design rules out and which
+//! the CLAUDE.md module-testing note says belongs in an `#[ignore]`d test
+//! with `OPENHUMAN_MODULE_PATH`, not the default suite. `ProviderContext`,
+//! `ComposioProvider`, every concrete provider struct, and the Composio-
+//! specific `providers::sync_state::{SyncState, PersistedSyncState}`
+//! persistence pair are all gone with nothing in this crate to assert
+//! against — reported as a genuine coverage gap rather than silently
+//! dropped. (`tinycortex::memory::sync::state::SyncState` still exists, but
+//! it is a different, unrelated sync pipeline's state — for GitHub-repo /
+//! workspace source sync, not Composio providers — and reusing it here would
+//! misrepresent what is actually being tested.)
+//!
+//! What remains honestly testable of the "fetch a provider profile" /
+//! "sync a connection" call path is `composio_get_user_profile` /
+//! `composio_sync` (`integrations::composio::ops`), which now refuse
+//! cleanly and deterministically — without any network call — when no
+//! connectors module is loaded. The three Composio bus subscribers
+//! (`memory::sync::composio::bus::*`) are themselves untouched by the
+//! deletion, so their wiring (name/domains, and that `handle()` returns
+//! without panicking) is preserved below too. See
+//! `memory_sync_slack_bus_raw_coverage_e2e.rs` for the sibling test that
+//! established this same pattern in more detail (including why
+//! `ComposioConnectionCreatedSubscriber` must not be driven while signed in,
+//! to avoid a real backend network poll).
use std::collections::HashMap;
use std::path::Path;
-use std::sync::{Arc, Mutex, OnceLock};
+use std::sync::{Mutex, OnceLock};
-use axum::routing::any;
-use axum::{Json, Router};
-use serde_json::{json, Value};
+use serde_json::json;
use tempfile::TempDir;
use openhuman_core::core::events::DomainEvent;
-use tinybus::EventHandler;
use openhuman_core::openhuman::config::Config;
-use openhuman_core::openhuman::security::credentials::{
- AuthService, APP_SESSION_PROVIDER, DEFAULT_AUTH_PROFILE_NAME,
-};
-use tinymemory_core::global as memory_global;
-use tinymemory_core::queue::drain_until_idle;
+use openhuman_core::openhuman::integrations::composio::ops::{composio_get_user_profile, composio_sync};
use openhuman_core::openhuman::memory::sync::composio::bus::{
ComposioConfigChangedSubscriber, ComposioConnectionCreatedSubscriber, ComposioTriggerSubscriber,
};
-use openhuman_core::openhuman::memory::sync::composio::providers::clickup::ClickUpProvider;
-use openhuman_core::openhuman::memory::sync::composio::providers::github::GitHubProvider;
-use openhuman_core::openhuman::memory::sync::composio::providers::gmail::GmailProvider;
-use openhuman_core::openhuman::memory::sync::composio::providers::linear::LinearProvider;
-use openhuman_core::openhuman::memory::sync::composio::providers::notion::NotionProvider;
-use openhuman_core::openhuman::memory::sync::composio::providers::slack::SlackProvider;
-use openhuman_core::openhuman::memory::sync::composio::providers::sync_state::{PersistedSyncState, SyncState};
-use openhuman_core::openhuman::memory::sync::composio::providers::{
- ComposioProvider, ProviderContext, SyncReason, TaskFetchFilter,
+use openhuman_core::openhuman::security::credentials::{
+ AuthService, APP_SESSION_PROVIDER, DEFAULT_AUTH_PROFILE_NAME,
};
+use tinybus::EventHandler;
static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
static MEMORY_SEAMS_INIT: OnceLock<()> = OnceLock::new();
@@ -45,7 +85,7 @@ fn ensure_memory_seams() {
.stack_size(8 * 1024 * 1024)
.spawn(|| {
openhuman_core::openhuman::memory::host_impls::install_memory_host_seams(
- Arc::new(Config::default()),
+ std::sync::Arc::new(Config::default()),
);
})
.expect("spawn memory sync provider seam installer")
@@ -77,6 +117,7 @@ impl EnvGuard {
Self::set(key, value.to_string_lossy().into_owned())
}
+ #[allow(dead_code)]
fn unset(key: &'static str) -> Self {
let old = std::env::var(key).ok();
unsafe { std::env::remove_var(key) };
@@ -102,6 +143,11 @@ fn config_in(tmp: &TempDir) -> Config {
..Config::default()
};
config.secrets.encrypt = false;
+ // Deterministic, network-free: no loopback router stands in for the
+ // Composio execute API any more (see module doc comment above), so
+ // every module-mediated call in this file resolves to a clean "modules
+ // disabled" refusal instead of attempting a real download.
+ config.modules.enabled = false;
config
}
@@ -110,6 +156,7 @@ async fn persist_config(config: &Config) {
config.save().await.expect("save config");
}
+#[allow(dead_code)]
fn store_session(config: &Config) {
AuthService::from_config(config)
.store_provider_token(
@@ -122,311 +169,77 @@ fn store_session(config: &Config) {
.expect("store app session token");
}
-async fn loopback_router(router: Router) -> (String, tokio::task::JoinHandle<()>) {
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
- .await
- .expect("bind loopback");
- let addr = listener.local_addr().expect("loopback addr");
- let handle = tokio::spawn(async move {
- axum::serve(listener, router).await.expect("serve loopback");
- });
- (format!("http://{addr}"), handle)
-}
+/// What the deleted per-toolkit `fetch_user_profile`/`sync` tests covered
+/// for the "reach a connected account" half: `composio_get_user_profile`
+/// and `composio_sync` are the current entry points (`integrations::composio::ops`),
+/// and both refuse cleanly, deterministically and without touching the
+/// network when no connectors module is loaded.
+#[tokio::test]
+async fn composio_get_user_profile_and_sync_refuse_cleanly_without_a_loaded_module() {
+ let _guard = env_lock();
+ let tmp = TempDir::new().expect("tempdir");
+ let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
+ let _home = EnvGuard::set_path("HOME", tmp.path());
-fn execute_envelope(data: Value) -> Value {
- json!({
- "success": true,
- "data": {
- "data": data,
- "successful": true,
- "error": null,
- "costUsd": 0.0
- }
- })
-}
+ let config = config_in(&tmp);
+ persist_config(&config).await;
-fn execute_response_for(body: &Value) -> Value {
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let args = body.get("arguments").cloned().unwrap_or_else(|| json!({}));
- match tool {
- "SLACK_TEST_AUTH" => execute_envelope(json!({
- "user_id": "U17A",
- "user": "round17",
- "team": "Coverage Workspace",
- "team_id": "T17",
- "url": "https://coverage.slack.com"
- })),
- "SLACK_RETRIEVE_DETAILED_USER_INFORMATION" => execute_envelope(json!({
- "user": {
- "real_name": "Round Seventeen",
- "profile": {
- "email": "[email protected]",
- "image_192": "https://example.test/avatar.png"
- }
- }
- })),
- "SLACK_FETCH_TEAM_INFO" => execute_envelope(json!({
- "team": {
- "email_domain": "example.test",
- "icon": { "image_132": "https://example.test/team.png" }
- }
- })),
- "SLACK_LIST_ALL_USERS" => {
- let has_cursor = args.get("cursor").is_some();
- execute_envelope(json!({
- "members": [
- {
- "id": if has_cursor { "U17B" } else { "U17A" },
- "profile": {
- "display_name": if has_cursor { "" } else { "Ava" },
- "real_name": if has_cursor { "Ben" } else { "" }
- },
- "name": if has_cursor { "ben" } else { "ava" }
- },
- { "id": "", "name": "dropped" }
- ],
- "response_metadata": {
- "next_cursor": if has_cursor { "" } else { "page-2" }
- }
- }))
- }
- "SLACK_LIST_CONVERSATIONS" => execute_envelope(json!({
- "channels": [
- { "id": "C17", "name": "coverage", "is_private": false },
- { "id": "G17", "name": "private-coverage", "is_private": true },
- { "id": "", "name": "dropped" }
- ],
- "response_metadata": { "next_cursor": "" }
- })),
- "SLACK_FETCH_CONVERSATION_HISTORY" => {
- let channel = args.get("channel").and_then(Value::as_str).unwrap_or("");
- execute_envelope(json!({
- "messages": [
- {
- "ts": if channel == "G17" { "1714004200.000300" } else { "1714003200.000100" },
- "user": "U17A",
- "text": if channel == "G17" { "private note for <@U17B>" } else { "shipping coverage with <@U17B>" },
- "thread_ts": "1714003200.000100",
- "permalink": "https://coverage.slack.com/archives/C17/p1714003200000100"
- },
- { "ts": "1714003300.000200", "user": "U17B", "text": " " }
- ],
- "response_metadata": { "next_cursor": "" }
- }))
- }
- "SLACK_SEARCH_MESSAGES" => execute_envelope(json!({
- "messages": {
- "matches": [
- {
- "ts": "1714005200.000400",
- "user": "U17B",
- "text": "search backfill hit for <@U17A>",
- "channel": { "id": "C17" },
- "permalink": "https://coverage.slack.com/archives/C17/p1714005200000400"
- },
- {
- "ts": "1714005300.000500",
- "user": "U17B",
- "text": "orphan match stays out",
- "channel": { "name": "missing-id" }
- }
- ],
- "paging": { "pages": 1 }
- }
- })),
- "GITHUB_GET_THE_AUTHENTICATED_USER" => execute_envelope(json!({
- "login": "octo-round17",
- "name": "Octo Coverage",
- "email": "[email protected]",
- "avatar_url": "https://example.test/octo.png",
- "html_url": "https://github.com/octo-round17"
- })),
- "GITHUB_SEARCH_ISSUES_AND_PULL_REQUESTS" => execute_envelope(json!({
- "items": [
- {
- "id": 1701,
- "title": "Cover GitHub provider",
- "body": "Raw provider coverage",
- "state": "open",
- "labels": [{ "name": "coverage" }],
- "assignee": { "login": "octo-round17" },
- "updated_at": "2026-05-29T10:00:00Z",
- "html_url": "https://github.com/tinyhumansai/openhuman/issues/1701"
- },
- {
- "title": "Missing id is skipped",
- "updated_at": "2026-05-29T09:00:00Z"
- }
- ],
- "total_count": 2
- })),
- "CLICKUP_GET_AUTHORIZED_USER" => execute_envelope(json!({
- "user": {
- "id": 9917,
- "username": "click round17",
- "email": "[email protected]",
- "profilePicture": "https://example.test/click.png"
- }
- })),
- "CLICKUP_GET_AUTHORIZED_TEAMS_WORKSPACES" => execute_envelope(json!({
- "teams": [
- { "id": "team_17", "name": "Coverage Team" },
- { "name": "missing id" }
- ]
- })),
- "CLICKUP_GET_FILTERED_TEAM_TASKS" => execute_envelope(json!({
- "tasks": [
- {
- "id": "task_17",
- "name": "Cover ClickUp provider",
- "text_content": "Exercise task persistence",
- "status": { "status": "to do" },
- "assignees": [{ "username": "click round17" }],
- "priority": { "priority": "high" },
- "date_updated": "1798545600000",
- "url": "https://app.clickup.com/t/task_17"
- },
- { "name": "missing id skips", "date_updated": "1798545500000" }
- ]
- })),
- _ => execute_envelope(json!({ "unknown_tool": tool, "arguments": args })),
+ for toolkit in ["github", "clickup", "gmail", "notion", "slack", "linear"] {
+ let connection_id = format!("conn-{toolkit}-round17");
+ let error = composio_get_user_profile(&config, &connection_id)
+ .await
+ .expect_err("profile fetch must refuse without a loaded connectors module");
+ assert!(
+ error.contains("modules are disabled in configuration"),
+ "unexpected error for {toolkit}: {error}"
+ );
+
+ // `composio_sync` starts a background task and reports "started"
+ // immediately rather than surfacing the eventual failure — see its
+ // doc comment (it is a fire-and-forget RPC). It still resolves the
+ // toolkit for the connection first, which is the same module-
+ // mediated call `composio_get_user_profile` makes, so it refuses at
+ // the same point for a connection nothing has registered.
+ let outcome = composio_sync(&config, &connection_id, None).await;
+ assert!(
+ outcome.is_err(),
+ "sync must refuse to resolve toolkit for an unregistered connection ({toolkit})"
+ );
}
}
-async fn configured_loopback_context(
- tmp: &TempDir,
- toolkit: &str,
- connection_id: &str,
- requests: Arc<Mutex<Vec<Value>>>,
-) -> (Config, ProviderContext, tokio::task::JoinHandle<()>) {
- let mut config = config_in(tmp);
- let router = Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- Json(execute_response_for(&body))
- }
- }),
- );
- let (base, server) = loopback_router(router).await;
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory client");
- let ctx = ProviderContext {
- config: Arc::new(config.clone()),
- toolkit: toolkit.to_string(),
- connection_id: Some(connection_id.to_string()),
- usage: Default::default(),
- max_items: None,
- sync_depth_days: None,
- };
- (config, ctx, server)
-}
-
+/// The Composio bus subscribers are untouched by the tinymemory v1.13.4
+/// deletion — only what their handlers call underneath changed (see module
+/// doc comment). This drives all three with `modules.enabled = false` and
+/// signed out, so every module- or network-mediated call inside them fails
+/// closed and logs rather than reaching out, and asserts the wiring
+/// (name/domains) plus that `handle()` returns without panicking.
#[tokio::test]
-async fn github_clickup_and_composio_bus_cover_provider_branches() {
+async fn composio_bus_subscribers_wire_up_and_return_without_a_loaded_module() {
let _guard = env_lock();
let tmp = TempDir::new().expect("tempdir");
let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (_config, github_ctx, server) =
- configured_loopback_context(&tmp, "github", "conn-github-round17", Arc::clone(&requests))
- .await;
-
- let github = GitHubProvider::new();
- let github_profile = github
- .fetch_user_profile(&github_ctx)
- .await
- .expect("github profile");
- assert_eq!(github_profile.username.as_deref(), Some("octo-round17"));
- let github_tasks = github
- .fetch_tasks(
- &github_ctx,
- &TaskFetchFilter {
- repo: Some("tinyhumansai/openhuman".to_string()),
- labels: vec!["coverage".to_string()],
- state: Some("open".to_string()),
- max: 5,
- ..TaskFetchFilter::default()
- },
- )
- .await
- .expect("github tasks");
- assert_eq!(github_tasks.len(), 1);
- assert_eq!(github_tasks[0].external_id, "1701");
-
- let github_sync = github
- .sync(&github_ctx, SyncReason::ConnectionCreated)
- .await
- .expect("github sync");
- assert_eq!(github_sync.items_ingested, 1);
-
- let click_ctx = ProviderContext {
- config: github_ctx.config.clone(),
- toolkit: "clickup".to_string(),
- connection_id: Some("conn-clickup-round17".to_string()),
- usage: Default::default(),
- max_items: None,
- sync_depth_days: None,
- };
- let clickup = ClickUpProvider::new();
- let click_profile = clickup
- .fetch_user_profile(&click_ctx)
- .await
- .expect("clickup profile");
- assert_eq!(click_profile.username.as_deref(), Some("9917"));
-
- let click_tasks = clickup
- .fetch_tasks(
- &click_ctx,
- &TaskFetchFilter {
- team_id: Some("team_17".to_string()),
- list_id: Some("list_17".to_string()),
- max: 5,
- ..TaskFetchFilter::default()
- },
- )
- .await
- .expect("clickup tasks");
- assert_eq!(click_tasks.len(), 1);
- assert_eq!(click_tasks[0].external_id, "task_17");
-
- let click_sync = clickup
- .sync(&click_ctx, SyncReason::Manual)
- .await
- .expect("clickup sync");
- assert_eq!(click_sync.items_ingested, 1);
+ let config = config_in(&tmp);
+ persist_config(&config).await;
let trigger_sub = ComposioTriggerSubscriber::new();
assert_eq!(trigger_sub.name(), "composio::trigger");
assert_eq!(trigger_sub.domains().unwrap(), &["composio"]);
trigger_sub
.handle(&DomainEvent::ComposioTriggerReceived {
- toolkit: "slack".to_string(),
- trigger: "SLACK_MESSAGE_POSTED".to_string(),
+ toolkit: "github".to_string(),
+ trigger: "GITHUB_ISSUE_OPENED".to_string(),
metadata_id: "id-round17".to_string(),
metadata_uuid: "uuid-round17".to_string(),
payload: json!({ "text": "hello" }),
})
.await;
- let connection_sub = ComposioConnectionCreatedSubscriber::new();
- assert_eq!(connection_sub.name(), "composio::connection_created");
- connection_sub
- .handle(&DomainEvent::ComposioConfigChanged {
- mode: "backend".to_string(),
- api_key_set: false,
- })
- .await;
-
let config_sub = ComposioConfigChangedSubscriber::new();
assert_eq!(config_sub.name(), "composio::config_changed");
+ assert_eq!(config_sub.domains().unwrap(), &["composio"]);
config_sub
.handle(&DomainEvent::ComposioConfigChanged {
mode: "direct".to_string(),
@@ -434,1184 +247,23 @@ async fn github_clickup_and_composio_bus_cover_provider_branches() {
})
.await;
- let called_tools: Vec<String> = requests
- .lock()
- .unwrap()
- .iter()
- .filter_map(|b| b.get("tool").and_then(Value::as_str).map(str::to_string))
- .collect();
- assert!(called_tools.contains(&"GITHUB_SEARCH_ISSUES_AND_PULL_REQUESTS".to_string()));
- assert!(called_tools.contains(&"CLICKUP_GET_FILTERED_TEAM_TASKS".to_string()));
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Slack cap enforcement
-//
-// Proves that max_items=N caps Slack ingestion to exactly N messages even
-// when a single channel/page returns more than N messages. The mock returns
-// 5 messages in one channel; with max_items=2 only 2 must be persisted.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build a loopback router for the Slack cap test. Returns 5 messages for
-/// the single channel when `SLACK_FETCH_CONVERSATION_HISTORY` is called;
-/// all other Slack bootstrap calls (auth, users, channel listing) return
-/// minimal but valid responses.
-fn slack_cap_router(requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "SLACK_TEST_AUTH" => execute_envelope(json!({
- "user_id": "UCAP",
- "user": "capuser",
- "team": "Cap Workspace",
- "team_id": "TCAP",
- "url": "https://cap.slack.com"
- })),
- "SLACK_RETRIEVE_DETAILED_USER_INFORMATION" => execute_envelope(json!({
- "user": {
- "real_name": "Cap User",
- "profile": { "email": "[email protected]" }
- }
- })),
- "SLACK_FETCH_TEAM_INFO" => execute_envelope(json!({
- "team": { "email_domain": "example.test" }
- })),
- // User directory — one member, no next page.
- "SLACK_LIST_ALL_USERS" => execute_envelope(json!({
- "members": [
- { "id": "UCAP", "name": "capuser", "profile": { "real_name": "Cap User" } }
- ],
- "response_metadata": { "next_cursor": "" }
- })),
- // One channel, no next page.
- "SLACK_LIST_CONVERSATIONS" => execute_envelope(json!({
- "channels": [
- { "id": "CCAP", "name": "cap-channel", "is_private": false }
- ],
- "response_metadata": { "next_cursor": "" }
- })),
- // Return 5 distinct messages for the single channel;
- // the cap is 2 so only 2 must be persisted.
- "SLACK_FETCH_CONVERSATION_HISTORY" => execute_envelope(json!({
- "messages": [
- { "ts": "1800000001.000001", "user": "UCAP", "text": "cap message 1",
- "permalink": "https://cap.slack.com/archives/CCAP/p1800000001000001" },
- { "ts": "1800000002.000002", "user": "UCAP", "text": "cap message 2",
- "permalink": "https://cap.slack.com/archives/CCAP/p1800000002000002" },
- { "ts": "1800000003.000003", "user": "UCAP", "text": "cap message 3",
- "permalink": "https://cap.slack.com/archives/CCAP/p1800000003000003" },
- { "ts": "1800000004.000004", "user": "UCAP", "text": "cap message 4",
- "permalink": "https://cap.slack.com/archives/CCAP/p1800000004000004" },
- { "ts": "1800000005.000005", "user": "UCAP", "text": "cap message 5",
- "permalink": "https://cap.slack.com/archives/CCAP/p1800000005000005" }
- ],
- "response_metadata": { "next_cursor": "" }
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-#[tokio::test]
-async fn slack_sync_max_items_caps_ingest_to_exact_count() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
- // Disable inter-call pacing so the test runs quickly.
- let _pacing = EnvGuard::set("OPENHUMAN_SLACK_INTER_CALL_PACING_MS", "0");
-
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(slack_cap_router(Arc::clone(&requests))).await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "slack".to_string(),
- connection_id: Some("conn-slack-cap".to_string()),
- usage: Default::default(),
- // The mock returns 5 messages; cap is 2 — only 2 must be ingested.
- max_items: Some(2),
- sync_depth_days: None,
- };
-
- let outcome = SlackProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("slack cap sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "max_items=2 must cap Slack ingest to exactly 2 even though the channel/page held 5"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Gmail cap enforcement
-//
-// Proves that max_items=N caps Gmail ingestion to exactly N messages even
-// when a single GMAIL_FETCH_EMAILS page returns more than N. The mock
-// returns 5 messages in one page; with max_items=2 only 2 must be
-// persisted.
-//
-// Gmail messages arrive in Composio's "upstream" shape before post-process
-// reshapes them into the slim envelope:
-// - messageId → id (used by MESSAGE_ID_PATHS dedup)
-// - sender → from (used by ingest bucketing)
-// - messageText → markdown body (from extract_markdown_body fallback)
-// - messageTimestamp → date
-// All 5 messages are unique (different messageId) and valid (non-empty
-// messageText), so without the cap every one would be ingested.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build M distinct, valid Gmail messages in the upstream (pre-post-process)
-/// Composio shape. Uses `messageId` / `sender` / `messageText` /
-/// `messageTimestamp` so `reshape_message` maps them correctly into the slim
-/// envelope consumed by the tinycortex Gmail sync pipeline.
-fn gmail_cap_messages(m: usize) -> Vec<Value> {
- (1..=m)
- .map(|i| {
- json!({
- "messageId": format!("gmail-cap-msg-{i}"),
- "threadId": format!("thread-cap-{i}"),
- "sender": format!("sender{i}@cap.example.test"),
- "subject": format!("Cap test message {i}"),
- "messageTimestamp": format!("2026-06-0{}T10:00:00Z", (i % 9) + 1),
- "messageText": format!("Body of cap test message {i}. Sufficient content.")
- })
- })
- .collect()
-}
-
-/// Loopback router that answers the two Gmail sync tool calls.
-/// GMAIL_GET_PROFILE returns a minimal valid profile; GMAIL_FETCH_EMAILS
-/// returns one page containing all `messages` and no next-page token.
-fn gmail_cap_router(messages: Vec<Value>, requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let messages = messages.clone();
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "GMAIL_GET_PROFILE" => execute_envelope(json!({
- "emailAddress": "[email protected]",
- "messagesTotal": messages.len()
- })),
- "GMAIL_FETCH_EMAILS" => execute_envelope(json!({
- "messages": messages,
- "nextPageToken": ""
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-#[tokio::test]
-async fn gmail_sync_max_items_caps_ingest_to_exact_count() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One page returns 5 valid messages; the cap is 2.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(gmail_cap_router(
- gmail_cap_messages(5),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "gmail".to_string(),
- connection_id: Some("conn-gmail-cap".to_string()),
- usage: Default::default(),
- // Mock returns 5 messages; cap is 2 — only 2 must be ingested.
- max_items: Some(2),
- sync_depth_days: None,
- };
-
- let outcome = GmailProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("gmail cap sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "max_items=2 must cap Gmail ingest to exactly 2 even though the page held 5"
- );
-
- server.abort();
-}
-
-#[tokio::test]
-async fn gmail_sync_depth_days_injects_after_floor_into_query() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // Gmail applies sync_depth_days server-side: on the first sync (no cursor)
- // it must inject an `after:<epoch>` qualifier into the GMAIL_FETCH_EMAILS
- // query, proving the window actually narrows what is fetched.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(gmail_cap_router(
- gmail_cap_messages(1),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "gmail".to_string(),
- connection_id: Some("conn-gmail-depth".to_string()),
- usage: Default::default(),
- max_items: None,
- sync_depth_days: Some(7),
- };
-
- GmailProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("gmail depth sync");
-
- let reqs = requests.lock().unwrap();
- let fetch = reqs
- .iter()
- .find(|b| b.get("tool").and_then(Value::as_str) == Some("GMAIL_FETCH_EMAILS"))
- .expect("a GMAIL_FETCH_EMAILS request was issued");
- let query = fetch
- .get("arguments")
- .and_then(|a| a.get("query"))
- .and_then(Value::as_str)
- .unwrap_or("");
- assert!(
- query.contains("after:"),
- "sync_depth_days=7 must inject an `after:` floor into the query, got: {query}"
- );
-
- server.abort();
-}
-
-/// Router whose `GMAIL_FETCH_EMAILS` always returns the same page **with a
-/// non-empty `nextPageToken`** — so absent an all-synced early stop the provider
-/// would paginate to the page ceiling. Used to pin `stop_on_empty_pending`.
-fn gmail_repeating_router(messages: Vec<Value>, requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let messages = messages.clone();
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "GMAIL_GET_PROFILE" => {
- execute_envelope(json!({ "emailAddress": "[email protected]" }))
- }
- "GMAIL_FETCH_EMAILS" => execute_envelope(json!({
- "messages": messages,
- "nextPageToken": "more"
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-// Replaces gmail's dropped `last_seen_id` head-unchanged optimization: prove the
-// provider halts after a single `GMAIL_FETCH_EMAILS` round-trip when the first
-// page is already fully synced. We pre-seed `synced_ids` with the page's message
-// ids and leave the cursor `None` — so the cursor/depth boundary cannot fire and
-// **only** `stop_on_empty_pending` can stop the loop. The router hands back a
-// perpetual `nextPageToken`, so without the early stop the provider would walk
-// every page up to the ceiling.
-#[tokio::test]
-async fn gmail_sync_stops_after_an_all_already_synced_page() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(gmail_repeating_router(
- gmail_cap_messages(3),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- let memory = memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- // Pre-seed: the 3 page-1 message ids are already synced; cursor stays None.
- let mut state = SyncState::new("gmail", "conn-gmail-stop");
- for i in 1..=3 {
- state.mark_synced(format!("gmail-cap-msg-{i}"));
- }
- let state_adapter =
- tinymemory_core::tinycortex::HostSyncAdapter::new(memory.clone());
- state
- .save(&state_adapter)
- .await
- .expect("save pre-seeded sync state");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "gmail".to_string(),
- connection_id: Some("conn-gmail-stop".to_string()),
- usage: Default::default(),
- max_items: None,
- sync_depth_days: None,
- };
-
- let outcome = GmailProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("gmail sync");
-
- assert_eq!(
- outcome.items_ingested, 0,
- "every page-1 message was already synced — nothing to ingest"
- );
- let fetch_calls = requests
- .lock()
- .unwrap()
- .iter()
- .filter(|b| b.get("tool").and_then(Value::as_str) == Some("GMAIL_FETCH_EMAILS"))
- .count();
- assert_eq!(
- fetch_calls, 1,
- "stop_on_empty_pending must halt after one all-synced page, not paginate the \
- perpetual nextPageToken — got {fetch_calls} GMAIL_FETCH_EMAILS calls"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Notion cap enforcement
-//
-// Proves that max_items=N caps Notion ingestion to exactly N pages even
-// when a single NOTION_FETCH_DATA page returns more than N. The mock
-// returns 5 pages; with max_items=2 only 2 must be persisted.
-//
-// sync_depth_days is None and all items carry a recent last_edited_time
-// so no depth-filter skipping happens — the cap is the only thing that
-// limits ingestion.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build M distinct, valid Notion page objects. Each has a unique id and a
-/// recent `last_edited_time` so the depth filter (when enabled) would keep
-/// them all.
-fn notion_cap_pages(m: usize) -> Vec<Value> {
- (1..=m)
- .map(|i| {
- json!({
- "id": format!("notion-cap-page-{i:04}"),
- "object": "page",
- "last_edited_time": format!("2026-06-0{}T10:00:00.000Z", (i % 9) + 1),
- "properties": {
- "Name": {
- "type": "title",
- "title": [{ "plain_text": format!("Cap page {i}") }]
- }
- },
- "url": format!("https://www.notion.so/cap-page-{i}")
- })
- })
- .collect()
-}
-
-/// Loopback router for the Notion cap test.
-/// NOTION_GET_ABOUT_ME returns a minimal identity; NOTION_FETCH_DATA returns
-/// one page with all `pages` as results and no next_cursor.
-fn notion_cap_router(pages: Vec<Value>, requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let pages = pages.clone();
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "NOTION_GET_ABOUT_ME" => execute_envelope(json!({
- "id": "notion-cap-user",
- "name": "Cap User",
- "type": "bot"
- })),
- "NOTION_FETCH_DATA" => execute_envelope(json!({
- "results": pages,
- "next_cursor": null,
- "has_more": false
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-#[tokio::test]
-async fn notion_sync_max_items_caps_ingest_to_exact_count() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One page returns 5 valid Notion pages; the cap is 2.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(notion_cap_router(
- notion_cap_pages(5),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "notion".to_string(),
- connection_id: Some("conn-notion-cap".to_string()),
- usage: Default::default(),
- // Mock returns 5 pages; cap is 2 — only 2 must be ingested.
- max_items: Some(2),
- sync_depth_days: None,
- };
-
- let outcome = NotionProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("notion cap sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "max_items=2 must cap Notion ingest to exactly 2 even though the page held 5"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Notion sync_depth_days enforcement (via the shared orchestrator)
-//
-// Proves the generic orchestrator's depth window actually drops items older
-// than the floor end-to-end through the real Notion provider. The mock returns
-// 2 recent pages (far-future `last_edited_time`, always inside the window) and
-// 3 ancient pages (year-2000, always outside it), in the descending order the
-// provider requests. With sync_depth_days=7 only the 2 recent pages persist —
-// the orchestrator truncates the page at the first item below the floor.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build `recent` + `old` Notion pages in descending `last_edited_time` order.
-/// Recent pages use a far-future timestamp (always within any depth window);
-/// old pages use a year-2000 timestamp (always outside it).
-fn notion_depth_pages(recent: usize, old: usize) -> Vec<Value> {
- let mut pages = Vec::new();
- for i in 0..recent {
- pages.push(json!({
- "id": format!("notion-recent-{i:04}"),
- "object": "page",
- "last_edited_time": format!("2099-12-{:02}T10:00:00.000Z", 28 - i),
- "properties": { "Name": { "type": "title", "title": [{ "plain_text": format!("Recent {i}") }] } }
- }));
- }
- for i in 0..old {
- pages.push(json!({
- "id": format!("notion-old-{i:04}"),
- "object": "page",
- "last_edited_time": format!("2000-01-{:02}T10:00:00.000Z", 3 - i),
- "properties": { "Name": { "type": "title", "title": [{ "plain_text": format!("Old {i}") }] } }
- }));
- }
- pages
-}
-
-#[tokio::test]
-async fn notion_sync_depth_days_filters_old_pages() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One page: 2 recent + 3 ancient. With a 7-day window only the 2 recent
- // pages must be ingested.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(notion_cap_router(
- notion_depth_pages(2, 3),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "notion".to_string(),
- connection_id: Some("conn-notion-depth".to_string()),
- usage: Default::default(),
- max_items: None,
- // 7-day window: drops the year-2000 pages, keeps the far-future ones.
- sync_depth_days: Some(7),
- };
-
- let outcome = NotionProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("notion depth sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "sync_depth_days=7 must drop the 3 year-2000 pages and keep the 2 recent ones"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Linear cap enforcement
-//
-// Proves that max_items=N caps Linear ingestion to exactly N issues even
-// when a single LINEAR_LIST_LINEAR_ISSUES page returns more than N.
-//
-// Tool sequence:
-// 1. LINEAR_LIST_LINEAR_USERS { isMe: true } → viewer id
-// 2. LINEAR_LIST_LINEAR_ISSUES (assigneeId=...) → nodes of issues
-//
-// Issues are in Linear's `{ nodes: [...], pageInfo: {...} }` shape.
-// We return hasNextPage=false so pagination stops after one page.
-// Each issue has a unique id and a recent updatedAt.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build M distinct, valid Linear issue objects.
-fn linear_cap_issues(m: usize) -> Vec<Value> {
- (1..=m)
- .map(|i| {
- json!({
- "id": format!("linear-cap-issue-{i:04}"),
- "identifier": format!("ENG-{i}"),
- "title": format!("Cap issue {i}"),
- "updatedAt": format!("2026-06-0{}T10:00:00.000Z", (i % 9) + 1),
- "url": format!("https://linear.app/cap/issue/ENG-{i}"),
- "description": format!("Description for cap issue {i}.")
- })
- })
- .collect()
-}
-
-/// Loopback router for the Linear cap test.
-/// LINEAR_LIST_LINEAR_USERS returns a single viewer node so `resolve_viewer_id`
-/// succeeds. LINEAR_LIST_LINEAR_ISSUES returns one page with all issues and no
-/// next-page cursor (hasNextPage=false).
-fn linear_cap_router(issues: Vec<Value>, requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let issues = issues.clone();
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "LINEAR_LIST_LINEAR_USERS" => execute_envelope(json!({
- "nodes": [
- { "id": "linear-cap-viewer", "name": "Cap Viewer", "email": "[email protected]" }
- ]
- })),
- "LINEAR_LIST_LINEAR_ISSUES" => execute_envelope(json!({
- "nodes": issues,
- "pageInfo": {
- "hasNextPage": false,
- "endCursor": null
- }
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-#[tokio::test]
-async fn linear_sync_max_items_caps_ingest_to_exact_count() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One page returns 5 valid Linear issues; the cap is 2.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(linear_cap_router(
- linear_cap_issues(5),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "linear".to_string(),
- connection_id: Some("conn-linear-cap".to_string()),
- usage: Default::default(),
- // Mock returns 5 issues; cap is 2 — only 2 must be ingested.
- max_items: Some(2),
- sync_depth_days: None,
- };
-
- let outcome = LinearProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("linear cap sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "max_items=2 must cap Linear ingest to exactly 2 even though the page held 5"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Linear sync_depth_days enforcement (via the shared orchestrator)
-//
-// Linear applies the depth window client-side (RFC3339 `updatedAt`). The mock
-// returns 2 recent issues (far-future) + 3 ancient (year-2000) in descending
-// order; with sync_depth_days=7 only the 2 recent issues persist — the
-// orchestrator truncates the page at the first item below the floor.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build `recent` + `old` Linear issues in descending `updatedAt` order.
-fn linear_depth_issues(recent: usize, old: usize) -> Vec<Value> {
- let mut issues = Vec::new();
- for i in 0..recent {
- issues.push(json!({
- "id": format!("linear-recent-{i:04}"),
- "identifier": format!("ENG-R{i}"),
- "title": format!("Recent {i}"),
- "updatedAt": format!("2099-12-{:02}T10:00:00.000Z", 28 - i),
- "url": format!("https://linear.app/cap/issue/ENG-R{i}"),
- "description": "recent",
- }));
- }
- for i in 0..old {
- issues.push(json!({
- "id": format!("linear-old-{i:04}"),
- "identifier": format!("ENG-O{i}"),
- "title": format!("Old {i}"),
- "updatedAt": format!("2000-01-{:02}T10:00:00.000Z", 3 - i),
- "url": format!("https://linear.app/cap/issue/ENG-O{i}"),
- "description": "old",
- }));
- }
- issues
-}
-
-#[tokio::test]
-async fn linear_sync_depth_days_filters_old_issues() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One page: 2 recent + 3 ancient. With a 7-day window only the 2 recent
- // issues must be ingested.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(linear_cap_router(
- linear_depth_issues(2, 3),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "linear".to_string(),
- connection_id: Some("conn-linear-depth".to_string()),
- usage: Default::default(),
- max_items: None,
- sync_depth_days: Some(7),
- };
-
- let outcome = LinearProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("linear depth sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "sync_depth_days=7 must drop the 3 year-2000 issues and keep the 2 recent ones"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// ClickUp cap enforcement
-//
-// Proves that max_items=N caps ClickUp ingestion to exactly N tasks even
-// when a single CLICKUP_GET_FILTERED_TEAM_TASKS page returns more than N.
-//
-// Tool sequence:
-// 1. CLICKUP_GET_AUTHORIZED_USER → user numeric id
-// 2. CLICKUP_GET_AUTHORIZED_TEAMS_WORKSPACES → one workspace
-// 3. CLICKUP_GET_FILTERED_TEAM_TASKS → tasks page
-//
-// The tasks page returns 5 items; with max_items=2 only 2 must be
-// persisted. Because INITIAL_PAGE_SIZE=100 for ConnectionCreated and we
-// return only 5 tasks (< 100), the short-page guard stops the loop so no
-// second page is requested, matching the cap path cleanly.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build M distinct, valid ClickUp task objects.
-fn clickup_cap_tasks(m: usize) -> Vec<Value> {
- // date_updated must be large enough to sort lexicographically correctly
- // but recent enough not to trip any depth filter. We use ms-since-epoch
- // strings in the vicinity of mid-2026 (≈1780000000000 ms).
- (1..=m)
- .map(|i| {
- json!({
- "id": format!("clickup-cap-task-{i:04}"),
- "name": format!("Cap task {i}"),
- "text_content": format!("Content for cap task {i}."),
- "status": { "status": "open" },
- "date_updated": format!("{}", 1_780_000_000_000_u64 + i as u64 * 1000),
- "url": format!("https://app.clickup.com/t/clickup-cap-task-{i:04}")
- })
- })
- .collect()
-}
-
-/// Loopback router for the ClickUp cap test.
-fn clickup_cap_router(tasks: Vec<Value>, requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let tasks = tasks.clone();
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "CLICKUP_GET_AUTHORIZED_USER" => execute_envelope(json!({
- "user": {
- "id": 42,
- "username": "cap-user",
- "email": "[email protected]",
- "profilePicture": null
- }
- })),
- "CLICKUP_GET_AUTHORIZED_TEAMS_WORKSPACES" => execute_envelope(json!({
- "teams": [
- { "id": "ws-cap-01", "name": "Cap Workspace" }
- ]
- })),
- "CLICKUP_GET_FILTERED_TEAM_TASKS" => execute_envelope(json!({
- "tasks": tasks
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-#[tokio::test]
-async fn clickup_sync_max_items_caps_ingest_to_exact_count() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One workspace, one page returning 5 valid tasks; the cap is 2.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(clickup_cap_router(
- clickup_cap_tasks(5),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "clickup".to_string(),
- connection_id: Some("conn-clickup-cap".to_string()),
- usage: Default::default(),
- // Mock returns 5 tasks; cap is 2 — only 2 must be ingested.
- max_items: Some(2),
- sync_depth_days: None,
- };
-
- let outcome = ClickUpProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("clickup cap sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "max_items=2 must cap ClickUp ingest to exactly 2 even though the page held 5"
- );
-
- server.abort();
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// ClickUp sync_depth_days enforcement (via the shared orchestrator)
-//
-// ClickUp's `date_updated` is an epoch-millis string, so the orchestrator's
-// depth floor must be epoch-ms too (ClickUpSource::depth_floor override). The
-// mock returns 2 recent tasks (year-2030 epoch-ms) + 3 ancient (year-2001) in
-// descending order; with sync_depth_days=7 only the 2 recent tasks persist.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build `recent` + `old` ClickUp tasks in descending `date_updated` order,
-/// using epoch-millis strings so the epoch-ms depth floor compares correctly.
-fn clickup_depth_tasks(recent: usize, old: usize) -> Vec<Value> {
- let mut tasks = Vec::new();
- for i in 0..recent {
- tasks.push(json!({
- "id": format!("clickup-recent-{i:04}"),
- "name": format!("Recent {i}"),
- "text_content": "recent",
- "status": { "status": "open" },
- "date_updated": format!("{}", 1_900_000_000_000_u64 + (recent - i) as u64),
- "url": format!("https://app.clickup.com/t/clickup-recent-{i:04}")
- }));
- }
- for i in 0..old {
- tasks.push(json!({
- "id": format!("clickup-old-{i:04}"),
- "name": format!("Old {i}"),
- "text_content": "old",
- "status": { "status": "open" },
- "date_updated": format!("{}", 1_000_000_000_000_u64 + (old - i) as u64),
- "url": format!("https://app.clickup.com/t/clickup-old-{i:04}")
- }));
- }
- tasks
-}
-
-#[tokio::test]
-async fn clickup_sync_depth_days_filters_old_tasks() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One workspace, one page: 2 recent + 3 ancient. With a 7-day window only
- // the 2 recent tasks must be ingested.
- let requests: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
- let (base, server) = loopback_router(clickup_cap_router(
- clickup_depth_tasks(2, 3),
- Arc::clone(&requests),
- ))
- .await;
-
- let mut config = config_in(&tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
-
- let ctx = ProviderContext {
- config: Arc::new(config),
- toolkit: "clickup".to_string(),
- connection_id: Some("conn-clickup-depth".to_string()),
- usage: Default::default(),
- max_items: None,
- sync_depth_days: Some(7),
- };
-
- let outcome = ClickUpProvider::new()
- .sync(&ctx, SyncReason::ConnectionCreated)
- .await
- .expect("clickup depth sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "sync_depth_days=7 must drop the 3 year-2001 tasks and keep the 2 recent ones"
- );
-
- server.abort();
-}
-
-fn walk_files(root: &Path) -> Vec<std::path::PathBuf> {
- let mut out = Vec::new();
- if !root.exists() {
- return out;
- }
- let mut stack = vec![root.to_path_buf()];
- while let Some(path) = stack.pop() {
- let entries = match std::fs::read_dir(&path) {
- Ok(entries) => entries,
- Err(_) => continue,
- };
- for entry in entries.flatten() {
- let child = entry.path();
- if child.is_dir() {
- stack.push(child);
- } else {
- out.push(child);
- }
- }
- }
- out
-}
-
-// ─────────────────────────────────────────────────────────────────────────
-// Sync-cap enforcement: max_items and sync_depth_days
-//
-// These verify the user-facing promise of the per-source sync settings:
-// - max_items=N ingests AT MOST N items, even when a single API page
-// returns more (precise mid-page cap, not just a page cap).
-// - sync_depth_days injects an `updated:>{floor}` date filter into the
-// GitHub search query so only recent items are requested.
-// The cap logic is shared verbatim across the gmail/notion/linear/clickup
-// providers, so GitHub stands in for all of them here.
-// ─────────────────────────────────────────────────────────────────────────
-
-/// Build `n` distinct, valid GitHub search items (each with an id + updated_at).
-fn github_issue_items(n: usize) -> Vec<Value> {
- (1..=n)
- .map(|i| {
- json!({
- "id": 3000 + i,
- "title": format!("Cap issue {i}"),
- "body": "cap enforcement body",
- "state": "open",
- "updated_at": format!("2026-05-{:02}T10:00:00Z", 10 + i),
- "html_url": format!("https://github.com/tinyhumansai/openhuman/issues/{i}")
- })
+ // Deliberately signed out: `ComposioConnectionCreatedSubscriber::handle`
+ // spawns a detached task that would otherwise reach a real backend via
+ // `wait_for_connection_active` once past its `create_composio_client`
+ // guard. Staying signed out fails that guard closed immediately.
+ let connection_sub = ComposioConnectionCreatedSubscriber::new();
+ assert_eq!(connection_sub.name(), "composio::connection_created");
+ assert_eq!(connection_sub.domains().unwrap(), &["composio"]);
+ connection_sub
+ .handle(&DomainEvent::ComposioConnectionCreated {
+ toolkit: "github".to_string(),
+ connection_id: "conn-github-round17".to_string(),
+ connect_url: "https://github.com/login/oauth/authorize".to_string(),
})
- .collect()
-}
-
-/// Loopback router that answers the two GitHub sync tools. The search tool
-/// always returns `items` (one page), and every request body is captured.
-fn github_cap_router(items: Vec<Value>, requests: Arc<Mutex<Vec<Value>>>) -> Router {
- Router::new().route(
- "/agent-integrations/composio/execute",
- any(move |Json(body): Json<Value>| {
- let items = items.clone();
- let requests = Arc::clone(&requests);
- async move {
- requests.lock().unwrap().push(body.clone());
- let tool = body.get("tool").and_then(Value::as_str).unwrap_or("");
- let resp = match tool {
- "GITHUB_GET_THE_AUTHENTICATED_USER" => execute_envelope(json!({
- "login": "octo-cap",
- "html_url": "https://github.com/octo-cap"
- })),
- "GITHUB_SEARCH_ISSUES_AND_PULL_REQUESTS" => execute_envelope(json!({
- "items": items,
- "total_count": items.len()
- })),
- _ => execute_envelope(json!({})),
- };
- Json(resp)
- }
- }),
- )
-}
-
-async fn github_cap_context(
- tmp: &TempDir,
- items: Vec<Value>,
- requests: Arc<Mutex<Vec<Value>>>,
-) -> (Config, tokio::task::JoinHandle<()>) {
- let (base, server) = loopback_router(github_cap_router(items, requests)).await;
- let mut config = config_in(tmp);
- config.api_url = Some(base);
- persist_config(&config).await;
- store_session(&config);
- memory_global::init(config.workspace_dir.clone()).expect("init global memory");
- (config, server)
-}
-
-fn github_ctx(
- config: &Config,
- max_items: Option<u32>,
- sync_depth_days: Option<u32>,
-) -> ProviderContext {
- ProviderContext {
- config: Arc::new(config.clone()),
- toolkit: "github".to_string(),
- connection_id: Some("conn-cap".to_string()),
- usage: Default::default(),
- max_items,
- sync_depth_days,
- }
-}
-
-#[tokio::test]
-async fn github_sync_max_items_caps_ingest_to_exact_count() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // One page returns 5 valid items; the cap is 2.
- let requests = Arc::new(Mutex::new(Vec::new()));
- let (config, server) =
- github_cap_context(&tmp, github_issue_items(5), Arc::clone(&requests)).await;
-
- let outcome = GitHubProvider::new()
- .sync(
- &github_ctx(&config, Some(2), None),
- SyncReason::ConnectionCreated,
- )
- .await
- .expect("github sync");
-
- assert_eq!(
- outcome.items_ingested, 2,
- "max_items=2 must cap ingest to exactly 2 even though the page held 5"
- );
- server.abort();
-}
-
-#[tokio::test]
-async fn github_sync_without_max_items_ingests_full_page() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- // Control: no cap → every valid item on the page is ingested.
- let requests = Arc::new(Mutex::new(Vec::new()));
- let (config, server) =
- github_cap_context(&tmp, github_issue_items(5), Arc::clone(&requests)).await;
-
- let outcome = GitHubProvider::new()
- .sync(
- &github_ctx(&config, None, None),
- SyncReason::ConnectionCreated,
- )
- .await
- .expect("github sync");
-
- assert_eq!(
- outcome.items_ingested, 5,
- "with no max_items cap, all 5 page items must be ingested"
- );
- server.abort();
-}
-
-#[tokio::test]
-async fn github_sync_depth_days_injects_updated_floor_into_query() {
- let _guard = env_lock();
- let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
- let _backend = EnvGuard::unset("BACKEND_URL");
-
- let requests = Arc::new(Mutex::new(Vec::new()));
- let (config, server) =
- github_cap_context(&tmp, github_issue_items(1), Arc::clone(&requests)).await;
-
- GitHubProvider::new()
- .sync(
- &github_ctx(&config, None, Some(7)),
- SyncReason::ConnectionCreated,
- )
- .await
- .expect("github sync");
+ .await;
- // The search request must carry an `updated:>{date}` floor derived from the
- // 7-day window, proving sync_depth_days actually narrows what is fetched.
- let reqs = requests.lock().unwrap();
- let search = reqs
- .iter()
- .find(|b| {
- b.get("tool").and_then(Value::as_str) == Some("GITHUB_SEARCH_ISSUES_AND_PULL_REQUESTS")
- })
- .expect("a search request was issued");
- let q = search
- .get("arguments")
- .and_then(|a| a.get("q"))
- .and_then(Value::as_str)
- .unwrap_or("");
- assert!(
- q.contains("updated:>"),
- "sync_depth_days=7 must inject an `updated:>` date floor, got query: {q}"
- );
- server.abort();
+ // Give the connection-created handler's detached `tokio::spawn` a beat
+ // to run and fail closed, so a panic inside that task has a chance to
+ // surface before the test process exits.
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}