zv 0.15.0

Ziglang Version Manager and Project Starter
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
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
//! Mirrors management and types for Zig versions
//!
//! This module provides functionality for managing HTTP mirrors that host Zig releases.
//! It supports different mirror layouts (flat and versioned), caching strategies,
//! and automatic failover between mirrors.
//!
//! # Key Components
//!
//! - [`Mirror`]: Represents a single HTTP mirror with its URL and layout
//! - [`MirrorManager`]: Manages a collection of mirrors with caching and loading strategies
//! - [`MirrorsIndex`]: Cached representation of mirrors with TTL support
//! - [`Layout`]: Defines how files are organized on a mirror (flat vs versioned)
//!
//! # Cache Strategies
//!
//! The module supports three caching strategies via [`CacheStrategy`]:
//! - `AlwaysRefresh`: Always fetch fresh mirrors from network
//! - `PreferCache`: Use cache if available, fallback to network
//! - `RespectTtl`: Use cache only if not expired, otherwise refresh
//!
//! # Example Usage
//!
//! ```rust,no_run
//! use std::sync::Arc;
//! use reqwest::Client;
//! use crate::app::network::mirror::MirrorManager;
//! use crate::app::network::CacheStrategy;
//!
//! async fn example() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = Arc::new(Client::new());
//!     let cache_path = "/tmp/mirrors.toml";
//!     
//!     let mut manager = MirrorManager::init_and_load(
//!         cache_path,
//!         CacheStrategy::RespectTtl,
//!         client
//!     ).await?;
//!     
//!     let random_mirror = manager.get_random_mirror().await?;
//!     println!("Using mirror: {}", random_mirror.base_url);
//!     
//!     Ok(())
//! }
//! ```

use std::{
    cmp::Ordering,
    collections::HashMap,
    convert::TryFrom,
    path::{Path, PathBuf},
    time::{Duration, Instant},
};

use super::download::download_file;
use super::{CacheStrategy, TARGET};
use crate::{
    CfgErr, NetErr,
    app::{
        MIRRORS_TTL_DAYS,
        constants::ZIG_COMMUNITY_MIRRORS,
        utils::{ProgressHandle, verify_checksum, zv_agent},
    },
};
use chrono::{DateTime, Utc};
use color_eyre::eyre::Result;
use futures::{StreamExt, stream};
use reqwest::{Client, StatusCode};
use semver::Version;
use serde::{Deserialize, Serialize};
use url::Url;

// ============================================================================
// LAYOUT AND MIRROR TYPES
// ============================================================================

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)]
pub enum Layout {
    /// Flat layout: {url}/{tarball}
    Flat,
    /// Versioned layout: {url}/{semver}/{tarball}
    #[default]
    Versioned,
}

impl std::ops::Not for Layout {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            Layout::Flat => Layout::Versioned,
            Layout::Versioned => Layout::Flat,
        }
    }
}

