#[allow(unused_imports)]
use std::sync::Arc;
#[allow(unused_imports)]
use super::bt_peer_snapshot::BtPeerSource;
#[allow(unused_imports)]
use super::group::RequestGroup;
#[allow(unused_imports)]
use super::group_id::GroupId;
#[allow(unused_imports)]
use super::options::DownloadOptions;
#[allow(unused_imports)]
use super::result_code::DownloadResultCode;
#[allow(unused_imports)]
use crate::download::DownloadContext;
#[test]
fn test_bt_peer_snapshots_roundtrip() {
let group = RequestGroup::new(GroupId::new(99), Vec::new(), DownloadOptions::default());
let snapshot = super::BtPeerSnapshot {
peer_id: [1; 20],
client: Some("peer-test/1.0".into()),
addr: "127.0.0.1:6881".parse().expect("valid test address"),
is_incoming: false,
source: BtPeerSource::Tracker,
bitfield: Some(vec![0x80]),
uploaded_bytes: 1,
downloaded_bytes: 2,
upload_speed: 3.0,
download_speed: 4.0,
avg_upload_speed: 5,
avg_download_speed: 6,
am_choking: true,
peer_choking: false,
am_interested: true,
peer_interested: false,
outstanding_upload_requests: 0,
outstanding_download_requests: 1,
seeder: Some(true),
connection_duration_secs: 7,
last_data_age_secs: 8,
is_snubbed: false,
is_banned: false,
};
group.set_bt_connection_count(1);
group.set_bt_peer_snapshots(vec![snapshot.clone()]);
assert_eq!(group.bt_peer_snapshots(), vec![snapshot]);
group.clear_bt_peer_snapshots();
assert!(group.bt_peer_snapshots().is_empty());
assert_eq!(group.active_connection_count(), 0);
}
#[test]
fn status_snapshot_uses_one_bt_peer_source_for_all_consumers() {
let group = RequestGroup::new(GroupId::new(100), Vec::new(), DownloadOptions::default());
group.set_total_length_atomic(1000);
group.set_completed_length(250);
group.set_uploaded_length(64);
group.set_download_speed_cached(128);
group.set_upload_speed_cached(32);
group.set_stream_connection_count(1);
group.set_bt_connection_count(1);
group.set_bt_metadata(2, 512, "01234567890123456789".to_string());
group.set_bt_bitfield(Some(vec![0xC0]));
let mut peers = Vec::new();
for (port, seeder) in [(6881, true), (6882, false)] {
peers.push(super::BtPeerSnapshot {
peer_id: [port as u8; 20],
client: None,
addr: format!("127.0.0.1:{port}")
.parse()
.expect("valid peer address"),
is_incoming: false,
source: BtPeerSource::Dht,
bitfield: None,
uploaded_bytes: 0,
downloaded_bytes: 1,
upload_speed: 0.0,
download_speed: 1.0,
avg_upload_speed: 0,
avg_download_speed: 1,
am_choking: false,
peer_choking: false,
am_interested: false,
peer_interested: false,
outstanding_upload_requests: 0,
outstanding_download_requests: 0,
seeder: Some(seeder),
connection_duration_secs: 1,
last_data_age_secs: 0,
is_snubbed: false,
is_banned: false,
});
}
group.set_bt_peer_snapshots(peers);
let snapshot = group.status_snapshot();
let bt = snapshot.bt.expect("BT metadata should be present");
assert_eq!(snapshot.completed_length, 250);
assert_eq!(snapshot.upload_length, 64);
assert_eq!(snapshot.download_speed, 128);
assert_eq!(snapshot.connections, 2);
assert_eq!(bt.peer_count(), 2);
assert_eq!(bt.seeder_count(), 1);
assert_eq!(bt.num_pieces, 2);
assert_eq!(bt.piece_length, 512);
assert_eq!(bt.bitfield, Some(vec![0xC0]));
}
#[test]
fn terminal_status_snapshots_report_zero_transfer_speeds() {
for (index, terminal_state) in ["complete", "error", "removed"].into_iter().enumerate() {
let mut group = RequestGroup::new(
GroupId::new(300 + index as u64),
Vec::new(),
DownloadOptions::default(),
);
match terminal_state {
"complete" => group.complete().expect("complete download"),
"error" => group.error("download failed").expect("failed download"),
"removed" => group.mark_removed(),
_ => unreachable!("test state is terminal"),
}
group.set_download_speed_cached(512);
group.set_upload_speed_cached(256);
let snapshot = group.status_snapshot();
assert!(snapshot.status.is_terminal(), "state: {terminal_state}");
assert_eq!(snapshot.download_speed, 0, "state: {terminal_state}");
assert_eq!(snapshot.upload_speed, 0, "state: {terminal_state}");
}
}
#[test]
fn active_connection_guard_clears_protocol_count_on_exit() {
let group = std::sync::Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(101),
Vec::new(),
DownloadOptions::default(),
)));
{
let guard = super::ActiveConnectionGuard::new(std::sync::Arc::clone(&group));
guard.set(3);
assert_eq!(
group
.read()
.expect("group lock should be readable")
.active_connection_count(),
3
);
group
.read()
.expect("group lock should be readable")
.set_bt_connection_count(2);
assert_eq!(
group
.read()
.expect("group lock should be readable")
.active_connection_count(),
5
);
}
assert_eq!(
group
.read()
.expect("group lock should be readable")
.active_connection_count(),
2
);
}
#[test]
fn test_connection_contexts_are_deduplicated_and_reset() {
use std::net::SocketAddr;
let group = RequestGroup::new(GroupId::new(99), Vec::new(), DownloadOptions::default());
let first: SocketAddr = "192.0.2.1:80".parse().unwrap();
let second: SocketAddr = "192.0.2.2:80".parse().unwrap();
let first_context = crate::network::ConnectionContext::new("example.test", 80, first);
group.set_connection_context(first_context.clone());
group.set_connection_context(first_context);
group.set_connection_context(crate::network::ConnectionContext::new(
"example.test",
80,
second,
));
assert_eq!(group.connection_contexts().len(), 2);
assert_eq!(group.latest_connection_context().unwrap().peer_addr, second);
group.set_connection_context(crate::network::ConnectionContext::new(
"example.test",
80,
first,
));
assert_eq!(group.latest_connection_context().unwrap().peer_addr, first);
assert_eq!(group.connection_contexts().len(), 2);
group.clear_connection_contexts();
assert!(group.connection_contexts().is_empty());
}
#[test]
fn test_connection_context_history_is_bounded() {
use std::net::SocketAddr;
let group = RequestGroup::new(GroupId::new(100), Vec::new(), DownloadOptions::default());
for index in 0..40u16 {
group.set_connection_context(crate::network::ConnectionContext::new(
"example.test",
80,
SocketAddr::from(([192, 0, 2, 1], index + 1)),
));
}
assert_eq!(group.connection_contexts().len(), 32);
assert_eq!(
group.latest_connection_context().unwrap().peer_addr,
SocketAddr::from(([192, 0, 2, 1], 40))
);
}
#[test]
fn test_followed_by_gids_are_idempotent() {
let group = RequestGroup::new(
GroupId::new(1),
vec!["http://example.com/file.zip".to_string()],
DownloadOptions::default(),
);
let child = GroupId::new(2);
group.add_followed_by_gid(child);
group.add_followed_by_gid(child);
assert_eq!(group.followed_by_gids(), vec![child]);
}
#[test]
fn test_child_relationship_fields_are_preserved() {
let group = RequestGroup::new(GroupId::new(2), Vec::new(), DownloadOptions::default());
let parent = GroupId::new(1);
group.set_following_gid(parent);
group.set_belongs_to_gid(parent);
assert_eq!(group.following_gid(), Some(parent));
assert_eq!(group.belongs_to_gid(), Some(parent));
}
#[test]
fn test_mark_paused_preserves_existing_error_details() {
let group = RequestGroup::new(
GroupId::new(3),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.set_last_error(DownloadResultCode::TimeOut, "previous timeout");
group.mark_paused();
assert!(group.status().is_paused());
assert_eq!(group.get_last_error_code(), DownloadResultCode::TimeOut);
assert_eq!(group.get_last_error_message(), "previous timeout");
}
#[test]
fn test_metadata_info_is_preserved_independently_of_parent_link() {
let group = RequestGroup::new(GroupId::new(2), Vec::new(), DownloadOptions::default());
group.set_metadata_info(super::metadata_info::MetadataInfo::new(
GroupId::new(9),
"https://example.test/metadata.torrent",
));
let info = group
.metadata_info()
.expect("metadata info should be attached");
assert_eq!(info.gid(), Some(GroupId::new(9)));
assert_eq!(info.uri(), "https://example.test/metadata.torrent");
assert!(group.belongs_to_gid().is_none());
}
#[test]
fn test_request_group_progress_fields_default() {
let group = RequestGroup::new(
GroupId::new(1),
vec!["http://example.com/file.zip".to_string()],
DownloadOptions::default(),
);
assert_eq!(
group.get_completed_length(),
0,
"completed_length_atomic should default to 0"
);
assert_eq!(
group.get_total_length_atomic(),
0,
"total_length_atomic should default to 0"
);
assert_eq!(
group.get_uploaded_length(),
0,
"uploaded_length should default to 0"
);
assert_eq!(
group.get_download_speed_cached(),
0,
"download_speed_cached should default to 0"
);
assert_eq!(
group.get_upload_speed_cached(),
0,
"upload_speed_cached should default to 0"
);
}
#[test]
fn test_set_get_completed_length() {
let group = RequestGroup::new(
GroupId::new(2),
vec!["http://test.com/file.bin".to_string()],
DownloadOptions::default(),
);
group.set_completed_length(1024);
assert_eq!(
group.get_completed_length(),
1024,
"Should return 1024 after setting"
);
group.set_completed_length(2048);
assert_eq!(
group.get_completed_length(),
2048,
"Should return 2048 after update"
);
group.set_completed_length(u64::MAX);
assert_eq!(
group.get_completed_length(),
u64::MAX,
"Should handle u64::MAX"
);
group.set_completed_length(0);
assert_eq!(group.get_completed_length(), 0, "Should handle 0");
}
#[test]
fn test_mark_complete_sets_completed_length_to_total() {
let group = RequestGroup::new(
GroupId::new(8),
vec!["http://example.com/complete.bin".to_string()],
DownloadOptions::default(),
);
group.set_total_length_atomic(4096);
group.set_completed_length(1024);
group.mark_complete();
assert!(group.status().is_completed());
assert_eq!(group.get_completed_length(), 4096);
}
#[test]
fn test_validate_total_length() {
let group = RequestGroup::new(
GroupId(1),
vec!["ftp://example/file".into()],
DownloadOptions::default(),
);
assert!(group.validate_total_length(0, 1024).is_ok());
assert!(group.validate_total_length(1024, 1024).is_ok());
assert!(group.validate_total_length(1024, 2048).is_err());
}
#[test]
fn test_set_get_total_length() {
let group = RequestGroup::new(
GroupId::new(3),
vec!["http://example.com/large.iso".to_string()],
DownloadOptions::default(),
);
group.set_total_length_atomic(1048576); assert_eq!(
group.get_total_length_atomic(),
1048576,
"Should return 1MB after setting"
);
group.set_total_length_atomic(1073741824); assert_eq!(
group.get_total_length_atomic(),
1073741824,
"Should return 1GB after update"
);
}
#[test]
fn test_set_get_bt_bitfield() {
let group = RequestGroup::new(
GroupId::new(4),
vec!["magnet:?xt=urn:btih:abc123".to_string()],
DownloadOptions::default(),
);
let bf = group.get_bt_bitfield();
assert!(bf.is_none(), "bt_bitfield should default to None");
let test_bitfield = vec![0xFF, 0xF0, 0x0F];
group.set_bt_bitfield(Some(test_bitfield.clone()));
let retrieved = group.get_bt_bitfield();
assert!(
retrieved.is_some(),
"bt_bitfield should be Some after setting"
);
assert_eq!(
retrieved.unwrap(),
test_bitfield,
"bitfield should match what was set"
);
group.set_bt_bitfield(None);
let bf_none = group.get_bt_bitfield();
assert!(
bf_none.is_none(),
"bt_bitfield should be None after clearing"
);
group.set_bt_bitfield(Some(vec![]));
let empty_bf = group.get_bt_bitfield();
assert!(empty_bf.is_some(), "empty bitfield should still be Some");
assert!(empty_bf.unwrap().is_empty(), "bitfield should be empty vec");
}
#[tokio::test]
async fn test_concurrent_access() {
let group = Arc::new(RequestGroup::new(
GroupId::new(5),
vec!["http://load.test/file.dat".to_string()],
DownloadOptions::default(),
));
let mut handles = Vec::new();
for i in 0..10 {
let g = Arc::clone(&group);
handles.push(tokio::spawn(async move {
g.set_completed_length(i * 100);
g.set_total_length_atomic(10000);
g.set_uploaded_length(i * 10);
g.set_download_speed_cached(i * 1000);
let _cl = g.get_completed_length();
let _tl = g.get_total_length_atomic();
let _ul = g.get_uploaded_length();
let _ds = g.get_download_speed_cached();
if i % 3 == 0 {
let bf = vec![i as u8; 8];
g.set_bt_bitfield(Some(bf));
let _retrieved = g.get_bt_bitfield();
}
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
}));
}
for handle in handles {
handle.await.expect("Task should complete without panic");
}
let final_cl = group.get_completed_length();
let final_tl = group.get_total_length_atomic();
let final_ul = group.get_uploaded_length();
let final_ds = group.get_download_speed_cached();
assert!(final_cl <= 900, "completed_length should be <= 900");
assert_eq!(final_tl, 10000, "total_length should be 10000");
assert!(final_ul <= 90, "uploaded_length should be <= 90");
assert!(final_ds <= 9000, "download_speed should be <= 9000");
}
#[test]
fn test_set_get_uploaded_length() {
let group = RequestGroup::new(
GroupId::new(6),
vec!["http://seed.test/file.torrent".to_string()],
DownloadOptions::default(),
);
assert_eq!(group.get_uploaded_length(), 0);
group.set_uploaded_length(512);
assert_eq!(group.get_uploaded_length(), 512);
group.set_uploaded_length(u64::MAX / 2);
assert_eq!(group.get_uploaded_length(), u64::MAX / 2);
}
#[test]
fn test_set_get_download_speed_cached() {
let group = RequestGroup::new(
GroupId::new(7),
vec!["http://speed.test/large.file".to_string()],
DownloadOptions::default(),
);
assert_eq!(group.get_download_speed_cached(), 0);
group.set_download_speed_cached(5242880);
assert_eq!(group.get_download_speed_cached(), 5242880);
group.set_download_speed_cached(10485760); assert_eq!(group.get_download_speed_cached(), 10485760);
}
#[test]
fn test_download_options_choking_config_defaults() {
let opts = DownloadOptions::default();
assert!(
opts.bt_max_upload_slots.is_none(),
"bt_max_upload_slots should default to None"
);
assert!(
opts.bt_optimistic_unchoke_interval.is_none(),
"bt_optimistic_unchoke_interval should default to None"
);
assert!(
opts.bt_snubbed_timeout.is_none(),
"bt_snubbed_timeout should default to None"
);
}
#[test]
fn test_download_options_choking_config_custom() {
let opts = DownloadOptions {
bt_max_upload_slots: Some(8),
bt_optimistic_unchoke_interval: Some(15),
bt_snubbed_timeout: Some(45),
..DownloadOptions::default()
};
assert_eq!(opts.bt_max_upload_slots, Some(8));
assert_eq!(opts.bt_optimistic_unchoke_interval, Some(15));
assert_eq!(opts.bt_snubbed_timeout, Some(45));
}
#[test]
fn test_download_options_choking_config_clone() {
let opts = DownloadOptions {
bt_max_upload_slots: Some(6),
bt_optimistic_unchoke_interval: Some(20),
bt_snubbed_timeout: Some(90),
..DownloadOptions::default()
};
let cloned = opts.clone();
assert_eq!(cloned.bt_max_upload_slots, Some(6));
assert_eq!(cloned.bt_optimistic_unchoke_interval, Some(20));
assert_eq!(cloned.bt_snubbed_timeout, Some(90));
}
#[test]
fn test_effective_min_split_size_uses_task_snapshot_and_runtime_override() {
let mut group = RequestGroup::new(
GroupId::new(12),
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
);
assert_eq!(
group.effective_min_split_size(),
crate::constants::DEFAULT_MIN_SPLIT_SIZE
);
group.set_option_snapshot(std::collections::HashMap::from([(
"min-split-size".to_string(),
serde_json::json!("10M"),
)]));
assert_eq!(group.effective_min_split_size(), 10 * 1024 * 1024);
group
.try_update_option("min-split-size", serde_json::json!("4M"))
.expect("min-split-size should accept a valid reserved-task update");
assert_eq!(group.effective_min_split_size(), 4 * 1024 * 1024);
}
#[test]
fn min_http_range_size_change_is_reserved_and_keeps_piece_budget_independent() {
let mut group = RequestGroup::new(
GroupId::new(13),
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
);
assert_eq!(
group.options().min_http_range_size,
Some(crate::constants::DEFAULT_HTTP_RANGE_SIZE_FLOOR_BYTES)
);
group
.try_update_option("min-http-range-size", serde_json::json!("32K"))
.expect("min-http-range-size should accept a valid reserved-task update");
assert_eq!(group.options().min_http_range_size, Some(32 * 1024));
assert_eq!(
group.options().min_split_size,
Some(crate::constants::DEFAULT_MIN_SPLIT_SIZE)
);
assert!(
group
.try_update_option("min-http-range-size", serde_json::json!("512"))
.is_err()
);
assert_eq!(group.options().min_http_range_size, Some(32 * 1024));
}
#[test]
fn test_bt_metadata_defaults() {
let group = RequestGroup::new(
GroupId::new(8),
vec!["http://example.com/file.zip".to_string()],
DownloadOptions::default(),
);
assert_eq!(
group.get_bt_num_pieces(),
0,
"bt_num_pieces should default to 0"
);
assert_eq!(
group.get_bt_piece_length(),
0,
"bt_piece_length should default to 0"
);
assert_eq!(
group.get_bt_info_hash_hex(),
None,
"bt_info_hash_hex should default to None"
);
}
#[test]
fn test_set_bt_metadata() {
let group = RequestGroup::new(
GroupId::new(9),
vec!["magnet:?xt=urn:btih:abc123def456".to_string()],
DownloadOptions::default(),
);
group.set_bt_metadata(
100,
262144,
"abc123def456789012345678901234567890abcd".to_string(),
);
assert_eq!(group.get_bt_num_pieces(), 100);
assert_eq!(group.get_bt_piece_length(), 262144); assert_eq!(
group.get_bt_info_hash_hex(),
Some("abc123def456789012345678901234567890abcd".to_string())
);
}
#[test]
fn test_bt_metadata_update() {
let group = RequestGroup::new(
GroupId::new(10),
vec!["bt://test.torrent".to_string()],
DownloadOptions::default(),
);
group.set_bt_metadata(50, 16384, "first_hash".to_string());
assert_eq!(group.get_bt_num_pieces(), 50);
group.set_bt_metadata(200, 524288, "updated_hash".to_string());
assert_eq!(group.get_bt_num_pieces(), 200);
assert_eq!(group.get_bt_piece_length(), 524288);
assert_eq!(
group.get_bt_info_hash_hex(),
Some("updated_hash".to_string())
);
}
#[test]
fn test_bt_info_hash_hex() {
let group = RequestGroup::new(
GroupId::new(11),
vec!["magnet:?xt=urn:btih:test".to_string()],
DownloadOptions::default(),
);
group.set_bt_metadata(10, 1024, "async_test_hash".to_string());
let hash = group.get_bt_info_hash_hex();
assert_eq!(hash, Some("async_test_hash".to_string()));
}
#[test]
fn test_try_update_option_new_runtime_changeable() {
let gid = GroupId::new(1);
let uris = vec!["http://example.com/file".to_string()];
let mut group = RequestGroup::new(gid, uris, DownloadOptions::default());
assert!(
group
.try_update_option("max-connection-per-server", serde_json::json!(4))
.unwrap()
);
assert_eq!(group.options().max_connection_per_server, Some(4));
assert!(
group
.try_update_option("bt-max-upload-slots", serde_json::json!(8))
.unwrap()
);
assert_eq!(group.options().bt_max_upload_slots, Some(8));
assert!(
group
.try_update_option("bt-snubbed-timeout", serde_json::json!(120))
.unwrap()
);
assert_eq!(group.options().bt_snubbed_timeout, Some(120));
assert!(
group
.try_update_option("bt-optimistic-unchoke-interval", serde_json::json!(45))
.unwrap()
);
assert_eq!(group.options().bt_optimistic_unchoke_interval, Some(45));
assert!(
group
.try_update_option("bt-endgame-threshold", serde_json::json!(50))
.unwrap()
);
assert_eq!(group.options().bt_endgame_threshold, 50);
assert!(
group
.try_update_option("seed-time", serde_json::json!(3600))
.unwrap()
);
assert_eq!(group.options().seed_time, Some(3600.0));
assert!(
group
.try_update_option("seed-ratio", serde_json::json!(2.0))
.unwrap()
);
assert_eq!(group.options().seed_ratio, Some(2.0));
assert!(
group
.try_update_option("max-download-limit", serde_json::json!("100K"))
.unwrap()
);
assert_eq!(group.options().max_download_limit, Some(100 * 1024));
assert!(
group
.try_update_option("max-tries", serde_json::json!("7"))
.unwrap()
);
assert_eq!(group.options().max_retries, 7);
assert!(
group
.try_update_option("bt-force-encrypt", serde_json::json!("true"))
.unwrap()
);
assert!(group.options().bt_force_encrypt);
assert!(
group
.try_update_option("bt-seed-unverified", serde_json::json!("true"))
.unwrap()
);
assert!(group.options().bt_seed_unverified);
assert!(
group
.try_update_option("allow-overwrite", serde_json::json!("true"))
.unwrap()
);
assert_eq!(
group.runtime_options().get("allow-overwrite"),
Some(&serde_json::json!("true"))
);
assert!(
group
.try_update_option("allow-overwrite", serde_json::json!("maybe"))
.is_err()
);
let previous_limit = group.options().max_download_limit;
let error = group
.try_update_option("max-download-limit", serde_json::json!("badvalue"))
.expect_err("invalid size must be rejected");
assert!(error.contains("max-download-limit"));
assert_eq!(group.options().max_download_limit, previous_limit);
let previous_retries = group.options().max_retries;
assert!(
group
.try_update_option("max-tries", serde_json::json!("not-a-number"))
.is_err()
);
assert_eq!(group.options().max_retries, previous_retries);
let previous_dht = group.options().enable_dht;
assert!(
group
.try_update_option("enable-dht", serde_json::json!("maybe"))
.is_err()
);
assert_eq!(group.options().enable_dht, previous_dht);
assert!(
!group
.try_update_option("unknown-option", serde_json::json!(1))
.unwrap()
);
}
#[test]
fn test_effective_option_snapshot_overlays_only_applied_runtime_changes() {
let mut group = RequestGroup::new(
GroupId::new(42),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.set_option_snapshot(std::collections::HashMap::from([
("dir".to_string(), serde_json::json!("/initial")),
("max-download-limit".to_string(), serde_json::json!("1024")),
]));
group
.apply_runtime_options(std::collections::HashMap::from([(
"max-download-limit".to_string(),
serde_json::json!("2048"),
)]))
.expect("runtime option update should apply");
let effective = group
.effective_option_snapshot()
.expect("task snapshot should be present");
assert_eq!(effective.get("dir"), Some(&serde_json::json!("/initial")));
assert_eq!(
effective.get("max-download-limit"),
Some(&serde_json::json!("2048"))
);
}
#[test]
fn test_file_not_found_limit_uses_effective_group_options() {
let mut group = RequestGroup::new(
GroupId::new(44),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.set_option_snapshot(std::collections::HashMap::from([(
"max-file-not-found".to_string(),
serde_json::json!("3"),
)]));
assert_eq!(group.max_file_not_found(), 3);
assert_eq!(
group.record_file_not_found(),
DownloadResultCode::ResourceNotFound
);
assert!(group.can_retry_file_not_found());
assert_eq!(
group.record_file_not_found(),
DownloadResultCode::ResourceNotFound
);
assert!(group.can_retry_file_not_found());
assert_eq!(
group.record_file_not_found(),
DownloadResultCode::MaxFileNotFound
);
assert!(!group.can_retry_file_not_found());
}
#[test]
fn test_file_not_found_zero_stops_without_retry() {
let mut group = RequestGroup::new(
GroupId::new(45),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.set_option_snapshot(std::collections::HashMap::from([(
"max-file-not-found".to_string(),
serde_json::json!("0"),
)]));
assert_eq!(
group.record_file_not_found(),
DownloadResultCode::ResourceNotFound
);
assert!(!group.can_retry_file_not_found());
}
#[test]
fn test_download_result_preserves_effective_option_snapshot() {
let mut group = RequestGroup::new(
GroupId::new(43),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.set_option_snapshot(std::collections::HashMap::from([
("dir".to_string(), serde_json::json!("/initial")),
("max-download-limit".to_string(), serde_json::json!("1024")),
]));
group
.apply_runtime_options(std::collections::HashMap::from([(
"max-download-limit".to_string(),
serde_json::json!("2048"),
)]))
.expect("runtime option update should apply");
let result = group.create_download_result();
let options = result
.option_snapshot()
.expect("stopped result must retain the group's effective option state");
assert_eq!(options.get("dir"), Some(&serde_json::json!("/initial")));
assert_eq!(
options.get("max-download-limit"),
Some(&serde_json::json!("2048"))
);
}
#[test]
fn stopped_download_result_zeroes_transfer_speeds() {
let group = RequestGroup::new(
GroupId::new(44),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.set_total_length(1024);
group.set_download_speed_cached(512);
group.set_upload_speed_cached(256);
group.mark_complete();
let result = group.create_download_result();
assert_eq!(result.status, super::status::DownloadStatus::Complete);
assert_eq!(result.download_speed, 0);
assert_eq!(result.upload_speed, 0);
}
#[test]
fn test_command_counter_does_not_underflow() {
let group = RequestGroup::new(
GroupId::new(1),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
assert_eq!(group.dec_commands(), 0);
assert_eq!(group.num_commands(), 0);
group.inc_commands();
assert_eq!(group.dec_commands(), 1);
assert_eq!(group.num_commands(), 0);
}
#[test]
fn test_runtime_option_updates_populate_execution_fields() {
let mut group = RequestGroup::new(
GroupId::new(2),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
assert!(
group
.try_update_option("check-integrity", serde_json::json!("true"))
.unwrap()
);
assert!(
group
.try_update_option("conditional-get", serde_json::json!("true"))
.unwrap()
);
assert!(
group
.try_update_option("connect-timeout", serde_json::json!("12"))
.unwrap()
);
assert!(
group
.try_update_option("lowest-speed-limit", serde_json::json!("4K"))
.unwrap()
);
assert!(
group
.try_update_option("timeout", serde_json::json!("30"))
.unwrap()
);
assert!(
group
.try_update_option("remote-time", serde_json::json!("true"))
.unwrap()
);
assert!(
group
.try_update_option("ftp-pasv", serde_json::json!("false"))
.unwrap()
);
assert!(
group
.try_update_option("ftp-user", serde_json::json!("alice"))
.unwrap()
);
assert!(
group
.try_update_option("ftp-passwd", serde_json::json!("secret"))
.unwrap()
);
assert!(
group
.try_update_option("http-auth-challenge", serde_json::json!("true"))
.unwrap()
);
assert!(
group
.try_update_option("http-user", serde_json::json!("bob"))
.unwrap()
);
assert!(
group
.try_update_option("http-passwd", serde_json::json!("password"))
.unwrap()
);
assert!(
group
.try_update_option("metalink-location", serde_json::json!("JP"))
.unwrap()
);
assert!(
group
.try_update_option("metalink-version", serde_json::json!("4.0"))
.unwrap()
);
assert!(
group
.try_update_option("follow-metalink", serde_json::json!("mem"))
.unwrap()
);
assert!(
group
.try_update_option(
"bt-tracker",
serde_json::json!("https://tracker.test/announce")
)
.unwrap()
);
let options = group.options();
assert!(options.check_integrity);
assert!(options.conditional_get);
assert_eq!(options.connect_timeout, Some(12));
assert_eq!(options.lowest_speed_limit, Some(4 * 1024));
assert_eq!(options.timeout, Some(30));
assert!(options.remote_time);
assert!(!options.ftp_pasv);
assert_eq!(options.ftp_user.as_deref(), Some("alice"));
assert_eq!(options.ftp_passwd.as_deref(), Some("secret"));
assert!(options.http_auth_challenge);
assert_eq!(options.http_user.as_deref(), Some("bob"));
assert_eq!(options.http_passwd.as_deref(), Some("password"));
assert_eq!(options.metalink_location.as_deref(), Some("JP"));
assert_eq!(options.metalink_version.as_deref(), Some("4.0"));
assert_eq!(options.follow_metalink, Some(super::FollowMode::Memory));
assert_eq!(
options
.bt_tracker
.as_ref()
.and_then(|values| values.first())
.map(String::as_str),
Some("https://tracker.test/announce")
);
}
#[test]
fn test_runtime_option_enum_validation_is_shared_with_registry() {
assert!(
RequestGroup::validate_option_update(
"metalink-preferred-protocol",
&serde_json::json!("https")
)
.unwrap()
);
assert!(
RequestGroup::validate_option_update(
"metalink-preferred-protocol",
&serde_json::json!("gopher")
)
.is_err()
);
assert!(RequestGroup::validate_option_update("ftp-type", &serde_json::json!("ascii")).unwrap());
assert!(
RequestGroup::validate_option_update("ftp-type", &serde_json::json!("invalid")).is_err()
);
}
#[cfg(feature = "bittorrent")]
#[test]
fn test_option_map_distinguishes_omitted_and_explicit_zero_seed_time() {
let omitted = DownloadOptions::from_option_strings(&std::collections::HashMap::new());
assert_eq!(omitted.seed_time, None);
let explicit_zero = DownloadOptions::from_option_strings(&std::collections::HashMap::from([(
"seed-time".to_string(),
"0".to_string(),
)]));
assert_eq!(explicit_zero.seed_time, Some(0.0));
}
#[test]
fn test_is_removed_reflects_status() {
let mut group = RequestGroup::new(
GroupId::new(1),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
assert!(!group.is_removed(), "fresh group should not be removed");
group.remove().unwrap();
assert!(
group.is_removed(),
"is_removed() must return true after group.remove()"
);
}
#[test]
fn test_is_removed_returns_false_when_write_locked() {
let mut group = RequestGroup::new(
GroupId::new(1),
vec!["http://example.com/file".to_string()],
DownloadOptions::default(),
);
group.remove().unwrap();
let _guard = group.status.write().unwrap();
assert!(
!group.is_removed(),
"is_removed() should return false when the status write lock is held (try_read fails)"
);
}
#[test]
fn test_download_context_default_is_none() {
let group = RequestGroup::new(
GroupId::new(1),
vec!["http://example.com/file.zip".to_string()],
DownloadOptions::default(),
);
assert!(
group.get_download_context().is_none(),
"download_context should default to None for non-BT downloads"
);
}
#[test]
fn test_set_and_get_download_context() {
let group = RequestGroup::new(
GroupId::new(2),
vec!["bt://test".to_string()],
DownloadOptions::default(),
);
let ctx = Arc::new(DownloadContext::new(1024, 4096, "/tmp/file.bin".into()));
group.set_download_context(Arc::clone(&ctx));
let retrieved = group.get_download_context();
assert!(retrieved.is_some(), "download_context should be set");
assert!(
Arc::ptr_eq(&retrieved.unwrap(), &ctx),
"should return the same Arc"
);
}
#[test]
#[cfg(feature = "bittorrent")]
fn test_shared_bt_bitfield_updates_only_valid_piece_bits() {
let group = RequestGroup::new(
GroupId::new(6),
vec!["magnet:?xt=urn:btih:shared".to_string()],
DownloadOptions::default(),
);
let shared = Arc::new(std::sync::RwLock::new(vec![0u8; 2]));
group.set_bt_bitfield_shared(Arc::clone(&shared));
group.update_bt_bitfield_piece(0, 9);
group.update_bt_bitfield_piece(8, 9);
group.update_bt_bitfield_piece(9, 9);
group.update_bt_bitfield_piece(u32::MAX, 9);
assert_eq!(group.get_bt_bitfield(), Some(vec![0x80, 0x80]));
assert_eq!(
shared
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_slice(),
&[0x80, 0x80]
);
}
#[test]
fn test_uri_memory_stats_reports_transferred_uri_duplication() {
let uri = "http://example.com/file.zip".to_string();
let group = RequestGroup::new(
GroupId::new(4),
vec![uri.clone()],
DownloadOptions::default(),
);
let mut ctx = DownloadContext::new(1024, 4096, "/tmp/file.bin".into());
ctx.get_file_entries_mut()[0].add_uri(&uri);
group.set_download_context(Arc::new(ctx));
let stats = group.uri_memory_stats();
assert_eq!(stats.stored_count, 2);
assert_eq!(stats.duplicate_logical_bytes, uri.len());
assert!(stats.capacity_bytes >= stats.logical_bytes);
assert!(stats.duplicate_capacity_bytes >= uri.len());
}
#[test]
fn test_torrent_attribute_on_download_context() {
use crate::download::download_context::{BtFileMode, ContextAttributeType, TorrentAttribute};
let group = RequestGroup::new(
GroupId::new(3),
vec!["bt://test".to_string()],
DownloadOptions::default(),
);
let info_hash = "0123456789abcdef0123456789abcdef01234567";
let mut ctx = DownloadContext::new(1024, 4096, "/tmp/file.bin".into());
let ta = TorrentAttribute {
name: "test-torrent".to_string(),
mode: BtFileMode::Single,
announce_list: vec![vec!["http://tracker.example.com/announce".to_string()]],
nodes: Vec::new(),
info_hash: info_hash.to_string(),
metadata: Vec::new(),
metadata_size: 0,
private_torrent: false,
creation_date: 0,
comment: String::new(),
created_by: String::new(),
url_list: Vec::new(),
};
ctx.set_attribute(ContextAttributeType::BitTorrent, Box::new(ta));
group.set_download_context(Arc::new(ctx));
let ctx_ref = group.get_download_context().unwrap();
let hash = ctx_ref.get_bt_info_hash_hex();
assert_eq!(hash, Some(info_hash.to_string()));
}