bunsen 0.33.0

bunsen is a batteries included common library for burn
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
//! # Disk Cache Management

use std::{
    path::{
        Path,
        PathBuf,
    },
    sync::Arc,
};

#[cfg(feature = "fetch")]
use crate::data::cache::{
    fetch_file,
    fetch_verified,
    file_name_from_url,
};
use crate::{
    data::cache::{
        BUNSEN_CACHE_CONFIG,
        TransferObserver,
        TransferObservers,
        path_utils,
    },
    errors::{
        BunsenError,
        BunsenResult,
    },
};

/// Environment variable key to override the default cache directory.
pub const BUNSEN_CACHE_DIR: &str = "BUNSEN_CACHE_DIR";
/// Environment variable key to override the default data directory.
pub const BUNSEN_DATA_DIR: &str = "BUNSEN_DATA_DIR";

/// Options for [`BunsenDiskCache`].
#[derive(Clone, Debug)]
pub struct BunsenDiskCacheOptions {
    /// Optional path to the cache directory.
    pub cache_dir: Option<PathBuf>,

    /// Optional path to the data directory.
    pub data_dir: Option<PathBuf>,

    /// Told about every transfer, in this order; see [`TransferObserver`].
    ///
    /// [`Default`] seeds it from [`default_transfer_observers`]: one
    /// `IndicatifObserver` with the `indicatif` feature, none without.
    pub transfer_observers: TransferObservers,
}

impl Default for BunsenDiskCacheOptions {
    fn default() -> Self {
        Self {
            cache_dir: None,
            data_dir: None,
            transfer_observers: default_transfer_observers(),
        }
    }
}

/// The observers [`BunsenDiskCacheOptions::default()`] carries: one
/// `IndicatifObserver` drawing on stderr with the `indicatif` feature, none
/// without.
pub fn default_transfer_observers() -> TransferObservers {
    // Only the feature-on build mutates it, and the workspace denies
    // `unused_mut`; the other build is told the `mut` is expected.
    #[cfg_attr(not(feature = "indicatif"), allow(unused_mut))]
    let mut observers: TransferObservers = Vec::new();

    #[cfg(feature = "indicatif")]
    {
        use crate::data::cache::IndicatifObserver;
        observers.push(Arc::new(IndicatifObserver::default()));
    }

    observers
}

impl BunsenDiskCacheOptions {
    /// Sets the cache directory.
    pub fn with_cache_dir<P: AsRef<Path>>(
        mut self,
        cache_dir: Option<P>,
    ) -> Self {
        self.cache_dir = cache_dir.map(|p| p.as_ref().to_path_buf());
        self
    }

    /// Sets the data directory.
    pub fn with_data_dir<P: AsRef<Path>>(
        mut self,
        data_dir: Option<P>,
    ) -> Self {
        self.data_dir = data_dir.map(|p| p.as_ref().to_path_buf());
        self
    }

    /// Adds an observer after the ones already there.
    pub fn with_transfer_observer(
        mut self,
        observer: Arc<dyn TransferObserver>,
    ) -> Self {
        self.transfer_observers.push(observer);
        self
    }

    /// Replaces the observers.
    pub fn with_transfer_observers(
        mut self,
        observers: TransferObservers,
    ) -> Self {
        self.transfer_observers = observers;
        self
    }

    /// Drops every observer, the default `indicatif` one included.
    pub fn without_transfer_observers(mut self) -> Self {
        self.transfer_observers.clear();
        self
    }
}

/// Disk cache for downloaded files.
///
/// [`PathResolver`](`super::PathResolver`) decides where the cache and data
/// directories are for a user/system combo, with environment overrides.
/// [`fetch_file`] brings a missing file in: streamed to a `.partial`,
/// digest-checked when a digest is given, and renamed into place. Every
/// transfer is reported to the [`TransferObserver`] stack the options
/// carried.
pub struct BunsenDiskCache {
    /// Cache directory.
    cache_dir: PathBuf,

    /// Data directory.
    data_dir: PathBuf,

    /// Told about every transfer, in this order.
    transfer_observers: TransferObservers,
}

impl Default for BunsenDiskCache {
    fn default() -> Self {
        Self::new(BunsenDiskCacheOptions::default()).unwrap()
    }
}

