Skip to content

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

test(cron): add coverage for client, documents port, and tinyflows

Steven Enamakelee8697017c65
diff --git a/crates/openhuman-core/src/cron/client_tests.rs b/crates/openhuman-core/src/cron/client_tests.rs
index 4563e55b7b..cb515438af 100644
--- a/crates/openhuman-core/src/cron/client_tests.rs
+++ b/crates/openhuman-core/src/cron/client_tests.rs
@@ -1 +1,184 @@
use super::*;
+use chrono::TimeZone;
+
+fn at(hour: u32) -> DateTime<Utc> {
+ Utc.with_ymd_and_hms(2030, 1, 1, hour, 0, 0).unwrap()
+}
+
+fn cron(expr: &str) -> Schedule {
+ Schedule::Cron {
+ expr: expr.into(),
+ tz: None,
+ active_hours: None,
+ }
+}
+
+#[tokio::test]
+async fn next_run_comes_from_the_module() {
+ let config = Config::default();
+ let next = next_run(&config, &cron("0 9 * * *"), at(8)).await.unwrap();
+ assert_eq!(next, at(9));
+ assert!(next_run(&config, &cron("not a cron"), at(8)).await.is_err());
+}
+
+#[tokio::test]
+async fn schedules_are_validated_and_the_agent_floor_applies() {
+ let config = Config::default();
+ validate_schedule(&config, &cron("0 9 * * *"), at(8), true)
+ .await
+ .unwrap();
+ let every_minute = Schedule::Every { every_ms: 60_000 };
+ validate_schedule(&config, &every_minute, at(8), false)
+ .await
+ .unwrap();
+ assert!(validate_schedule(&config, &every_minute, at(8), true)
+ .await
+ .is_err());
+ let past = Schedule::At { at: at(1) };
+ assert!(validate_schedule(&config, &past, at(8), false).await.is_err());
+}
+
+#[tokio::test]
+async fn expressions_normalise_and_report_their_cron_form() {
+ let config = Config::default();
+ assert_eq!(
+ normalize_expression(&config, "0 9 * * 1-5").await.unwrap(),
+ "0 0 9 * * 2-6"
+ );
+ assert!(schedule_cron_expression(&config, &cron("0 9 * * *"))
+ .await
+ .unwrap()
+ .is_some());
+ assert!(
+ schedule_cron_expression(&config, &Schedule::Every { every_ms: 1000 })
+ .await
+ .unwrap()
+ .is_none()
+ );
+}
+
+#[tokio::test]
+async fn delays_parse_and_delivery_defaults_follow_the_origin() {
+ let config = Config::default();
+ assert_eq!(
+ parse_human_delay(&config, "2h").await.unwrap(),
+ Duration::hours(2)
+ );
+ assert!(parse_human_delay(&config, "soon").await.is_err());
+ assert_eq!(
+ default_delivery(&config, None).await.unwrap().mode,
+ "proactive"
+ );
+ let origin = JobOrigin::Web {
+ thread_id: "t1".into(),
+ agent_id: None,
+ };
+ assert_eq!(
+ default_delivery(&config, Some(&origin)).await.unwrap().mode,
+ "origin"
+ );
+}
+
+#[tokio::test]
+async fn origin_delivery_without_an_origin_is_refused() {
+ let config = Config::default();
+ let origin_delivery = default_delivery(
+ &config,
+ Some(&JobOrigin::Web {
+ thread_id: "t1".into(),
+ agent_id: None,
+ }),
+ )
+ .await
+ .unwrap();
+ assert!(
+ check_origin_requirements(&config, &SessionTarget::Isolated, &origin_delivery, None)
+ .await
+ .is_err()
+ );
+ check_origin_requirements(
+ &config,
+ &SessionTarget::Isolated,
+ &DeliveryConfig::default(),
+ None,
+ )
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn long_output_is_cut_at_the_contract_bound() {
+ let config = Config::default();
+ let long = "x".repeat(tinyflows_bus::schedule::MAX_CRON_OUTPUT_BYTES * 2);
+ let cut = truncate_cron_output(&config, &long).await.unwrap();
+ assert!(cut.len() <= tinyflows_bus::schedule::MAX_CRON_OUTPUT_BYTES + 32);
+ assert!(cut.ends_with(tinyflows_bus::schedule::TRUNCATED_OUTPUT_MARKER));
+}
+
+#[tokio::test]
+async fn runs_closer_than_reports_the_tightest_pair() {
+ let config = Config::default();
+ let hit = runs_closer_than(&config, &cron("*/3 * * * *"), at(8), Duration::minutes(5))
+ .await
+ .unwrap();
+ assert!(hit.is_some());
+ let none = runs_closer_than(&config, &cron("0 9 * * *"), at(8), Duration::minutes(5))
+ .await
+ .unwrap();
+ assert!(none.is_none());
+}
+
+#[tokio::test]
+async fn a_module_error_keeps_its_own_message() {
+ let tmp = tempfile::TempDir::new().unwrap();
+ let config = Config {
+ workspace_dir: tmp.path().join("workspace"),
+ ..Config::default()
+ };
+ std::fs::create_dir_all(&config.workspace_dir).unwrap();
+ let error = store::<CronJob>(
+ &config,
+ StoreOperation::Get {
+ job_id: "missing".into(),
+ },
+ )
+ .await
+ .unwrap_err();
+ assert_eq!(error.to_string(), "Cron job 'missing' not found");
+}
+
+#[tokio::test]
+async fn a_switched_off_component_answers_component_disabled() {
+ let mut config = Config::default();
+ config.components.tinyflows = false;
+ let error = parse_human_delay(&config, "1h").await.unwrap_err();
+ assert!(
+ error.to_string().starts_with("COMPONENT_DISABLED:tinyflows"),
+ "{error}"
+ );
+}
+
+#[test]
+fn an_untouched_agent_id_is_not_sent_but_an_explicit_clear_is() {
+ let untouched = StoreOperation::Update {
+ job_id: "j".into(),
+ patch: CronJobPatch {
+ enabled: Some(false),
+ ..CronJobPatch::default()
+ },
+ };
+ assert!(wire_operation(&untouched).unwrap()["patch"]
+ .get("agent_id")
+ .is_none());
+ let cleared = StoreOperation::Update {
+ job_id: "j".into(),
+ patch: CronJobPatch {
+ agent_id: Some(None),
+ ..CronJobPatch::default()
+ },
+ };
+ assert_eq!(
+ wire_operation(&cleared).unwrap()["patch"]["agent_id"],
+ Value::Null
+ );
+}
diff --git a/crates/openhuman-core/src/cron/documents_port_tests.rs b/crates/openhuman-core/src/cron/documents_port_tests.rs
index 4563e55b7b..be1f0ca367 100644
--- a/crates/openhuman-core/src/cron/documents_port_tests.rs
+++ b/crates/openhuman-core/src/cron/documents_port_tests.rs
@@ -1 +1,92 @@
use super::*;
+use crate::storage::{MemoryStorage, Scope, StorageBackend};
+use serde_json::json;
+use tinyflows_bus::schedule::{DOCUMENT_INTERFACE, DOCUMENT_METHOD};
+
+async fn port() -> (tinybus::Proxy, CallbackAddress) {
+ let config = Config::default();
+ // Start the module runtime and attach the module before touching the bus.
+ module::proxy(&config).await.expect("module attaches");
+ let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let scope = DocScope::on_backend(&config, backend, Scope::local());
+ let address = address(&scope).await.expect("port is served");
+ let runtime = crate::modules::runtime::runtime().await.unwrap();
+ let proxy = runtime
+ .connection()
+ .proxy(
+ &address.destination,
+ &address.object_path,
+ DOCUMENT_INTERFACE,
+ )
+ .expect("the contract's document interface is the one served");
+ (proxy, address)
+}
+
+async fn ask(proxy: &tinybus::Proxy, request: Value) -> Value {
+ proxy.call(DOCUMENT_METHOD, (request,)).await.unwrap()
+}
+
+#[tokio::test]
+async fn the_port_serves_the_contracts_document_interface_and_reports_capabilities() {
+ let (proxy, _) = port().await;
+ let reply = ask(&proxy, json!({"op": "capabilities"})).await;
+ assert!(reply["value"]["transactions"].is_boolean(), "{reply}");
+}
+
+#[tokio::test]
+async fn a_second_absent_write_comes_back_as_a_conflict_kind() {
+ let (proxy, _) = port().await;
+ let put = json!({"op":"put","collection":"cron_jobs","id":"dup","doc":{"a":1},"precondition":"absent"});
+ // The collection must exist first; the module declares it, the port relays.
+ let ensured = ask(
+ &proxy,
+ json!({"op":"ensure","spec":{"name":"cron_jobs"}}),
+ )
+ .await;
+ assert!(ensured.get("error").is_none(), "{ensured}");
+ let first = ask(&proxy, put.clone()).await;
+ assert!(first.get("error").is_none(), "{first}");
+ let second = ask(&proxy, put).await;
+ assert_eq!(second["error"]["kind"], "conflict", "{second}");
+}
+
+#[tokio::test]
+async fn a_collection_outside_the_schedule_port_is_refused() {
+ let (proxy, _) = port().await;
+ for request in [
+ json!({"op":"get","collection":"memory_items","id":"x"}),
+ json!({"op":"count","collection":"flows","filter":{}}),
+ json!({"op":"ensure","spec":{"name":"secrets"}}),
+ json!({"op":"atomic_batch","ops":[
+ {"op":"put","collection":"cron_jobs","id":"ok","doc":{},"precondition":"none"},
+ {"op":"delete","collection":"approvals","id":"x","precondition":"none"}
+ ]}),
+ ] {
+ let reply = ask(&proxy, request.clone()).await;
+ assert_eq!(reply["error"]["kind"], "invalid_input", "{request} -> {reply}");
+ }
+}
+
+#[tokio::test]
+async fn an_unknown_operation_is_refused_not_guessed() {
+ let (proxy, _) = port().await;
+ let reply = ask(&proxy, json!({"op":"drop_collection","collection":"cron_jobs"})).await;
+ assert_eq!(reply["error"]["kind"], "invalid_input", "{reply}");
+}
+
+#[tokio::test]
+async fn one_object_is_served_per_scope_and_reused() {
+ let config = Config::default();
+ module::proxy(&config).await.unwrap();
+ let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let a = DocScope::on_backend(&config, Arc::clone(&backend), Scope::local());
+ let b = DocScope::on_backend(&config, backend, crate::storage::scope_for_agent("other"));
+ let (first, again, other) = (
+ address(&a).await.unwrap(),
+ address(&a).await.unwrap(),
+ address(&b).await.unwrap(),
+ );
+ assert_eq!(first.object_path, again.object_path);
+ assert_ne!(first.object_path, other.object_path);
+ assert_eq!(first.destination, module::HOST_BUS_NAME);
+}
diff --git a/crates/openhuman-core/src/modules/tinyflows_tests.rs b/crates/openhuman-core/src/modules/tinyflows_tests.rs
index 4563e55b7b..cee809599e 100644
--- a/crates/openhuman-core/src/modules/tinyflows_tests.rs
+++ b/crates/openhuman-core/src/modules/tinyflows_tests.rs
@@ -1 +1,37 @@
use super::*;
+
+#[test]
+fn the_disabled_message_names_the_component_and_the_switch() {
+ let message = disabled_message();
+ assert!(message.starts_with("COMPONENT_DISABLED:tinyflows"), "{message}");
+ assert!(message.contains("[components] tinyflows = true"));
+}
+
+#[test]
+fn the_module_is_available_only_while_its_component_is_on() {
+ let mut config = Config::default();
+ assert_eq!(available(&config), cfg!(feature = "modules"));
+ config.components.tinyflows = false;
+ assert!(!available(&config));
+}
+
+#[tokio::test]
+async fn a_switched_off_component_never_loads_the_module() {
+ let mut config = Config::default();
+ config.components.tinyflows = false;
+ let error = proxy(&config).await.unwrap_err();
+ assert!(error.starts_with("COMPONENT_DISABLED:tinyflows"), "{error}");
+ let error = run_proxy(&config).await.unwrap_err();
+ assert!(error.starts_with("COMPONENT_DISABLED:tinyflows"), "{error}");
+}
+
+#[tokio::test]
+async fn the_schedule_object_answers_on_the_contracts_names() {
+ let config = Config::default();
+ let proxy = proxy(&config).await.expect("module attaches");
+ let millis: i64 = proxy
+ .call(tinyflows_bus::schedule::members::PARSE_HUMAN_DELAY, ("30m",))
+ .await
+ .unwrap();
+ assert_eq!(millis, 30 * 60 * 1000);
+}