satkit 0.21.0

Satellite Toolkit
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
//! Download / refresh the data files satkit needs.
//!
//! Static files (ephemeris, IERS tables, gravity coefficients, leap-second
//! list) come from the embedded [data manifest](crate::utils::manifest)
//! and are SHA-256 verified; the regularly updated files (EOP, space weather)
//! are listed in the manifest's `refresh` section and fetched unverified from
//! celestrak on every run. See `data/README.md` for the design.

use super::download::{self, download_file_async};
use super::manifest::{self, FetchOutcome};
use crate::utils::datadir;
use std::path::PathBuf;
use std::thread::JoinHandle;
use thiserror::Error;

/// Errors produced by [`update_datafiles`].
#[derive(Debug, Error)]
pub enum Error {
    /// A refresh-manifest URL did not use `https://`.
    #[error("Manifest URL {url:?} must use https://")]
    InsecureManifestUrl { url: String },

    /// A manifest file name was not a single plain path component
    /// (absolute, contained `..`, or contained a path separator). Such a
    /// name would be joined onto the data directory and could escape it.
    #[error("Manifest file name {name:?} is not a plain path component")]
    InvalidManifestPath { name: String },

    /// The configured data directory is read-only and cannot receive
    /// new or refreshed files.
    #[error(
        "Data directory is read-only. Try setting SATKIT_DATA environment variable \
         to a writeable directory and re-starting"
    )]
    DataDirReadOnly,

    /// A worker thread launched by [`download_file_async`] or the static
    /// fetch panicked.
    #[error("Background download thread panicked")]
    ThreadPanic,

    #[error(transparent)]
    Json(#[from] serde_json::Error),

    #[error(transparent)]
    Io(#[from] std::io::Error),

    #[error(transparent)]
    Datadir(#[from] crate::utils::datadir::Error),

    #[error(transparent)]
    Download(#[from] download::Error),
}

/// Convenient type alias used throughout the `update_data` module.
pub type Result<T> = std::result::Result<T, Error>;

/// Fetch every default static file of the embedded manifest into `dir`,
/// in parallel, verifying each against its pinned size and SHA-256.
///
/// Returns one `(name, outcome)` per file. `force` re-downloads even when a
/// matching file is already present.
pub fn download_static_files(
    dir: &std::path::Path,
    force: bool,
) -> Result<Vec<(String, FetchOutcome)>> {
    let m = manifest::embedded();
    let handles: Vec<(String, JoinHandle<download::Result<FetchOutcome>>)> = m
        .default_files()
        .map(|entry| {
            let entry = entry.clone();
            let dir = dir.to_path_buf();
            let name = entry.name.clone();
            (
                name,
                std::thread::spawn(move || manifest::fetch_static_file(&entry, &dir, force)),
            )
        })
        .collect();
    let mut out = Vec::with_capacity(handles.len());
    for (name, jh) in handles {
        let outcome = jh.join().map_err(|_| Error::ThreadPanic)??;
        out.push((name, outcome));
    }
    Ok(out)
}

/// Download the regularly refreshed files (EOP, space weather) listed in the
/// manifest's `refresh` section, always overwriting.
fn download_refresh_files(dir: &std::path::Path) -> Result<()> {
    let m = manifest::embedded();
    let handles: Vec<JoinHandle<download::Result<bool>>> = m
        .refresh
        .iter()
        .map(|url| -> Result<_> {
            if !url.starts_with("https://") {
                return Err(Error::InsecureManifestUrl { url: url.clone() });
            }
            Ok(download_file_async(url.clone(), dir, true))
        })
        .collect::<Result<Vec<_>>>()?;
    for jh in handles {
        jh.join().map_err(|_| Error::ThreadPanic)??;
    }
    Ok(())
}

///
/// Download and update any necessary data files for "satkit" calculations
///
/// # Arguments
/// dir: The directory to download to, optional.  If not provided, the default data directory is used.
/// overwrite_if_exists: If true, re-download static files even when a verified copy is present.
///   If false, a static file whose size and SHA-256 already match the manifest is left alone.
///
/// # Returns
/// Result<()>
///
/// # Notes
///
/// Static files (JPL ephemeris, IERS nutation tables, gravity coefficients,
/// leap-second list) are described by the embedded
/// [data manifest](crate::utils::manifest): each is fetched from the first
/// working source (`SATKIT_DATA_URL` mirror if set, then the GitHub release
/// asset, the origin server, and the legacy bucket) and is only accepted
/// when its size and SHA-256 match the manifest.
///
/// The space weather and Earth orientation files are refreshed from
/// celestrak on every call, and the NOAA solar-cycle forecast is fetched;
/// these change daily and are not pinned.
///
pub fn update_datafiles(dir: Option<PathBuf>, overwrite_if_exists: bool) -> Result<()> {
    let downloaddir = match dir {
        Some(d) => d,
        None => datadir()?,
    };
    if !downloaddir.is_dir() {
        std::fs::create_dir_all(&downloaddir)?;
    }
    if downloaddir.metadata()?.permissions().readonly() {
        return Err(Error::DataDirReadOnly);
    }

    let m = manifest::embedded();
    println!(
        "Downloading data files ({}) to {}",
        m.data_version,
        downloaddir.to_string_lossy()
    );
    if let Some(mirror) = manifest::mirror_base() {
        println!("  {} = {mirror} (tried first)", manifest::MIRROR_ENV);
    }
    for (name, outcome) in download_static_files(&downloaddir, overwrite_if_exists)? {
        match outcome {
            FetchOutcome::AlreadyPresent => println!("  {name}: present and verified"),
            FetchOutcome::Downloaded { url } => println!("  {name}: downloaded from {url}"),
        }
    }

    println!("Now downloading files that are regularly updated:");
    println!("  Space Weather & Earth Orientation Parameters");
    download_refresh_files(&downloaddir)?;

    println!("  Solar Cycle Forecast");
    if let Err(e) = crate::solar_cycle_forecast::update() {
        eprintln!("Warning: could not download solar cycle forecast: {e}");
    }

    // Refresh the in-memory space-weather / EOP singletons from the files just
    // downloaded, so a process whose lazy first load failed (e.g. it started
    // before the data directory was populated) recovers without a restart.
    let sw_path = downloaddir.join("SW-All.csv");
    if sw_path.is_file() {
        if let Err(e) = crate::spaceweather::init_from_path(&sw_path) {
            eprintln!("Warning: could not load downloaded space-weather file: {e}");
        }
    }
    let eop_path = downloaddir.join("EOP-All.csv");
    if eop_path.is_file() {
        if let Err(e) = crate::earth_orientation_params::init_from_path(&eop_path) {
            eprintln!("Warning: could not load downloaded EOP file: {e}");
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::manifest::{sha256_hex, ManifestEntry};
    use std::collections::HashMap;
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};

    // All fetch tests hold `ENV_LOCK`: `candidate_urls()` reads SATKIT_DATA_URL,
    // and the mirror test sets it, so they must not run concurrently.

    /// A minimal in-process HTTP/1.1 server: `GET /<path>` returns the bytes
    /// registered for that path or 404. Counts requests so tests can assert
    /// what was (not) downloaded. Stops when `stop` is set.
    struct TestServer {
        base: String,
        hits: Arc<AtomicUsize>,
        stop: Arc<AtomicBool>,
        thread: Option<std::thread::JoinHandle<()>>,
    }

    impl TestServer {
        fn start(files: HashMap<String, Vec<u8>>) -> Self {
            let listener = TcpListener::bind("127.0.0.1:0").unwrap();
            listener.set_nonblocking(true).unwrap();
            let port = listener.local_addr().unwrap().port();
            let hits = Arc::new(AtomicUsize::new(0));
            let stop = Arc::new(AtomicBool::new(false));
            let files = Arc::new(Mutex::new(files));
            let (h2, s2, f2) = (hits.clone(), stop.clone(), files.clone());
            let thread = std::thread::spawn(move || {
                while !s2.load(Ordering::Relaxed) {
                    match listener.accept() {
                        Ok((mut sock, _)) => {
                            h2.fetch_add(1, Ordering::Relaxed);
                            sock.set_nonblocking(false).unwrap();
                            let mut buf = vec![0u8; 4096];
                            let n = sock.read(&mut buf).unwrap_or(0);
                            let req = String::from_utf8_lossy(&buf[..n]).to_string();
                            let path = req
                                .lines()
                                .next()
                                .and_then(|l| l.split_whitespace().nth(1))
                                .unwrap_or("/")
                                .trim_start_matches('/')
                                .to_string();
                            let body = f2.lock().unwrap().get(&path).cloned();
                            let resp = match body {
                                Some(b) => {
                                    let mut r = format!(
                                        "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                                        b.len()
                                    )
                                    .into_bytes();
                                    r.extend_from_slice(&b);
                                    r
                                }
                                None => b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec(),
                            };
                            let _ = sock.write_all(&resp);
                            let _ = sock.flush();
                        }
                        Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                            std::thread::sleep(std::time::Duration::from_millis(5));
                        }
                        Err(_) => break,
                    }
                }
            });
            Self {
                base: format!("http://127.0.0.1:{port}"),
                hits,
                stop,
                thread: Some(thread),
            }
        }
        fn url(&self, path: &str) -> String {
            format!("{}/{path}", self.base)
        }
        fn hits(&self) -> usize {
            self.hits.load(Ordering::Relaxed)
        }
    }

    impl Drop for TestServer {
        fn drop(&mut self) {
            self.stop.store(true, Ordering::Relaxed);
            if let Some(t) = self.thread.take() {
                let _ = t.join();
            }
        }
    }

    fn entry(name: &str, bytes: &[u8], urls: Vec<String>) -> ManifestEntry {
        ManifestEntry {
            name: name.into(),
            size: bytes.len() as u64,
            sha256: sha256_hex(bytes),
            urls,
            source: "test".into(),
            license: String::new(),
            tier: "core".into(),
            default: true,
        }
    }

    fn tmpdir(tag: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("satkit_fetch_{tag}_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        std::fs::create_dir_all(&d).unwrap();
        d
    }

    /// Under offline mode a lazy fetch is a typed error and **no HTTP
    /// request is made**: the in-process server sees zero hits.
    #[test]
    fn offline_mode_blocks_fetch_without_network_io() {
        let _guard = crate::utils::manifest::ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let bytes = b"offline test bytes".to_vec();
        let server = TestServer::start(HashMap::from([("f.txt".to_string(), bytes.clone())]));
        let e = entry("f.txt", &bytes, vec![server.url("f.txt")]);
        let dir = tmpdir("offline");
        download::set_offline(true);
        let err = manifest::fetch_static_file(&e, &dir, false).unwrap_err();
        download::set_offline(false);
        // Leave the process in its environment-driven state afterwards.
        struct Restore;
        impl Drop for Restore {
            fn drop(&mut self) {
                download::clear_offline_override();
            }
        }
        let _restore = Restore;
        assert!(
            matches!(&err, download::Error::Offline { name, urls, .. } if name == "f.txt" && urls.len() == 1),
            "{err}"
        );
        assert!(err.to_string().contains(&server.url("f.txt")));
        assert_eq!(server.hits(), 0, "offline mode must not open a connection");
        assert!(!dir.join("f.txt").exists());
        // With offline mode lifted the same fetch succeeds.
        manifest::fetch_static_file(&e, &dir, false).unwrap();
        assert_eq!(server.hits(), 1);
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// `set_offline` overrides `SATKIT_OFFLINE` in both directions; with no
    /// setter call the environment decides.
    #[test]
    fn offline_setter_overrides_environment() {
        let _guard = crate::utils::manifest::ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_env = std::env::var_os(download::OFFLINE_ENV);
        // Env says offline, setter says online -> online.
        std::env::set_var(download::OFFLINE_ENV, "1");
        download::set_offline(false);
        assert!(!download::is_offline());
        // Env says online, setter says offline -> offline.
        std::env::remove_var(download::OFFLINE_ENV);
        download::set_offline(true);
        assert!(download::is_offline());
        download::set_offline(false);
        assert!(!download::is_offline());
        // Back to environment-driven: with the var unset that is "online".
        download::clear_offline_override();
        assert!(!download::is_offline());
        match prior_env {
            Some(v) => std::env::set_var(download::OFFLINE_ENV, v),
            None => std::env::remove_var(download::OFFLINE_ENV),
        }
    }

    #[test]
    fn fetch_success_is_verified_and_cached() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let data = b"the quick brown fox".to_vec();
        let srv = TestServer::start(HashMap::from([("good.bin".to_string(), data.clone())]));
        let dir = tmpdir("ok");
        let e = entry("good.bin", &data, vec![srv.url("good.bin")]);

        let out = manifest::fetch_static_file(&e, &dir, false).unwrap();
        assert_eq!(
            out,
            FetchOutcome::Downloaded {
                url: srv.url("good.bin")
            }
        );
        assert_eq!(std::fs::read(dir.join("good.bin")).unwrap(), data);
        assert!(!dir.join("good.bin.part").exists());
        assert_eq!(srv.hits(), 1);

        // Second call: present + hash matches -> no request at all.
        let out = manifest::fetch_static_file(&e, &dir, false).unwrap();
        assert_eq!(out, FetchOutcome::AlreadyPresent);
        assert_eq!(srv.hits(), 1, "verified file must not be re-downloaded");

        // force = true re-downloads.
        let out = manifest::fetch_static_file(&e, &dir, true).unwrap();
        assert!(matches!(out, FetchOutcome::Downloaded { .. }));
        assert_eq!(srv.hits(), 2);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn fetch_falls_through_404_to_next_url() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let data = b"payload".to_vec();
        let first = TestServer::start(HashMap::new()); // serves nothing -> 404
        let second = TestServer::start(HashMap::from([("f.bin".to_string(), data.clone())]));
        let dir = tmpdir("fallthrough");
        let e = entry(
            "f.bin",
            &data,
            vec![first.url("f.bin"), second.url("f.bin")],
        );
        let out = manifest::fetch_static_file(&e, &dir, false).unwrap();
        assert_eq!(
            out,
            FetchOutcome::Downloaded {
                url: second.url("f.bin")
            }
        );
        assert_eq!(first.hits(), 1);
        assert_eq!(second.hits(), 1);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn fetch_rejects_hash_mismatch_and_tries_next_url() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let good = b"correct bytes".to_vec();
        let bad = b"corrupt bytes".to_vec(); // same length: exercises the sha check, not the size check
        let first = TestServer::start(HashMap::from([("f.bin".to_string(), bad)]));
        let second = TestServer::start(HashMap::from([("f.bin".to_string(), good.clone())]));
        let dir = tmpdir("mismatch");
        let e = entry(
            "f.bin",
            &good,
            vec![first.url("f.bin"), second.url("f.bin")],
        );
        let out = manifest::fetch_static_file(&e, &dir, false).unwrap();
        assert_eq!(
            out,
            FetchOutcome::Downloaded {
                url: second.url("f.bin")
            }
        );
        assert_eq!(std::fs::read(dir.join("f.bin")).unwrap(), good);
        assert!(
            !dir.join("f.bin.part").exists(),
            "corrupt partial must be removed"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn fetch_reports_every_failed_source() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let a = TestServer::start(HashMap::new());
        let b = TestServer::start(HashMap::from([("f.bin".to_string(), b"wrong".to_vec())]));
        let dir = tmpdir("allfail");
        let e = entry("f.bin", b"right", vec![a.url("f.bin"), b.url("f.bin")]);
        let err = manifest::fetch_static_file(&e, &dir, false).unwrap_err();
        match &err {
            download::Error::AllSourcesFailed { name, attempts } => {
                assert_eq!(name, "f.bin");
                assert_eq!(attempts.len(), 2);
                assert!(attempts[0].starts_with(&a.url("f.bin")), "{}", attempts[0]);
                assert!(attempts[1].starts_with(&b.url("f.bin")), "{}", attempts[1]);
                assert!(attempts[1].contains("mismatch"), "{}", attempts[1]);
            }
            other => panic!("unexpected error {other}"),
        }
        assert!(!dir.join("f.bin").exists());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn mirror_override_is_tried_before_manifest_urls() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let data = b"mirror payload".to_vec();
        let mirror = TestServer::start(HashMap::from([("f.bin".to_string(), data.clone())]));
        let official = TestServer::start(HashMap::from([("f.bin".to_string(), data.clone())]));
        let dir = tmpdir("mirror");
        let e = entry("f.bin", &data, vec![official.url("f.bin")]);
        std::env::set_var(manifest::MIRROR_ENV, &mirror.base);
        let out = manifest::fetch_static_file(&e, &dir, false);
        std::env::remove_var(manifest::MIRROR_ENV);
        assert_eq!(
            out.unwrap(),
            FetchOutcome::Downloaded {
                url: mirror.url("f.bin")
            }
        );
        assert_eq!(mirror.hits(), 1);
        assert_eq!(
            official.hits(),
            0,
            "official URL must not be contacted when the mirror works"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn existing_corrupt_file_is_replaced() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let data = b"fresh".to_vec();
        let srv = TestServer::start(HashMap::from([("f.bin".to_string(), data.clone())]));
        let dir = tmpdir("corrupt");
        std::fs::write(dir.join("f.bin"), b"stale").unwrap(); // same size, wrong hash
        let e = entry("f.bin", &data, vec![srv.url("f.bin")]);
        let out = manifest::fetch_static_file(&e, &dir, false).unwrap();
        assert!(matches!(out, FetchOutcome::Downloaded { .. }));
        assert_eq!(std::fs::read(dir.join("f.bin")).unwrap(), data);
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Real network, full run: `update_datafiles` into a temp dir; prints the
    /// Many concurrent fetches of the same file: each writes its own
    /// `.part.<pid>.<seq>`, exactly one verified final file results, no
    /// temporary file is left behind and every caller succeeds.
    #[test]
    fn concurrent_fetches_of_one_file_yield_one_verified_copy() {
        let _guard = crate::utils::manifest::ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let bytes: Vec<u8> = (0..200_000u32).map(|i| (i % 251) as u8).collect();
        let server = TestServer::start(HashMap::from([("big.bin".to_string(), bytes.clone())]));
        let e = std::sync::Arc::new(entry("big.bin", &bytes, vec![server.url("big.bin")]));
        let dir = std::sync::Arc::new(tmpdir("concurrent"));
        let handles: Vec<_> = (0..8)
            .map(|_| {
                let (e, dir) = (e.clone(), dir.clone());
                std::thread::spawn(move || {
                    crate::utils::manifest::fetch_static_file(&e, &dir, false)
                })
            })
            .collect();
        for h in handles {
            let outcome = h.join().unwrap().expect("every concurrent fetch succeeds");
            assert!(matches!(
                outcome,
                FetchOutcome::Downloaded { .. } | FetchOutcome::AlreadyPresent
            ));
        }
        assert!(
            e.verify(&dir.join("big.bin")).unwrap(),
            "final file verified"
        );
        let leftovers: Vec<String> = std::fs::read_dir(&*dir)
            .unwrap()
            .flatten()
            .map(|d| d.file_name().to_string_lossy().into_owned())
            .filter(|n| n.contains(".part"))
            .collect();
        assert!(leftovers.is_empty(), "leftover temp files: {leftovers:?}");
        assert!(server.hits() >= 1 && server.hits() <= 8);
        let _ = std::fs::remove_dir_all(&*dir);
    }

    /// An on-disk manifest-pinned file is hashed once and then trusted via
    /// the sidecar marker until it changes; a wrong copy is `CorruptFile`.
    #[test]
    fn on_disk_file_is_verified_once_via_sidecar_marker() {
        use crate::utils::download::Error;
        use crate::utils::manifest::Verified;
        let bytes = b"correct contents of a pinned file".to_vec();
        let e = entry(
            "pinned.bin",
            &bytes,
            vec!["https://example.invalid/p".into()],
        );
        let dir = tmpdir("sidecar");
        let path = dir.join("pinned.bin");
        let marker = ManifestEntry::verified_marker_path(&path);

        // Wrong bytes, right size -> corrupt, no marker written.
        std::fs::write(&path, b"wrong!! contents of a pinned file").unwrap();
        let err = e.ensure_verified(&path).unwrap_err();
        assert!(
            matches!(err, Error::CorruptFile { what: "sha256", .. }),
            "{err}"
        );
        assert!(!marker.exists());
        // Wrong size -> corrupt without hashing.
        std::fs::write(&path, b"short").unwrap();
        assert!(matches!(
            e.ensure_verified(&path).unwrap_err(),
            Error::CorruptFile { what: "size", .. }
        ));

        // Right bytes -> hashed once, marker created, then cached.
        std::fs::write(&path, &bytes).unwrap();
        assert_eq!(e.ensure_verified(&path).unwrap(), Verified::Hashed);
        assert!(marker.exists());
        assert_eq!(e.ensure_verified(&path).unwrap(), Verified::Cached);

        // Same size, different bytes, newer mtime -> the marker no longer
        // matches, the file is re-hashed and the corruption is caught.
        std::fs::write(&path, b"wrong!! contents of a pinned file").unwrap();
        let later = std::time::SystemTime::now() + std::time::Duration::from_secs(5);
        std::fs::File::options()
            .write(true)
            .open(&path)
            .unwrap()
            .set_modified(later)
            .unwrap();
        assert!(matches!(
            e.ensure_verified(&path).unwrap_err(),
            Error::CorruptFile { what: "sha256", .. }
        ));

        // Restored bytes with yet another mtime -> hashed again, then cached.
        std::fs::write(&path, &bytes).unwrap();
        assert_eq!(e.ensure_verified(&path).unwrap(), Verified::Hashed);
        assert_eq!(e.ensure_verified(&path).unwrap(), Verified::Cached);
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// ureq's default agent reads `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY`
    /// (and honours `NO_PROXY`); constructing it with a proxy set must not
    /// fail or touch the network.
    #[test]
    fn proxy_env_is_accepted_by_the_agent() {
        let _guard = crate::utils::manifest::ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::env::set_var("HTTPS_PROXY", "http://proxy.invalid:3128");
        let agent = ureq::Agent::new_with_defaults();
        let has_proxy = agent.config().proxy().is_some();
        std::env::remove_var("HTTPS_PROXY");
        assert!(
            has_proxy,
            "ureq should pick the proxy up from the environment"
        );
    }

    /// URL each file came from. `cargo test --lib real_network_update -- --ignored --nocapture`.
    #[test]
    #[ignore = "requires network access; downloads ~110 MB"]
    fn real_network_update_datafiles_into_tmp() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = tmpdir("full");
        let t0 = std::time::Instant::now();
        update_datafiles(Some(dir.clone()), false).unwrap();
        println!("update_datafiles took {:.1} s", t0.elapsed().as_secs_f64());
        for e in manifest::embedded().default_files() {
            assert!(
                e.verify(&dir.join(&e.name)).unwrap(),
                "{} not verified",
                e.name
            );
        }
        assert!(dir.join("EOP-All.csv").is_file() && dir.join("SW-All.csv").is_file());
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Real network: exercises the GitHub-asset → origin/GCS fallthrough for
    /// the smallest manifest file. `cargo test -- --ignored real_network`.
    #[test]
    #[ignore = "requires network access"]
    fn real_network_fetch_smallest_file() {
        let _guard = manifest::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let m = manifest::embedded();
        let e = m.entry("tab5.2d.txt").unwrap();
        let dir = tmpdir("net");
        let out = manifest::fetch_static_file(e, &dir, false).unwrap();
        println!("{out:?}");
        assert!(e.verify(&dir.join("tab5.2d.txt")).unwrap());
        let _ = std::fs::remove_dir_all(&dir);
    }
}