impl BunsenDiskCache {
    /// Constructs a new [`BunsenDiskCache`].
    pub fn new(options: BunsenDiskCacheOptions) -> BunsenResult<Self> {
        let cache_dir = BUNSEN_CACHE_CONFIG
            .resolve_cache_dir(options.cache_dir)
            .ok_or(BunsenError::ResourceNotFound(
                "failed to resolve cache directory".to_string(),
            ))?;

        let data_dir = BUNSEN_CACHE_CONFIG
            .resolve_data_dir(options.data_dir)
            .ok_or(BunsenError::ResourceNotFound(
                "failed to resolve data directory".to_string(),
            ))?;

        Ok(Self {
            cache_dir,
            data_dir,
            transfer_observers: options.transfer_observers,
        })
    }

    /// Returns the cache directory.
    pub fn cache_dir(&self) -> &Path {
        &self.cache_dir
    }

    /// Returns the data directory.
    pub fn data_dir(&self) -> &Path {
        &self.data_dir
    }

    /// The transfer observers, in the order each transfer reaches them.
    pub fn transfer_observers(&self) -> &[Arc<dyn TransferObserver>] {
        &self.transfer_observers
    }

    /// Returns the cache path for the given key.
    ///
    /// * Does not check that the path exists.
    /// * Does not initialize the containing directories.
    ///
    /// # Arguments
    /// * `context` - prefix dirs, inserted between `self.cache_dir` and `file`.
    /// * `file` - the final file name.
    pub fn cache_path<C, F>(
        &self,
        context: &[C],
        file: F,
    ) -> PathBuf
    where
        C: AsRef<Path>,
        F: AsRef<Path>,
    {
        path_utils::extend_path(&self.cache_dir, context, file)
    }

    /// Returns the data path for the given key.
    ///
    /// * Does not check that the path exists.
    /// * Does not initialize the containing directories.
    ///
    /// # Arguments
    /// * `context` - prefix dirs, inserted between `self.cache_dir` and `file`.
    /// * `file` - the final file name.
    pub fn data_path<C, F>(
        &self,
        context: &[C],
        file: F,
    ) -> PathBuf
    where
        C: AsRef<Path>,
        F: AsRef<Path>,
    {
        path_utils::extend_path(&self.data_dir, context, file)
    }
}

/// The network side: every method here needs the `fetch` feature.
#[cfg(feature = "fetch")]
impl BunsenDiskCache {
    /// Streams `url` to `dest`, verified against `sha256`, reporting to the
    /// observer stack; see [`fetch_verified`](super::fetch_verified).
    ///
    /// # Errors
    /// As [`fetch_verified`](super::fetch_verified).
    pub fn fetch_verified(
        &self,
        url: &str,
        dest: &Path,
        sha256: &str,
    ) -> BunsenResult<()> {
        fetch_verified(url, dest, sha256, &self.transfer_observers)
    }

    /// Streams `url` to `dest`, checked against `sha256` when one is given,
    /// reporting to the observer stack; see [`fetch_file`](super::fetch_file).
    ///
    /// # Errors
    /// As [`fetch_file`](super::fetch_file).
    pub fn fetch_file(
        &self,
        url: &str,
        dest: &Path,
        sha256: Option<&str>,
    ) -> BunsenResult<()> {
        fetch_file(url, dest, sha256, &self.transfer_observers)
    }

    /// Brings `dest` in from `urls`, tried in order, checked against
    /// `sha256` when one is given; the first URL whose file checks out wins.
    /// Every attempt reports to the observer stack.
    ///
    /// # Errors
    /// With one URL its error comes back as is; with several,
    /// [`BunsenError::External`] naming every failure.
    /// [`BunsenError::Invalid`] with no URL at all.
    pub fn fetch_from_urls(
        &self,
        urls: &[&str],
        dest: &Path,
        sha256: Option<&str>,
    ) -> BunsenResult<()> {
        if urls.is_empty() {
            return Err(BunsenError::Invalid(format!(
                "{}: no URL to fetch from",
                dest.display()
            )));
        }
        let mut failures = Vec::with_capacity(urls.len());
        for url in urls {
            match fetch_file(url, dest, sha256, &self.transfer_observers) {
                Ok(()) => return Ok(()),
                Err(e) => failures.push((*url, e)),
            }
        }
        if failures.len() == 1 {
            return Err(failures.pop().expect("one failure").1);
        }
        let failures: Vec<String> = failures
            .iter()
            .map(|(url, e)| format!("{url}: {e}"))
            .collect();
        Err(BunsenError::External(format!(
            "{}: no URL could be fetched: {}",
            dest.display(),
            failures.join("; ")
        )))
    }

