rget-cli 0.3.3

High-performance resumable download manager
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
//! Resume and crash-recovery tests (PRD §33, §39).
//!
//! These kill the **real binary** with SIGKILL rather than simulating an error
//! inside Rust. That is the only way to exercise what the PRD actually promises:
//! that the process can die after any instruction and the file still comes out
//! byte-for-byte correct.

mod harness;

use std::process::Stdio;
use std::time::{Duration, Instant};

use harness::{Config, Server, Workspace, sha256};
use tokio::process::Command;

const BIN: &str = env!("CARGO_BIN_EXE_rget");

/// A body big enough, and slow enough, that we can reliably kill mid-transfer.
fn slow_config(size: usize) -> Config {
    Config {
        // ~1.6 MiB/s per connection.
        throttle: Some((32 * 1024, Duration::from_millis(20))),
        ..Config::with_body(size)
    }
}

fn rget(ws: &Workspace, args: &[&str]) -> Command {
    let mut cmd = Command::new(BIN);
    cmd.args(args)
        .env("RGET_DB", ws.db())
        // Keep the child's output out of the test log unless we inspect it.
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true);
    cmd
}

/// Durable progress as recorded in the shared state database.
fn durable_bytes(ws: &Workspace) -> u64 {
    let store = ws.store();
    store
        .list()
        .unwrap()
        .first()
        .map(|d| d.durable_bytes)
        .unwrap_or(0)
}

/// Wait until the running download has checkpointed at least `min` bytes.
async fn wait_for_progress(ws: &Workspace, min: u64, timeout: Duration) -> u64 {
    let deadline = Instant::now() + timeout;
    loop {
        let got = durable_bytes(ws);
        if got >= min {
            return got;
        }
        if Instant::now() > deadline {
            panic!("no durable progress after {timeout:?} (saw {got} bytes)");
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

#[tokio::test]
async fn survives_repeated_kills_and_produces_the_right_file() {
    let ws = Workspace::new("kills");
    // Large enough that three successive kills all land mid-transfer even when
    // the machine is busy: each round resumes further in, so the remaining work
    // shrinks and a small file could finish between two polls.
    let server = Server::start(slow_config(24 << 20)).await;
    let body = server.body();
    let url = server.url("/big.bin");
    let dest = ws.path("big.bin");

    let mut previous = 0u64;
    let mut kills = 0;
    // Kill it three times at increasing depths, exactly like PRD §39.
    for round in 0..3 {
        let mut child = rget(
            &ws,
            &[
                "--quiet",
                &url,
                "--dir",
                &ws.dir.to_string_lossy(),
                "-c",
                "4",
            ],
        )
        .spawn()
        .expect("spawn rget");

        let target = previous + (256 * 1024);
        let reached = wait_for_progress(&ws, target, Duration::from_secs(60)).await;
        child.kill().await.expect("kill rget");
        let _ = child.wait().await;

        // Progress must be monotonic across kills: nothing is ever lost.
        let after_kill = durable_bytes(&ws);
        assert!(
            after_kill >= previous,
            "round {round}: progress went backwards, {after_kill} < {previous}"
        );
        assert!(
            after_kill >= reached,
            "round {round}: checkpointed bytes vanished"
        );
        previous = after_kill;
        kills += 1;
        if previous >= body.len() as u64 {
            // It slipped over the line between two polls. That is a scheduling
            // accident, not a failure: what this test is about is that repeated
            // kills still produce the right bytes, which the checksum below
            // proves either way.
            break;
        }
    }
    assert!(kills >= 1, "the test never managed to interrupt a download");

    // Now let it finish, and make it prove the result cryptographically.
    server.set(|c| c.throttle = None);
    let digest = sha256(&body);
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "4",
            "--sha256",
            &digest,
        ],
    )
    .output()
    .await
    .expect("run rget");

    assert!(
        out.status.success(),
        "final run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        std::fs::read(&dest).unwrap(),
        body,
        "file differs from source"
    );

    // It genuinely resumed rather than starting over: total bytes served is far
    // less than four full copies.
    let served: usize = body.len();
    assert!(
        served > 0 && durable_bytes(&ws) == body.len() as u64,
        "final durable byte count should equal the file size"
    );
}

#[tokio::test]
async fn resume_does_not_refetch_completed_ranges() {
    let ws = Workspace::new("norefetch");
    let server = Server::start(slow_config(8 << 20)).await;
    let body = server.body();
    let url = server.url("/frugal.bin");

    let mut child = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
        ],
    )
    .spawn()
    .unwrap();
    let reached = wait_for_progress(&ws, 1 << 20, Duration::from_secs(60)).await;
    child.kill().await.unwrap();
    let _ = child.wait().await;
    // Read it again after the kill: the child may have checkpointed more
    // between the poll that satisfied us and the signal actually landing.
    let durable_at_kill = durable_bytes(&ws);

    // Measure the second run alone, and let it finish quickly.
    server.reset_stats();
    server.set(|c| c.throttle = None);
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
            "--sha256",
            &sha256(&body),
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        out.status.success(),
        "resume failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // The honest test of "it resumed": the second run pulled roughly the
    // outstanding bytes, not the whole file again. A little slack covers the
    // probe plus the bounded re-download of anything not yet durable.
    let served = server.bytes_served();
    let outstanding = body.len() - durable_at_kill as usize;
    assert!(
        served < outstanding + (2 * 1024 * 1024),
        "resume re-downloaded too much: the second run served {served} bytes, but only \
         {outstanding} were outstanding ({durable_at_kill} of {} durable at kill time; the poll \
         that triggered the kill saw {reached})",
        body.len()
    );
    assert!(
        served > 0,
        "the second run should have transferred something"
    );
    assert_eq!(std::fs::read(ws.path("frugal.bin")).unwrap(), body);
}

