Skip to content

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

test(devices): cover device controller schemas and parsers

Steven Enamakela7b529986ede
diff --git a/crates/openhuman-core/src/core/all_bound_controller_tests.rs b/crates/openhuman-core/src/core/all_bound_controller_tests.rs
index 233371d6e6..0b1f56d7a8 100644
--- a/crates/openhuman-core/src/core/all_bound_controller_tests.rs
+++ b/crates/openhuman-core/src/core/all_bound_controller_tests.rs
@@ -42,6 +42,11 @@ fn staged_bound_methods() -> Vec<&'static str> {
"voice.live_test_provider",
]);
methods.push("threads.upsert");
+ methods.extend([
+ "devices.create_pairing",
+ "devices.list",
+ "devices.revoke",
+ ]);
methods.extend([
"profiles.provision",
"profiles.deprovision",
@@ -686,3 +691,6 @@ mod doctor_controller_tests;
#[path = "all_app_state_controller_tests.rs"]
mod app_state_controller_tests;
+
+#[path = "all_devices_controller_tests.rs"]
+mod devices_controller_tests;
diff --git a/crates/openhuman-core/src/core/all_devices_controller_tests.rs b/crates/openhuman-core/src/core/all_devices_controller_tests.rs
new file mode 100644
index 0000000000..dc97ae80ae
--- /dev/null
+++ b/crates/openhuman-core/src/core/all_devices_controller_tests.rs
@@ -0,0 +1,119 @@
+use super::*;
+use crate::core::TypeSchema;
+
+#[test]
+fn raw_devices_security_registry_matches_its_independent_contract() {
+ let entries: Vec<_> = registry()
+ .iter()
+ .filter(|row| row.controller.schema.namespace == "devices")
+ .collect();
+ let methods = [
+ "devices.create_pairing",
+ "devices.list",
+ "devices.revoke",
+ ];
+ let contracts = crate::security::devices::devices_parameter_contracts();
+
+ assert_eq!(entries.len(), methods.len(), "complete raw devices inventory");
+ assert_eq!(
+ entries
+ .iter()
+ .map(|row| row.controller.schema.method_name())
+ .collect::<Vec<_>>(),
+ methods,
+ "device registration order is a wire contract"
+ );
+ assert!(entries
+ .iter()
+ .all(|row| row.group == DomainGroup::Security));
+ assert!(internal_registry()
+ .iter()
+ .all(|row| row.controller.schema.namespace != "devices"));
+ assert_eq!(
+ entries
+ .iter()
+ .map(|row| row.controller.rpc_method_name())
+ .collect::<Vec<_>>(),
+ [
+ "openhuman.devices_create_pairing",
+ "openhuman.devices_list",
+ "openhuman.devices_revoke"
+ ]
+ );
+
+ let create = &entries[0].controller.schema;
+ assert_eq!(create.inputs.len(), 1);
+ assert_eq!(create.inputs[0].name, "label");
+ assert!(!create.inputs[0].required);
+ assert!(matches!(
+ &create.inputs[0].ty,
+ TypeSchema::Option(inner) if matches!(inner.as_ref(), TypeSchema::String)
+ ));
+ assert_eq!(
+ create
+ .outputs
+ .iter()
+ .map(|field| field.name)
+ .collect::<Vec<_>>(),
+ ["channel_id", "pairing_token", "core_pubkey", "rpc_url", "expires_at"]
+ );
+ assert_eq!(create.outputs.iter().filter(|field| field.required).count(), 4);
+
+ let list = &entries[1].controller.schema;
+ assert!(list.inputs.is_empty());
+ assert_eq!(list.outputs.len(), 1);
+ assert_eq!(list.outputs[0].name, "devices");
+ assert!(list.outputs[0].required);
+ assert!(matches!(
+ &list.outputs[0].ty,
+ TypeSchema::Array(inner) if matches!(inner.as_ref(), TypeSchema::Ref("PairedDevice"))
+ ));
+
+ let revoke = &entries[2].controller.schema;
+ assert_eq!(revoke.inputs.len(), 1);
+ assert_eq!(revoke.inputs[0].name, "channel_id");
+ assert!(revoke.inputs[0].required);
+ assert!(matches!(&revoke.inputs[0].ty, TypeSchema::String));
+ assert_eq!(revoke.outputs.len(), 1);
+ assert_eq!(revoke.outputs[0].name, "success");
+ assert!(revoke.outputs[0].required);
+ assert!(matches!(&revoke.outputs[0].ty, TypeSchema::Bool));
+
+ let entries: Vec<_> = entries
+ .into_iter()
+ .map(|row| row.controller.clone())
+ .collect();
+ crate::core::all::verify_parameter_contracts(&entries, &contracts)
+ .expect("raw devices entries use their declared schemas and runtime decoders");
+}
+
+#[test]
+fn raw_devices_decoders_ignore_extra_fields_and_list_parameters() {
+ use serde_json::json;
+
+ let entries: Vec<_> = registry()
+ .iter()
+ .filter(|row| row.controller.schema.namespace == "devices")
+ .map(|row| row.controller.clone())
+ .collect();
+
+ entries[0]
+ .decode_params(Map::from_iter([
+ ("label".into(), json!(" preserve label ")),
+ ("unknown_scalar".into(), json!(true)),
+ ("unknown_nested".into(), json!({"ignored": [1, null]})),
+ ]))
+ .expect("create_pairing ignores undeclared fields");
+ entries[1]
+ .decode_params(Map::from_iter([
+ ("anything".into(), json!({"ignored": true})),
+ ("label".into(), json!(false)),
+ ]))
+ .expect("list ignores its entire parameter map");
+ entries[2]
+ .decode_params(Map::from_iter([
+ ("channel_id".into(), json!(" raw channel id ")),
+ ("extra".into(), json!(false)),
+ ]))
+ .expect("revoke ignores undeclared fields");
+}
diff --git a/crates/openhuman-core/src/security/devices/schemas_tests.rs b/crates/openhuman-core/src/security/devices/schemas_tests.rs
index f316dd171d..4269bf3f78 100644
--- a/crates/openhuman-core/src/security/devices/schemas_tests.rs
+++ b/crates/openhuman-core/src/security/devices/schemas_tests.rs
@@ -2,9 +2,57 @@ use super::*;
use crate::core::all::{
assert_schema_controller_parity, expect_schema_fields, expect_schema_identity,
expect_schema_input_order, expect_schema_output_fields, expect_schema_output_names,
+ ControllerHandler, ControllerParamContract,
};
use serde_json::json;
+#[test]
+fn legacy_handlers_keep_their_identity_and_order() {
+ let expected: [(&str, ControllerHandler); 3] = [
+ ("create_pairing", handle_create_pairing),
+ ("list", handle_list),
+ ("revoke", handle_revoke),
+ ];
+ let controllers = all_registered_controllers();
+ assert_eq!(controllers.len(), expected.len());
+ for ((function, handler), controller) in expected.iter().zip(&controllers) {
+ assert_eq!(controller.schema.namespace, "devices");
+ assert_eq!(controller.schema.function, *function);
+ assert!(std::ptr::fn_addr_eq(controller.handler, *handler));
+ }
+}
+
+pub(super) fn parameter_contracts() -> Vec<ControllerParamContract> {
+ vec![
+ ControllerParamContract {
+ method: "devices.create_pairing",
+ required: &[],
+ optional: &["label"],
+ examples: vec![
+ json!({}),
+ json!({"label": null}),
+ json!({"label": ""}),
+ json!({"label": " preserve label "}),
+ ],
+ },
+ ControllerParamContract {
+ method: "devices.list",
+ required: &[],
+ optional: &[],
+ examples: vec![json!({})],
+ },
+ ControllerParamContract {
+ method: "devices.revoke",
+ required: &["channel_id"],
+ optional: &[],
+ examples: vec![
+ json!({"channel_id": ""}),
+ json!({"channel_id": " raw channel id "}),
+ ],
+ },
+ ]
+}
+
#[test]
fn device_schema_wire_contracts_are_preserved() {
assert_schema_controller_parity(
@@ -38,22 +86,66 @@ fn device_schema_wire_contracts_are_preserved() {
}
#[test]
-fn read_required_errors_when_key_missing() {
- let params = Map::new();
- let err = read_required::<String>(&params, "channel_id").unwrap_err();
- assert!(err.contains("missing required param 'channel_id'"));
+fn create_pairing_parser_preserves_optional_labels_and_strict_type_errors() {
+ for (params, expected) in [
+ (Map::new(), None),
+ (Map::from_iter([("label".into(), json!(null))]), None),
+ (
+ Map::from_iter([("label".into(), json!(""))]),
+ Some(String::new()),
+ ),
+ (
+ Map::from_iter([("label".into(), json!(" preserve label "))]),
+ Some(" preserve label ".to_owned()),
+ ),
+ ] {
+ assert_eq!(parse_create_pairing(params).unwrap(), expected);
+ }
+
+ for (value, kind) in [
+ (json!(true), "bool"),
+ (json!(7), "number"),
+ (json!([]), "array"),
+ (json!({}), "object"),
+ ] {
+ assert_eq!(
+ parse_create_pairing(Map::from_iter([("label".into(), value)])),
+ Err(format!("invalid 'label': expected string, got {kind}"))
+ );
+ }
}
#[test]
-fn read_optional_string_absent_key_is_none() {
- let result = read_optional_string(&Map::new(), "label").unwrap();
- assert!(result.is_none());
+fn list_parser_ignores_the_entire_parameter_map() {
+ parse_list(Map::from_iter([
+ ("unexpected".into(), json!({"nested": [true, null]})),
+ ("channel_id".into(), json!(false)),
+ ]))
+ .expect("devices.list ignores all parameters");
}
#[test]
-fn read_optional_string_present_value_returned() {
- let mut params = Map::new();
- params.insert("label".into(), json!("iPhone 15"));
- let result = read_optional_string(&params, "label").unwrap();
- assert_eq!(result, Some("iPhone 15".to_string()));
+fn revoke_parser_requires_and_preserves_a_string_channel_id() {
+ assert_eq!(
+ parse_revoke(Map::new()).unwrap_err(),
+ "missing required param 'channel_id'"
+ );
+ for (value, serde_error) in [
+ (json!(null), "invalid type: null, expected a string"),
+ (json!(true), "invalid type: boolean `true`, expected a string"),
+ (json!(7), "invalid type: integer `7`, expected a string"),
+ (json!([]), "invalid type: sequence, expected a string"),
+ (json!({}), "invalid type: map, expected a string"),
+ ] {
+ assert_eq!(
+ parse_revoke(Map::from_iter([("channel_id".into(), value)])),
+ Err(format!("invalid 'channel_id': {serde_error}"))
+ );
+ }
+ for value in ["", "ordinary-channel", " raw channel id "] {
+ assert_eq!(
+ parse_revoke(Map::from_iter([("channel_id".into(), json!(value))])).unwrap(),
+ value
+ );
+ }
}