    /// Finds `context/<file>` under `root`, fetching it from `urls` when it
    /// is not there. The file is named by the last path segment of the first
    /// URL.
    ///
    /// The fetch is [`fetch_from_urls`](Self::fetch_from_urls).
    fn _load_resource<P, C, S>(
        &self,
        root: &P,
        context: &[C],
        urls: &[S],
        download: bool,
        sha256: Option<&str>,
    ) -> BunsenResult<PathBuf>
    where
        P: AsRef<Path>,
        C: AsRef<Path>,
        S: AsRef<str>,
    {
        let urls: Vec<&str> = urls.iter().map(AsRef::as_ref).collect();
        let Some(first) = urls.first() else {
            return Err(BunsenError::Invalid("no URL to load from".to_string()));
        };
        let Some(file_name) = file_name_from_url(first) else {
            return Err(BunsenError::Invalid(format!(
                "{first}: the URL names no file"
            )));
        };
        let path = path_utils::extend_path(root, context, file_name);

        if path.exists() {
            return Ok(path);
        }
        if !download {
            return Err(BunsenError::ResourceNotFound(format!(
                "cached file not found: {}",
                path.display()
            )));
        }

        self.fetch_from_urls(&urls, &path, sha256)?;
        Ok(path)
    }

    /// Loads a file under the cache directory, fetching it if it is not
    /// there.
    ///
    /// # Arguments
    /// * `context`: A slice of `C` containing path-related context used in
    ///   determining the cache location. These paths are combined to build the
    ///   cached file's location.
    /// * `urls`: The URLs to fetch the file from, tried in order, if it is not
    ///   already cached. The file is named by the first URL's last path
    ///   segment.
    /// * `download`: A boolean flag indicating whether to attempt downloading
    ///   the file from the provided URLs if it does not already exist in the
    ///   cache.
    /// * `sha256`: The lowercase hex SHA-256 a fetched file must have, when
    ///   pinned. A file already in the cache is trusted as it is.
    ///
    /// # Returns
    /// * Returns a [`PathBuf`] pointing to the cached file if it exists or is
    ///   successfully downloaded.
    ///
    /// # Errors
    /// * [`BunsenError::ResourceNotFound`] if the file is not cached and
    ///   `download` is `false`.
    /// * [`BunsenError::Invalid`] if a fetched file does not match `sha256`, or
    ///   no URL names a file.
    /// * [`BunsenError::External`] if the transfer fails; with several URLs,
    ///   the message names every failure.
    pub fn load_cached_path<C, S>(
        &self,
        context: &[C],
        urls: &[S],
        download: bool,
        sha256: Option<&str>,
    ) -> BunsenResult<PathBuf>
    where
        C: AsRef<Path>,
        S: AsRef<str>,
    {
        self._load_resource(&self.cache_dir, context, urls, download, sha256)
    }

    /// Loads a file under the data directory, fetching it if it is not there.
    ///
    /// # Arguments
    /// * `context`: A slice of `C` containing path-related context used in
    ///   determining the cache location. These paths are combined to build the
    ///   data file's location.
    /// * `urls`: The URLs to fetch the file from, tried in order, if it is not
    ///   already there. The file is named by the first URL's last path segment.
    /// * `download`: A boolean flag indicating whether to attempt downloading
    ///   the file from the provided URLs if it does not already exist in the
    ///   cache.
    /// * `sha256`: The lowercase hex SHA-256 a fetched file must have, when
    ///   pinned. A file already there is trusted as it is.
    ///
    /// # Returns
    /// * Returns a [`PathBuf`] pointing to the data file if it exists or is
    ///   successfully downloaded.
    ///
    /// # Errors
    /// As [`load_cached_path`](Self::load_cached_path).
    pub fn load_data_path<C, S>(
        &self,
        context: &[C],
        urls: &[S],
        download: bool,
        sha256: Option<&str>,
    ) -> BunsenResult<PathBuf>
    where
        C: AsRef<Path>,
        S: AsRef<str>,
    {
        self._load_resource(&self.data_dir, context, urls, download, sha256)
    }
}

#[cfg(test)]
mod tests {
    use std::env;

    use serial_test::serial;

    use super::*;
    use crate::data::cache::testing::RecordingObserver;

