use typed_openrpc::{MethodDoc, Registry, RpcMethod};
struct PingMethod;
impl RpcMethod for PingMethod {
const NAME: &'static str = "ping";
const SUMMARY: &'static str = "Return a boolean ping response";
const TAGS: &'static [&'static str] = &["health"];
type Params = ();
type Result = bool;
}
#[test]
fn method_doc_from_trait_populates_metadata() {
let doc = MethodDoc::from::<PingMethod>();
assert_eq!(doc.name, "ping");
assert_eq!(doc.summary, "Return a boolean ping response");
assert_eq!(doc.tags, vec!["health".to_string()]);
assert!(doc.deprecation.is_none());
}
#[test]
fn registry_collects_methods() {
let mut registry = Registry::new();
registry.register_method::<PingMethod>();
let methods = registry.methods();
assert_eq!(methods.len(), 1);
assert_eq!(methods[0].name, "ping");
}
#[test]
fn openrpc_document_includes_registered_method() {
let mut registry = Registry::new();
registry.register(MethodDoc::from::<PingMethod>());
let doc = registry.generate_openrpc_doc();
let methods = doc
.get("methods")
.and_then(|value| value.as_array())
.expect("methods array missing");
assert_eq!(methods.len(), 1);
assert_eq!(
methods[0].get("name").and_then(|v| v.as_str()),
Some("ping")
);
assert_eq!(
methods[0]
.get("tags")
.and_then(|v| v.as_array())
.map(|tags| tags.len()),
Some(1)
);
}