aria2-core 0.3.1

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
#[allow(unused_imports)]
use std::sync::Arc;

#[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],
        addr: "127.0.0.1:6881".parse().expect("valid test address"),
        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,
        seeder: Some(true),
        connection_duration_secs: 7,
        last_data_age_secs: 8,
        is_snubbed: false,
        is_banned: false,
    };
    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());
}

#[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_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() {
    // New RequestGroup should have all zeros/None defaults for progress fields
    let group = RequestGroup::new(
        GroupId::new(1),
        vec!["http://example.com/file.zip".to_string()],
        DownloadOptions::default(),
    );

    // Verify all atomic fields default to 0
    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(),
    );

    // Test set/get roundtrip
    group.set_completed_length(1024);
    assert_eq!(
        group.get_completed_length(),
        1024,
        "Should return 1024 after setting"
    );

    // Test update to different value
    group.set_completed_length(2048);
    assert_eq!(
        group.get_completed_length(),
        2048,
        "Should return 2048 after update"
    );

    // Test large value
    group.set_completed_length(u64::MAX);
    assert_eq!(
        group.get_completed_length(),
        u64::MAX,
        "Should handle u64::MAX"
    );

    // Test zero
    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(),
    );

    // Test set/get roundtrip
    group.set_total_length_atomic(1048576); // 1MB
    assert_eq!(
        group.get_total_length_atomic(),
        1048576,
        "Should return 1MB after setting"
    );

    // Test update
    group.set_total_length_atomic(1073741824); // 1GB
    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(),
    );

    // Default should be None
    let bf = group.get_bt_bitfield();
    assert!(bf.is_none(), "bt_bitfield should default to None");

    // Set and retrieve bitfield
    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"
    );

    // Set back to None
    group.set_bt_bitfield(None);
    let bf_none = group.get_bt_bitfield();
    assert!(
        bf_none.is_none(),
        "bt_bitfield should be None after clearing"
    );

    // Test with empty bitfield
    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(),
    ));

    // Spawn multiple tasks that read/write progress concurrently
    let mut handles = Vec::new();

    for i in 0..10 {
        let g = Arc::clone(&group);
        handles.push(tokio::spawn(async move {
            // Write progress
            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);

            // Read progress (should not deadlock)
            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();

            // Occasionally write bitfield (sync)
            if i % 3 == 0 {
                let bf = vec![i as u8; 8];
                g.set_bt_bitfield(Some(bf));
                let _retrieved = g.get_bt_bitfield();
            }

            // Small delay to increase chance of race conditions
            tokio::time::sleep(std::time::Duration::from_millis(1)).await;
        }));
    }

    // Wait for all tasks to complete without deadlock
    for handle in handles {
        handle.await.expect("Task should complete without panic");
    }

    // Verify final state is consistent
    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();

    // Values should be from one of the concurrent writers (we don't know which)
    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(),
    );

    // Test default
    assert_eq!(group.get_uploaded_length(), 0);

    // Test set/get
    group.set_uploaded_length(512);
    assert_eq!(group.get_uploaded_length(), 512);

    // Test large value
    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(),
    );

    // Test default
    assert_eq!(group.get_download_speed_cached(), 0);

    // Test realistic download speed (e.g., 5 MB/s = 5242880 bytes/s)
    group.set_download_speed_cached(5242880);
    assert_eq!(group.get_download_speed_cached(), 5242880);

    // Test speed update (simulating periodic updates)
    group.set_download_speed_cached(10485760); // 10 MB/s
    assert_eq!(group.get_download_speed_cached(), 10485760);
}

#[test]
fn test_download_options_choking_config_defaults() {
    // New DownloadOptions should have None for choking algorithm fields (opt-in)
    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() {
    // Verify that custom choking config values can be set
    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() {
    // Verify choking config fields are preserved through 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);
}

// ==================== BT Metadata Tests ====================

#[test]
fn test_bt_metadata_defaults() {
    let group = RequestGroup::new(
        GroupId::new(8),
        vec!["http://example.com/file.zip".to_string()],
        DownloadOptions::default(),
    );

    // Non-BT downloads should have 0/None defaults
    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(),
    );

    // Set BT metadata
    group.set_bt_metadata(
        100,
        262144,
        "abc123def456789012345678901234567890abcd".to_string(),
    );

    // Verify values
    assert_eq!(group.get_bt_num_pieces(), 100);
    assert_eq!(group.get_bt_piece_length(), 262144); // 256KB
    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(),
    );

    // Initial set
    group.set_bt_metadata(50, 16384, "first_hash".to_string());
    assert_eq!(group.get_bt_num_pieces(), 50);

    // Update with new values
    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(),
    );

    // Set via blocking method
    group.set_bt_metadata(10, 1024, "async_test_hash".to_string());

    // Read via sync method
    let hash = group.get_bt_info_hash_hex();
    assert_eq!(hash, Some("async_test_hash".to_string()));
}

