use serde_json::{Value, json};
use crate::jsonrpc::{ConnectionContext, Router, RpcError};
pub fn register(router: &mut Router) {
router.register("ping", ping);
router.register("health", health);
}
async fn ping(_params: Value, _ctx: ConnectionContext) -> Result<Value, RpcError> {
Ok(json!({"pong": true}))
}
async fn health(_params: Value, _ctx: ConnectionContext) -> Result<Value, RpcError> {
Ok(health_payload())
}
pub(crate) fn health_payload() -> Value {
json!({
"server": "tcode",
"version": crate::VERSION,
"status": "ok",
})
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::sync::mpsc;
fn test_ctx() -> ConnectionContext {
let (tx, _rx) = mpsc::unbounded_channel();
ConnectionContext::new(tx)
}
#[tokio::test]
async fn ping_returns_pong_true() {
let result = ping(Value::Null, test_ctx())
.await
.expect("ping must not fail");
assert_eq!(result, json!({"pong": true}));
}
#[tokio::test]
async fn health_returns_server_identity() {
let result = health(Value::Null, test_ctx())
.await
.expect("health must not fail");
assert_eq!(result["server"], "tcode");
assert_eq!(result["status"], "ok");
assert_eq!(result["version"], crate::VERSION);
}
#[test]
fn health_payload_has_expected_shape() {
let v = health_payload();
assert_eq!(v["server"], "tcode");
assert_eq!(v["status"], "ok");
assert_eq!(v["version"], crate::VERSION);
}
#[tokio::test]
async fn methods_register_wires_ping_and_health() {
use trusty_common::mcp::Request;
let mut router = Router::new();
register(&mut router);
for method in ["ping", "health"] {
let req = Request {
jsonrpc: Some("2.0".to_string()),
id: Some(json!(1)),
method: method.to_string(),
params: None,
};
let resp = router.dispatch(req, &test_ctx()).await;
assert!(
resp.error.is_none(),
"{method} should be registered, got {:?}",
resp.error
);
}
}
}