use clap::{Arg, Command};
use brontes::Config;
#[test]
fn generate_tools_returns_expected_tool_set() {
let root = Command::new("testcli")
.subcommand(
Command::new("get")
.about("Get a resource")
.arg(Arg::new("name").required(false)),
)
.subcommand(
Command::new("list")
.about("List resources")
.arg(Arg::new("filter").long("filter")),
);
let tools = brontes::generate_tools(&root, &Config::default())
.expect("generate_tools should succeed on a well-formed tree");
let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();
assert!(
names.contains(&"testcli_get"),
"expected testcli_get in {names:?}"
);
assert!(
names.contains(&"testcli_list"),
"expected testcli_list in {names:?}"
);
for tool in &tools {
let desc = tool
.description
.as_ref()
.unwrap_or_else(|| panic!("tool {:?} missing description", tool.name));
assert!(
!desc.is_empty(),
"tool {:?} has empty description",
tool.name
);
assert!(
!tool.input_schema.is_empty(),
"tool {:?} has empty input_schema",
tool.name
);
}
}
#[test]
fn cmd_paths_to_tool_names() {
let root = Command::new("omctl")
.subcommand_required(true)
.subcommand(Command::new("get").about("Get a resource"))
.subcommand(
Command::new("list")
.subcommand_required(true)
.subcommand(Command::new("all").about("List all")),
)
.subcommand(
Command::new("create").subcommand_required(true).subcommand(
Command::new("this")
.subcommand_required(true)
.subcommand(Command::new("item").about("Create this item")),
),
);
let tools =
brontes::generate_tools(&root, &Config::default()).expect("generate_tools should succeed");
let mut names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();
names.sort_unstable();
let expected = vec!["omctl_create_this_item", "omctl_get", "omctl_list_all"];
assert_eq!(
names, expected,
"tool names must match exactly the leaf paths after prefix + underscore joining"
);
}
#[test]
fn empty_tree_returns_one_root_tool() {
let root = Command::new("testcli");
let tools = brontes::generate_tools(&root, &Config::default())
.expect("generate_tools should succeed on a root-only tree");
assert_eq!(
tools.len(),
1,
"root with no subcommands produces exactly one tool (the root itself)"
);
assert_eq!(tools[0].name.as_ref(), "testcli");
}
#[test]
fn single_leaf_tree_returns_root_and_leaf_tools() {
let root = Command::new("testcli").subcommand(Command::new("hello").about("Say hello"));
let tools =
brontes::generate_tools(&root, &Config::default()).expect("generate_tools should succeed");
let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();
assert!(
names.contains(&"testcli_hello"),
"expected testcli_hello leaf tool in {names:?}"
);
assert_eq!(
tools.len(),
2,
"root + one leaf produces exactly two tools: {names:?}"
);
}