#[test]
fn test_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());

    // max-connection-per-server
    assert!(group.update_option("max-connection-per-server", serde_json::json!(4)));
    assert_eq!(group.options().max_connection_per_server, Some(4));

    // bt-max-upload-slots
    assert!(group.update_option("bt-max-upload-slots", serde_json::json!(8)));
    assert_eq!(group.options().bt_max_upload_slots, Some(8));

    // bt-snubbed-timeout
    assert!(group.update_option("bt-snubbed-timeout", serde_json::json!(120)));
    assert_eq!(group.options().bt_snubbed_timeout, Some(120));

    // bt-optimistic-unchoke-interval
    assert!(group.update_option("bt-optimistic-unchoke-interval", serde_json::json!(45)));
    assert_eq!(group.options().bt_optimistic_unchoke_interval, Some(45));

    // bt-endgame-threshold
    assert!(group.update_option("bt-endgame-threshold", serde_json::json!(50)));
    assert_eq!(group.options().bt_endgame_threshold, 50);

    // seed-time
    assert!(group.update_option("seed-time", serde_json::json!(3600)));
    assert_eq!(group.options().seed_time, Some(3600.0));

    // seed-ratio
    assert!(group.update_option("seed-ratio", serde_json::json!(2.0)));
    assert_eq!(group.options().seed_ratio, Some(2.0));

    // RPC clients send option values as strings, including aria2 size suffixes.
    assert!(group.update_option("max-download-limit", serde_json::json!("100K")));
    assert_eq!(group.options().max_download_limit, Some(100 * 1024));
    assert!(group.update_option("max-tries", serde_json::json!("7")));
    assert_eq!(group.options().max_retries, 7);
    assert!(group.update_option("bt-force-encrypt", serde_json::json!("true")));
    assert!(group.options().bt_force_encrypt);

    // bt-seed-unverified
    assert!(group.update_option("bt-seed-unverified", serde_json::json!("true")));
    assert!(group.options().bt_seed_unverified);

    // Canonical reserved options without a dedicated execution field still
    // use the shared registry validator and remain visible after applying.
    assert!(group.update_option("allow-overwrite", serde_json::json!("true")));
    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()
    );
    // A recognized key with an invalid value must be observable by RPC
    // callers and must not partially update the group.
    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);

    // Unknown option returns false
    assert!(!group.update_option("unknown-option", serde_json::json!(1)));
}

#[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 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.update_option("check-integrity", serde_json::json!("true")));
    assert!(group.update_option("conditional-get", serde_json::json!("true")));
    assert!(group.update_option("connect-timeout", serde_json::json!("12")));
    assert!(group.update_option("lowest-speed-limit", serde_json::json!("4K")));
    assert!(group.update_option("timeout", serde_json::json!("30")));
    assert!(group.update_option("remote-time", serde_json::json!("true")));
    assert!(group.update_option("ftp-pasv", serde_json::json!("false")));
    assert!(group.update_option("ftp-user", serde_json::json!("alice")));
    assert!(group.update_option("ftp-passwd", serde_json::json!("secret")));
    assert!(group.update_option("http-auth-challenge", serde_json::json!("true")));
    assert!(group.update_option("http-user", serde_json::json!("bob")));
    assert!(group.update_option("http-passwd", serde_json::json!("password")));
    assert!(group.update_option("metalink-location", serde_json::json!("JP")));
    assert!(group.update_option("metalink-version", serde_json::json!("4.0")));
    assert!(group.update_option("follow-metalink", serde_json::json!("mem")));
    assert!(group.update_option(
        "bt-tracker",
        serde_json::json!("https://tracker.test/announce")
    ));

    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));
}

/// Verify that `is_removed()` correctly reflects the group's Removed
/// status, and that it is non-blocking (does not deadlock when the
/// status lock is contended).
#[test]
fn test_is_removed_reflects_status() {
    let mut group = RequestGroup::new(
        GroupId::new(1),
        vec!["http://example.com/file".to_string()],
        DownloadOptions::default(),
    );

    // Fresh group is in Waiting state, not Removed.
    assert!(!group.is_removed(), "fresh group should not be removed");

    // Mark as Removed (as RequestGroupMan::remove_group does).
    group.remove().unwrap();
    assert!(
        group.is_removed(),
        "is_removed() must return true after group.remove()"
    );
}

/// `is_removed()` must be safe to call while a write lock on the status
/// is held elsewhere. It uses `try_read` internally, so it should return
/// `false` (not deadlock, not block) when the lock is contended by a writer.
#[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();

    // Hold a write lock on the inner status to simulate contention.
    // This blocks try_read() on the same lock.
    let _guard = group.status.write().unwrap();
    // is_removed() uses try_read(), which fails when a write lock is held.
    // It must return false (not block, not panic).
    assert!(
        !group.is_removed(),
        "is_removed() should return false when the status write lock is held (try_read fails)"
    );
    // Lock released when _guard drops.
}

// -----------------------------------------------------------------------
// DownloadContext integration tests
// -----------------------------------------------------------------------

#[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]
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()));
}