ptox 0.2.0

A language-neutral SDK for building file-search clients across many networks.
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
use super::{temp_path, verify_sha256, FetchError, FetchRequest, Fetched, Fetcher, ProgressFn};
use async_trait::async_trait;
use futures::StreamExt;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

/// A descriptive default; many hosts (Wikimedia, etc.) reject requests without one.
const DEFAULT_USER_AGENT: &str = concat!(
    "ptox/",
    env!("CARGO_PKG_VERSION"),
    " (+https://github.com/despulii-bit/ptox)"
);

/// How many times an HTTP GET is attempted and how long to wait between tries.
///
/// The default is **no retry**: exactly one attempt. Build one with
/// [`RetryPolicy::new`] to allow retries, or [`RetryPolicy::none`] to be
/// explicit. Only transient failures are retried — connection errors, mid-stream
/// read errors, and `408`/`429`/`5xx` responses — never `4xx` client errors or a
/// hash mismatch. GETs are idempotent, so replaying them is safe.
///
/// ```
/// use ptox::RetryPolicy;
/// use std::time::Duration;
///
/// let policy = RetryPolicy::new(3).with_base_delay(Duration::from_millis(200));
/// assert_eq!(policy.max_attempts(), 3);
/// assert_eq!(policy.base_delay(), Duration::from_millis(200));
/// ```
#[derive(Debug, Clone)]
pub struct RetryPolicy {
    max_attempts: u32,
    base_delay: Duration,
    max_delay: Duration,
}

impl RetryPolicy {
    /// The default delay before the first retry (doubles for each later one).
    pub const DEFAULT_BASE_DELAY: Duration = Duration::from_millis(200);
    /// The default cap on the delay between attempts.
    pub const DEFAULT_MAX_DELAY: Duration = Duration::from_secs(10);

    /// Allow up to `max_attempts` total attempts (a value of `1`, or `0`, means
    /// a single attempt and no retry), waiting [`RetryPolicy::DEFAULT_BASE_DELAY`]
    /// before the first retry and doubling each time up to
    /// [`RetryPolicy::DEFAULT_MAX_DELAY`].
    pub fn new(max_attempts: u32) -> Self {
        Self {
            max_attempts: max_attempts.max(1),
            base_delay: Self::DEFAULT_BASE_DELAY,
            max_delay: Self::DEFAULT_MAX_DELAY,
        }
    }

    /// A policy that makes exactly one attempt (the default).
    pub fn none() -> Self {
        Self::new(1)
    }

    /// Total attempts, including the first.
    pub fn max_attempts(&self) -> u32 {
        self.max_attempts
    }

    /// The delay before the first retry.
    pub fn base_delay(&self) -> Duration {
        self.base_delay
    }

    /// The upper bound on the delay between attempts.
    pub fn max_delay(&self) -> Duration {
        self.max_delay
    }

    /// Override the delay before the first retry; later retries double it.
    pub fn with_base_delay(mut self, base_delay: Duration) -> Self {
        self.base_delay = base_delay;
        self
    }

    /// Override the cap on the delay between attempts.
    pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
        self.max_delay = max_delay;
        self
    }

    /// The delay to wait after `failed_attempt` (1-based) before trying again:
    /// `base_delay * 2^(failed_attempt - 1)`, capped at [`RetryPolicy::max_delay`].
    fn delay_for(&self, failed_attempt: u32) -> Duration {
        let exponent = failed_attempt.saturating_sub(1).min(31);
        let factor = 1u32 << exponent;
        self.base_delay.saturating_mul(factor).min(self.max_delay)
    }
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self::none()
    }
}

/// An HTTP status worth retrying: request timeout, rate limiting, or server
/// error. Client errors (`4xx`) are permanent — the request will not improve.
fn is_transient_status(status: reqwest::StatusCode) -> bool {
    status.is_server_error()
        || status == reqwest::StatusCode::REQUEST_TIMEOUT
        || status == reqwest::StatusCode::TOO_MANY_REQUESTS
}

/// Classifies a single attempt's failure so the retry loop knows whether to try
/// again.
enum AttemptError {
    /// A failure that might succeed on another attempt.
    Transient(FetchError),
    /// A failure retrying cannot fix (bad request, hash mismatch, disk error).
    Permanent(FetchError),
}

