refactor: Separate tests into dedicated files to reduce source file sizes

- Extract all tests from cache.rs, files.rs, config.rs to separate test files
- Create cache_tests.rs, files_tests.rs, config_tests.rs
- Reduce cache.rs: 421 -> 220 lines (-191 lines)
- Reduce files.rs: 390 -> 264 lines (-126 lines)
- Reduce config.rs: 354 -> 275 lines (-79 lines)
- Total reduction: 396 lines from main source files
- All 31 tests still passing
This commit is contained in:
2025-12-29 00:05:08 +05:30
parent a9dbaf36be
commit bbf88fc4fc
7 changed files with 412 additions and 414 deletions

79
src/config_tests.rs Normal file
View File

@@ -0,0 +1,79 @@
use crate::config::*;
#[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() {
assert!(validate_api_key("AIzaSyB1234567890123456789012345678"));
assert!(validate_api_key("AIzaSyB123456789012345678901234567890"));
}
#[test]
fn test_validate_api_key_invalid() {
assert!(!validate_api_key(""));
assert!(!validate_api_key("invalid_key"));
assert!(!validate_api_key("BizaSyB1234567890123456789012345678"));
assert!(!validate_api_key("short"));
}
#[test]
fn test_validate_folder_path_valid() {
let temp_dir = tempfile::tempdir().unwrap();
assert!(validate_folder_path(temp_dir.path()));
}
#[test]
fn test_validate_folder_path_invalid() {
assert!(!validate_folder_path(Path::new("/nonexistent/path/that/does/not/exist")));
let temp_file = tempfile::NamedTempFile::new().unwrap();
assert!(!validate_folder_path(temp_file.path()));
}
#[test]
fn test_expand_home_with_tilde() {
if let Some(base_dirs) = BaseDirs::new() {
let home = base_dirs.home_dir();
let expanded = expand_home("~/test/path");
assert!(expanded.starts_with(home.to_string_lossy().as_ref()));
assert!(expanded.contains("test/path"));
}
}
#[test]
fn test_expand_home_without_tilde() {
let expanded = expand_home("/absolute/path");
assert_eq!(expanded, "/absolute/path");
let expanded = expand_home("relative/path");
assert_eq!(expanded, "relative/path");
}
#[test]
fn test_get_default_downloads_folder() {
let path = get_default_downloads_folder();
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());
}