#[cfg(test)]
mod cli_tests {
use crate::cli::Cli;
use clap::Parser;
#[test]
fn test_cli_parsing() {
let cli = Cli::try_parse_from(["agpm", "--help"]);
assert!(cli.is_err());
let cli = Cli::try_parse_from(["agpm", "list"]);
assert!(cli.is_ok());
}
#[test]
fn test_cli_verbose_flag() {
let cli = Cli::try_parse_from(["agpm", "--verbose", "list"]);
assert!(cli.is_ok());
let cli = cli.unwrap();
assert!(cli.verbose);
}
#[test]
fn test_cli_quiet_flag() {
let cli = Cli::try_parse_from(["agpm", "--quiet", "list"]);
assert!(cli.is_ok());
let cli = cli.unwrap();
assert!(cli.quiet);
}
#[test]
fn test_cli_no_progress_flag() {
let cli = Cli::try_parse_from(["agpm", "--no-progress", "list"]);
assert!(cli.is_ok());
let cli = cli.unwrap();
assert!(cli.no_progress);
}
#[test]
fn test_cli_config_option() {
let cli = Cli::try_parse_from(["agpm", "--config", "/path/to/config", "list"]);
assert!(cli.is_ok());
let cli = cli.unwrap();
assert_eq!(cli.config, Some("/path/to/config".to_string()));
}
#[test]
fn test_cli_all_commands() {
let commands = vec![
vec!["agpm", "init"],
vec!["agpm", "add", "source", "test", "https://github.com/test/repo.git"],
vec!["agpm", "install"],
vec!["agpm", "update"],
vec!["agpm", "list"],
vec!["agpm", "validate"],
vec!["agpm", "cache", "info"],
vec!["agpm", "config", "show"],
];
for cmd in commands {
let result = Cli::try_parse_from(cmd.clone());
assert!(result.is_ok(), "Failed to parse: {cmd:?}");
}
}
#[tokio::test]
async fn test_cli_execute_with_flags() {
use crate::cli::CliConfig;
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().canonicalize().unwrap();
let manifest_path = temp_path.join("agpm.toml");
std::fs::write(&manifest_path, "[sources]\n").unwrap();
assert!(manifest_path.exists(), "Manifest file was not created");
let cli = Cli::try_parse_from(["agpm", "--verbose", "config", "path"]).unwrap();
assert!(cli.verbose);
let config = cli.build_config();
assert_eq!(config.log_level, Some("debug".to_string()));
assert!(!config.no_progress);
let test_config = CliConfig::new(); let result = cli.execute_with_config(test_config).await;
assert!(result.is_ok(), "Failed to execute with verbose flag");
let cli = Cli::try_parse_from(["agpm", "--quiet", "config", "path"]).unwrap();
assert!(cli.quiet);
let config = cli.build_config();
assert_eq!(config.log_level, None);
let test_config = CliConfig::new();
let result = cli.execute_with_config(test_config).await;
assert!(result.is_ok(), "Failed to execute with quiet flag");
let cli = Cli::try_parse_from(["agpm", "--no-progress", "config", "path"]).unwrap();
assert!(cli.no_progress);
let config = cli.build_config();
assert!(config.no_progress);
assert_eq!(config.log_level, Some("info".to_string()));
let test_config = CliConfig::new();
let result = cli.execute_with_config(test_config).await;
assert!(result.is_ok(), "Failed to execute with no-progress flag");
let cli =
Cli::try_parse_from(["agpm", "--verbose", "--no-progress", "config", "path"]).unwrap();
assert!(cli.verbose);
assert!(cli.no_progress);
let config = cli.build_config();
assert_eq!(config.log_level, Some("debug".to_string()));
assert!(config.no_progress);
let test_config = CliConfig::new();
let result = cli.execute_with_config(test_config).await;
assert!(result.is_ok(), "Failed to execute with combined flags");
}
#[test]
fn test_cli_config_builder() {
let cli = Cli::try_parse_from(["agpm", "--verbose", "list"]).unwrap();
let config = cli.build_config();
assert_eq!(config.log_level, Some("debug".to_string()));
assert!(!config.no_progress);
let cli = Cli::try_parse_from(["agpm", "--quiet", "list"]).unwrap();
let config = cli.build_config();
assert_eq!(config.log_level, None);
let cli = Cli::try_parse_from(["agpm", "list"]).unwrap();
let config = cli.build_config();
assert_eq!(config.log_level, Some("info".to_string()));
let cli = Cli::try_parse_from(["agpm", "--no-progress", "list"]).unwrap();
let config = cli.build_config();
assert!(config.no_progress);
let cli = Cli::try_parse_from(["agpm", "--config", "/custom/path", "list"]).unwrap();
let config = cli.build_config();
assert_eq!(config.config_path, Some("/custom/path".to_string()));
}
#[tokio::test]
async fn test_cli_execute_all_commands() {
use crate::cli::CliConfig;
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().canonicalize().unwrap();
let manifest_path = temp_path.join("agpm.toml");
std::fs::write(&manifest_path, "[sources]\n").unwrap();
assert!(manifest_path.exists(), "Manifest file was not created");
let test_config = CliConfig::new();
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"list",
])
.unwrap();
let result = cli.execute_with_config(test_config.clone()).await;
assert!(result.is_ok(), "list command failed: {result:?}");
assert!(manifest_path.exists(), "Manifest disappeared after list");
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"validate",
])
.unwrap();
let result = cli.execute_with_config(test_config.clone()).await;
assert!(result.is_ok(), "validate command failed: {result:?}");
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"cache",
"info",
])
.unwrap();
let result = cli.execute_with_config(test_config.clone()).await;
assert!(result.is_ok(), "cache info command failed: {result:?}");
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"install",
])
.unwrap();
let result = cli.execute_with_config(test_config.clone()).await;
assert!(result.is_ok(), "install command failed: {result:?}");
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"update",
])
.unwrap();
let result = cli.execute_with_config(test_config.clone()).await;
assert!(result.is_ok(), "update command failed: {result:?}");
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"add",
"source",
"test",
"https://github.com/test/repo.git",
])
.unwrap();
let result = cli.execute_with_config(test_config.clone()).await;
assert!(result.is_ok(), "add source command failed: {result:?}");
}
#[tokio::test]
async fn test_cli_execute_method() {
let cli = Cli::try_parse_from(["agpm", "config", "path"]).unwrap();
let result = cli.execute().await;
assert!(result.is_ok(), "execute method failed: {result:?}");
}
#[tokio::test]
async fn test_cli_execute_init_command() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().to_path_buf();
let cli =
Cli::try_parse_from(["agpm", "init", "--path", temp_path.to_str().unwrap()]).unwrap();
let result = cli.execute().await;
assert!(result.is_ok(), "Init command failed: {result:?}");
let manifest_path = temp_path.join("agpm.toml");
assert!(manifest_path.exists());
}
#[tokio::test]
async fn test_cli_execute_cache_command() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().canonicalize().unwrap();
std::fs::write(temp_path.join("agpm.toml"), "[sources]\n").unwrap();
let cli = Cli::try_parse_from(["agpm", "cache", "info"]).unwrap();
let result = cli.execute().await;
assert!(result.is_ok(), "Cache command failed: {result:?}");
}
#[tokio::test]
async fn test_cli_execute_remove_command() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().to_path_buf();
std::fs::write(
temp_path.join("agpm.toml"),
r#"[sources]
test-source = "https://github.com/test/repo.git"
[agents]
[snippets]
[commands]
[mcp-servers]
"#,
)
.unwrap();
let cli = Cli::try_parse_from(["agpm", "remove", "source", "nonexistent"]).unwrap();
let result = cli.execute().await;
assert!(result.is_err(), "Remove command should fail for non-existent source");
}
#[tokio::test]
async fn test_cli_execute_config_command() {
use tempfile::TempDir;
let _temp_dir = TempDir::new().unwrap();
let cli = Cli::try_parse_from(["agpm", "config", "path"]).unwrap();
let result = cli.execute().await;
assert!(result.is_ok(), "Config command failed: {result:?}");
}
#[test]
fn test_cli_global_flags_work_with_all_commands() {
let commands = vec!["init", "install", "update", "list", "validate"];
let flags = vec!["--verbose", "--quiet", "--no-progress"];
for cmd in &commands {
for flag in &flags {
let result = Cli::try_parse_from(["agpm", flag, cmd]);
assert!(result.is_ok(), "Failed with {flag} {cmd}");
}
}
}
}
#[cfg(test)]
mod cli_execution_tests {
}