#[tokio::test]
async fn refuses_to_resume_when_the_remote_changed() {
    let ws = Workspace::new("changed");
    let server = Server::start(slow_config(8 << 20)).await;
    let url = server.url("/mutable.bin");

    let mut child = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
        ],
    )
    .spawn()
    .unwrap();
    wait_for_progress(&ws, 512 * 1024, Duration::from_secs(60)).await;
    child.kill().await.unwrap();
    let _ = child.wait().await;

    // The resource is replaced: new content, new ETag, different size.
    let new_body = harness::test_body((8 << 20) + 4096);
    server.set(|c| {
        c.body = new_body.clone();
        c.etag = Some("\"v2\"".into());
        c.throttle = None;
    });

    let out = rget(&ws, &[&url, "--dir", &ws.dir.to_string_lossy()])
        .output()
        .await
        .unwrap();
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(!out.status.success(), "must refuse to resume");
    assert!(
        stderr.contains("Remote file changed"),
        "expected a clear explanation, got: {stderr}"
    );
    assert!(stderr.contains("could corrupt"), "{stderr}");
    assert!(
        stderr.contains("--restart"),
        "should suggest the way out: {stderr}"
    );

    // --restart is the explicit way forward, and it yields the *new* file.
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--restart",
            "--sha256",
            &sha256(&new_body),
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        out.status.success(),
        "--restart should succeed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(std::fs::read(ws.path("mutable.bin")).unwrap(), new_body);
}

#[tokio::test]
async fn recovers_after_the_server_disappears_and_returns() {
    let ws = Workspace::new("netloss");
    let server = Server::start(slow_config(4 << 20)).await;
    let body = server.body();
    let url = server.url("/patchy.bin");

    let mut child = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
        ],
    )
    .spawn()
    .unwrap();
    wait_for_progress(&ws, 256 * 1024, Duration::from_secs(60)).await;
    child.kill().await.unwrap();
    let _ = child.wait().await;
    let before_outage = durable_bytes(&ws);

    // The network goes away: connections are accepted then dropped.
    server.set(|c| c.refuse = true);
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--retries",
            "2",
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        !out.status.success(),
        "should fail while the server is down"
    );
    // Crucially, the outage cost us nothing.
    assert_eq!(
        durable_bytes(&ws),
        before_outage,
        "a failed attempt must not lose progress"
    );

    // The network comes back.
    server.set(|c| {
        c.refuse = false;
        c.throttle = None;
    });
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--sha256",
            &sha256(&body),
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        out.status.success(),
        "should finish once the server returns: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(std::fs::read(ws.path("patchy.bin")).unwrap(), body);
}

