use help_probe::{
ProbeConfig, discover_all_subcommands, discover_subcommand_hierarchy, probe_command,
};
#[tokio::test]
async fn test_automatic_help_flag_detection() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("ls", &[], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(result.help_flag_detected);
assert!(!result.args.is_empty());
assert!(result.args.iter().any(|arg| {
matches!(
arg.as_str(),
"--help" | "-h" | "help" | "--usage" | "-?" | "/?"
)
}));
assert!(!result.raw_stdout.is_empty() || !result.raw_stderr.is_empty());
}
#[tokio::test]
async fn test_preserves_existing_help_flag() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("ls", &["--help".to_string()], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(result.help_flag_detected);
assert!(result.args.contains(&"--help".to_string()));
assert!(!result.raw_stdout.is_empty() || !result.raw_stderr.is_empty());
}
#[tokio::test]
async fn test_different_help_flag_formats() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("ls", &["-h".to_string()], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(result.help_flag_detected);
assert!(result.args.contains(&"-h".to_string()));
let result = probe_command("ls", &["--usage".to_string()], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(result.help_flag_detected);
}
#[tokio::test]
async fn test_automatic_help_flag_selection() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("cargo", &[], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(result.help_flag_detected);
assert!(!result.raw_stdout.is_empty() || !result.raw_stderr.is_empty());
assert!(
!result.usage_blocks.is_empty()
|| !result.options.is_empty()
|| !result.subcommands.is_empty()
);
}
#[tokio::test]
async fn test_fallback_to_default_help_flag() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("echo", &[], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(!result.args.is_empty());
}
#[tokio::test]
async fn test_automatic_help_flag_with_extra_args() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("git", &["status".to_string()], &config).await;
if let Ok(result) = result {
if result.help_flag_detected {
assert!(!result.args.is_empty());
}
}
}
#[tokio::test]
async fn test_help_text_detection_with_nonzero_exit() {
let config = ProbeConfig {
timeout_secs: 3,
require_help_flag: false,
cache: None,
};
let result = probe_command("ls", &[], &config).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(
result.help_flag_detected || !result.raw_stdout.is_empty() || !result.raw_stderr.is_empty()
);
}
#[tokio::test]
async fn test_help_flag_detection_timeout() {
let config = ProbeConfig {
timeout_secs: 1, require_help_flag: false,
cache: None,
};
let result = probe_command("ls", &[], &config).await;
assert!(result.is_ok() || result.is_err());
}
#[tokio::test]
async fn test_discover_subcommand_hierarchy() {
let config = ProbeConfig {
timeout_secs: 5,
require_help_flag: false,
cache: None,
};
let result = discover_subcommand_hierarchy("cargo", &config, 2).await;
assert!(result.is_ok());
let subcommands = result.unwrap();
assert!(!subcommands.is_empty());
}
#[tokio::test]
async fn test_discover_all_subcommands() {
let config = ProbeConfig {
timeout_secs: 5,
require_help_flag: false,
cache: None,
};
let result = discover_all_subcommands("cargo", &config, Some(2)).await;
assert!(result.is_ok());
let tree = result.unwrap();
assert_eq!(tree.command, "cargo");
assert!(!tree.subcommands.is_empty() || !tree.options.is_empty());
}