use crate::cli::CliHandlerRegistration;
use crate::prelude::ApiError;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
fn test_handler_call_handler(
_args: HashMap<String, String>,
) -> Pin<Box<dyn Future<Output = Result<(), ApiError>> + Send + 'static>> {
Box::pin(async { Ok(()) })
}
inventory::submit!(CliHandlerRegistration {
name: "test_handler_call",
handler: test_handler_call_handler,
});
#[tokio::test]
async fn test_handler_registration_call() {
let found = inventory::iter::<CliHandlerRegistration>()
.find(|h| h.name == "test_handler_call")
.expect("test_handler_call registration must be present");
let args = HashMap::new();
let result = (found.handler)(args).await;
assert!(result.is_ok(), "happy-path handler must return Ok(())");
}
fn echo_handler(
args: HashMap<String, String>,
) -> Pin<Box<dyn Future<Output = Result<(), ApiError>> + Send + 'static>> {
Box::pin(async move {
if args.get("input").map(String::as_str) == Some("hello") {
Ok(())
} else {
Err(ApiError::InvalidInput {
message: "expected `input=hello`".into(),
field: Some("input".into()),
value: None,
})
}
})
}
inventory::submit!(CliHandlerRegistration {
name: "echo_handler",
handler: echo_handler,
});
#[tokio::test]
async fn test_handler_registration_reads_args() {
let found = inventory::iter::<CliHandlerRegistration>()
.find(|h| h.name == "echo_handler")
.expect("echo_handler registration must be present");
let mut args = HashMap::new();
args.insert("input".to_string(), "hello".to_string());
let result = (found.handler)(args).await;
assert!(result.is_ok(), "echo_handler with input=hello must succeed");
let empty = HashMap::new();
let result = (found.handler)(empty).await;
assert!(result.is_err(), "echo_handler without input must fail");
match result.unwrap_err() {
ApiError::InvalidInput { field, .. } => {
assert_eq!(field.as_deref(), Some("input"));
}
other => panic!("expected InvalidInput, got {other:?}"),
}
}