impl From<&str> for Layout {
    fn from(s: &str) -> Self {
        match s {
            "flat" => Layout::Flat,
            "versioned" => Layout::Versioned,
            _ => Layout::default(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// A HTTP mirror for Zig releases
pub struct Mirror {
    pub base_url: Url,
    pub layout: Layout,
    pub rank: u8,
}

// ============================================================================
// MIRROR IMPLEMENTATION
// ============================================================================

impl Mirror {
    /// Attempt to download both tarball and minisig files using this mirror
    ///
    /// This function will automatically try both layouts (flat and versioned) if the first
    /// attempt fails with HTTP 404. If both layouts fail, it returns the error.
    ///
    /// # Arguments
    ///
    /// * `client` - HTTP client for making requests
    /// * `semver_version` - Version to download
    /// * `zig_tarball` - Name of the tarball file
    /// * `tarball_path` - Path where tarball should be saved
    /// * `minisig_path` - Path where minisig file should be saved
    /// * `expected_shasum` - Optional expected SHA256 checksum for verification
    /// * `expected_size` - Optional expected size of the tarball in bytes
    /// * `progress_handle` - Handle for progress reporting
    ///
    /// # Returns
    ///
    /// `Ok(Layout)` with the layout that was successfully used if download succeeds,
    /// otherwise returns the appropriate `NetErr` with detailed context about the failure.
    pub async fn download(
        &self,
        client: &reqwest::Client,
        semver_version: &semver::Version,
        zig_tarball: &str,
        tarball_path: &Path,
        minisig_path: &Path,
        expected_shasum: Option<&str>,
        expected_size: Option<u64>,
        progress_handle: &ProgressHandle,
    ) -> Result<Layout, NetErr> {
        const TARGET: &str = "zv::network::mirror::download";
        tracing::debug!(target: TARGET, "Starting download with mirror: {} (rank: {})", self.base_url, self.rank);

        // Try download with current layout, fall back to alternate on HTTP 404
        match self
            .try_download_with_layout(
                client,
                semver_version,
                zig_tarball,
                tarball_path,
                minisig_path,
                expected_shasum,
                expected_size,
                progress_handle,
                false,
            )
            .await
        {
            Ok(layout) => Ok(layout),
            Err(net_err) => {
                // If the failure was an HTTP 404, try the alternate layout
                if matches!(net_err, NetErr::HTTP(status) if status.as_u16() == 404) {
                    tracing::info!(target: TARGET,
                                  "Initial layout failed with HTTP 404. Trying alternate layout for mirror {}...",
                                  self.base_url);

                    return self
                        .try_download_with_layout(
                            client,
                            semver_version,
                            zig_tarball,
                            tarball_path,
                            minisig_path,
                            expected_shasum,
                            expected_size,
                            progress_handle,
                            true,
                        )
                        .await;
                }

                // Otherwise propagate the concrete network error
                Err(net_err)
            }
        }
    }

    /// Internal helper to try download with a specific layout
    async fn try_download_with_layout(
        &self,
        client: &reqwest::Client,
        semver_version: &semver::Version,
        zig_tarball: &str,
        tarball_path: &Path,
        minisig_path: &Path,
        expected_shasum: Option<&str>,
        expected_size: Option<u64>,
        progress_handle: &ProgressHandle,
        use_alternate_layout: bool,
    ) -> Result<Layout, NetErr> {
        const TARGET: &str = "zv::network::mirror::try_download_with_layout";

        // Determine which layout to use
        let mirror_for_download = if use_alternate_layout {
            let mut alternate = self.clone();
            alternate.layout = !alternate.layout;
            alternate
        } else {
            self.clone()
        };

        // Get download URLs
        let tarball_url = mirror_for_download.get_download_url(semver_version, zig_tarball);
        let minisig_filename = format!("{}.minisig", zig_tarball);
        let minisig_url = mirror_for_download.get_download_url(semver_version, &minisig_filename);

        tracing::trace!(target: TARGET, "Download URLs configured:");
        tracing::trace!(target: TARGET, "  Tarball: {}", tarball_url);
        tracing::trace!(target: TARGET, "  Minisig:  {}", minisig_url);
        if let Some(size) = expected_size {
            tracing::trace!(target: TARGET, "  Expected size: {} bytes ({:.1} MB)", size, size as f64 / 1_048_576.0);
        } else {
            tracing::trace!(target: TARGET, "  Expected size: unknown");
        }
        if let Some(shasum) = expected_shasum {
            tracing::trace!(target: TARGET, "  Expected checksum: {}", shasum);
        } else {
            tracing::trace!(target: TARGET, "  Expected checksum: unknown");
        }

        // Initialize progress reporting
        let progress_msg = format!(
            "Downloading {} from {}",
            zig_tarball, mirror_for_download.base_url
        );
        match progress_handle.start(&progress_msg).await {
            Ok(()) => {}
            Err(e) => {
                tracing::debug!(target: TARGET, "Failed to start progress reporting: {} - continuing without progress updates", e);
            }
        };

        // Phase 1: Download tarball
        match download_file(
            client,
            &tarball_url,
            tarball_path,
            expected_size.unwrap_or(0),
            progress_handle,
        )
        .await
        {
            Ok(()) => {
                tracing::debug!(target: TARGET, "Proceeding to checksum verification...");
            }
            Err(net_err) => {
                tracing::trace!(target: TARGET, "Tarball download failed from mirror {}: {}", mirror_for_download.base_url, net_err);

                match net_err {
                    crate::NetErr::HTTP(status) => {
                        tracing::trace!(target: TARGET, "HTTP error {} during tarball download - mirror may be experiencing issues", status);
                    }
                    crate::NetErr::Timeout(_) => {
                        tracing::trace!(target: TARGET, "Timeout during tarball download - network or mirror performance issues");
                    }
                    _ => {
                        tracing::trace!(target: TARGET, "Network error during tarball download: {}", net_err);
                    }
                }

                return Err(net_err);
            }
        }

        // Phase 2: Verify checksum (if available)
        if let Some(shasum) = expected_shasum {
            tracing::debug!(target: TARGET, "Verifying tarball integrity");
            match verify_checksum(tarball_path, shasum).await {
                Ok(()) => {
                    tracing::debug!(target: TARGET, "Checksum verification successful");
                }
                Err(e) => {
                    tracing::error!(target: TARGET, "Checksum verification failed for tarball from mirror {}: {}", mirror_for_download.base_url, e);
                    // Clean up the corrupted file
                    if tarball_path.exists() {
                        if let Err(cleanup_err) = tokio::fs::remove_file(tarball_path).await {
                            tracing::warn!(target: TARGET, "Failed to remove corrupted tarball file: {}", cleanup_err);
                        } else {
                            tracing::debug!(target: TARGET, "Removed corrupted tarball file");
                        }
                    }
                    return Err(NetErr::Checksum(e.into()));
                }
            }
        } else {
            tracing::debug!(target: TARGET, "Skipping checksum verification - no expected checksum provided");
        }

        // Phase 3: Download minisig file
        tracing::debug!(target: TARGET, "Downloading signature file from {}", minisig_url);
        match progress_handle
            .update("Downloading signature file...")
            .await
        {
            Ok(()) => {
                tracing::debug!(target: TARGET, "Progress updated for minisig download");
            }
            Err(e) => {
                tracing::warn!(target: TARGET, "Failed to update progress for minisig download: {} - continuing", e);
            }
        }

        // For minisig, we don't have size info, so use 0
        match download_file(client, &minisig_url, minisig_path, 0, progress_handle).await {
            Ok(()) => {
                tracing::debug!(target: TARGET, "Minisig download completed successfully");
            }
            Err(net_err) => {
                tracing::error!(target: TARGET, "Minisig download failed from mirror {}: {}", mirror_for_download.base_url, net_err);

                // Provide context about the failure
                match net_err {
                    NetErr::HTTP(status) => {
                        tracing::error!(target: TARGET, "HTTP error {} during minisig download - signature file may not exist on this mirror", status);
                    }
                    NetErr::Timeout(_) => {
                        tracing::error!(target: TARGET, "Timeout during minisig download - network or mirror performance issues");
                    }
                    _ => {
                        tracing::error!(target: TARGET, "Network error during minisig download: {}", net_err);
                    }
                }

                // Clean up the tarball since we couldn't get the signature
                if tarball_path.exists() {
                    if let Err(cleanup_err) = tokio::fs::remove_file(tarball_path).await {
                        tracing::trace!(target: TARGET, "Failed to remove tarball after minisig failure: {}", cleanup_err);
                    } else {
                        tracing::trace!(target: TARGET, "Cleaned up tarball after minisig download failure");
                    }
                }
                return Err(net_err);
            }
        }

        // Verify both files exist and have reasonable sizes
        let tarball_size = match tokio::fs::metadata(tarball_path).await {
            Ok(metadata) => {
                let size = metadata.len();
                tracing::debug!(target: TARGET, "Final tarball size: {} bytes ({:.1} MB)", size, size as f64 / 1_048_576.0);

                if let Some(expected) = expected_size {
                    if size != expected {
                        tracing::warn!(target: TARGET, "Tarball size {} doesn't match expected size {} - this may indicate an issue", size, expected);
                    }
                } else {
                    tracing::debug!(target: TARGET, "No expected size provided for verification");
                }

                size
            }
            Err(e) => {
                tracing::error!(target: TARGET, "Failed to verify final tarball file: {}", e);
                return Err(NetErr::FileIo(e));
            }
        };

        let minisig_size = match tokio::fs::metadata(minisig_path).await {
            Ok(metadata) => {
                let size = metadata.len();
                tracing::debug!(target: TARGET, "Final minisig size: {} bytes", size);

                if size == 0 {
                    tracing::warn!(target: TARGET, "Minisig file is empty - this may indicate a download issue");
                } else if size > 1024 {
                    tracing::warn!(target: TARGET, "Minisig file is unusually large ({} bytes) - this may indicate an error page was downloaded", size);
                }

                size
            }
            Err(e) => {
                tracing::error!(target: TARGET, "Failed to verify final minisig file: {}", e);
                return Err(NetErr::FileIo(e));
            }
        };

        tracing::debug!(target: TARGET, "Download attempt completed successfully with mirror {} - tarball: {:.1} MB, minisig: {} bytes",
                     self.base_url, tarball_size as f64 / 1_048_576.0, minisig_size);

        Ok(mirror_for_download.layout)
    }

    /// Get the primary download URL based on layout
    pub fn get_download_url(&self, version: &Version, tarball: &str) -> String {
        match self.layout {
            Layout::Flat => format!(
                "{}/{tarball}?source={}",
                self.base_url.to_string().trim_end_matches('/'),
                zv_agent()
            ),
            Layout::Versioned => format!(
                "{}/{}/{}?source={}",
                self.base_url.to_string().trim_end_matches('/'),
                version,
                tarball,
                zv_agent()
            ),
        }
    }

    /// Get the download URL with layout inverted
    pub fn get_alternate_url(&self, version: &Version, tarball: &str) -> String {
        let alternate = Mirror {
            base_url: self.base_url.clone(),
            layout: !self.layout,
            rank: self.rank,
        };
        alternate.get_download_url(version, tarball)
    }
    pub fn promote(&mut self) {
        // Lower rank = better
        if self.rank > 1 {
            self.rank -= 1;
        }
    }

    pub fn demote(&mut self) {
        // Higher rank = worse
        self.rank = self.rank.saturating_add(1);
    }
}

impl TryFrom<&str> for Mirror {
    type Error = url::ParseError;

    fn try_from(input: &str) -> Result<Self, Self::Error> {
        let url_str = if input.starts_with("http://") || input.starts_with("https://") {
            input.to_string()
        } else {
            format!("https://{input}")
        };

        let base_url = Url::parse(&url_str)?;

        // Validate scheme
        match base_url.scheme() {
            "http" | "https" => {}
            _ => return Err(url::ParseError::RelativeUrlWithoutBase),
        }
        let layout = match base_url.as_str() {
            u if u.contains("zig.florent.dev") => Layout::Flat,
            u if u.contains("zig.squirl.dev") => Layout::Flat,
            u if u.contains("zigmirror.meox.dev") => Layout::Flat,
            u if u.contains("zig-mirror.tsimnet.eu") => Layout::Flat,
            u if u.contains("pkg.earth") => Layout::Flat,
            u if u.contains("ziglang.freetls.fastly.net") => Layout::Flat,
            u if u.contains("zig.tilok.dev") => Layout::Flat,
            _ => Layout::Versioned,
        };

        Ok(Mirror {
            layout,
            base_url,
            rank: 1,
        })
    }
}

// ============================================================================
// MIRRORS INDEX (CACHE REPRESENTATION)
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Represents the cached mirrors.toml file
pub struct MirrorsIndex {
    /// List of community mirrors
    pub mirrors: Vec<Mirror>,
    /// Timestamp when mirrors were last synced
    pub last_synced: DateTime<Utc>,
}

// ============================================================================
// MIRROR BENCHMARKING
// ============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RankApplyPolicy {
    Overwrite,
    Blend,
}

#[derive(Debug, Clone, Serialize)]
pub struct MirrorBenchmarkResult {
    pub base_url: Url,
    pub old_rank: u8,
    pub old_layout: Layout,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub measured_layout: Option<Layout>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bytes_read: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub elapsed_ms: Option<u128>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bytes_per_second: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

impl MirrorBenchmarkResult {
    pub fn is_success(&self) -> bool {
        self.bytes_per_second.is_some()
    }
}

#[derive(Debug)]
enum BenchmarkProbeError {
    Http(StatusCode),
    Network(reqwest::Error),
    EmptyBody,
}

impl std::fmt::Display for BenchmarkProbeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Http(status) => write!(f, "HTTP {}", status),
            Self::Network(err) if err.is_timeout() => write!(f, "request timed out"),
            Self::Network(err) => write!(f, "{err}"),
            Self::EmptyBody => write!(f, "response body was empty"),
        }
    }
}

impl MirrorsIndex {
    /// Create a new index with current timestamp
    pub fn new(mirrors: Vec<Mirror>) -> Self {
        Self {
            mirrors,
            last_synced: Utc::now(),
        }
    }