#[tokio::test]
async fn state_database_survives_a_kill_mid_update() {
    let ws = Workspace::new("dbkill");
    let server = Server::start(slow_config(8 << 20)).await;
    let url = server.url("/dbtest.bin");

    // Kill repeatedly with no delay alignment, so some kills land inside a
    // commit transaction. The database must always still be readable.
    for round in 0..6 {
        let mut child = rget(
            &ws,
            &[
                "--quiet",
                &url,
                "--dir",
                &ws.dir.to_string_lossy(),
                "-c",
                "4",
            ],
        )
        .spawn()
        .unwrap();
        tokio::time::sleep(Duration::from_millis(300 + round * 97)).await;
        child.kill().await.unwrap();
        let _ = child.wait().await;

        // Reopening and reading must work every single time (PRD Invariant 7).
        let store = ws.store();
        let downloads = store.list().expect("state database must stay readable");
        if let Some(d) = downloads.first() {
            let ranges = store.load_ranges(&d.id).expect("ranges must stay readable");
            let sum: u64 = ranges.iter().map(|r| r.bytes_written).sum();
            assert_eq!(
                sum, d.durable_bytes,
                "round {round}: durable_bytes disagrees with the range rows"
            );
        }
    }

    server.set(|c| c.throttle = None);
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--sha256",
            &sha256(&server.body()),
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        out.status.success(),
        "should still finish correctly: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

#[tokio::test]
async fn sigint_pauses_and_saves_progress() {
    let ws = Workspace::new("sigint");
    // Big enough that the signal always arrives mid-transfer, even under load.
    let server = Server::start(slow_config(24 << 20)).await;
    let url = server.url("/interrupt.bin");

    let child = rget(&ws, &[&url, "--dir", &ws.dir.to_string_lossy(), "-c", "2"])
        .spawn()
        .unwrap();
    wait_for_progress(&ws, 256 * 1024, Duration::from_secs(60)).await;

    let pid = child.id().expect("child pid") as i32;
    unsafe {
        // SIGINT, as Ctrl+C would.
        libc_kill(pid, 2);
    }

    let out = tokio::time::timeout(Duration::from_secs(20), child.wait_with_output())
        .await
        .expect("graceful shutdown must not hang")
        .unwrap();

    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("paused") || stderr.contains("Pausing"),
        "{stderr}"
    );
    assert_eq!(
        out.status.code(),
        Some(130),
        "interrupted download should exit 130: {stderr}"
    );

    let store = ws.store();
    let record = &store.list().unwrap()[0];
    // Normally this is `paused`. If the machine is loaded enough that graceful
    // shutdown misses its deadline, the bounded force-exit leaves it
    // `downloading` instead — also correct, and also resumable. What must hold
    // either way is that progress was saved and the download can continue.
    assert!(
        record.status.is_resumable(),
        "status after Ctrl+C should be resumable, got {}",
        record.status
    );
    assert!(record.durable_bytes > 0);

    // And the same command resumes it.
    server.set(|c| c.throttle = None);
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--sha256",
            &sha256(&server.body()),
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        out.status.success(),
        "resume after Ctrl+C failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// PRD §39's definition of done, run as a loop. Excluded from the default run
/// because it takes minutes; `cargo nextest run --run-ignored only` exercises it.
#[tokio::test]
#[ignore = "long-running soak test"]
async fn soak_random_kills_always_produce_the_right_checksum() {
    for iteration in 0..15u64 {
        let ws = Workspace::new(&format!("soak{iteration}"));
        let server = Server::start(slow_config(4 << 20)).await;
        let body = server.body();
        let url = server.url("/soak.bin");
        let digest = sha256(&body);

        // Between one and four kills at pseudo-random depths.
        let kills = 1 + (iteration % 4);
        for k in 0..kills {
            let mut child = rget(
                &ws,
                &[
                    "--quiet",
                    &url,
                    "--dir",
                    &ws.dir.to_string_lossy(),
                    "-c",
                    "4",
                ],
            )
            .spawn()
            .unwrap();
            let delay = 120 + ((iteration * 31 + k * 71) % 400);
            tokio::time::sleep(Duration::from_millis(delay)).await;
            child.kill().await.unwrap();
            let _ = child.wait().await;

            // The server also flaps.
            if k % 2 == 0 {
                server.set(|c| c.kill_after = Some(64 * 1024));
            } else {
                server.set(|c| c.kill_after = None);
            }
        }

        server.set(|c| {
            c.kill_after = None;
            c.throttle = None;
        });
        let out = rget(
            &ws,
            &[
                "--quiet",
                &url,
                "--dir",
                &ws.dir.to_string_lossy(),
                "-c",
                "4",
                "--sha256",
                &digest,
            ],
        )
        .output()
        .await
        .unwrap();
        assert!(
            out.status.success(),
            "iteration {iteration} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            std::fs::read(ws.path("soak.bin")).unwrap(),
            body,
            "iteration {iteration} produced a different file"
        );
    }
}

// -- management commands ---------------------------------------------------

#[tokio::test]
async fn list_info_resume_and_forget() {
    let ws = Workspace::new("manage");
    let server = Server::start(slow_config(8 << 20)).await;
    let url = server.url("/managed.bin");

    let mut child = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
        ],
    )
    .spawn()
    .unwrap();
    wait_for_progress(&ws, 256 * 1024, Duration::from_secs(60)).await;
    child.kill().await.unwrap();
    let _ = child.wait().await;

    // list
    let out = rget(&ws, &["list"]).output().await.unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success());
    assert!(stdout.contains("managed.bin"), "{stdout}");
    assert!(stdout.contains("ID"), "{stdout}");

    let id = ws
        .store()
        .list()
        .unwrap()
        .first()
        .map(|d| d.id.clone())
        .expect("a download exists");

    // info
    let out = rget(&ws, &["info", &id[..4]]).output().await.unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(stdout.contains("managed.bin"), "{stdout}");
    assert!(stdout.contains("ranges"), "{stdout}");
    assert!(stdout.contains("\"v1\""), "etag should be shown: {stdout}");

    // resume by id, with no other flags
    server.set(|c| c.throttle = None);
    let out = rget(&ws, &["--quiet", "resume", &id])
        .output()
        .await
        .unwrap();
    assert!(
        out.status.success(),
        "resume by id failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        std::fs::read(ws.path("managed.bin")).unwrap(),
        server.body()
    );

    // forget leaves the file alone
    let out = rget(&ws, &["forget", &id]).output().await.unwrap();
    assert!(out.status.success());
    assert!(
        ws.path("managed.bin").exists(),
        "forget must not delete files"
    );
    assert!(ws.store().list().unwrap().is_empty());
}