/// Fetches `http`/`https` URLs. Writes atomically and verifies sha256.
pub struct HttpFetcher {
    client: reqwest::Client,
    user_agent: Option<String>,
    connect_timeout: Option<Duration>,
    read_timeout: Option<Duration>,
    schemes: Vec<String>,
    retry: RetryPolicy,
    resume: bool,
}

impl HttpFetcher {
    /// A fetcher for `http`/`https` using the default `User-Agent`.
    ///
    /// The client has **no timeouts** by default (reqwest's default), so a
    /// stalled connection or a server that stops sending can hang a fetch. Opt
    /// in with [`HttpFetcher::with_connect_timeout`] and
    /// [`HttpFetcher::with_read_timeout`]. Retries are off (one attempt) and
    /// resuming is off; see [`HttpFetcher::with_retry`] and
    /// [`HttpFetcher::with_resume`].
    pub fn new() -> Self {
        Self::with_user_agent(DEFAULT_USER_AGENT)
    }

    /// Build the underlying client from the current options, falling back to a
    /// default client if reqwest rejects the configuration.
    fn build_client(
        user_agent: Option<&str>,
        connect_timeout: Option<Duration>,
        read_timeout: Option<Duration>,
    ) -> reqwest::Client {
        let mut builder = reqwest::Client::builder();
        if let Some(user_agent) = user_agent {
            builder = builder.user_agent(user_agent);
        }
        if let Some(timeout) = connect_timeout {
            builder = builder.connect_timeout(timeout);
        }
        if let Some(timeout) = read_timeout {
            builder = builder.read_timeout(timeout);
        }
        builder.build().unwrap_or_else(|_| reqwest::Client::new())
    }

    /// Override the `User-Agent` sent with requests.
    pub fn with_user_agent(user_agent: &str) -> Self {
        Self {
            client: Self::build_client(Some(user_agent), None, None),
            user_agent: Some(user_agent.to_owned()),
            connect_timeout: None,
            read_timeout: None,
            schemes: vec!["http".to_owned(), "https".to_owned()],
            retry: RetryPolicy::default(),
            resume: false,
        }
    }

    /// Bound how long the client waits to establish a connection.
    ///
    /// Off by default (reqwest waits indefinitely). This is the TCP/TLS connect
    /// phase only; use [`HttpFetcher::with_read_timeout`] to bound a transfer
    /// that has connected but gone quiet.
    ///
    /// ```
    /// use ptox::HttpFetcher;
    /// use std::time::Duration;
    ///
    /// let fetcher = HttpFetcher::new().with_connect_timeout(Duration::from_secs(5));
    /// assert_eq!(fetcher.connect_timeout(), Some(Duration::from_secs(5)));
    /// ```
    pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = Some(timeout);
        self.client = Self::build_client(
            self.user_agent.as_deref(),
            self.connect_timeout,
            self.read_timeout,
        );
        self
    }

    /// Bound how long the client waits for more bytes between reads.
    ///
    /// The timeout resets after every successful read, so it detects a stalled
    /// connection without capping a legitimately large or slow download (unlike
    /// a total request deadline). Off by default.
    ///
    /// ```
    /// use ptox::HttpFetcher;
    /// use std::time::Duration;
    ///
    /// let fetcher = HttpFetcher::new().with_read_timeout(Duration::from_secs(30));
    /// assert_eq!(fetcher.read_timeout(), Some(Duration::from_secs(30)));
    /// ```
    pub fn with_read_timeout(mut self, timeout: Duration) -> Self {
        self.read_timeout = Some(timeout);
        self.client = Self::build_client(
            self.user_agent.as_deref(),
            self.connect_timeout,
            self.read_timeout,
        );
        self
    }

    /// The configured connect timeout, if any.
    pub fn connect_timeout(&self) -> Option<Duration> {
        self.connect_timeout
    }

    /// The configured read timeout, if any.
    pub fn read_timeout(&self) -> Option<Duration> {
        self.read_timeout
    }

    /// Opt in to retrying transient failures with `policy`.
    ///
    /// Retries are off by default (one attempt). Because GETs are idempotent, a
    /// client talking to a flaky mirror can enable a bounded exponential backoff:
    ///
    /// ```no_run
    /// use ptox::{HttpFetcher, RetryPolicy};
    /// use std::time::Duration;
    ///
    /// let fetcher = HttpFetcher::new().with_retry(
    ///     RetryPolicy::new(3).with_base_delay(Duration::from_millis(200)),
    /// );
    /// ```
    pub fn with_retry(mut self, policy: RetryPolicy) -> Self {
        self.retry = policy;
        self
    }

    /// The retry policy in effect.
    pub fn retry_policy(&self) -> &RetryPolicy {
        &self.retry
    }

    /// Enable resuming an interrupted download from `<destination>.part`.
    ///
    /// Resuming is **off by default**. When enabled, a `.part` file left by an
    /// earlier attempt (or an earlier process) is re-hashed and continued with an
    /// HTTP `Range` request instead of restarting from zero, and a transient
    /// failure keeps the partial so the next attempt can pick it up. The final
    /// sha256 still covers the whole file, so verification is unaffected.
    ///
    /// A sidecar file (`.part.src`) records which address a partial came from, so
    /// a resume never splices bytes from two different mirrors. Because a resume
    /// trusts the saved prefix, it is most useful when the result carries a
    /// sha256: a corrupt or truncated partial is then rejected at verification.
    ///
    /// ```no_run
    /// use ptox::HttpFetcher;
    ///
    /// let fetcher = HttpFetcher::new().with_resume(true);
    /// assert!(fetcher.resume_enabled());
    /// ```
    pub fn with_resume(mut self, resume: bool) -> Self {
        self.resume = resume;
        self
    }

    /// Whether partial-download resuming is enabled.
    pub fn resume_enabled(&self) -> bool {
        self.resume
    }
}