    /// Check if the cache has expired based on TTL
    pub fn is_expired(&self) -> bool {
        self.last_synced + chrono::Duration::days(*MIRRORS_TTL_DAYS) < Utc::now()
    }

    /// Load mirrors index from disk (PreferCache strategy)
    pub async fn load_from_disk(path: impl AsRef<Path>) -> Result<Self, CfgErr> {
        let content = tokio::fs::read_to_string(path.as_ref())
            .await
            .map_err(|io_err| CfgErr::NotFound(io_err.into()))?;

        toml::from_str::<Self>(&content).map_err(|e| CfgErr::ParseFail(e.into()))
    }

    /// Load mirrors index from disk, failing if expired (RespectTtl strategy)
    #[allow(unused)]
    pub async fn load_from_disk_expire_checked(path: impl AsRef<Path>) -> Result<Self, CfgErr> {
        let index = Self::load_from_disk(path.as_ref()).await?;

        if index.is_expired() {
            return Err(CfgErr::CacheExpired(
                path.as_ref().to_string_lossy().to_string(),
            ));
        }

        Ok(index)
    }

    /// Save mirrors index to disk
    pub async fn save(&self, path: impl AsRef<Path>) -> Result<(), CfgErr> {
        let content = toml::to_string_pretty(self).map_err(CfgErr::SerializeFail)?;

        tokio::fs::write(path, content)
            .await
            .map_err(|io_err| CfgErr::WriteFail(io_err.into(), String::from("mirrors index")))?;

        Ok(())
    }
}

// ============================================================================
// MIRROR MANAGER
// ============================================================================

#[derive(Debug, Clone)]
pub struct MirrorManager {
    /// HTTP client for network requests
    client: Client,
    /// Currently loaded mirrors
    mirrors: Vec<Mirror>,
    /// Cached mirrors index (lazy loaded)
    mirrors_index: Option<MirrorsIndex>,
    /// Path to the mirrors cache file
    cache_path: PathBuf,
}

impl MirrorManager {
    // ============================================================================
    // MIRROR MANAGER - CONSTRUCTION AND INITIALIZATION
    // ============================================================================
    /// Create a new mirror manager (doesn't load mirrors yet)
    pub fn new(cache_path: impl AsRef<Path>) -> Result<Self> {
        Ok(Self {
            client: super::create_client()?,
            mirrors: Vec::with_capacity(7), // 7 mirrors listed as of September 2025
            mirrors_index: None,
            cache_path: cache_path.as_ref().to_path_buf(),
        })
    }