#[tokio::test]
async fn forget_all_and_forget_all_files() {
    let ws = Workspace::new("forgetall");
    let server = Server::start(Config::with_body(64 * 1024)).await;

    for name in ["one.bin", "two.bin"] {
        let url = server.url(&format!("/{name}"));
        let out = rget(
            &ws,
            &[
                "--quiet",
                &url,
                "--dir",
                &ws.dir.to_string_lossy(),
                "-o",
                name,
            ],
        )
        .output()
        .await
        .unwrap();
        assert!(
            out.status.success(),
            "download {name} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert!(ws.path(name).exists());
    }
    assert_eq!(ws.store().list().unwrap().len(), 2);

    // --all drops metadata, leaves files
    let out = rget(&ws, &["forget", "--all"]).output().await.unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(ws.store().list().unwrap().is_empty());
    assert!(ws.path("one.bin").exists());
    assert!(ws.path("two.bin").exists());

    // Fresh downloads, then --all --files removes metadata and files.
    for name in ["three.bin", "four.bin"] {
        let url = server.url(&format!("/{name}"));
        let out = rget(
            &ws,
            &[
                "--quiet",
                &url,
                "--dir",
                &ws.dir.to_string_lossy(),
                "-o",
                name,
            ],
        )
        .output()
        .await
        .unwrap();
        assert!(
            out.status.success(),
            "download {name} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }
    assert_eq!(ws.store().list().unwrap().len(), 2);

    let out = rget(&ws, &["forget", "--all", "--files"])
        .output()
        .await
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(ws.store().list().unwrap().is_empty());
    assert!(
        !ws.path("three.bin").exists(),
        "forget --all --files must delete files"
    );
    assert!(
        !ws.path("four.bin").exists(),
        "forget --all --files must delete files"
    );
    // Earlier files from the metadata-only forget are untouched.
    assert!(ws.path("one.bin").exists());
    assert!(ws.path("two.bin").exists());
}

#[tokio::test]
async fn resume_all_picks_up_every_interrupted_download() {
    let ws = Workspace::new("resumeall");
    let server = Server::start(slow_config(4 << 20)).await;
    let body = server.body();

    for name in ["a.bin", "b.bin"] {
        let url = server.url(&format!("/{name}"));
        let mut child = rget(
            &ws,
            &[
                "--quiet",
                &url,
                "--dir",
                &ws.dir.to_string_lossy(),
                "-c",
                "2",
            ],
        )
        .spawn()
        .unwrap();
        // Each download needs its own progress before we stop it.
        let target = 128 * 1024;
        let deadline = Instant::now() + Duration::from_secs(60);
        loop {
            let found = ws
                .store()
                .list()
                .unwrap()
                .iter()
                .find(|d| d.filename == name)
                .map(|d| d.durable_bytes)
                .unwrap_or(0);
            if found >= target || Instant::now() > deadline {
                break;
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
        child.kill().await.unwrap();
        let _ = child.wait().await;
    }

    assert_eq!(ws.store().list_resumable().unwrap().len(), 2);

    server.set(|c| c.throttle = None);
    let out = rget(&ws, &["--quiet", "resume", "--all"])
        .output()
        .await
        .unwrap();
    assert!(
        out.status.success(),
        "resume --all failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    for name in ["a.bin", "b.bin"] {
        assert_eq!(
            std::fs::read(ws.path(name)).unwrap(),
            body,
            "{name} did not finish correctly"
        );
    }
    assert!(ws.store().list_resumable().unwrap().is_empty());
}

#[tokio::test]
async fn json_output_is_machine_readable() {
    let ws = Workspace::new("json");
    let server = Server::start(Config::with_body(2 << 20)).await;
    let url = server.url("/data.bin");

    let out = rget(&ws, &["--json", &url, "--dir", &ws.dir.to_string_lossy()])
        .output()
        .await
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        !stdout.contains('\x1b'),
        "JSON output must have no ANSI codes"
    );

    let mut kinds = Vec::new();
    for line in stdout.lines().filter(|l| !l.trim().is_empty()) {
        let value: serde_json::Value =
            serde_json::from_str(line).unwrap_or_else(|e| panic!("bad JSON line `{line}`: {e}"));
        kinds.push(value["event"].as_str().unwrap_or("?").to_string());
    }
    assert!(kinds.contains(&"download_started".to_string()), "{kinds:?}");
    assert!(
        kinds.contains(&"download_completed".to_string()),
        "{kinds:?}"
    );
}

#[tokio::test]
async fn non_interactive_output_has_no_ansi_escapes() {
    let ws = Workspace::new("plain");
    let server = Server::start(Config::with_body(1 << 20)).await;
    let url = server.url("/quiet.bin");

    let out = rget(&ws, &[&url, "--dir", &ws.dir.to_string_lossy()])
        .output()
        .await
        .unwrap();
    assert!(out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        !stderr.contains('\x1b'),
        "piped stderr must be free of cursor control: {stderr:?}"
    );
}

#[tokio::test]
async fn help_is_shown_when_no_url_is_given() {
    let ws = Workspace::new("nohelp");
    let out = rget(&ws, &[]).output().await.unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("Usage"), "{stdout}");
    assert!(!out.status.success());
}

/// `kill(2)`, declared directly so the test suite needs no `libc` dependency.
unsafe fn libc_kill(pid: i32, sig: i32) {
    unsafe extern "C" {
        fn kill(pid: i32, sig: i32) -> i32;
    }
    unsafe {
        kill(pid, sig);
    }
}

/// Deleting the partially-downloaded file is a perfectly ordinary thing to do,
/// and it used to leave the download wedged: the file was recreated empty on
/// open, so the identity check saw a "replaced" file and refused, with no
/// obvious way out.
#[tokio::test]
async fn deleting_the_file_starts_the_download_over() {
    let ws = Workspace::new("deleted");
    let server = Server::start(slow_config(8 << 20)).await;
    let url = server.url("/deleted.bin");
    let dest = ws.path("deleted.bin");

    let mut child = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
        ],
    )
    .spawn()
    .unwrap();
    wait_for_progress(&ws, 256 * 1024, Duration::from_secs(60)).await;
    child.kill().await.unwrap();
    let _ = child.wait().await;

    // The user tidies up by hand.
    std::fs::remove_file(&dest).unwrap();
    assert!(ws.store().list().unwrap()[0].durable_bytes > 0);

    server.set(|c| c.throttle = None);
    let out = rget(
        &ws,
        &[
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--sha256",
            &sha256(&server.body()),
        ],
    )
    .output()
    .await
    .unwrap();

    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "deleting the file should start it over, not wedge it: {stderr}"
    );
    assert!(
        !stderr.contains("not the file this download was writing to"),
        "{stderr}"
    );
    assert!(
        stderr.contains("nothing to resume"),
        "should say why: {stderr}"
    );
    assert_eq!(std::fs::read(&dest).unwrap(), server.body());
}