    #[test]
    #[serial]
    fn test_resolve_dirs() {
        let orig_cache_dir = env::var(BUNSEN_CACHE_DIR);
        let orig_data_dir = env::var(BUNSEN_DATA_DIR);

        let pds = BUNSEN_CACHE_CONFIG
            .project_dirs()
            .expect("failed to get project dirs");

        let user_cache_dir = PathBuf::from("/tmp/bunsen/cache");
        let user_data_dir = PathBuf::from("/tmp/bunsen/data");

        let env_cache_dir = PathBuf::from("/tmp/bunsen/env_cache");
        let env_data_dir = PathBuf::from("/tmp/bunsen/env_data");

        // No env vars
        unsafe {
            env::remove_var(BUNSEN_CACHE_DIR);
            env::remove_var(BUNSEN_DATA_DIR);
        }

        let cache = BunsenDiskCache::new(
            BunsenDiskCacheOptions::default()
                .with_cache_dir(Some(user_cache_dir.clone()))
                .with_data_dir(Some(user_data_dir.clone())),
        )
        .unwrap();
        assert_eq!(&cache.cache_dir(), &user_cache_dir);
        assert_eq!(&cache.data_dir(), &user_data_dir);

        let cache = BunsenDiskCache::new(BunsenDiskCacheOptions::default()).unwrap();
        assert_eq!(&cache.cache_dir(), &pds.cache_dir().to_path_buf());
        assert_eq!(&cache.data_dir(), &pds.data_dir().to_path_buf());

        // With env var.
        unsafe {
            env::set_var(BUNSEN_CACHE_DIR, env_cache_dir.to_str().unwrap());
            env::set_var(BUNSEN_DATA_DIR, env_data_dir.to_str().unwrap());
        }

        let cache = BunsenDiskCache::new(
            BunsenDiskCacheOptions::default()
                .with_cache_dir(Some(user_cache_dir.clone()))
                .with_data_dir(Some(user_data_dir.clone())),
        )
        .unwrap();
        assert_eq!(&cache.cache_dir(), &user_cache_dir);
        assert_eq!(&cache.data_dir(), &user_data_dir);

        let cache = BunsenDiskCache::new(BunsenDiskCacheOptions::default()).unwrap();
        assert_eq!(&cache.cache_dir(), &env_cache_dir);
        assert_eq!(&cache.data_dir(), &env_data_dir);

        // restore original env var.
        match orig_cache_dir {
            Ok(original) => unsafe { env::set_var(BUNSEN_CACHE_DIR, original) },
            Err(_) => unsafe { env::remove_var(BUNSEN_CACHE_DIR) },
        }
        match orig_data_dir {
            Ok(original) => unsafe { env::set_var(BUNSEN_DATA_DIR, original) },
            Err(_) => unsafe { env::remove_var(BUNSEN_DATA_DIR) },
        }
    }

    #[test]
    fn test_data_path() {
        let cache = BunsenDiskCache::new(BunsenDiskCacheOptions::default()).unwrap();
        let path = cache.data_path(&["prefix"], "file.txt");
        assert_eq!(path, cache.data_dir.join("prefix").join("file.txt"));
    }

    #[test]
    fn test_cache_path() {
        let cache = BunsenDiskCache::new(BunsenDiskCacheOptions::default()).unwrap();
        let path = cache.cache_path(&["prefix"], "file.txt");
        assert_eq!(path, cache.cache_dir.join("prefix").join("file.txt"));
    }

    /// `Default` seeds the stack from the `indicatif` feature: one observer
    /// with it, none without.
    #[test]
    fn test_default_transfer_observers_follow_the_feature() {
        let expected = if cfg!(feature = "indicatif") { 1 } else { 0 };
        assert_eq!(default_transfer_observers().len(), expected);
        assert_eq!(
            BunsenDiskCacheOptions::default().transfer_observers.len(),
            expected
        );
    }

    /// The builders: one appends after the defaults, one replaces, one
    /// clears; and the cache carries the result through, in order.
    #[test]
    fn test_transfer_observer_builders() {
        let a: Arc<dyn TransferObserver> = Arc::new(RecordingObserver::default());
        let b: Arc<dyn TransferObserver> = Arc::new(RecordingObserver::default());

        let options = BunsenDiskCacheOptions::default().with_transfer_observer(a.clone());
        assert_eq!(
            options.transfer_observers.len(),
            default_transfer_observers().len() + 1
        );
        assert!(Arc::ptr_eq(options.transfer_observers.last().unwrap(), &a));

        let options = options.without_transfer_observers();
        assert!(options.transfer_observers.is_empty());

        let options =
            BunsenDiskCacheOptions::default().with_transfer_observers(vec![a.clone(), b.clone()]);
        assert_eq!(options.transfer_observers.len(), 2);
        assert!(Arc::ptr_eq(&options.transfer_observers[0], &a));
        assert!(Arc::ptr_eq(&options.transfer_observers[1], &b));

        let cache = BunsenDiskCache::new(options).unwrap();
        assert_eq!(cache.transfer_observers().len(), 2);
        assert!(Arc::ptr_eq(&cache.transfer_observers()[0], &a));
        assert!(Arc::ptr_eq(&cache.transfer_observers()[1], &b));
    }
}