    /// Create manager and immediately load mirrors
    pub async fn init_and_load(
        cache_path: impl AsRef<Path>,
        cache_strategy: CacheStrategy,
    ) -> Result<Self, NetErr> {
        let mut manager = Self::new(cache_path)?;
        manager.load_mirrors(cache_strategy).await?;
        Ok(manager)
    }

    // ============================================================================
    // MIRROR MANAGER - LOADING AND CACHING
    // ============================================================================
    /// Load mirrors (self.mirrors) according to the specified cache strategy
    pub async fn load_mirrors(&mut self, cache_strategy: CacheStrategy) -> Result<(), NetErr> {
        match cache_strategy {
            CacheStrategy::AlwaysRefresh => {
                self.refresh_from_network().await?;
            }
            CacheStrategy::PreferCache => {
                if self.try_load_index_from_cache().await.is_err() {
                    tracing::warn!(target: TARGET, "Failed to load cached mirrors, fetching from network");
                    self.refresh_from_network().await?;
                }
            }
            CacheStrategy::OnlyCache => {
                if self.try_load_index_from_cache().await.is_err() {
                    tracing::warn!(target: TARGET, "mirrors cache not found. OnlyCache strategy... returning EmptyMirrors");
                    return Err(NetErr::EmptyMirrors);
                }
            }
            CacheStrategy::RespectTtl => match self.try_load_index_from_cache().await {
                Ok(()) => {
                    if self.is_cache_expired() {
                        tracing::debug!(target: TARGET, "Mirrors cache expired, refreshing");
                        self.refresh_from_network().await?;
                    } else {
                        tracing::debug!(target: TARGET, "Using cached mirrors");
                        self.apply_cached_mirrors_index();
                    }
                }
                Err(_) => {
                    tracing::debug!(target: TARGET, "No valid cache, fetching from network");
                    self.refresh_from_network().await?;
                }
            },
        }
        Ok(())
    }

