#![cfg(feature = "cli")]
use sdforge::cli::CliBuilder;
use sdforge::core::ApiError;
use sdforge::service_api;
#[service_api(
name = "cli_test_echo",
version = "1.0",
description = "Echo a greeting",
cli = true
)]
async fn cli_test_echo(name: String) -> Result<String, ApiError> {
Ok(format!("Hello, {}!", name))
}
#[service_api(name = "cli_test_ping", version = "1.0", cli = true)]
async fn cli_test_ping() -> Result<String, ApiError> {
Ok("pong".to_string())
}
#[test]
fn test_cli_builder_includes_echo_subcommand() {
let cmd = CliBuilder::new().build();
let echo = cmd
.find_subcommand("cli_test_echo")
.expect("cli_test_echo must be a subcommand after build()");
let about = echo.get_about().map(|s| s.to_string()).unwrap_or_default();
assert!(
about.contains("Echo a greeting"),
"echo subcommand about must contain description, got: {}",
about
);
}
#[test]
fn test_render_help_contains_command_names() {
let mut cmd = CliBuilder::new().build();
let help = cmd.render_help().to_string();
assert!(
help.contains("cli_test_echo"),
"help output must contain 'cli_test_echo' subcommand: {}",
help
);
assert!(
help.contains("cli_test_ping"),
"help output must contain 'cli_test_ping' subcommand: {}",
help
);
}
#[test]
fn test_cli_ping_registered_with_version() {
let cmd = CliBuilder::new().build();
let ping = cmd
.find_subcommand("cli_test_ping")
.expect("cli_test_ping must be a subcommand");
assert_eq!(
ping.get_version(),
Some("1.0"),
"cli_test_ping version must be '1.0'"
);
}
#[test]
fn test_echo_subcommand_has_name_option() {
let cmd = CliBuilder::new().build();
let echo = cmd
.find_subcommand("cli_test_echo")
.expect("cli_test_echo must be a subcommand");
let name_arg = echo
.get_arguments()
.find(|a| a.get_id().as_str() == "name")
.expect("echo must have a `name` argument");
assert!(
name_arg.get_long().is_some(),
"name arg must have a --long flag (Body → option)"
);
assert!(
name_arg.is_required_set(),
"name arg must be required (String, not Option)"
);
}