use ricecoder_cli::commands::{ChatCommand, Command};
use ricecoder_storage::PathResolver;
use std::path::PathBuf;
#[test]
fn test_chat_command_loads_specs_from_correct_location() {
let cmd = ChatCommand::new(None, None, None);
let result = cmd.execute();
assert!(result.is_ok(), "Chat command should execute successfully");
}
#[test]
fn test_chat_command_project_path_resolution() {
let project_path = PathResolver::resolve_project_path();
assert_eq!(project_path, PathBuf::from(".agent"));
}
#[test]
fn test_chat_command_specs_path_construction() {
let project_path = PathResolver::resolve_project_path();
let specs_path = project_path.join("specs");
assert_eq!(specs_path, PathBuf::from(".agent/specs"));
}
#[test]
fn test_chat_command_error_handling_for_missing_specs() {
let cmd = ChatCommand::new(None, None, None);
let result = cmd.execute();
assert!(
result.is_ok(),
"Chat command should handle missing specs gracefully"
);
}
#[test]
fn test_chat_command_with_environment_variable_override() {
let original = std::env::var("RICECODER_HOME").ok();
std::env::set_var("RICECODER_HOME", "/tmp/test-ricecoder");
let cmd = ChatCommand::new(None, None, None);
let result = cmd.execute();
assert!(
result.is_ok(),
"Chat command should work with RICECODER_HOME set"
);
if let Some(orig) = original {
std::env::set_var("RICECODER_HOME", orig);
} else {
std::env::remove_var("RICECODER_HOME");
}
}
#[test]
fn test_chat_command_without_environment_variable() {
let original = std::env::var("RICECODER_HOME").ok();
std::env::remove_var("RICECODER_HOME");
let cmd = ChatCommand::new(None, None, None);
let result = cmd.execute();
assert!(
result.is_ok(),
"Chat command should work without RICECODER_HOME"
);
if let Some(orig) = original {
std::env::set_var("RICECODER_HOME", orig);
}
}
#[test]
fn test_chat_command_provider_validation() {
let cmd = ChatCommand::new(None, Some("openai".to_string()), None);
let result = cmd.execute();
assert!(result.is_ok(), "Chat command should accept valid provider");
}
#[test]
fn test_chat_command_invalid_provider_error_handling() {
let cmd = ChatCommand::new(None, Some("invalid_provider".to_string()), None);
let result = cmd.execute();
assert!(
result.is_err(),
"Chat command should reject invalid provider"
);
}
#[test]
fn test_chat_command_model_defaults() {
let cmd = ChatCommand::new(None, None, None);
assert!(cmd.model.is_none(), "Model should be None initially");
}
#[test]
fn test_chat_command_with_custom_model() {
let cmd = ChatCommand::new(None, None, Some("gpt-4".to_string()));
assert_eq!(cmd.model, Some("gpt-4".to_string()));
}
#[test]
fn test_path_resolver_project_path_consistency() {
let path1 = PathResolver::resolve_project_path();
let path2 = PathResolver::resolve_project_path();
let path3 = PathResolver::resolve_project_path();
assert_eq!(path1, path2);
assert_eq!(path2, path3);
}
#[test]
fn test_path_resolver_project_path_is_agent() {
let project_path = PathResolver::resolve_project_path();
assert_eq!(project_path, PathBuf::from(".agent"));
}
#[test]
fn test_path_resolver_global_path_ends_with_ricecoder() {
let original = std::env::var("RICECODER_HOME").ok();
std::env::remove_var("RICECODER_HOME");
let global_path = PathResolver::resolve_global_path().expect("Should resolve global path");
let path_str = global_path.to_str().expect("Path should be valid UTF-8");
assert!(
path_str.ends_with(".ricecoder") || path_str.ends_with(".ricecoder/"),
"Global path should end with .ricecoder, got: {}",
path_str
);
if let Some(orig) = original {
std::env::set_var("RICECODER_HOME", orig);
}
}
#[test]
fn test_chat_command_uses_path_resolver_for_specs() {
let cmd = ChatCommand::new(None, None, None);
let result = cmd.execute();
assert!(
result.is_ok(),
"Chat command should use PathResolver successfully"
);
}
#[test]
fn test_chat_command_specs_path_is_agent_specs() {
let project_path = PathResolver::resolve_project_path();
let specs_path = project_path.join("specs");
assert_eq!(specs_path, PathBuf::from(".agent/specs"));
}
#[test]
fn test_chat_command_knowledge_base_path_resolution() {
let global_path = PathResolver::resolve_global_path();
match global_path {
Ok(path) => {
let kb_path = path.join("knowledge_base");
assert!(kb_path.to_str().is_some());
}
Err(_) => {
}
}
}
#[test]
fn test_chat_command_multiple_executions_consistent() {
let cmd1 = ChatCommand::new(None, None, None);
let cmd2 = ChatCommand::new(None, None, None);
let result1 = cmd1.execute();
let result2 = cmd2.execute();
assert!(result1.is_ok());
assert!(result2.is_ok());
}
#[test]
fn test_chat_command_with_message_and_path_resolution() {
let cmd = ChatCommand::new(Some("Hello".to_string()), None, None);
let result = cmd.execute();
assert!(result.is_ok(), "Chat command should work with message");
}
#[test]
fn test_chat_command_with_provider_and_path_resolution() {
let cmd = ChatCommand::new(None, Some("openai".to_string()), None);
let result = cmd.execute();
assert!(result.is_ok(), "Chat command should work with provider");
}
#[test]
fn test_chat_command_with_model_and_path_resolution() {
let cmd = ChatCommand::new(None, None, Some("gpt-4".to_string()));
let result = cmd.execute();
assert!(result.is_ok(), "Chat command should work with model");
}
#[test]
fn test_chat_command_with_all_options_and_path_resolution() {
let cmd = ChatCommand::new(
Some("Hello".to_string()),
Some("openai".to_string()),
Some("gpt-4".to_string()),
);
let result = cmd.execute();
assert!(result.is_ok(), "Chat command should work with all options");
}