    /// Try to load mirrors index from cache
    async fn try_load_index_from_cache(&mut self) -> Result<(), NetErr> {
        let index = MirrorsIndex::load_from_disk(&self.cache_path)
            .await
            .map_err(|err| {
                tracing::debug!(target: TARGET, "Failed to load mirrors cache from disk: {err}");
                NetErr::EmptyMirrors
            })?;

        self.mirrors_index = Some(index);
        Ok(())
    }

    /// Apply cached mirrors to active mirrors list
    fn apply_cached_mirrors_index(&mut self) {
        if let Some(ref index) = self.mirrors_index {
            self.mirrors = index.mirrors.clone();
        }
    }

    /// Refresh mirrors from network and cache them, preserving existing layouts and ranks
    async fn refresh_from_network(&mut self) -> Result<(), NetErr> {
        let fresh_mirrors = self.fetch_network_mirrors().await?;

        // Try to load existing cached mirrors to preserve layouts and ranks
        let merged_mirrors = match MirrorsIndex::load_from_disk(&self.cache_path).await {
            Ok(cached_index) => {
                let cached_mirrors_map: std::collections::HashMap<String, Mirror> = cached_index
                    .mirrors
                    .into_iter()
                    .map(|m| (m.base_url.to_string(), m))
                    .collect();

                let merged: Vec<Mirror> = fresh_mirrors
                    .into_iter()
                    .map(|mut fresh_mirror| {
                        if let Some(cached_mirror) =
                            cached_mirrors_map.get(fresh_mirror.base_url.as_str())
                        {
                            fresh_mirror.layout = cached_mirror.layout;
                            fresh_mirror.rank = cached_mirror.rank;
                        }
                        fresh_mirror
                    })
                    .collect();

                tracing::debug!(target: TARGET, "Merged layouts and ranks from {} cached mirrors into {} fresh mirrors",
                             cached_mirrors_map.len(), merged.len());
                merged
            }
            Err(_) => {
                tracing::debug!(target: TARGET, "No cached mirrors found, using fresh mirrors from network");
                fresh_mirrors
            }
        };

        self.mirrors = merged_mirrors;
        let index = MirrorsIndex::new(self.mirrors.clone());

        // Save to cache (log errors but don't fail)
        if let Err(e) = index.save(&self.cache_path).await {
            tracing::error!(target: TARGET, "Failed to save mirrors cache: {}", e);
        }

        self.mirrors_index = Some(index);
        Ok(())
    }

    /// Fetch mirrors from the network
    async fn fetch_network_mirrors(&self) -> Result<Vec<Mirror>, NetErr> {
        tracing::debug!(target: TARGET, "Fetching mirrors from {}", ZIG_COMMUNITY_MIRRORS);

        let mirrors: Vec<Mirror> = self
            .client
            .get(ZIG_COMMUNITY_MIRRORS)
            .send()
            .await
            .map_err(NetErr::Reqwest)?
            .text()
            .await
            .map_err(NetErr::Reqwest)?
            .lines()
            .filter(|line| !line.trim().is_empty()) // Skip empty lines
            .filter_map(|line| {
                Mirror::try_from(line.trim())
                    .inspect_err(|&e| {
                        tracing::warn!(target: TARGET, "Failed to parse mirror '{}': {}", line, e);
                    })
                    .ok()
            })
            .collect();

        if mirrors.is_empty() {
            tracing::error!(target: TARGET, "No valid mirrors found in response");
            return Err(NetErr::EmptyMirrors);
        }

        tracing::debug!(target: TARGET, "Successfully fetched {} mirrors", mirrors.len());
        Ok(mirrors)
    }
    // ============================================================================
    // MIRROR MANAGER - INTERNAL HELPERS
    // ============================================================================
    /// Ensure mirrors are loaded (no-op if mirrors-index is already loaded)
    async fn ensure_mirrors_loaded(&mut self) -> Result<(), NetErr> {
        if self.mirrors_index.is_none() {
            match MirrorsIndex::load_from_disk(&self.cache_path).await {
                Ok(index) => {
                    self.mirrors_index = Some(index);
                }
                Err(_) => {
                    // No cache exists, fetch from network
                    self.refresh_from_network().await?;
                }
            }
        }

        // Apply mirrors from index if we don't have them loaded
        if self.mirrors.is_empty() {
            self.apply_cached_mirrors_index();
        }

        Ok(())
    }
    /// Check if the cached mirrors have expired
    #[inline]
    fn is_cache_expired(&self) -> bool {
        match &self.mirrors_index {
            Some(index) => index.is_expired(),
            None => true, // No cache loaded means it's "expired"
        }
    }
    // ============================================================================
    // MIRROR MANAGER - PUBLIC API
    // ============================================================================
    /// Get all available mirrors from MirrorManager.mirrors (loading if needed)
    pub async fn all_mirrors_mut(&mut self) -> Result<&mut [Mirror], NetErr> {
        if self.mirrors.is_empty() {
            self.ensure_mirrors_loaded().await?;
        }
        Ok(&mut self.mirrors)
    }
    /// Get a random mirror for load balancing, preferring lower rank
    pub async fn get_random_mirror(&mut self) -> Result<&mut Mirror, NetErr> {
        use rand::Rng;
        let mirrors = self.all_mirrors_mut().await?;
        if mirrors.is_empty() {
            return Err(NetErr::EmptyMirrors);
        }

        // If only one mirror, return it
        if mirrors.len() == 1 {
            return Ok(&mut mirrors[0]);
        }

        // Calculate weights inversely proportional to rank
        // Lower rank = higher weight
        let weights: Vec<f64> = mirrors
            .iter()
            .map(|m| 1.0f64 / m.rank as f64) // Rank 1 = weight 1.0, rank 2 = 0.5, rank 5 = 0.2
            .collect();

        // Simple weighted random selection
        let mut rng = rand::rng();
        let total_weight: f64 = weights.iter().sum();
        let mut random_weight = rng.random::<f64>() * total_weight;

        for (i, &weight) in weights.iter().enumerate() {
            random_weight -= weight;
            if random_weight <= 0.0 {
                return Ok(&mut mirrors[i]);
            }
        }

        // Fallback to first mirror (should not happen with correct weights)
        Ok(&mut mirrors[0])
    }
    /// Sort mirrors by rank and return mutable reference to the sorted mirror list
    pub async fn sort_by_rank(&mut self) -> Result<&mut Vec<Mirror>, NetErr> {
        let mirrors = self.all_mirrors_mut().await?;
        mirrors.sort_by_key(|m| m.rank);
        Ok(&mut self.mirrors)
    }
    /// Save the current mirrors to disk (overwriting existing cache)
    /// If no mirrors are loaded, we return EmptyMirrors error
    pub async fn save_index_to_disk(&mut self) -> Result<(), NetErr> {
        // Ensure we have mirrors loaded
        if self.mirrors.is_empty() {
            tracing::debug!(target: TARGET, "No mirrors loaded, cannot save index to disk");
            Err(NetErr::EmptyMirrors)?;
        }

        // Create a fresh index with current mirrors and timestamp
        let index = MirrorsIndex::new(self.mirrors.clone());

        // Save to disk
        index.save(&self.cache_path).await.map_err(|cfg_err| {
            tracing::error!(target: TARGET, "Failed to save mirrors index to disk: {}", cfg_err);
            NetErr::Other(cfg_err.into())
        })?;

        // Update our cached index
        self.mirrors_index = Some(index);

        tracing::debug!(target: TARGET, "Successfully saved mirrors index to {}", self.cache_path.display());
        Ok(())
    }

