use ricecoder_cli::commands::{Command, VersionCommand, HelpCommand, InitCommand, ChatCommand};
use std::fs;
use std::path::Path;
use tempfile::TempDir;
#[test]
fn test_version_command_displays_version() {
let cmd = VersionCommand::new();
let result = cmd.execute();
assert!(result.is_ok(), "Version command should succeed");
}
#[test]
fn test_version_command_contains_version_number() {
let cmd = VersionCommand::new();
let result = cmd.execute();
assert!(result.is_ok(), "Version command should execute without error");
}
#[test]
fn test_version_command_format_is_valid() {
let cmd = VersionCommand::new();
let result = cmd.execute();
assert!(result.is_ok(), "Version command should produce valid output");
}
#[test]
fn test_version_command_matches_cargo_version() {
let cmd = VersionCommand::new();
let result = cmd.execute();
assert!(result.is_ok(), "Version should match Cargo.toml");
}
#[test]
fn test_version_command_includes_build_info() {
let cmd = VersionCommand::new();
let result = cmd.execute();
assert!(result.is_ok(), "Version should include build information");
}
#[test]
fn test_help_command_displays_main_help() {
let cmd = HelpCommand::new(None);
let result = cmd.execute();
assert!(result.is_ok(), "Help command should succeed");
}
#[test]
fn test_help_command_with_topic() {
let cmd = HelpCommand::new(Some("init".to_string()));
let result = cmd.execute();
assert!(result.is_ok(), "Help command with topic should succeed");
}
#[test]
fn test_help_command_tutorial_topic() {
let cmd = HelpCommand::new(Some("tutorial".to_string()));
let result = cmd.execute();
assert!(result.is_ok(), "Help tutorial should succeed");
}
#[test]
fn test_help_command_troubleshooting_topic() {
let cmd = HelpCommand::new(Some("troubleshooting".to_string()));
let result = cmd.execute();
assert!(result.is_ok(), "Help troubleshooting should succeed");
}
#[test]
fn test_help_command_with_invalid_topic() {
let cmd = HelpCommand::new(Some("invalid_topic_xyz".to_string()));
let result = cmd.execute();
assert!(result.is_ok(), "Help with invalid topic should handle gracefully");
}
#[test]
fn test_help_command_for_each_command() {
let commands = vec!["init", "gen", "chat", "config"];
for cmd_name in commands {
let cmd = HelpCommand::new(Some(cmd_name.to_string()));
let result = cmd.execute();
assert!(
result.is_ok(),
"Help for command '{}' should succeed",
cmd_name
);
}
}
#[test]
fn test_help_command_contains_usage_information() {
let cmd = HelpCommand::new(Some("init".to_string()));
let result = cmd.execute();
assert!(result.is_ok(), "Help should contain usage information");
}
#[test]
fn test_init_command_creates_configuration() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let result = cmd.execute();
assert!(result.is_ok(), "Init command should succeed");
let config_file = Path::new(temp_path).join(".agent/ricecoder.toml");
assert!(
config_file.exists(),
"Configuration file should be created"
);
}
#[test]
fn test_init_command_configuration_is_valid() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let _ = cmd.execute();
let config_file = Path::new(temp_path).join(".agent/ricecoder.toml");
let content = fs::read_to_string(&config_file).expect("Failed to read config");
assert!(
content.contains("[project]"),
"Configuration should have [project] section"
);
assert!(
content.contains("[providers]"),
"Configuration should have [providers] section"
);
}
#[test]
fn test_init_command_creates_example_spec() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let _ = cmd.execute();
let spec_file = Path::new(temp_path).join(".agent/example-spec.md");
assert!(spec_file.exists(), "Example spec should be created");
}
#[test]
fn test_init_command_creates_readme() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let _ = cmd.execute();
let readme_file = Path::new(temp_path).join("README.md");
assert!(readme_file.exists(), "README should be created");
}
#[test]
fn test_init_command_all_files_created() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let _ = cmd.execute();
let files = vec![
".agent/ricecoder.toml",
".agent/example-spec.md",
"README.md",
];
for file in files {
let path = Path::new(temp_path).join(file);
assert!(path.exists(), "File {} should be created", file);
}
}
#[test]
fn test_init_command_configuration_readable() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let _ = cmd.execute();
let config_file = Path::new(temp_path).join(".agent/ricecoder.toml");
let content = fs::read_to_string(&config_file);
assert!(content.is_ok(), "Configuration should be readable");
}
#[test]
fn test_chat_command_basic_execution() {
let cmd = ChatCommand::new(None, None, None);
assert!(cmd.message.is_none(), "Chat command should accept no message");
}
#[test]
fn test_chat_command_with_message() {
let cmd = ChatCommand::new(Some("Hello".to_string()), None, None);
assert_eq!(
cmd.message,
Some("Hello".to_string()),
"Chat command should store message"
);
}
#[test]
fn test_chat_command_with_provider() {
let cmd = ChatCommand::new(None, Some("openai".to_string()), None);
assert_eq!(
cmd.provider,
Some("openai".to_string()),
"Chat command should store provider"
);
}
#[test]
fn test_chat_command_with_model() {
let cmd = ChatCommand::new(None, None, Some("gpt-4".to_string()));
assert_eq!(
cmd.model,
Some("gpt-4".to_string()),
"Chat command should store model"
);
}
#[test]
fn test_chat_command_with_all_parameters() {
let cmd = ChatCommand::new(
Some("Hello".to_string()),
Some("openai".to_string()),
Some("gpt-4".to_string()),
);
assert_eq!(cmd.message, Some("Hello".to_string()));
assert_eq!(cmd.provider, Some("openai".to_string()));
assert_eq!(cmd.model, Some("gpt-4".to_string()));
}
#[test]
fn test_chat_command_supports_multiple_providers() {
let providers = vec!["openai", "anthropic", "local"];
for provider in providers {
let cmd = ChatCommand::new(None, Some(provider.to_string()), None);
assert_eq!(
cmd.provider,
Some(provider.to_string()),
"Chat command should support provider: {}",
provider
);
}
}
#[test]
fn test_version_command_always_succeeds() {
for _ in 0..10 {
let cmd = VersionCommand::new();
let result = cmd.execute();
assert!(result.is_ok(), "Version command should always succeed");
}
}
#[test]
fn test_help_command_always_succeeds() {
let topics = vec![None, Some("init".to_string()), Some("tutorial".to_string())];
for topic in topics {
let cmd = HelpCommand::new(topic);
let result = cmd.execute();
assert!(result.is_ok(), "Help command should always succeed");
}
}
#[test]
fn test_init_command_idempotent() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let result1 = cmd.execute();
assert!(result1.is_ok(), "First init should succeed");
let result2 = cmd.execute();
assert!(result2.is_ok(), "Second init should succeed (idempotent)");
}
#[test]
fn test_init_creates_valid_configuration_structure() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path().to_str().unwrap();
let cmd = InitCommand::new(Some(temp_path.to_string())).with_interactive(false);
let _ = cmd.execute();
let config_file = Path::new(temp_path).join(".agent/ricecoder.toml");
let content = fs::read_to_string(&config_file).expect("Failed to read config");
let required_sections = vec!["[project]", "[providers]", "[storage]"];
for section in required_sections {
assert!(
content.contains(section),
"Configuration should contain section: {}",
section
);
}
}
#[test]
fn test_chat_command_parameter_combinations() {
let test_cases = vec![
(None, None, None),
(Some("msg".to_string()), None, None),
(None, Some("openai".to_string()), None),
(None, None, Some("gpt-4".to_string())),
(
Some("msg".to_string()),
Some("openai".to_string()),
Some("gpt-4".to_string()),
),
];
for (msg, provider, model) in test_cases {
let cmd = ChatCommand::new(msg.clone(), provider.clone(), model.clone());
assert_eq!(cmd.message, msg);
assert_eq!(cmd.provider, provider);
assert_eq!(cmd.model, model);
}
}
#[test]
fn test_version_command_implements_command_trait() {
let cmd = VersionCommand::new();
let _: &dyn Command = &cmd;
}
#[test]
fn test_help_command_implements_command_trait() {
let cmd = HelpCommand::new(None);
let _: &dyn Command = &cmd;
}
#[test]
fn test_init_command_implements_command_trait() {
let cmd = InitCommand::new(None);
let _: &dyn Command = &cmd;
}
#[test]
fn test_chat_command_implements_command_trait() {
let cmd = ChatCommand::new(None, None, None);
let _: &dyn Command = &cmd;
}
#[test]
fn test_all_verification_commands_available() {
let _version = VersionCommand::new();
let _help = HelpCommand::new(None);
let _init = InitCommand::new(None);
let _chat = ChatCommand::new(None, None, None);
}