2025-12-30 02:08:26 +05:30
|
|
|
use crate::settings::*;
|
2025-12-29 00:35:14 +05:30
|
|
|
use std::path::Path;
|
2025-12-30 02:08:26 +05:30
|
|
|
use std::path::PathBuf;
|
2025-12-29 00:05:08 +05:30
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_config_serialization() {
|
|
|
|
|
let config = Config {
|
|
|
|
|
api_key: "test_key_12345".to_string(),
|
|
|
|
|
download_folder: PathBuf::from("/test/path"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let toml_str = toml::to_string_pretty(&config).unwrap();
|
|
|
|
|
assert!(toml_str.contains("test_key_12345"));
|
|
|
|
|
|
|
|
|
|
let deserialized: Config = toml::from_str(&toml_str).unwrap();
|
|
|
|
|
assert_eq!(config.api_key, deserialized.api_key);
|
|
|
|
|
assert_eq!(config.download_folder, deserialized.download_folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_validate_api_key_valid() {
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(Prompter::validate_api_key(
|
2025-12-29 00:54:29 +05:30
|
|
|
"AIzaSyB1234567890123456789012345678"
|
|
|
|
|
));
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(Prompter::validate_api_key(
|
2025-12-29 00:54:29 +05:30
|
|
|
"AIzaSyB123456789012345678901234567890"
|
|
|
|
|
));
|
2025-12-29 00:05:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_validate_api_key_invalid() {
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(!Prompter::validate_api_key(""));
|
|
|
|
|
assert!(!Prompter::validate_api_key("invalid_key"));
|
|
|
|
|
assert!(!Prompter::validate_api_key(
|
2025-12-29 00:54:29 +05:30
|
|
|
"BizaSyB1234567890123456789012345678"
|
|
|
|
|
));
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(!Prompter::validate_api_key("short"));
|
2025-12-29 00:05:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_validate_folder_path_valid() {
|
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(Prompter::validate_folder_path(
|
2025-12-29 00:54:29 +05:30
|
|
|
temp_dir.path()
|
|
|
|
|
));
|
2025-12-29 00:05:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_validate_folder_path_invalid() {
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(!Prompter::validate_folder_path(Path::new(
|
2025-12-29 00:54:29 +05:30
|
|
|
"/nonexistent/path/that/does/not/exist"
|
|
|
|
|
)));
|
2025-12-29 00:05:08 +05:30
|
|
|
|
|
|
|
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
2025-12-30 02:08:26 +05:30
|
|
|
assert!(!Prompter::validate_folder_path(
|
2025-12-29 00:54:29 +05:30
|
|
|
temp_file.path()
|
|
|
|
|
));
|
2025-12-29 00:05:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_expand_home_with_tilde() {
|
2025-12-29 00:35:14 +05:30
|
|
|
if let Some(base_dirs) = directories::BaseDirs::new() {
|
2025-12-29 00:05:08 +05:30
|
|
|
let home = base_dirs.home_dir();
|
2025-12-30 02:08:26 +05:30
|
|
|
let expanded = Prompter::expand_home("~/test/path");
|
2025-12-29 00:05:08 +05:30
|
|
|
assert!(expanded.starts_with(home.to_string_lossy().as_ref()));
|
|
|
|
|
assert!(expanded.contains("test/path"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_expand_home_without_tilde() {
|
2025-12-30 02:08:26 +05:30
|
|
|
let expanded = Prompter::expand_home("/absolute/path");
|
2025-12-29 00:05:08 +05:30
|
|
|
assert_eq!(expanded, "/absolute/path");
|
|
|
|
|
|
2025-12-30 02:08:26 +05:30
|
|
|
let expanded = Prompter::expand_home("relative/path");
|
2025-12-29 00:05:08 +05:30
|
|
|
assert_eq!(expanded, "relative/path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get_default_downloads_folder() {
|
2025-12-30 02:08:26 +05:30
|
|
|
let path = Prompter::get_default_downloads_folder();
|
2025-12-29 00:05:08 +05:30
|
|
|
assert!(path.ends_with("Downloads"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_config_empty_api_key_error() {
|
|
|
|
|
let config = Config {
|
|
|
|
|
api_key: String::new(),
|
|
|
|
|
download_folder: PathBuf::from("/test/path"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(config.api_key.is_empty());
|
|
|
|
|
}
|