    /// Benchmark all loaded mirrors using bounded partial downloads.
    pub async fn benchmark_mirrors(
        &mut self,
        semver_version: &Version,
        zig_tarball: &str,
        sample_size: u64,
        concurrency: usize,
    ) -> Result<Vec<MirrorBenchmarkResult>, NetErr> {
        let mirrors = self.all_mirrors_mut().await?.to_vec();
        if mirrors.is_empty() {
            return Err(NetErr::EmptyMirrors);
        }

        let sample_size = sample_size.max(1);
        let concurrency = concurrency.max(1);
        let client = self.client.clone();
        let semver_version = semver_version.clone();
        let zig_tarball = zig_tarball.to_string();

        let mut results = stream::iter(mirrors.into_iter().map(|mirror| {
            let client = client.clone();
            let semver_version = semver_version.clone();
            let zig_tarball = zig_tarball.clone();
            async move {
                benchmark_single_mirror(client, mirror, semver_version, zig_tarball, sample_size)
                    .await
            }
        }))
        .buffer_unordered(concurrency)
        .collect::<Vec<_>>()
        .await;

        results.sort_by(|a, b| {
            benchmark_result_sort_key(a, b)
                .then_with(|| a.base_url.as_str().cmp(b.base_url.as_str()))
        });

        Ok(results)
    }

    /// Apply benchmark results to in-memory mirrors and persist the updated mirror cache.
    pub async fn apply_benchmark_results(
        &mut self,
        results: &[MirrorBenchmarkResult],
        policy: RankApplyPolicy,
    ) -> Result<(), NetErr> {
        self.all_mirrors_mut().await?;

        let ordered = ordered_benchmark_results(results, policy);
        let rank_by_url: HashMap<String, u8> = ordered
            .iter()
            .enumerate()
            .map(|(idx, result)| (result.base_url.to_string(), rank_for_index(idx)))
            .collect();
        let layout_by_url: HashMap<String, Layout> = results
            .iter()
            .filter_map(|result| {
                result
                    .measured_layout
                    .map(|layout| (result.base_url.to_string(), layout))
            })
            .collect();

        for mirror in &mut self.mirrors {
            if let Some(rank) = rank_by_url.get(mirror.base_url.as_str()) {
                mirror.rank = *rank;
            }
            if let Some(layout) = layout_by_url.get(mirror.base_url.as_str()) {
                mirror.layout = *layout;
            }
        }

        self.mirrors.sort_by(|a, b| {
            a.rank
                .cmp(&b.rank)
                .then_with(|| a.base_url.cmp(&b.base_url))
        });
        self.save_index_to_disk().await
    }
}

async fn benchmark_single_mirror(
    client: Client,
    mirror: Mirror,
    semver_version: Version,
    zig_tarball: String,
    sample_size: u64,
) -> MirrorBenchmarkResult {
    let old_rank = mirror.rank;
    let old_layout = mirror.layout;

    match probe_mirror_layout(&client, &mirror, &semver_version, &zig_tarball, sample_size).await {
        Ok(measurement) => benchmark_success(mirror.base_url, old_rank, old_layout, measurement),
        Err(BenchmarkProbeError::Http(status)) if status.as_u16() == 404 => {
            let mut alternate = mirror.clone();
            alternate.layout = !alternate.layout;
            match probe_mirror_layout(
                &client,
                &alternate,
                &semver_version,
                &zig_tarball,
                sample_size,
            )
            .await
            {
                Ok(measurement) => {
                    benchmark_success(mirror.base_url, old_rank, old_layout, measurement)
                }
                Err(err) => benchmark_failure(mirror.base_url, old_rank, old_layout, err),
            }
        }
        Err(err) => benchmark_failure(mirror.base_url, old_rank, old_layout, err),
    }
}

struct BenchmarkMeasurement {
    layout: Layout,
    bytes_read: u64,
    elapsed: Duration,
}

async fn probe_mirror_layout(
    client: &Client,
    mirror: &Mirror,
    semver_version: &Version,
    zig_tarball: &str,
    sample_size: u64,
) -> Result<BenchmarkMeasurement, BenchmarkProbeError> {
    let url = mirror.get_download_url(semver_version, zig_tarball);
    let range_end = sample_size.saturating_sub(1);
    let start = Instant::now();
    let response = client
        .get(url)
        .header(reqwest::header::RANGE, format!("bytes=0-{range_end}"))
        .timeout(Duration::from_secs(15))
        .send()
        .await
        .map_err(BenchmarkProbeError::Network)?;

    match response.status() {
        StatusCode::OK | StatusCode::PARTIAL_CONTENT => {}
        status => return Err(BenchmarkProbeError::Http(status)),
    }

    let mut stream = response.bytes_stream();
    let mut bytes_read = 0u64;

    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(BenchmarkProbeError::Network)?;
        let remaining = sample_size.saturating_sub(bytes_read);
        if remaining == 0 {
            break;
        }
        bytes_read += (chunk.len() as u64).min(remaining);
        if bytes_read >= sample_size {
            break;
        }
    }