/// The same, for a download that had already finished.
#[tokio::test]
async fn deleting_a_completed_file_downloads_it_again() {
    let ws = Workspace::new("deleted-complete");
    let server = Server::start(Config::with_body(2 << 20)).await;
    let url = server.url("/again.bin");
    let dest = ws.path("again.bin");

    let out = rget(&ws, &["--quiet", &url, "--dir", &ws.dir.to_string_lossy()])
        .output()
        .await
        .unwrap();
    assert!(out.status.success());
    assert_eq!(std::fs::read(&dest).unwrap(), server.body());

    std::fs::remove_file(&dest).unwrap();
    let out = rget(&ws, &["--quiet", &url, "--dir", &ws.dir.to_string_lossy()])
        .output()
        .await
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(std::fs::read(&dest).unwrap(), server.body());
}

/// Truncating the file to nothing is the same situation as deleting it: there
/// are no bytes left to protect.
#[tokio::test]
async fn emptying_the_file_starts_the_download_over() {
    let ws = Workspace::new("emptied");
    let server = Server::start(slow_config(8 << 20)).await;
    let url = server.url("/emptied.bin");
    let dest = ws.path("emptied.bin");

    let mut child = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "-c",
            "2",
        ],
    )
    .spawn()
    .unwrap();
    wait_for_progress(&ws, 256 * 1024, Duration::from_secs(60)).await;
    child.kill().await.unwrap();
    let _ = child.wait().await;

    std::fs::write(&dest, b"").unwrap();

    server.set(|c| c.throttle = None);
    let out = rget(
        &ws,
        &[
            "--quiet",
            &url,
            "--dir",
            &ws.dir.to_string_lossy(),
            "--sha256",
            &sha256(&server.body()),
        ],
    )
    .output()
    .await
    .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(std::fs::read(&dest).unwrap(), server.body());
}