Skip to content

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

test: cover desktop connection status and RPC dispatch

Steven Enamakelc8ab1cf62039
diff --git a/crates/openhuman-core/src/desktop/control/ops.rs b/crates/openhuman-core/src/desktop/control/ops.rs
index 56f032882d..ae86a3f074 100644
--- a/crates/openhuman-core/src/desktop/control/ops.rs
+++ b/crates/openhuman-core/src/desktop/control/ops.rs
@@ -158,6 +158,14 @@ fn permission(data: &serde_json::Value, field: &str) -> String {
}
pub async fn status(config: &Config) -> DesktopStatus {
+ status_with(config, || crate::modules::desktop::permissions(config)).await
+}
+
+async fn status_with<F, Fut>(config: &Config, permissions: F) -> DesktopStatus
+where
+ F: FnOnce() -> Fut,
+ Fut: std::future::Future<Output = Result<tinydesktop_bus::DesktopResponse, String>>,
+{
let local_enabled = enabled(config);
let (module_state, mut reason) = crate::modules::desktop::state(config);
let mut result = DesktopStatus {
@@ -176,7 +184,7 @@ pub async fn status(config: &Config) -> DesktopStatus {
"desktop control requires macOS or Windows and a loopback core listener".to_owned(),
);
} else if local_enabled {
- match crate::modules::desktop::permissions(config).await {
+ match permissions().await {
Ok(response) if response.ok => {
if let Some(data) = response.data.as_ref() {
result.accessibility = permission(data, "accessibility");
@@ -194,6 +202,34 @@ pub async fn status(config: &Config) -> DesktopStatus {
}
pub async fn probe(config: &Config) -> DesktopProbe {
+ probe_with(config, |member| async move {
+ match member {
+ names::methods::PERMISSIONS => crate::modules::desktop::permissions(config).await,
+ names::methods::SNAPSHOT => {
+ crate::modules::desktop::call(
+ config,
+ member,
+ SnapshotRequest {
+ skeleton: true,
+ ..SnapshotRequest::default()
+ },
+ )
+ .await
+ }
+ names::methods::LIST_APPS => {
+ crate::modules::desktop::call(config, member, ListAppsRequest::default()).await
+ }
+ _ => unreachable!("probe calls only its three fixed module members"),
+ }
+ })
+ .await
+}
+
+async fn probe_with<F, Fut>(config: &Config, mut call: F) -> DesktopProbe
+where
+ F: FnMut(&'static str) -> Fut,
+ Fut: std::future::Future<Output = Result<tinydesktop_bus::DesktopResponse, String>>,
+{
if !supported() || !enabled(config) {
return DesktopProbe {
ok: false,
@@ -201,7 +237,7 @@ pub async fn probe(config: &Config) -> DesktopProbe {
reason: Some("desktop control is unavailable or disabled".to_owned()),
};
}
- let permissions = match crate::modules::desktop::permissions(config).await {
+ let permissions = match call(names::methods::PERMISSIONS).await {
Ok(reply) if reply.ok => reply,
Ok(reply) => {
return DesktopProbe {
@@ -231,23 +267,10 @@ pub async fn probe(config: &Config) -> DesktopProbe {
reason: Some("Accessibility permission is not granted to the core process".to_owned()),
};
}
- let snapshot = crate::modules::desktop::call(
- config,
- names::methods::SNAPSHOT,
- SnapshotRequest {
- skeleton: true,
- ..SnapshotRequest::default()
- },
- )
- .await;
+ let snapshot = call(names::methods::SNAPSHOT).await;
match snapshot {
Ok(response) if response.ok => {
- let apps = crate::modules::desktop::call(
- config,
- names::methods::LIST_APPS,
- ListAppsRequest::default(),
- )
- .await;
+ let apps = call(names::methods::LIST_APPS).await;
DesktopProbe {
ok: true,
app_count: apps.ok().and_then(|reply| reply.data).and_then(|data| {
diff --git a/crates/openhuman-core/src/desktop/control/ops_tests.rs b/crates/openhuman-core/src/desktop/control/ops_tests.rs
index 36f8858776..8107baec5b 100644
--- a/crates/openhuman-core/src/desktop/control/ops_tests.rs
+++ b/crates/openhuman-core/src/desktop/control/ops_tests.rs
@@ -12,6 +12,180 @@ fn disabled_by_default_and_persists() {
assert!(!enabled(&config));
}
+#[test]
+fn unreadable_or_corrupt_state_never_enables_desktop() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = dir.path().to_path_buf();
+ let path = state_path(&config);
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(&path, "not json").unwrap();
+ assert!(!enabled(&config));
+ std::fs::write(&path, r#"{"enabled":"true"}"#).unwrap();
+ assert!(!enabled(&config));
+ std::fs::remove_file(&path).unwrap();
+ std::fs::create_dir(&path).unwrap();
+ assert!(!enabled(&config));
+ assert!(save(&config, true).is_err());
+ assert!(!enabled(&config));
+}
+
+#[tokio::test]
+async fn disabled_probe_never_calls_the_module() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = dir.path().to_path_buf();
+ let result = probe_with(&config, |_| async {
+ panic!("disabled probe reached module")
+ })
+ .await;
+ assert!(!result.ok);
+ assert_eq!(result.app_count, None);
+ assert_eq!(
+ result.reason.as_deref(),
+ Some("desktop control is unavailable or disabled")
+ );
+}
+
+#[tokio::test]
+async fn disabled_status_reports_local_setting_without_contacting_module() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = dir.path().to_path_buf();
+ let result = status_with(&config, || async {
+ panic!("disabled status reached module")
+ })
+ .await;
+ assert!(!result.enabled);
+ assert!(!result.approvals_enabled);
+ assert_eq!(result.accessibility, "unknown");
+ assert_eq!(result.screen_recording, "unknown");
+ assert_eq!(result.platform, std::env::consts::OS);
+}
+
+#[cfg(any(target_os = "macos", target_os = "windows"))]
+#[tokio::test]
+async fn enabled_status_reports_module_permissions_and_errors() {
+ use tinydesktop_bus::{DesktopError, DesktopResponse};
+
+ set_listener_is_loopback(true);
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = dir.path().to_path_buf();
+ save(&config, true).unwrap();
+
+ let permitted = status_with(&config, || async {
+ Ok(DesktopResponse::ok(
+ "permissions",
+ serde_json::json!({
+ "accessibility":{"state":"granted"},
+ "screen_recording":{"state":"not_required"}
+ }),
+ ))
+ })
+ .await;
+ assert!(permitted.supported && permitted.enabled);
+ assert_eq!(permitted.accessibility, "granted");
+ assert_eq!(permitted.screen_recording, "not_required");
+
+ let refused = status_with(&config, || async {
+ Ok(DesktopResponse::err(
+ "permissions",
+ DesktopError::new("PERM_DENIED", "Grant Accessibility"),
+ ))
+ })
+ .await;
+ assert_eq!(refused.reason.as_deref(), Some("Grant Accessibility"));
+ assert_eq!(refused.accessibility, "unknown");
+
+ let unavailable = status_with(&config, || async {
+ Err("module could not start".to_owned())
+ })
+ .await;
+ assert_eq!(
+ unavailable.reason.as_deref(),
+ Some("module could not start")
+ );
+}
+
+#[tokio::test]
+async fn disabling_desktop_persists_even_if_state_was_enabled() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = dir.path().to_path_buf();
+ save(&config, true).unwrap();
+ let result = set_enabled(&config, false).await.unwrap();
+ assert!(!result.enabled);
+ assert!(!enabled(&config));
+}
+
+#[cfg(any(target_os = "macos", target_os = "windows"))]
+#[tokio::test]
+async fn probe_requires_accessibility_and_snapshot_before_listing_apps() {
+ use tinydesktop_bus::{DesktopError, DesktopResponse};
+
+ set_listener_is_loopback(true);
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = dir.path().to_path_buf();
+ save(&config, true).unwrap();
+
+ let denied = probe_with(&config, |member| async move {
+ assert_eq!(member, names::methods::PERMISSIONS);
+ Ok(DesktopResponse::ok(
+ member,
+ serde_json::json!({
+ "accessibility":{"state":"denied"}
+ }),
+ ))
+ })
+ .await;
+ assert!(!denied.ok);
+ assert_eq!(
+ denied.reason.as_deref(),
+ Some("Accessibility permission is not granted to the core process")
+ );
+
+ let failed = probe_with(&config, |member| async move {
+ match member {
+ names::methods::PERMISSIONS => Ok(DesktopResponse::ok(
+ member,
+ serde_json::json!({
+ "accessibility":{"state":"granted"}
+ }),
+ )),
+ names::methods::SNAPSHOT => Ok(DesktopResponse::err(
+ member,
+ DesktopError::new("PERM_DENIED", "Screen Recording required"),
+ )),
+ _ => panic!("failed snapshot must prevent app listing"),
+ }
+ })
+ .await;
+ assert!(!failed.ok);
+ assert_eq!(failed.reason.as_deref(), Some("Screen Recording required"));
+
+ let success = probe_with(&config, |member| async move {
+ Ok(DesktopResponse::ok(
+ member,
+ match member {
+ names::methods::PERMISSIONS => {
+ serde_json::json!({"accessibility":{"state":"granted"}})
+ }
+ names::methods::SNAPSHOT => serde_json::json!({"elements":[]}),
+ names::methods::LIST_APPS => {
+ serde_json::json!({"apps":[{"name":"TextEdit"},{"name":"Finder"}]})
+ }
+ _ => panic!("unexpected member"),
+ },
+ ))
+ })
+ .await;
+ assert!(success.ok);
+ assert_eq!(success.app_count, Some(2));
+ assert!(success.reason.is_none());
+}
+
#[test]
fn permission_states_are_normalized() {
let data = serde_json::json!({"accessibility":{"state":"granted"},
diff --git a/crates/openhuman-core/src/desktop/control/schemas.rs b/crates/openhuman-core/src/desktop/control/schemas.rs
index 0d880f2a19..b3bdf9e47b 100644
--- a/crates/openhuman-core/src/desktop/control/schemas.rs
+++ b/crates/openhuman-core/src/desktop/control/schemas.rs
@@ -130,3 +130,7 @@ fn confirm(params: Map<String, Value>) -> ControllerFuture {
super::confirmation::confirm(&config, id, approve).await
})
}
+
+#[cfg(test)]
+#[path = "schemas_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/desktop/control/schemas_tests.rs b/crates/openhuman-core/src/desktop/control/schemas_tests.rs
new file mode 100644
index 0000000000..d95a3fee92
--- /dev/null
+++ b/crates/openhuman-core/src/desktop/control/schemas_tests.rs
@@ -0,0 +1,54 @@
+use super::*;
+
+fn controller(name: &str) -> RegisteredController {
+ all_registered_controllers()
+ .into_iter()
+ .find(|item| item.schema.function == name)
+ .unwrap()
+}
+
+#[tokio::test]
+async fn desktop_controllers_expose_expected_rpc_contracts_and_dispatch_pending() {
+ let controllers = all_registered_controllers();
+ assert_eq!(controllers.len(), 5);
+ for item in &controllers {
+ assert_eq!(item.schema.namespace, "desktop");
+ assert!(item.rpc_method_name().starts_with("openhuman.desktop_"));
+ }
+ assert_eq!(controller("set_enabled").schema.inputs.len(), 1);
+ assert_eq!(controller("confirm").schema.inputs.len(), 2);
+ let value = (controller("pending").handler)(Map::new()).await.unwrap();
+ assert!(value.is_array());
+}
+
+#[tokio::test]
+async fn malformed_controller_inputs_fail_before_loading_config_or_acting() {
+ for invalid in [
+ Value::Null,
+ Value::String("true".to_owned()),
+ Value::Number(1.into()),
+ ] {
+ let mut params = Map::new();
+ params.insert("enabled".to_owned(), invalid);
+ assert_eq!(
+ (controller("set_enabled").handler)(params)
+ .await
+ .unwrap_err(),
+ "enabled must be a boolean"
+ );
+ }
+ let mut params = Map::new();
+ params.insert("confirmation_id".to_owned(), Value::String(String::new()));
+ params.insert("approve".to_owned(), Value::Bool(true));
+ assert_eq!(
+ (controller("confirm").handler)(params).await.unwrap_err(),
+ "confirmation_id is required"
+ );
+ let mut params = Map::new();
+ params.insert("confirmation_id".to_owned(), Value::String("id".to_owned()));
+ params.insert("approve".to_owned(), Value::String("yes".to_owned()));
+ assert_eq!(
+ (controller("confirm").handler)(params).await.unwrap_err(),
+ "approve must be a boolean"
+ );
+}