    if bytes_read == 0 {
        return Err(BenchmarkProbeError::EmptyBody);
    }

    Ok(BenchmarkMeasurement {
        layout: mirror.layout,
        bytes_read,
        elapsed: start.elapsed(),
    })
}

fn benchmark_success(
    base_url: Url,
    old_rank: u8,
    old_layout: Layout,
    measurement: BenchmarkMeasurement,
) -> MirrorBenchmarkResult {
    let elapsed_secs = measurement.elapsed.as_secs_f64().max(0.001);
    MirrorBenchmarkResult {
        base_url,
        old_rank,
        old_layout,
        measured_layout: Some(measurement.layout),
        bytes_read: Some(measurement.bytes_read),
        elapsed_ms: Some(measurement.elapsed.as_millis()),
        bytes_per_second: Some(measurement.bytes_read as f64 / elapsed_secs),
        error: None,
    }
}

fn benchmark_failure(
    base_url: Url,
    old_rank: u8,
    old_layout: Layout,
    err: BenchmarkProbeError,
) -> MirrorBenchmarkResult {
    MirrorBenchmarkResult {
        base_url,
        old_rank,
        old_layout,
        measured_layout: None,
        bytes_read: None,
        elapsed_ms: None,
        bytes_per_second: None,
        error: Some(err.to_string()),
    }
}

fn benchmark_result_sort_key(a: &MirrorBenchmarkResult, b: &MirrorBenchmarkResult) -> Ordering {
    match (a.bytes_per_second, b.bytes_per_second) {
        (Some(a_bps), Some(b_bps)) => b_bps.partial_cmp(&a_bps).unwrap_or(Ordering::Equal),
        (Some(_), None) => Ordering::Less,
        (None, Some(_)) => Ordering::Greater,
        (None, None) => a.old_rank.cmp(&b.old_rank),
    }
}

fn ordered_benchmark_results(
    results: &[MirrorBenchmarkResult],
    policy: RankApplyPolicy,
) -> Vec<&MirrorBenchmarkResult> {
    match policy {
        RankApplyPolicy::Overwrite => {
            let mut ordered = results.iter().collect::<Vec<_>>();
            ordered.sort_by(|a, b| {
                benchmark_result_sort_key(a, b)
                    .then_with(|| a.base_url.as_str().cmp(b.base_url.as_str()))
            });
            ordered
        }
        RankApplyPolicy::Blend => {
            let mut successes = results
                .iter()
                .filter(|result| result.is_success())
                .collect::<Vec<_>>();
            successes.sort_by(|a, b| {
                benchmark_result_sort_key(a, b)
                    .then_with(|| a.base_url.as_str().cmp(b.base_url.as_str()))
            });

            let speed_rank_by_url: HashMap<String, usize> = successes
                .iter()
                .enumerate()
                .map(|(idx, result)| (result.base_url.to_string(), idx + 1))
                .collect();

            let mut failures = results
                .iter()
                .filter(|result| !result.is_success())
                .collect::<Vec<_>>();
            failures.sort_by(|a, b| {
                a.old_rank
                    .cmp(&b.old_rank)
                    .then_with(|| a.base_url.as_str().cmp(b.base_url.as_str()))
            });

            successes.sort_by(|a, b| {
                let a_speed_rank = speed_rank_by_url
                    .get(a.base_url.as_str())
                    .copied()
                    .unwrap_or(usize::MAX);
                let b_speed_rank = speed_rank_by_url
                    .get(b.base_url.as_str())
                    .copied()
                    .unwrap_or(usize::MAX);
                let a_score = a.old_rank as f64 + a_speed_rank as f64;
                let b_score = b.old_rank as f64 + b_speed_rank as f64;
                a_score
                    .partial_cmp(&b_score)
                    .unwrap_or(Ordering::Equal)
                    .then_with(|| a.base_url.as_str().cmp(b.base_url.as_str()))
            });

            successes.extend(failures);
            successes
        }
    }
}