impl Default for HttpFetcher {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Fetcher for HttpFetcher {
    fn name(&self) -> &str {
        "http"
    }

    fn schemes(&self) -> &[String] {
        &self.schemes
    }

    async fn fetch(&self, request: &FetchRequest) -> Result<Fetched, FetchError> {
        self.fetch_with_progress(request, &|_: u64| {}).await
    }

    async fn fetch_with_progress(
        &self,
        request: &FetchRequest,
        progress: &ProgressFn<'_>,
    ) -> Result<Fetched, FetchError> {
        let temp = temp_path(&request.destination);
        let marker = resume_marker_path(&temp);
        let attempts = self.retry.max_attempts();
        // A failed attempt may report bytes before failing; clamp later reports
        // to the high-water mark so a retry never makes progress regress.
        let high_water = AtomicU64::new(0);
        let monotonic = |bytes: u64| {
            let previous = high_water.fetch_max(bytes, Ordering::Relaxed);
            if bytes > previous {
                progress(bytes);
            }
        };

        let cleanup = || async {
            let _ = tokio::fs::remove_file(&temp).await;
            let _ = tokio::fs::remove_file(&marker).await;
        };

        let mut last_error = None;
        for attempt in 1..=attempts {
            match self.fetch_once(request, &monotonic).await {
                Ok(fetched) => return Ok(fetched),
                Err(AttemptError::Transient(error)) if attempt < attempts => {
                    last_error = Some(error);
                    // With resume on, keep the partial so the next attempt can
                    // continue it; otherwise start clean.
                    if !self.resume {
                        cleanup().await;
                    }
                    let delay = self.retry.delay_for(attempt);
                    if !delay.is_zero() {
                        tokio::time::sleep(delay).await;
                    }
                }
                Err(AttemptError::Transient(error)) => {
                    if !self.resume {
                        cleanup().await;
                    }
                    return Err(error);
                }
                Err(AttemptError::Permanent(error)) => {
                    // A bad request, hash mismatch, or disk error cannot be fixed
                    // by resuming, so never leave the partial behind.
                    cleanup().await;
                    return Err(error);
                }
            }
        }
        Err(last_error.unwrap_or_else(|| FetchError::Network("retry exhausted".to_owned())))
    }
}

impl HttpFetcher {
    /// Perform one HTTP GET + download, classifying failures as transient or
    /// permanent for the retry loop.
    ///
    /// When [`HttpFetcher::with_resume`] is on, an interrupted
    /// `<destination>.part` left by an earlier attempt (or an earlier process) is
    /// continued with a `Range` request instead of being restarted. Cleanup of
    /// the partial on failure is handled by [`Fetcher::fetch_with_progress`], so
    /// the retry loop can choose to keep it for a later resume.
    async fn fetch_once(
        &self,
        request: &FetchRequest,
        progress: &ProgressFn<'_>,
    ) -> Result<Fetched, AttemptError> {
        let temp = temp_path(&request.destination);
        let marker = resume_marker_path(&temp);

        // Decide the resume point before the request: the saved prefix must be
        // re-hashed to keep the final sha256 correct, and a prefix that cannot be
        // re-read means starting over from zero.
        let mut resume_from = if self.resume {
            resumable_prefix(&temp, &marker, &request.address.uri).await
        } else {
            0
        };
        let mut hasher = Sha256::new();
        if resume_from > 0 {
            match hash_prefix(&temp, resume_from, &mut hasher).await {
                Ok(()) => {}
                Err(_) => {
                    resume_from = 0;
                    hasher = Sha256::new();
                }
            }
        }
        if self.resume && resume_from == 0 {
            // A fresh partial: record the source now so a crash mid-download can
            // be resumed, and drop any stale partial from another address.
            let _ = tokio::fs::remove_file(&temp).await;
            tokio::fs::write(&marker, &request.address.uri)
                .await
                .map_err(|error| AttemptError::Permanent(FetchError::Io(error)))?;
        }

        let mut builder = self.client.get(&request.address.uri);
        if resume_from > 0 {
            builder = builder.header(reqwest::header::RANGE, format!("bytes={resume_from}-"));
        }
        let response = builder
            .send()
            .await
            .map_err(|error| AttemptError::Transient(FetchError::Network(error.to_string())))?;
        let status = response.status();

        // A complete partial: the server refuses the range because there is
        // nothing left to send. Verify what we already have and finish it.
        if resume_from > 0 && status == reqwest::StatusCode::RANGE_NOT_SATISFIABLE {
            if let Some(total) = content_range_total(response.headers()) {
                if resume_from >= total {
                    return finish_partial(request, &temp, &marker, resume_from, hasher).await;
                }
            }
            return Err(AttemptError::Permanent(FetchError::Network(format!(
                "HTTP {status} for {}",
                request.address.uri
            ))));
        }

        if !status.is_success() {
            let error = FetchError::Network(format!("HTTP {status} for {}", request.address.uri));
            return Err(if is_transient_status(status) {
                AttemptError::Transient(error)
            } else {
                AttemptError::Permanent(error)
            });
        }

        // Append only when the server honored the exact range we asked for. A
        // `200` (range ignored, or no range requested) restarts from zero; a
        // mismatched `206` is rejected rather than silently truncating.
        let appending = resume_from > 0
            && status == reqwest::StatusCode::PARTIAL_CONTENT
            && content_range_start(response.headers()) == Some(resume_from);
        if resume_from > 0 && status == reqwest::StatusCode::PARTIAL_CONTENT && !appending {
            return Err(AttemptError::Permanent(FetchError::Network(format!(
                "unexpected Content-Range for a range request on {}",
                request.address.uri
            ))));
        }
        let (hasher, total) = if appending {
            (hasher, resume_from)
        } else {
            (Sha256::new(), 0)
        };

        match download(response, &temp, request, progress, hasher, total, appending).await {
            Ok(fetched) => {
                if self.resume {
                    let _ = tokio::fs::remove_file(&marker).await;
                }
                Ok(fetched)
            }
            Err(error) => Err(match error {
                network @ FetchError::Network(_) => AttemptError::Transient(network),
                other => AttemptError::Permanent(other),
            }),
        }
    }
}

/// Verify and rename a partial the server reports as already complete.
async fn finish_partial(
    request: &FetchRequest,
    temp: &Path,
    marker: &Path,
    bytes: u64,
    hasher: Sha256,
) -> Result<Fetched, AttemptError> {
    let digest = hex::encode(hasher.finalize());
    verify_sha256(request.expected_sha256.as_deref(), &digest).map_err(AttemptError::Permanent)?;
    tokio::fs::rename(temp, &request.destination)
        .await
        .map_err(|error| AttemptError::Permanent(FetchError::Io(error)))?;
    let _ = tokio::fs::remove_file(marker).await;
    Ok(Fetched {
        path: request.destination.clone(),
        bytes,
        sha256: Some(digest),
        address: request.address.clone(),
    })
}

/// Stream `response` to `temp`, verify its hash, then rename it into place.
///
/// `hasher` and `total` may carry a pre-hashed prefix when resuming; `append`
/// selects appending to an existing `temp` instead of truncating it.
async fn download(
    response: reqwest::Response,
    temp: &Path,
    request: &FetchRequest,
    progress: &ProgressFn<'_>,
    mut hasher: Sha256,
    mut total: u64,
    append: bool,
) -> Result<Fetched, FetchError> {
    if let Some(parent) = temp.parent() {
        tokio::fs::create_dir_all(parent).await?;
    }
    let mut file = if append {
        tokio::fs::OpenOptions::new()
            .append(true)
            .open(temp)
            .await?
    } else {
        tokio::fs::File::create(temp).await?
    };
    if total > 0 {
        progress(total);
    }
    let mut stream = response.bytes_stream();

    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(|error| FetchError::Network(error.to_string()))?;
        hasher.update(&chunk);
        file.write_all(&chunk).await?;
        total += chunk.len() as u64;
        progress(total);
    }
    file.flush().await?;

