#[cfg(test)]
#[allow(clippy::module_inception)]
mod tests {
use super::super::apply::OptionHandlerApply;
use super::super::{OptionHandler, built_in_defaults, detect_value_type};
use crate::config::option::OptionValue;
#[test]
fn test_defaults_populated() {
let handler = OptionHandler::new();
let expected_count = built_in_defaults().len();
assert_eq!(handler.default_count(), expected_count);
assert!(handler.default_count() > 0);
assert_eq!(handler.get("dir").as_str().unwrap_or(""), ".");
assert_eq!(handler.get("split").as_usize(), 5);
assert_eq!(handler.get("max-concurrent-downloads").as_usize(), 5);
assert_eq!(handler.get("max-connection-per-server").as_usize(), 1);
assert_eq!(handler.get("min-split-size").as_usize(), 1_048_576);
assert!(handler.get("continue").as_bool().unwrap_or(false));
assert!(!handler.get("quiet").as_bool().unwrap_or(false));
assert_eq!(handler.get("seed-ratio").as_f64().unwrap_or(0.0), 0.0);
assert_eq!(handler.get("rpc-listen-port").as_usize(), 6800);
assert_eq!(
handler.get("console-log-level").as_str().unwrap_or(""),
"info"
);
}
#[test]
fn test_set_get_roundtrip() {
let mut handler = OptionHandler::new();
handler.set("dir", OptionValue::Str("/tmp/downloads".into()));
assert_eq!(handler.get("dir").as_str().unwrap_or(""), "/tmp/downloads");
handler.set("split", OptionValue::Usize(16));
assert_eq!(handler.get("split").as_usize(), 16);
handler.set("seed-ratio", OptionValue::Float(2.5));
assert!((handler.get("seed-ratio").as_f64().unwrap_or(0.0) - 2.5).abs() < f64::EPSILON);
handler.set("quiet", OptionValue::Bool(true));
assert!(handler.get("quiet").as_bool().unwrap_or(false));
handler.set(
"header",
OptionValue::List(vec!["X-Custom: foo".into(), "X-Bar: baz".into()]),
);
assert_eq!(handler.get("header").as_str_vec().len(), 2);
handler.set("split", OptionValue::Usize(32));
assert_eq!(handler.get("split").as_usize(), 32);
assert!(handler.get("nonexistent-key").is_none());
}
#[test]
#[allow(clippy::approx_constant)]
fn test_load_config_file() {
let mut handler = OptionHandler::new();
let config_content = r#"
# This is a comment
dir="/home/user/downloads"
split=16
max-connection-per-server=8
quiet=true
seed-ratio=1.5
custom-list=['header1', 'header2', 'header3']
bool-flag=yes
number-key=42
float-key=3.14
# Another comment
allow-overwrite=false
"#;
let tmp_dir = std::env::temp_dir();
let config_path = tmp_dir.join(format!("aria2_test_config_{}.aria2rc", std::process::id()));
std::fs::write(&config_path, config_content).expect("Failed to write temp config");
let result = handler.load_config_file(&config_path);
assert!(
result.is_ok(),
"load_config_file should succeed: {:?}",
result.err()
);
assert_eq!(
handler.get("dir").as_str().unwrap_or(""),
"/home/user/downloads"
);
assert_eq!(handler.get("split").as_usize(), 16);
assert_eq!(handler.get("max-connection-per-server").as_usize(), 8);
assert!(handler.get("quiet").as_bool().unwrap_or(false));
assert!((handler.get("seed-ratio").as_f64().unwrap_or(0.0) - 1.5).abs() < f64::EPSILON);
assert!(!handler.get("allow-overwrite").as_bool().unwrap_or(true));
let list_val = handler.get("custom-list");
assert_eq!(list_val.as_str_vec().len(), 3);
assert_eq!(list_val.as_str_vec()[0], "header1");
assert!(handler.get("bool-flag").as_bool().unwrap_or(false)); assert_eq!(handler.get("number-key").as_usize(), 42);
let float_val = handler.get("float-key").as_f64().unwrap_or(0.0);
assert!((float_val - 3.14).abs() < f64::EPSILON);
assert_eq!(handler.get("rpc-listen-port").as_usize(), 6800);
let _ = std::fs::remove_file(&config_path);
}
#[test]
fn test_apply_args_overrides_config() {
let mut handler = OptionHandler::new();
let config_content = r#"
dir=/config/dir
split=4
quiet=false
"#;
let tmp_dir = std::env::temp_dir();
let config_path = tmp_dir.join(format!(
"aria2_test_override_{}.aria2rc",
std::process::id()
));
std::fs::write(&config_path, config_content).expect("Failed to write config");
handler
.load_config_file(&config_path)
.expect("Should load config");
assert_eq!(handler.get("dir").as_str().unwrap_or(""), "/config/dir");
assert_eq!(handler.get("split").as_usize(), 4);
assert!(!handler.get("quiet").as_bool().unwrap_or(false));
let cli_args: Vec<String> = vec![
"--dir=/cli/dir".to_string(),
"--split=12".to_string(),
"--quiet".to_string(), "--max-connection-per-server=8".to_string(),
"--seed-ratio=2.0".to_string(),
"--no-continue".to_string(), ];
handler.apply_args(&cli_args);
assert_eq!(handler.get("dir").as_str().unwrap_or(""), "/cli/dir");
assert_eq!(handler.get("split").as_usize(), 12);
assert!(handler.get("quiet").as_bool().unwrap_or(false)); assert_eq!(handler.get("max-connection-per-server").as_usize(), 8);
assert!((handler.get("seed-ratio").as_f64().unwrap_or(0.0) - 2.0).abs() < f64::EPSILON);
assert!(!handler.get("continue").as_bool().unwrap_or(true));
let _ = std::fs::remove_file(&config_path);
}
#[test]
fn test_to_download_options() {
let mut handler = OptionHandler::new();
handler.set("split", OptionValue::Usize(8));
handler.set("max-connection-per-server", OptionValue::Usize(4));
handler.set("max-download-limit", OptionValue::Usize(102400));
handler.set("max-upload-limit", OptionValue::Usize(51200));
handler.set("dir", OptionValue::Str("/data".to_string()));
handler.set("out", OptionValue::Str("output.bin".to_string()));
handler.set("seed-time", OptionValue::Usize(300));
handler.set("seed-ratio", OptionValue::Float(2.0));
handler.set("follow-torrent", OptionValue::Str("mem".to_string()));
handler.set("follow-metalink", OptionValue::Str("false".to_string()));
let opts = handler.to_download_options();
assert_eq!(opts.split, Some(8));
assert_eq!(opts.max_connection_per_server, Some(4));
assert_eq!(opts.max_download_limit, Some(102400));
assert_eq!(opts.max_upload_limit, Some(51200));
assert_eq!(opts.dir, Some("/data".to_string()));
assert_eq!(opts.out, Some("output.bin".to_string()));
assert_eq!(opts.seed_time, Some(300.0));
assert_eq!(opts.seed_ratio, Some(2.0));
assert_eq!(
opts.follow_torrent,
Some(crate::request::request_group::FollowMode::Memory)
);
assert_eq!(
opts.follow_metalink,
Some(crate::request::request_group::FollowMode::Disabled)
);
let handler2 = OptionHandler::new();
let opts2 = handler2.to_download_options();
assert_eq!(opts2.split, Some(5)); assert_eq!(opts2.max_connection_per_server, Some(1)); assert_eq!(opts2.dir, Some(".".to_string())); assert_eq!(opts2.out, None);
handler.reset_to_default("split");
assert_eq!(handler.get("split").as_usize(), 5); assert!(!handler.is_explicitly_set("split"));
}
#[test]
#[allow(clippy::approx_constant)]
fn test_detect_value_type_edge_cases() {
assert_eq!(detect_value_type("true"), Some(OptionValue::Bool(true)));
assert_eq!(detect_value_type("false"), Some(OptionValue::Bool(false)));
assert_eq!(detect_value_type("yes"), Some(OptionValue::Bool(true)));
assert_eq!(detect_value_type("no"), Some(OptionValue::Bool(false)));
assert_eq!(detect_value_type("42"), Some(OptionValue::Usize(42)));
assert_eq!(detect_value_type("-10"), Some(OptionValue::Int(-10)));
let detected = detect_value_type("3.14159")
.unwrap()
.as_f64()
.unwrap_or(0.0);
assert!((detected - 3.14159).abs() < 0.001); assert_eq!(
detect_value_type("\"quoted string\""),
Some(OptionValue::Str("quoted string".into()))
);
assert_eq!(
detect_value_type("['a','b','c']"),
Some(OptionValue::List(vec!["a".into(), "b".into(), "c".into()]))
);
assert_eq!(detect_value_type(""), Some(OptionValue::None));
assert_eq!(
detect_value_type("plain_text"),
Some(OptionValue::Str("plain_text".into()))
);
}
#[test]
fn test_option_value_display() {
assert_eq!(OptionValue::Bool(true).to_string(), "true");
assert_eq!(OptionValue::Usize(42).to_string(), "42");
assert_eq!(OptionValue::Int(-10).to_string(), "-10");
assert_eq!(
format!("{:.2}", {
#[allow(clippy::approx_constant)]
OptionValue::Float(3.14).to_string().parse::<f64>().unwrap()
}),
"3.14"
); assert_eq!(OptionValue::Str("hello".to_string()).to_string(), "hello");
assert_eq!(
OptionValue::List(vec!["a".into(), "b".into()]).to_string(),
"a,b"
);
assert_eq!(OptionValue::None.to_string(), "");
}
#[test]
fn test_to_map_includes_all() {
let mut handler = OptionHandler::new();
handler.set("custom-key", OptionValue::Str("custom-value".into()));
let map = handler.to_map();
assert!(map.contains_key("dir"));
assert!(map.contains_key("split"));
assert!(map.contains_key("custom-key"));
assert_eq!(
map.get("custom-key").unwrap().as_str().unwrap_or(""),
"custom-value"
);
assert!(map.len() >= built_in_defaults().len());
}
}