Files
openhuman/src/rpc/dispatch.rs
T
CodeGhost21andGitHub ea6895f19d test(coverage): batch 5–8 — Rust unit tests toward 80% for 20 modules (#607)
* test(coverage): batch 5–8 — Rust unit tests toward 80% for 20 modules (#530)

Add 244 new unit tests across 20 modules to push line coverage from
75.06% → 75.70% overall. Modules improved:

- api/socket: 77% → 100%
- rpc/dispatch: 75% → 100%
- config/schema/channels: 77% → 100%
- composio/gmail/sync: 78% → 97%
- composio/notion/sync: 77% → 96%
- webhooks/router: 78% → 96%
- agent/hooks: 78% → 92%
- config/schema/proxy: 76% → 90%
- local_ai/device: 79% → 89%
- text_input/schemas: 73% → 89%
- agent/harness/interrupt: 76% → 88%
- billing/schemas: 79% → 87%
- screen_intelligence/schemas: 78% → 81%
- about_app/types, health/schemas, app_state/schemas,
  core/types, config/schema/identity_cost, cron/scheduler

Tests cover: schema catalog integrity, param deserialization,
serde roundtrips, helper functions, edge cases, error paths.
All 3974 tests pass.

* test(coverage): batch 9–10 — browser_open, update_memory_md, credentials profiles, cost tracker (#530)

Add 53 new tests across 4 more modules:
- browser_open: IPv4/IPv6 private ranges, host matching, normalize_domain edges
- update_memory_md: empty file, section creation, unknown action, param validation
- credentials/profiles: token expiry, CRUD operations, active profile management
- cost/tracker: budget warnings, monthly exceeded, model stats aggregation

All 4027 tests pass.

* test(coverage): batch 11–12 — channels schemas, conversation store (#530)

Add 33 new tests:
- channels/controllers/schemas: per-function input validation, param
  deserialization, helper coverage
- memory/conversations/store: thread lifecycle (create, delete, idempotent),
  multi-thread, empty/nonexistent thread, purge empty store

All 4060 tests pass.
2026-04-16 10:13:33 -07:00

77 lines
2.5 KiB
Rust

//! Main dispatcher for domain-specific RPC methods.
//!
//! This module routes RPC calls to their respective domain handlers (e.g.,
//! security, memory, local AI). It serves as an extension point for
//! domain-level functionality in the OpenHuman platform.
use serde::Serialize;
use crate::rpc::RpcOutcome;
/// Helper to convert an [`RpcOutcome`] into a JSON value compatible with the CLI.
fn rpc_json<T: Serialize>(outcome: RpcOutcome<T>) -> Result<serde_json::Value, String> {
outcome.into_cli_compatible_json()
}
/// Dispatches an RPC method to its domain-specific handler.
///
/// If the method is recognized, it executes the handler and returns the
/// result wrapped in a `Some(Result)`. If not recognized, it returns `None`.
///
/// # Arguments
///
/// * `method` - The name of the RPC method to invoke.
/// * `params` - The parameters for the call as a JSON value.
///
/// # Returns
///
/// `Some(Ok(Value))` if successful, `Some(Err(String))` if the handler failed,
/// or `None` if the method was not found in this dispatcher.
pub async fn try_dispatch(
method: &str,
_params: serde_json::Value,
) -> Option<Result<serde_json::Value, String>> {
match method {
// Core security policy information.
"openhuman.security_policy_info" => Some(rpc_json(
crate::openhuman::security::rpc::security_policy_info(),
)),
_ => None,
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::try_dispatch;
/// Unknown methods must return `None` so callers can fall through.
#[tokio::test]
async fn dispatch_returns_none_for_unknown_method() {
let result = try_dispatch("nonexistent.method", json!({})).await;
assert!(result.is_none(), "unknown methods should return None");
}
#[tokio::test]
async fn dispatch_security_policy_info_returns_some() {
let result = try_dispatch("openhuman.security_policy_info", json!({})).await;
assert!(result.is_some(), "security_policy_info should be handled");
let inner = result.unwrap();
assert!(inner.is_ok(), "security_policy_info should succeed");
}
#[tokio::test]
async fn dispatch_empty_method_returns_none() {
let result = try_dispatch("", json!({})).await;
assert!(result.is_none());
}
#[tokio::test]
async fn dispatch_close_but_wrong_method_returns_none() {
let result = try_dispatch("openhuman.security_policy", json!({})).await;
assert!(result.is_none());
}
}