    let digest = hex::encode(hasher.finalize());
    verify_sha256(request.expected_sha256.as_deref(), &digest)?;
    tokio::fs::rename(temp, &request.destination).await?;

    Ok(Fetched {
        path: request.destination.clone(),
        bytes: total,
        sha256: Some(digest),
        address: request.address.clone(),
    })
}

/// `<destination>.part.src`: records which address a partial download came from,
/// so a resume never splices bytes from two different mirrors.
fn resume_marker_path(temp: &Path) -> PathBuf {
    let mut name = temp.as_os_str().to_os_string();
    name.push(".src");
    PathBuf::from(name)
}

/// The number of bytes of `temp` that may be resumed, or `0` when there is no
/// partial matching `uri`.
async fn resumable_prefix(temp: &Path, marker: &Path, uri: &str) -> u64 {
    let Ok(recorded) = tokio::fs::read_to_string(marker).await else {
        return 0;
    };
    if recorded.trim() != uri {
        return 0;
    }
    match tokio::fs::metadata(temp).await {
        Ok(metadata) if metadata.is_file() => metadata.len(),
        _ => 0,
    }
}

/// Feed the first `len` bytes of `path` into `hasher` so a resumed download's
/// final digest covers the whole file.
async fn hash_prefix(path: &Path, len: u64, hasher: &mut Sha256) -> std::io::Result<()> {
    let mut file = tokio::fs::File::open(path).await?;
    let mut remaining = len;
    let mut buffer = vec![0u8; 64 * 1024];
    while remaining > 0 {
        let want = remaining.min(buffer.len() as u64) as usize;
        let read = file.read(&mut buffer[..want]).await?;
        if read == 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "partial file shrank while resuming",
            ));
        }
        hasher.update(&buffer[..read]);
        remaining -= read as u64;
    }
    Ok(())
}