#[cfg(all(test, feature = "fetch"))]
mod fetch_tests {
    use std::fs;

    use super::*;
    use crate::data::cache::{
        partial_path,
        testing::{
            ABC_SHA256,
            CacheProgressEvent,
            RecordingObserver,
            refused_url,
            serve_once,
        },
    };

    /// A download opens one transfer on every observer: the URL and the
    /// cache path in `begin`, the byte count as it lands, `Complete` at the
    /// end.
    #[test]
    fn test_load_cached_path_reports_the_transfer() {
        let dir = tempfile::tempdir().unwrap();
        let observer = Arc::new(RecordingObserver::default());
        let cache = BunsenDiskCache::new(
            BunsenDiskCacheOptions::default()
                .with_cache_dir(Some(dir.path().join("cache")))
                .without_transfer_observers()
                .with_transfer_observer(observer.clone()),
        )
        .unwrap();

        let body: &'static [u8] = b"hello, observers";
        let url = serve_once("hello.txt", body);
        let path = cache
            .load_cached_path(&["t"], &[url.as_str()], true, None)
            .unwrap();
        assert_eq!(fs::read(&path).unwrap(), body);

        let events = observer.events();
        assert_eq!(
            events.first(),
            Some(&CacheProgressEvent::Begin {
                source: url.clone(),
                dest: path.clone(),
                total: Some(body.len() as u64),
            })
        );
        assert_eq!(events.last(), Some(&CacheProgressEvent::Finish(Ok(()))));
        let last_position = events.iter().rev().find_map(|e| match e {
            CacheProgressEvent::Position(n) => Some(*n),
            _ => None,
        });
        assert_eq!(last_position, Some(body.len() as u64));
    }

    /// The cache's `fetch_verified` reports through the observers it
    /// carries.
    #[test]
    fn test_fetch_verified_reports_through_the_cache_observers() {
        let dir = tempfile::tempdir().unwrap();
        let observer = Arc::new(RecordingObserver::default());
        let cache = BunsenDiskCache::new(
            BunsenDiskCacheOptions::default()
                .with_cache_dir(Some(dir.path().join("cache")))
                .without_transfer_observers()
                .with_transfer_observer(observer.clone()),
        )
        .unwrap();

        let url = serve_once("abc.bin", b"abc");
        let dest = cache.cache_path(&["t"], "abc.bin");
        cache.fetch_verified(&url, &dest, ABC_SHA256).unwrap();

        assert_eq!(fs::read(&dest).unwrap(), b"abc");
        let events = observer.events();
        assert!(matches!(
            events.first(),
            Some(CacheProgressEvent::Begin { .. })
        ));
        assert_eq!(events.last(), Some(&CacheProgressEvent::Finish(Ok(()))));
    }

    /// `load_data_path` looks under the data directory, `load_cached_path`
    /// under the cache directory, and neither sees the other's files.
    #[test]
    fn test_load_paths_root_at_their_own_dirs() {
        let dir = tempfile::tempdir().unwrap();
        let cache = BunsenDiskCache::new(
            BunsenDiskCacheOptions::default()
                .with_cache_dir(Some(dir.path().join("cache")))
                .with_data_dir(Some(dir.path().join("data"))),
        )
        .unwrap();

        // `download = false` throughout: the file is found, or it is not.
        let context = ["prefix"];
        let urls = ["https://example.invalid/mirror/file.txt"];

        let data_file = cache.data_path(&context, "file.txt");
        let cache_file = cache.cache_path(&context, "file.txt");
        assert_ne!(data_file, cache_file);

        // Nothing on disk: neither finds anything.
        assert!(matches!(
            cache.load_data_path(&context, &urls, false, None),
            Err(BunsenError::ResourceNotFound(_))
        ));
        assert!(matches!(
            cache.load_cached_path(&context, &urls, false, None),
            Err(BunsenError::ResourceNotFound(_))
        ));

        // A data file is found by `load_data_path` only.
        fs::create_dir_all(data_file.parent().unwrap()).unwrap();
        fs::write(&data_file, b"data").unwrap();
        assert_eq!(
            cache.load_data_path(&context, &urls, false, None).unwrap(),
            data_file
        );
        assert!(matches!(
            cache.load_cached_path(&context, &urls, false, None),
            Err(BunsenError::ResourceNotFound(_))
        ));

        // A cache file is found by `load_cached_path` only.
        fs::remove_file(&data_file).unwrap();
        fs::create_dir_all(cache_file.parent().unwrap()).unwrap();
        fs::write(&cache_file, b"cache").unwrap();
        assert_eq!(
            cache
                .load_cached_path(&context, &urls, false, None)
                .unwrap(),
            cache_file
        );
        assert!(matches!(
            cache.load_data_path(&context, &urls, false, None),
            Err(BunsenError::ResourceNotFound(_))
        ));
    }