fn rank_for_index(idx: usize) -> u8 {
    u8::try_from(idx + 1).unwrap_or(u8::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use tempfile::tempdir;
    use wiremock::{
        Mock, MockServer, ResponseTemplate,
        matchers::{header, method, path},
    };

    fn test_mirror(url: &str, rank: u8) -> Mirror {
        Mirror {
            base_url: Url::parse(url).unwrap(),
            layout: Layout::Flat,
            rank,
        }
    }

    fn benchmark_result(
        url: &str,
        old_rank: u8,
        bytes_per_second: Option<f64>,
    ) -> MirrorBenchmarkResult {
        MirrorBenchmarkResult {
            base_url: Url::parse(url).unwrap(),
            old_rank,
            old_layout: Layout::Flat,
            measured_layout: bytes_per_second.map(|_| Layout::Flat),
            bytes_read: bytes_per_second.map(|_| 1024),
            elapsed_ms: bytes_per_second.map(|_| 10),
            bytes_per_second,
            error: if bytes_per_second.is_some() {
                None
            } else {
                Some("failed".to_string())
            },
        }
    }

    #[tokio::test]
    async fn overwrite_policy_sets_speed_order_and_places_failures_last() {
        let dir = tempdir().unwrap();
        let cache_path = dir.path().join("mirrors.toml");
        let mut manager = MirrorManager::new(&cache_path).unwrap();
        manager.mirrors = vec![
            test_mirror("https://slow.example", 1),
            test_mirror("https://fast.example", 5),
            test_mirror("https://failed.example", 2),
        ];
        manager.mirrors_index = Some(MirrorsIndex {
            mirrors: manager.mirrors.clone(),
            last_synced: Utc::now(),
        });

        let results = vec![
            benchmark_result("https://slow.example/", 1, Some(10.0)),
            benchmark_result("https://fast.example/", 5, Some(100.0)),
            benchmark_result("https://failed.example/", 2, None),
        ];

        manager
            .apply_benchmark_results(&results, RankApplyPolicy::Overwrite)
            .await
            .unwrap();

        let ranks = manager
            .mirrors
            .iter()
            .map(|m| (m.base_url.as_str().to_string(), m.rank))
            .collect::<HashMap<_, _>>();

        assert_eq!(ranks["https://fast.example/"], 1);
        assert_eq!(ranks["https://slow.example/"], 2);
        assert_eq!(ranks["https://failed.example/"], 3);
        assert!(cache_path.is_file());
    }

    #[test]
    fn blend_policy_keeps_existing_rank_signal() {
        let results = vec![
            benchmark_result("https://current-best.example/", 1, Some(50.0)),
            benchmark_result("https://fast-but-low-priority.example/", 10, Some(100.0)),
            benchmark_result("https://middle.example/", 5, Some(75.0)),
        ];

        let ordered = ordered_benchmark_results(&results, RankApplyPolicy::Blend);
        let urls = ordered
            .iter()
            .map(|result| result.base_url.as_str())
            .collect::<Vec<_>>();

        assert_eq!(
            urls,
            vec![
                "https://current-best.example/",
                "https://middle.example/",
                "https://fast-but-low-priority.example/"
            ]
        );
    }

    #[tokio::test]
    async fn benchmark_uses_range_requests_and_records_throughput() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/zig.tar.xz"))
            .and(header("range", "bytes=0-9"))
            .respond_with(ResponseTemplate::new(206).set_body_bytes(vec![7; 10]))
            .mount(&server)
            .await;

        let cache_path = tempdir().unwrap().path().join("mirrors.toml");
        let mut manager = MirrorManager::new(cache_path).unwrap();
        manager.mirrors = vec![test_mirror(&server.uri(), 1)];
        manager.mirrors_index = Some(MirrorsIndex {
            mirrors: manager.mirrors.clone(),
            last_synced: Utc::now(),
        });

        let results = manager
            .benchmark_mirrors(&Version::new(0, 15, 1), "zig.tar.xz", 10, 1)
            .await
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].is_success());
        assert_eq!(results[0].bytes_read, Some(10));
        assert_eq!(results[0].measured_layout, Some(Layout::Flat));
    }

    #[tokio::test]
    async fn benchmark_tries_alternate_layout_after_404() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/0.15.1/zig.tar.xz"))
            .respond_with(ResponseTemplate::new(404))
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/zig.tar.xz"))
            .respond_with(ResponseTemplate::new(206).set_body_bytes(vec![7; 10]))
            .mount(&server)
            .await;

        let cache_path = tempdir().unwrap().path().join("mirrors.toml");
        let mut manager = MirrorManager::new(cache_path).unwrap();
        let mut mirror = test_mirror(&server.uri(), 1);
        mirror.layout = Layout::Versioned;
        manager.mirrors = vec![mirror];
        manager.mirrors_index = Some(MirrorsIndex {
            mirrors: manager.mirrors.clone(),
            last_synced: Utc::now(),
        });

        let results = manager
            .benchmark_mirrors(&Version::new(0, 15, 1), "zig.tar.xz", 10, 1)
            .await
            .unwrap();

        assert!(results[0].is_success());
        assert_eq!(results[0].old_layout, Layout::Versioned);
        assert_eq!(results[0].measured_layout, Some(Layout::Flat));
        assert_eq!(manager.mirrors[0].layout, Layout::Versioned);
    }
}