/// The start offset of a `Content-Range: bytes <start>-<end>/<total>` header.
fn content_range_start(headers: &reqwest::header::HeaderMap) -> Option<u64> {
    let value = headers.get(reqwest::header::CONTENT_RANGE)?.to_str().ok()?;
    let rest = value.trim().strip_prefix("bytes")?.trim_start();
    let (range, _total) = rest.split_once('/')?;
    let (start, _end) = range.split_once('-')?;
    start.trim().parse().ok()
}

/// The total size in a `Content-Range` header (a `416` sends `bytes */<total>`).
fn content_range_total(headers: &reqwest::header::HeaderMap) -> Option<u64> {
    let value = headers.get(reqwest::header::CONTENT_RANGE)?.to_str().ok()?;
    let (_range, total) = value.trim().rsplit_once('/')?;
    total.trim().parse().ok()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn retry_defaults_to_a_single_attempt() {
        assert_eq!(RetryPolicy::default().max_attempts(), 1);
        assert_eq!(RetryPolicy::none().max_attempts(), 1);
        assert_eq!(RetryPolicy::new(0).max_attempts(), 1);
        assert_eq!(RetryPolicy::new(4).max_attempts(), 4);
    }

    #[test]
    fn retry_backoff_doubles_then_caps() {
        let policy = RetryPolicy::new(5)
            .with_base_delay(Duration::from_millis(100))
            .with_max_delay(Duration::from_millis(250));
        assert_eq!(policy.delay_for(1), Duration::from_millis(100));
        assert_eq!(policy.delay_for(2), Duration::from_millis(200));
        assert_eq!(policy.delay_for(3), Duration::from_millis(250)); // capped
        assert_eq!(policy.delay_for(100), Duration::from_millis(250));
    }

    #[test]
    fn only_server_side_statuses_are_transient() {
        use reqwest::StatusCode;
        for status in [
            StatusCode::INTERNAL_SERVER_ERROR,
            StatusCode::BAD_GATEWAY,
            StatusCode::SERVICE_UNAVAILABLE,
            StatusCode::GATEWAY_TIMEOUT,
            StatusCode::REQUEST_TIMEOUT,
            StatusCode::TOO_MANY_REQUESTS,
        ] {
            assert!(is_transient_status(status), "{status} should be transient");
        }
        for status in [
            StatusCode::OK,
            StatusCode::BAD_REQUEST,
            StatusCode::NOT_FOUND,
            StatusCode::FORBIDDEN,
        ] {
            assert!(!is_transient_status(status), "{status} should be permanent");
        }
    }

    #[test]
    fn resume_is_off_by_default() {
        assert!(!HttpFetcher::new().resume_enabled());
        assert!(HttpFetcher::new().with_resume(true).resume_enabled());
    }

    #[test]
    fn timeouts_are_off_by_default_and_chain() {
        let fetcher = HttpFetcher::new();
        assert_eq!(fetcher.connect_timeout(), None);
        assert_eq!(fetcher.read_timeout(), None);

        let fetcher = HttpFetcher::new()
            .with_connect_timeout(Duration::from_secs(3))
            .with_read_timeout(Duration::from_secs(17));
        assert_eq!(fetcher.connect_timeout(), Some(Duration::from_secs(3)));
        assert_eq!(fetcher.read_timeout(), Some(Duration::from_secs(17)));
        // User-Agent changes survive later timeout changes (the client is rebuilt).
        assert_eq!(fetcher.user_agent.as_deref(), Some(DEFAULT_USER_AGENT));
    }

    #[test]
    fn parses_content_range_start_and_total() {
        use reqwest::header::{HeaderMap, HeaderValue, CONTENT_RANGE};
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_RANGE, HeaderValue::from_static("bytes 5-99/1000"));
        assert_eq!(content_range_start(&headers), Some(5));
        assert_eq!(content_range_total(&headers), Some(1000));

        headers.insert(CONTENT_RANGE, HeaderValue::from_static("bytes */1000"));
        assert_eq!(content_range_start(&headers), None);
        assert_eq!(content_range_total(&headers), Some(1000));

        headers.insert(CONTENT_RANGE, HeaderValue::from_static("nonsense"));
        assert_eq!(content_range_start(&headers), None);
        assert_eq!(content_range_total(&headers), None);
    }

    #[tokio::test]
    async fn resume_marker_must_match_a_readable_partial() {
        let dir = std::env::temp_dir().join(format!("ptox-resume-unit-{}", std::process::id()));
        tokio::fs::create_dir_all(&dir).await.expect("mkdir");
        let temp = dir.join("file.part");
        let marker = resume_marker_path(&temp);
        assert_eq!(marker, dir.join("file.part.src"));

        // No marker yet.
        tokio::fs::write(&temp, b"hello").await.expect("write");
        assert_eq!(resumable_prefix(&temp, &marker, "https://a/x").await, 0);

        // Matching source and partial.
        tokio::fs::write(&marker, "https://a/x")
            .await
            .expect("marker");
        assert_eq!(resumable_prefix(&temp, &marker, "https://a/x").await, 5);

        // A different source must not resume another mirror's bytes.
        assert_eq!(resumable_prefix(&temp, &marker, "https://b/x").await, 0);

        let _ = tokio::fs::remove_dir_all(&dir).await;
    }
}