Repository navigation
Expand file tree
/
Copy pathall_tests.rs
More file actions
2518 lines (2343 loc) · 91.6 KB
/
Copy pathall_tests.rs
File metadata and controls
2518 lines (2343 loc) · 91.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use serde_json::Map;
use super::*;
use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
fn schema(
namespace: &'static str,
function: &'static str,
inputs: Vec<FieldSchema>,
) -> ControllerSchema {
ControllerSchema {
namespace,
function,
description: "test",
inputs,
outputs: vec![],
}
}
fn noop_handler(_params: Map<String, Value>) -> ControllerFuture {
Box::pin(async { Ok(Value::Null) })
}
/// Wrap raw controllers as [`GroupedController`]s (all `Platform`) so the
/// `validate_registry` unit tests — which build hand-made `RegisteredController`
/// lists — can feed the grouped-registry signature (#4796). The group is
/// irrelevant to `validate_registry`, which only inspects `.controller.schema`.
fn grouped(controllers: Vec<RegisteredController>) -> Vec<GroupedController> {
controllers
.into_iter()
.map(|controller| GroupedController {
group: DomainGroup::Platform,
capability: None,
controller,
})
.collect()
}
#[test]
fn validate_registry_rejects_duplicate_namespace_function() {
let declared = vec![schema("dup", "fn", vec![]), schema("dup", "fn", vec![])];
let registered = vec![
RegisteredController {
schema: declared[0].clone(),
handler: noop_handler,
},
RegisteredController {
schema: declared[1].clone(),
handler: noop_handler,
},
];
let err = validate_registry(&grouped(registered)).expect_err("expected duplicate error");
assert!(err.contains("duplicate registered controller `dup.fn`"));
}
#[test]
fn validate_registry_rejects_duplicate_required_inputs() {
let declared = vec![schema(
"doctor",
"models",
vec![
FieldSchema {
name: "use_cache",
ty: TypeSchema::Bool,
comment: "x",
required: true,
},
FieldSchema {
name: "use_cache",
ty: TypeSchema::Bool,
comment: "x",
required: true,
},
],
)];
let registered = vec![RegisteredController {
schema: declared[0].clone(),
handler: noop_handler,
}];
let err = validate_registry(&grouped(registered)).expect_err("expected duplicate input");
assert!(err.contains("duplicate required input `use_cache` in `doctor.models`"));
}
#[test]
fn validate_registry_accepts_valid_registry() {
let declared = vec![
schema("ns1", "fn1", vec![]),
schema("ns1", "fn2", vec![]),
schema("ns2", "fn1", vec![]),
];
let registered = declared
.iter()
.map(|s| RegisteredController {
schema: s.clone(),
handler: noop_handler,
})
.collect::<Vec<_>>();
assert!(validate_registry(&grouped(registered)).is_ok());
}
#[test]
fn rpc_method_name_formats_correctly() {
let s = schema("memory", "doc_put", vec![]);
assert_eq!(rpc_method_name(&s), "openhuman.memory_doc_put");
}
#[test]
fn registered_controller_rpc_method_name() {
let s = schema("billing", "get_balance", vec![]);
let rc = RegisteredController {
schema: s,
handler: noop_handler,
};
assert_eq!(rc.rpc_method_name(), "openhuman.billing_get_balance");
}
#[test]
fn namespace_description_known_namespaces() {
assert!(namespace_description("memory").is_some());
assert!(namespace_description("memory_tree").is_some());
assert!(namespace_description("billing").is_some());
assert!(namespace_description("config").is_some());
assert!(namespace_description("health").is_some());
assert!(namespace_description("subsystems").is_some());
assert!(namespace_description("security").is_some());
assert!(namespace_description("tool_registry").is_some());
assert!(namespace_description("voice").is_some());
assert!(namespace_description("webhooks").is_some());
assert!(namespace_description("notification").is_some());
}
#[test]
fn namespace_description_unknown_returns_none() {
assert!(namespace_description("nonexistent_xyz").is_none());
}
#[test]
fn validate_params_accepts_valid_params() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "key",
ty: TypeSchema::String,
comment: "a key",
required: true,
}],
);
let mut params = Map::new();
params.insert("key".into(), Value::String("value".into()));
assert!(validate_params(&s, ¶ms).is_ok());
}
#[test]
fn validate_params_rejects_missing_required() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "key",
ty: TypeSchema::String,
comment: "a key",
required: true,
}],
);
let params = Map::new();
let err = validate_params(&s, ¶ms).unwrap_err();
assert!(err.contains("missing required param 'key'"));
}
#[test]
fn validate_params_rejects_unknown_param() {
let s = schema("test", "fn", vec![]);
let mut params = Map::new();
params.insert("unknown".into(), Value::Null);
let err = validate_params(&s, ¶ms).unwrap_err();
assert!(err.contains("unknown param 'unknown'"));
}
#[test]
fn validate_params_accepts_empty_for_no_required() {
let s = schema("test", "fn", vec![]);
assert!(validate_params(&s, &Map::new()).is_ok());
}
#[test]
fn all_registered_controllers_is_nonempty() {
let controllers = all_registered_controllers();
assert!(
controllers.len() > 50,
"expected many controllers, got {}",
controllers.len()
);
}
#[test]
fn all_controller_schemas_matches_registered_count() {
let schemas = all_controller_schemas();
let controllers = all_registered_controllers();
assert_eq!(schemas.len(), controllers.len());
}
/// With the `voice` feature on (the default), the voice + audio_toolkit
/// controllers are compiled in and registered — the desktop build is
/// byte-identical.
#[test]
#[cfg(feature = "voice")]
fn voice_and_audio_controllers_registered_when_feature_on() {
let schemas = all_controller_schemas();
assert!(
schemas.iter().any(|s| s.namespace == "voice"),
"voice controllers must be registered when the `voice` feature is on"
);
assert!(
schemas.iter().any(|s| s.namespace == "audio_toolkit"),
"audio_toolkit controllers must be registered when the `voice` feature is on"
);
}
/// With the `voice` feature off, both domains are compiled out: their
/// controllers never enter the registry, so voice/audio RPC methods are
/// unknown-method and absent from `/schema`. This is the compile-time
/// stub-facade correctness gate (see `openhuman::voice::stub`).
#[test]
#[cfg(not(feature = "voice"))]
fn voice_and_audio_controllers_absent_when_feature_off() {
let schemas = all_controller_schemas();
assert!(
!schemas
.iter()
.any(|s| s.name
10000
space == "voice" || s.namespace == "audio_toolkit"),
"voice/audio_toolkit controllers must be compiled out when the `voice` feature is off"
);
}
/// With the `inference` feature on (the default), the `cpal` audio-device stack
/// is compiled in — `INFERENCE_COMPILED_IN` reflects that, and the
/// microphone-permission probe can actually inspect a device (dependency shed
/// proven separately by `cargo tree -i cpal`).
#[test]
#[cfg(feature = "inference")]
fn inference_engine_compiled_in_when_feature_on() {
assert!(crate::openhuman::inference::INFERENCE_COMPILED_IN);
}
/// With the `inference` feature off, the marker flips and `cpal` leaves the
/// dependency graph. The observable effect is the microphone-permission probe:
/// it reports `Unknown` rather than a real verdict, because there is no
/// audio-device API compiled in to ask. Speech-to-text is unaffected in either
/// direction — it is a hosted HTTP call now, not an in-process engine.
#[test]
#[cfg(not(feature = "inference"))]
fn inference_engine_compiled_out_when_feature_off() {
use crate::openhuman::desktop::accessibility::{detect_microphone_permission, PermissionState};
assert!(!crate::openhuman::inference::INFERENCE_COMPILED_IN);
assert_eq!(
detect_microphone_permission(),
PermissionState::Unknown,
"without `inference` there is no audio-device API to probe"
);
}
/// With the `skills` feature on (the default), all three skill domains are
/// compiled in and registered — the desktop build is byte-identical.
#[test]
#[cfg(feature = "skills")]
fn skill_controllers_registered_when_feature_on() {
let schemas = all_controller_schemas();
for ns in ["skills", "skill_runtime", "skill_registry"] {
assert!(
schemas.iter().any(|s| s.namespace == ns),
"`{ns}` controllers must be registered when the `skills` feature is on"
);
}
}
/// With the `skills` feature off, all three domains are compiled out: their
/// controllers never enter the registry, so skills RPC methods are
/// unknown-method and absent from `/schema`. This is the compile-time
/// stub-facade correctness gate (see `openhuman::skills::stub`).
///
/// Note this does NOT cover `skills::types` / `skills::ops_types`: those stay
/// compiled in both directions (the type carve-out — `tools::traits` re-exports
/// `ToolResult`/`ToolContent` out of them), but they expose no controllers, so
/// the namespaces are absent either way.
#[test]
#[cfg(not(feature = "skills"))]
fn skill_controllers_absent_when_feature_off() {
let schemas = all_controller_schemas();
assert!(
!schemas.iter().any(|s| s.namespace == "skills"
|| s.namespace == "skill_runtime"
|| s.namespace == "skill_registry"),
"skills/skill_runtime/skill_registry controllers must be compiled out \
when the `skills` feature is off"
);
}
/// With the `web3` feature on (the default), the wallet + web3 + x402
/// controllers are compiled in and registered, and the high-level web3 agent
/// tools (swap/bridge/dapp) are present — the desktop build is byte-identical.
#[test]
#[cfg(feature = "web3")]
fn wallet_web3_x402_controllers_registered_when_feature_on() {
let schemas = all_controller_schemas();
assert!(
schemas.iter().any(|s| s.namespace == "wallet"),
"wallet controllers must be registered when the `web3` feature is on"
);
assert!(
schemas.iter().any(|s| s.namespace.starts_with("web3_")),
"web3 (swap/bridge/dapp) controllers must be registered when the `web3` feature is on"
);
assert!(
schemas.iter().any(|s| s.namespace == "x402"),
"x402 controllers must be registered when the `web3` feature is on"
);
assert!(
!crate::openhuman::web3::all_web3_agent_tools().is_empty(),
"web3 agent tools must be present when the `web3` feature is on"
);
}
/// With the `web3` feature off, all three domains are compiled out: their
/// controllers never enter the registry (wallet/web3/x402 RPC methods are
/// unknown-method and absent from `/schema`) and the web3 agent tools are
/// gone. This is the compile-time stub-facade correctness gate (see
/// `openhuman::web3::{self,wallet,x402}::stub`).
#[test]
#[cfg(not(feature = "web3"))]
fn wallet_web3_x402_controllers_absent_when_feature_off() {
let schemas = all_controller_schemas();
assert!(
!schemas.iter().any(|s| s.namespace == "wallet"
|| s.namespace.starts_with("web3_")
|| s.namespace == "x402"),
"wallet/web3/x402 controllers must be compiled out when the `web3` feature is off"
);
assert!(
crate::openhuman::web3::all_web3_agent_tools().is_empty(),
"web3 agent tools must be gone when the `web3` feature is off"
);
}
#[test]
fn schema_for_rpc_method_finds_known_method() {
let schema = schema_for_rpc_method("openhuman.health_snapshot");
assert!(schema.is_some(), "health.snapshot should be findable");
let s = schema.unwrap();
assert_eq!(s.namespace, "health");
assert_eq!(s.function, "snapshot");
}
#[test]
fn schema_for_rpc_method_finds_security_policy_info() {
let schema = schema_for_rpc_method("openhuman.security_policy_info");
assert!(schema.is_some(), "security.policy_info should be findable");
let s = schema.unwrap();
assert_eq!(s.namespace, "security");
assert_eq!(s.function, "policy_info");
}
#[test]
#[cfg(feature = "mcp")]
fn schema_for_rpc_method_finds_internal_mcp_audit_list() {
let schema = schema_for_rpc_method("openhuman.mcp_audit_list");
assert!(
schema.is_some(),
"mcp_audit.list should be internally routable"
);
let s = schema.unwrap();
assert_eq!(s.namespace, "mcp_audit");
assert_eq!(s.function, "list");
}
#[test]
fn schema_for_rpc_method_finds_internal_orchestration_pairing_link_session() {
let schema = schema_for_rpc_method("openhuman.orchestration_pairing_link_session");
assert!(
schema.is_some(),
"orchestration_pairing.link_session should be internally routable"
);
let s = schema.unwrap();
assert_eq!(s.namespace, "orchestration_pairing");
assert_eq!(s.function, "link_session");
}
#[test]
fn rpc_method_from_parts_does_not_expose_internal_mcp_audit_list() {
assert!(
rpc_method_from_parts("mcp_audit", "list").is_none(),
"internal MCP audit RPC must not appear in the public controller registry"
);
}
#[test]
fn rpc_method_from_parts_does_not_expose_internal_orchestration_pairing() {
assert!(
rpc_method_from_parts("orchestration_pairing", "link_session").is_none(),
"pairing write RPCs must not appear in the public controller registry"
);
}
#[test]
fn schema_for_rpc_method_returns_none_for_unknown() {
assert!(schema_for_rpc_method("openhuman.nonexistent_method_xyz").is_none());
}
#[test]
fn rpc_method_from_parts_finds_known() {
let method = rpc_method_from_parts("health", "snapshot");
assert_eq!(method.as_deref(), Some("openhuman.health_snapshot"));
}
#[test]
fn rpc_method_from_parts_returns_none_for_unknown() {
assert!(rpc_method_from_parts("fake", "method").is_none());
}
#[test]
fn no_duplicate_rpc_methods_in_registry() {
let controllers = all_registered_controllers();
let mut methods: Vec<String> = controllers.iter().map(|c| c.rpc_method_name()).collect();
let original_len = methods.len();
methods.sort();
methods.dedup();
assert_eq!(
methods.len(),
original_len,
"duplicate RPC methods found in registry"
);
}
// --- validate_params edge cases -----------------------------------------
#[test]
fn validate_params_accepts_missing_optional_param() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "filter",
ty: TypeSchema::String,
comment: "optional filter",
required: false,
}],
);
assert!(validate_params(&s, &Map::new()).is_ok());
}
#[test]
fn validate_params_accepts_optional_param_when_present() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "filter",
ty: TypeSchema::String,
comment: "",
required: false,
}],
);
let mut p = Map::new();
p.insert("filter".into(), Value::String("abc".into()));
assert!(validate_params(&s, &p).is_ok());
}
#[test]
fn validate_params_missing_required_error_includes_comment() {
// The comment text helps callers (esp. the CLI/UI) understand what
// the missing field is for — lock this in so error messages don't
// regress to bare field names.
let s = schema(
"memory",
"doc_put",
vec![FieldSchema {
name: "namespace",
ty: TypeSchema::String,
comment: "namespace to write into",
required: true,
}],
);
let err = validate_params(&s, &Map::new()).unwrap_err();
assert!(err.contains("missing required param 'namespace'"));
assert!(err.contains("namespace to write into"));
}
#[test]
fn validate_params_unknown_error_includes_namespace_and_function() {
let s = schema("billing", "top_up", vec![]);
let mut p = Map::new();
p.insert("typo".into(), Value::Null);
let err = validate_params(&s, &p).unwrap_err();
assert!(err.contains("unknown param 'typo'"));
assert!(err.contains("billing.top_up"));
}
#[test]
fn validate_params_reports_missing_required_before_unknown() {
// If a call both omits a required param AND has an unknown one,
// the missing-required error fires first (it's strictly more
// actionable for callers).
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "key",
ty: TypeSchema::String,
comment: "",
required: true,
}],
);
let mut p = Map::new();
p.insert("unknown".into(), Value::Null);
let err = validate_params(&s, &p).unwrap_err();
assert!(err.contains("missing required param 'key'"), "got: {err}");
}
#[test]
fn validate_params_null_for_required_is_acceptable() {
// JSON-RPC semantics: `null` is a valid value for an optional field
// sent explicitly. For a required field, presence (not value) is
// what we check — null does satisfy the "key present" check.
// Handlers enforce stronger type contracts downstream.
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "key",
ty: TypeSchema::String,
comment: "",
required: true,
}],
);
let mut p = Map::new();
p.insert("key".into(), Value::Null);
assert!(validate_params(&s, &p).is_ok());
}
// --- validate_params type checking (C12) --------------------------------
#[test]
fn validate_params_rejects_wrong_scalar_type() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "count",
ty: TypeSchema::U64,
comment: "",
required: true,
}],
);
let mut p = Map::new();
p.insert("count".into(), Value::String("nope".into()));
let err = validate_params(&s, &p).unwrap_err();
assert!(err.contains("invalid type for param 'count'"), "got: {err}");
assert!(err.contains("expected unsigned integer"), "got: {err}");
}
#[test]
fn validate_params_accepts_correct_scalar_type() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "flag",
ty: TypeSchema::Bool,
comment: "",
required: true,
}],
);
let mut p = Map::new();
p.insert("flag".into(), Value::Bool(true));
assert!(validate_params(&s, &p).is_ok());
}
#[test]
fn validate_params_validates_array_element_types() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "ids",
ty: TypeSchema::Array(Box::new(TypeSchema::String)),
comment: "",
required: true,
}],
);
let mut ok = Map::new();
ok.insert(
"ids".into(),
Value::Array(vec![Value::String("a".into()), Value::String("b".into())]),
);
assert!(validate_params(&s, &ok).is_ok());
let mut bad = Map::new();
bad.insert(
"ids".into(),
Value::Array(vec![Value::String("a".into()), Value::Bool(true)]),
);
let err = validate_params(&s, &bad).unwrap_err();
assert!(err.contains("invalid type for param 'ids'"), "got: {err}");
}
#[test]
fn validate_params_enforces_enum_variants() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "mode",
ty: TypeSchema::Enum {
variants: vec!["read", "write"],
},
comment: "",
required: true,
}],
);
let mut ok = Map::new();
ok.insert("mode".into(), Value::String("read".into()));
assert!(validate_params(&s, &ok).is_ok());
let mut bad = Map::new();
bad.insert("mode".into(), Value::String("delete".into()));
let err = validate_params(&s, &bad).unwrap_err();
assert!(err.contains("enum variants"), "got: {err}");
}
#[test]
fn validate_params_option_accepts_null_and_inner_type() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "limit",
ty: TypeSchema::Option(Box::new(TypeSchema::U64)),
comment: "",
required: false,
}],
);
let mut null_p = Map::new();
null_p.insert("limit".into(), Value::Null);
assert!(validate_params(&s, &null_p).is_ok());
let mut val_p = Map::new();
val_p.insert("limit".into(), Value::Number(5.into()));
assert!(validate_params(&s, &val_p).is_ok());
let mut bad_p = Map::new();
bad_p.insert("limit".into(), Value::String("x".into()));
assert!(validate_params(&s, &bad_p).is_err());
}
#[test]
fn validate_params_json_type_accepts_anything() {
let s = schema(
"test",
"fn",
vec![FieldSchema {
name: "payload",
ty: TypeSchema::Json,
comment: "",
required: true,
}],
);
let mut p = Map::new();
p.insert("payload".into(), Value::Array(vec![Value::Bool(true)]));
assert!(validate_params(&s, &p).is_ok());
}
// --- validate_registry edge cases ---------------------------------------
#[test]
fn validate_registry_rejects_empty_namespace() {
let declared = vec![schema("", "fn", vec![])];
let registered = vec![RegisteredController {
schema: declared[0].clone(),
handler: noop_handler,
}];
let err = validate_registry(&grouped(registered)).unwrap_err();
assert!(err.contains("namespace must not be empty"));
}
#[test]
fn validate_registry_rejects_empty_function() {
let declared = vec![schema("ns", "", vec![])];
let registered = vec![RegisteredController {
schema: declared[0].clone(),
handler: noop_handler,
}];
let err = validate_registry(&grouped(registered)).unwrap_err();
assert!(err.contains("function must not be empty"));
}
#[test]
fn validate_registry_rejects_whitespace_only_namespace() {
// `trim().is_empty()` is the invariant — a namespace of " " must
// be rejected to prevent `openhuman. _fn` nonsense RPC method names.
let declared = vec![schema(" ", "fn", vec![])];
let registered = vec![RegisteredController {
schema: declared[0].clone(),
handler: noop_handler,
}];
let err = validate_registry(&grouped(registered)).unwrap_err();
assert!(err.contains("namespace must not be empty"));
}
// Note: the previous `declared_without_registered` / `registered_without_declared`
// drift tests were removed with the registry collapse (Phase 2) — schemas are now
// derived from the registered controllers, so the two lists cannot drift.
#[test]
fn validate_registry_rejects_duplicate_registered_controllers() {
let s = schema("a", "b", vec![]);
let registered = vec![
RegisteredController {
schema: s.clone(),
handler: noop_handler,
},
RegisteredController {
schema: s,
handler: noop_handler,
},
];
let err = validate_registry(&grouped(registered)).unwrap_err();
assert!(err.contains("duplicate registered controller `a.b`"));
}
// --- try_invoke_registered_rpc routing ---------------------------------
#[tokio::test]
async fn try_invoke_registered_rpc_returns_none_for_unknown_method() {
let out = try_invoke_registered_rpc("openhuman.not_a_real_method_xyz_123", Map::new()).await;
assert!(out.is_none(), "unknown methods must return None");
}
#[tokio::test]
async fn try_invoke_registered_rpc_returns_some_for_known_method() {
// `openhuman.health_snapshot` is registered at startup and takes no
// required params — it must route and produce Some(_).
let out = try_invoke_registered_rpc("openhuman.health_snapshot", Map::new()).await;
assert!(out.is_some(), "known method must route");
}
#[tokio::test]
async fn try_invoke_registered_rpc_routes_security_policy_info() {
let out = try_invoke_registered_rpc("openhuman.security_policy_info", Map::new())
.await
.expect("security policy info should be registered")
.expect("security policy info should succeed");
assert!(
out.get("result").is_some() || out.get("autonomy").is_some(),
"security policy info should return policy payload: {out}"
);
}
#[test]
fn rpc_method_name_handles_multi_underscore_function() {
// Functions often contain underscores — the RPC method name must
// preserve them verbatim, separated from the namespace with `_`.
let s = schema("team", "change_member_role", vec![]);
assert_eq!(rpc_method_name(&s), "openhuman.team_change_member_role");
}
#[test]
fn every_registered_controller_has_matching_declared_schema() {
// Global invariant: the registry is consistent by construction.
// This test re-asserts the contract to catch drift.
use std::collections::BTreeSet;
let registered: BTreeSet<String> = all_registered_controllers()
.into_iter()
.map(|c| format!("{}.{}", c.schema.namespace, c.schema.function))
.collect();
let declared: BTreeSet<String> = all_controller_schemas()
.into_iter()
.map(|s| format!("{}.{}", s.namespace, s.function))
.collect();
assert_eq!(
registered, declared,
"registry/schema sets must be identical"
);
}
// --- DomainSet registration filter (#4796) ------------------------------
use crate::core::runtime::context::CoreContext;
use crate::core::runtime::DomainSet;
/// The [`DomainGroup`] a registered controller (agent-facing OR internal) is
/// tagged with, looked up by its namespace. Test-only helper over the private
/// grouped registry.
fn group_for_namespace(ns: &str) -> Option<DomainGroup> {
registry()
.iter()
.chain(internal_registry().iter())
.find(|g| g.controller.schema.namespace == ns)
.map(|g| g.group)
}
#[test]
fn subsystems_namespace_is_registered_under_platform() {
assert_eq!(
group_for_namespace("subsystems"),
Some(DomainGroup::Platform)
);
}
#[test]
fn full_registration_is_byte_identical() {
// With no ambient CoreContext (⇒ full, no filter), the public
// `all_registered_controllers()` must equal the raw grouped registry — same
// length AND same rpc-method-name sequence IN ORDER. This is the DoD (1)
// proof that wrapping every entry in a `GroupedController` + filtering by the
// ambient DomainSet changes neither the membership nor the ordering of the
// full() surface.
//
// The baseline is the raw `registry()` view rather than a checked-in method
// snapshot (a #4808 review suggestion): `all_registered_controllers()` and
// `registry()` are DIFFERENT code paths — the former exercises the ambient
// filter (`group_allowed`) and re-collects, the latter is the unfiltered
// source — so this asserts the filter is an order-preserving identity under
// full(). A frozen snapshot would instead ossify the controller list and
// force churn on every legitimate new controller; git history is the
// authoritative pre-#4796 baseline for "did the raw list itself change".
let filtered_methods: Vec<String> = all_registered_controllers()
.iter()
.map(|c| c.rpc_method_name())
.collect();
let raw_methods: Vec<String> = registry()
.iter()
.map(|g| g.controller.rpc_method_name())
.collect();
assert_eq!(
filtered_methods.len(),
raw_methods.len(),
"unfiltered all_registered_controllers() must equal raw registry length"
);
// Ordered comparison — NOT sorted. A reordering (or a drop/add) under full()
// would change dispatch/schema iteration order and must fail here.
assert_eq!(
filtered_methods, raw_methods,
"unfiltered rpc-method sequence must be byte-identical (order + membership) to the raw registry"
);
}
#[tokio::test]
async fn harness_excludes_gated_namespaces() {
use std::collections::BTreeSet;
// Baseline (full, no scope) — every family present.
let full_ns: BTreeSet<&str> = all_controller_schemas()
.iter()
.map(|s| s.namespace)
.collect();
#[cfg(feature = "flows")]
assert!(full_ns.contains("flows"), "full() must expose flows");
// `voice` was the pathfinder gate (#4803) and predates this per-assert cfg
// convention; gate it like its siblings so the disabled build passes (#5022).
#[cfg(feature = "voice")]
assert!(full_ns.contains("voice"), "full() must expose voice");
#[cfg(feature = "channels")]
assert!(full_ns.contains("channels"), "full() must expose channels");
let ctx = CoreContext::for_test(DomainSet::harness(), None, None);
let harness_ns: BTreeSet<&'static str> =
CoreContext::scope(ctx, async { all_controller_schemas() })
.await
.iter()
.map(|s| s.namespace)
.collect();
// Harness families remain.
for present in ["memory", "threads", "config", "security", "agent"] {
assert!(
harness_ns.contains(present),
"harness() must keep the `{present}` namespace"
);
}
// Gate families + platform-only namespaces are gone.
for absent in [
"flows",
"voice",
"skills",
"wallet",
"meet",
"channels",
"mcp_clients",
"health",
// The subsystem status surface is Platform-tagged for the same reason
// `health` is: it is kernel operator surface with no family. An
// embedded harness host reads driver capabilities through
// `memory.provider_status`, which stays reachable.
"subsystems",
] {
assert!(
!harness_ns.contains(absent),
"harness() must omit the gated/platform `{absent}` namespace"
);
}
assert!(
harness_ns.len() < full_ns.len(),
"harness() must expose strictly fewer namespaces than full()"
);
}
// Uses a `flows.*` method as its gated-family vehicle, so the whole test is
// `#[cfg(feature = "flows")]`: without the feature there is no flows controller
// in the registry at all and the `.expect()` below would panic. The runtime
// gating this proves is orthogonal to the compile-time gate, and CI runs the
// test suite on default features (flows ON), so no coverage is lost there.
#[cfg(feature = "flows")]
#[tokio::test]
async fn dispatch_returns_none_for_gated_method() {
// A method whose group is gated OFF under the ambient DomainSet must
// dispatch as an unknown method (None) — indistinguishable from absent.
let gated_method = all_registered_controllers()
.into_iter()
.find(|c| c.schema.namespace == "flows")
.map(|c| c.rpc_method_name())
.expect("a flows.* method exists in the full registry");
let ctx = CoreContext::for_test(DomainSet::harness(), None, None);
let out = CoreContext::scope(ctx, try_invoke_registered_rpc(&gated_method, Map::new())).await;
assert!(
out.is_none(),
"gated method `{gated_method}` must dispatch as None under harness()"
);
// A harness-family method still routes (Some) — security.policy_info needs
// no workspace, so it is a clean positive control.
let ctx = CoreContext::for_test(DomainSet::harness(), None, None);
let out = CoreContext::scope(
ctx,
try_invoke_registered_rpc("openhuman.security_policy_info", Map::new()),
)
.await;
assert!(
out.is_some(),
"harness-family security.policy_info must still route under harness()"
);
}
// Same flows-vehicle reasoning as `dispatch_returns_none_for_gated_method`.
#[cfg(feature = "flows")]
#[tokio::test]
async fn schema_lookup_is_gated_in_lockstep_with_dispatch() {
// #4808 review: `schema_for_rpc_method` must gate identically to
// `try_invoke_registered_rpc`, otherwise `invoke_method_inner` validates a
// gated method's params BEFORE the dispatch gate fires — returning the
// controller's validation error instead of method-not-found and leaking the
// hidden RPC surface. Prove the schema lookup returns None for a gated
// method under harness() (so no validation runs) while a harness-family
// method still resolves.
let gated_method = all_registered_controllers()
.into_iter()
.find(|c| c.schema.namespace == "flows")
.map(|c| c.rpc_method_name())
.expect("a flows.* method exists in the full registry");
// Full (no scope): the gated method's schema IS visible — proves the None
// below is the gate, not a missing method.
assert!(
schema_for_rpc_method(&gated_method).is_some(),
"under full() the schema for `{gated_method}` must resolve"
);
let ctx = CoreContext::for_test(DomainSet::harness(), None, None);
let gated_schema =
CoreContext::scope(ctx, async { schema_for_rpc_method(&gated_method) }).await;
assert!(
gated_schema.is_none(),
"schema lookup for gated `{gated_method}` must be None under harness() (no param validation, no surface leak)"
);
let ctx = CoreContext::for_test(DomainSet::harness(), None, None);
let kept_schema = CoreContext::scope(ctx, async {
schema_for_rpc_method("openhuman.security_policy_info")
})
.await;
assert!(
kept_schema.is_some(),
"harness-family security.policy_info schema must still resolve under harness()"
);
}
#[test]
fn group_mapping_smoke() {
// Representative controller from each harness family maps to its group…
assert_eq!(group_for_namespace("memory"), Some(DomainGroup::Memory));
assert_eq!(group_for_namespace("threads"), Some(DomainGroup::Threads));
assert_eq!(group_for_namespace("config"), Some(DomainGroup::Config));
assert_eq!(group_for_namespace("security"), Some(DomainGroup::Security));
assert_eq!(group_for_namespace("agent"), Some(DomainGroup::Agent));
assert_eq!(group_for_namespace("plan_review"), Some(DomainGroup::Agent));
// …and a representative gated one maps to its gate group. `group_for_namespace`