    fn observing_cache(
        dir: &Path,
        observer: &Arc<RecordingObserver>,
    ) -> BunsenDiskCache {
        BunsenDiskCache::new(
            BunsenDiskCacheOptions::default()
                .with_cache_dir(Some(dir.join("cache")))
                .without_transfer_observers()
                .with_transfer_observer(observer.clone()),
        )
        .unwrap()
    }

    /// URLs are tried in order: one with nothing listening is passed over,
    /// the next serves the file, and only that transfer is reported.
    #[test]
    fn test_load_cached_path_tries_urls_in_order() {
        let dir = tempfile::tempdir().unwrap();
        let observer = Arc::new(RecordingObserver::default());
        let cache = observing_cache(dir.path(), &observer);

        let dead = refused_url("abc.bin");
        let live = serve_once("abc.bin", b"abc");
        let path = cache
            .load_cached_path(
                &["t"],
                &[dead.as_str(), live.as_str()],
                true,
                Some(ABC_SHA256),
            )
            .unwrap();
        assert_eq!(fs::read(&path).unwrap(), b"abc");

        let begins: Vec<_> = observer
            .events()
            .into_iter()
            .filter(|e| matches!(e, CacheProgressEvent::Begin { .. }))
            .collect();
        assert_eq!(
            begins,
            vec![CacheProgressEvent::Begin {
                source: live.clone(),
                dest: path.clone(),
                total: Some(3),
            }]
        );
    }

    /// A pinned file whose bytes do not match is refused, leaves nothing on
    /// disk, and comes back `Invalid` when it was the only URL.
    #[test]
    fn test_load_cached_path_refuses_a_bad_digest() {
        let dir = tempfile::tempdir().unwrap();
        let observer = Arc::new(RecordingObserver::default());
        let cache = observing_cache(dir.path(), &observer);

        let url = serve_once("abc.bin", b"abd");
        let result = cache.load_cached_path(&["t"], &[url.as_str()], true, Some(ABC_SHA256));

        assert!(matches!(result, Err(BunsenError::Invalid(_))), "{result:?}");
        let path = cache.cache_path(&["t"], "abc.bin");
        assert!(!path.exists());
        assert!(!partial_path(&path).exists());
    }

    /// When every URL fails, the error names each one.
    #[test]
    fn test_load_cached_path_names_every_failed_url() {
        let dir = tempfile::tempdir().unwrap();
        let observer = Arc::new(RecordingObserver::default());
        let cache = observing_cache(dir.path(), &observer);

        let a = refused_url("abc.bin");
        let b = refused_url("abc.bin");
        match cache.load_cached_path(&["t"], &[a.as_str(), b.as_str()], true, None) {
            Err(BunsenError::External(message)) => {
                assert!(message.contains(&a), "{message}");
                assert!(message.contains(&b), "{message}");
            }
            other => panic!("expected External, got {other:?}"),
        }
        assert!(observer.events().is_empty());
    }

    /// A URL that names no file is refused before anything is fetched.
    #[test]
    fn test_load_cached_path_needs_a_file_name() {
        let dir = tempfile::tempdir().unwrap();
        let observer = Arc::new(RecordingObserver::default());
        let cache = observing_cache(dir.path(), &observer);

        let result = cache.load_cached_path(&["t"], &["https://example.invalid/dir/"], true, None);
        assert!(matches!(result, Err(BunsenError::Invalid(_))), "{result:?}");
        let result = cache.load_cached_path::<&str, &str>(&["t"], &[], true, None);
        assert!(matches!(result, Err(BunsenError::Invalid(_))), "{result:?}");
    }
}