self_update 1.3.0

Self updates for standalone executables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
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
/*!
Updates from a static JSON release manifest served over HTTP(S).

Use this backend to update from any plain file server (S3 static hosting, a CDN, GitHub Pages, a
bare nginx, ...) that serves a single JSON document describing your releases, plus the release
artifacts alongside it. It is the service-agnostic fallback for hosts the forge backends
(`github`, `gitlab`, `gitea`, `s3`) don't cover, and needs no server-side API, just a static
`manifest.json` you regenerate at release time.

The backend fetches the manifest, parses it into [`Release`]s, and drives the crate's usual
compare -> select-asset -> download -> verify -> extract -> install flow (via the same pipeline
the [`custom`](crate::backends::custom) backend uses).

```no_run
# fn run() -> Result<(), Box<dyn std::error::Error>> {
use self_update::cargo_crate_version;

let status = self_update::backends::manifest::Update::configure()
    .manifest_url("https://example.net/releases/manifest.json")
    .bin_name("app")
    .current_version(cargo_crate_version!())
    .build()?
    .update()?;
println!("update status: `{}`", status.version());
# Ok(())
# }
```

# Manifest schema

The manifest is a JSON object with a `schema` version and a list of `releases`. Unknown fields are
ignored (forward compatibility), and `date`, `notes_url`, and per-asset `digest` are optional.

```json
{
  "schema": 1,
  "releases": [
    {
      "version": "1.2.3",
      "date": "2026-07-16T00:00:00Z",
      "notes_url": "https://example.net/releases/1.2.3",
      "assets": [
        {
          "name": "app-1.2.3-x86_64-unknown-linux-gnu.tar.gz",
          "url": "app-1.2.3-x86_64-unknown-linux-gnu.tar.gz",
          "digest": "sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
        }
      ]
    }
  ]
}
```

- `schema` is the manifest format version. This crate supports schema `1`; a manifest declaring
  any other `schema` fails with [`Error::InvalidResponse`](crate::errors::Error::InvalidResponse)
  naming the version it found, so an old client refuses a manifest it can't understand rather than
  silently mis-parsing it.
- Each release's `version` must be a bare semver string (no leading `v`). A release whose version
  is not valid semver is **skipped** (logged at `debug`), not an error, so a manifest mixing a
  rolling `nightly` entry with real releases stays usable, matching how the forge backends skip
  non-semver tags.
- An asset `url` may be **relative**: it is resolved against the manifest URL's directory (the URL
  up to and including its last `/`). An absolute `http(s)://` URL is used verbatim. `..` path
  segments are not specially handled.
- `digest` (in `algorithm:hex` form, e.g. `sha256:...`) is mapped onto the asset and, with the
  `checksums` feature, verified against the downloaded artifact before installing (see
  `verify_release_digest` on the builder).

# Async

With the `async` feature, [`build_async`](UpdateBuilder::build_async) returns an [`AsyncUpdate`]
whose `*_async` verbs run the same flow asynchronously.
*/

use std::sync::Arc;

use crate::backends::common::{CommonBuilderConfig, CommonConfig, RequestConfig};
#[cfg(feature = "async")]
use crate::backends::send_async;
use crate::backends::{MAX_LISTING_BODY_BYTES, send};
use crate::errors::*;
use crate::http_client;
use crate::update::{Release, ReleaseAsset, ReleaseSource, ReleaseUpdate, Releases};

/// The manifest `schema` version this crate understands. A manifest declaring any other
/// version is rejected (see [`parse_manifest`]).
const MANIFEST_SCHEMA_VERSION: u64 = 1;

// --- Manifest schema (serde) -------------------------------------------------------------------
//
// No `deny_unknown_fields`: unknown fields are ignored so a newer manifest producer can add fields
// without breaking older clients (the `schema` bump is the explicit break signal instead).

#[derive(serde::Deserialize)]
struct Manifest {
    schema: u64,
    #[serde(default)]
    releases: Vec<ManifestRelease>,
}

#[derive(serde::Deserialize)]
struct ManifestRelease {
    version: String,
    #[serde(default)]
    date: Option<String>,
    #[serde(default)]
    notes_url: Option<String>,
    #[serde(default)]
    assets: Vec<ManifestAsset>,
}

#[derive(serde::Deserialize)]
struct ManifestAsset {
    name: String,
    url: String,
    #[serde(default)]
    digest: Option<String>,
}

/// Resolve an asset URL against the manifest URL.
///
/// An absolute `http(s)://` URL passes through verbatim. A relative URL is joined onto the
/// manifest URL's "directory": everything up to and including the manifest URL's last `/`. This is
/// a deliberately simple string join (no `url` crate, which is an optional s3-only dependency), so
/// `..` segments are NOT collapsed and a manifest URL carrying a query string with a `/` in it
/// would split at that `/`. Point `manifest_url` at a plain path ending in the manifest file name
/// and keep asset URLs as sibling file names (or absolute URLs) to stay in the well-defined case.
fn resolve_asset_url(manifest_url: &str, asset_url: &str) -> String {
    if asset_url.starts_with("http://") || asset_url.starts_with("https://") {
        return asset_url.to_string();
    }
    match manifest_url.rfind('/') {
        // `..=idx` keeps the trailing '/', so "https://h/a/manifest.json" + "app.tgz"
        // -> "https://h/a/app.tgz".
        Some(idx) => format!("{}{}", &manifest_url[..=idx], asset_url),
        // No '/' at all (degenerate): fall back to the asset URL as given.
        None => asset_url.to_string(),
    }
}

/// Parse a JSON release manifest body into [`Release`]s. Transport-free, so it is shared by the
/// sync and async fetch paths and exercised directly by the unit tests.
///
/// `manifest_url` is used only to resolve relative asset URLs (see [`resolve_asset_url`]).
///
/// * Errors
///     * [`Error::InvalidResponse`](crate::errors::Error::InvalidResponse) if the body is not the
///       expected JSON (including a missing required field), or if the manifest declares a
///       `schema` other than the version this crate supports.
///
/// A release whose `version` is not valid semver is skipped (logged at `debug`), not an error.
pub fn parse_manifest(body: &str, manifest_url: &str) -> Result<Vec<Release>> {
    let manifest: Manifest = serde_json::from_str(body).map_err(Error::invalid_response)?;
    if manifest.schema != MANIFEST_SCHEMA_VERSION {
        return Err(Error::invalid_response(format!(
            "unsupported manifest schema version {}; this crate supports schema {}",
            manifest.schema, MANIFEST_SCHEMA_VERSION
        )));
    }

    let mut releases = Vec::new();
    for mr in manifest.releases {
        let mut builder = Release::builder();
        builder.version(&mr.version);
        if let Some(date) = &mr.date {
            builder.date(date);
        }
        if let Some(notes_url) = &mr.notes_url {
            builder.release_notes_url(notes_url);
        }
        for asset in &mr.assets {
            let url = resolve_asset_url(manifest_url, &asset.url);
            let mut release_asset = ReleaseAsset::new(&*asset.name, url);
            if let Some(digest) = &asset.digest {
                release_asset = release_asset.with_digest(&**digest);
            }
            builder.asset(release_asset);
        }
        match builder.build() {
            Ok(release) => releases.push(release),
            // A non-semver version is not a release the updater can compare; skip it rather than
            // failing the whole manifest, so a manifest mixing rolling entries with real releases
            // stays updatable. Matches the forge backends' skip-non-semver-tags precedent (#190).
            Err(e @ Error::SemVer(_)) => {
                log::debug!("self_update: skipping manifest release: {e}");
            }
            Err(e) => return Err(e),
        }
    }
    Ok(releases)
}

/// Base headers sent with the manifest fetch (a user `request_header(..)` merges on top of these).
fn base_headers() -> http_client::HeaderMap {
    let mut headers = http_client::HeaderMap::new();
    headers.insert(
        http_client::header::ACCEPT,
        http_client::header::HeaderValue::from_static("application/json"),
    );
    headers
}

/// Read a manifest response body into a `String`, bounded by [`MAX_LISTING_BODY_BYTES`] so a
/// misconfigured or malicious endpoint cannot force unbounded memory use.
fn read_body(resp: Box<dyn http_client::HttpResponse>) -> Result<String> {
    use std::io::Read as _;
    // Read one byte past the cap to distinguish "exactly at the cap" (fine) from "over it" (error).
    let mut limited = resp.body().take((MAX_LISTING_BODY_BYTES + 1) as u64);
    let mut body = Vec::new();
    limited.read_to_end(&mut body)?;
    if body.len() > MAX_LISTING_BODY_BYTES {
        return Err(Error::invalid_response(format!(
            "manifest body exceeded the {MAX_LISTING_BODY_BYTES}-byte cap; a release manifest is \
             much smaller"
        )));
    }
    String::from_utf8(body)
        .map_err(|e| Error::invalid_response(format!("manifest was not valid UTF-8: {e}")))
}

/// A [`ReleaseSource`] that fetches a JSON release manifest from `url`.
///
/// This is the source the [`Update`] facade wraps, but it can also be used directly with the
/// [`custom`](crate::backends::custom) backend (`custom::Update::configure().source(..)`) when you
/// want the manifest source with the custom builder's surface. The transport setters
/// ([`timeout`](Self::timeout), [`request_header`](Self::request_header),
/// [`retries`](Self::retries), ...) configure the manifest fetch.
#[derive(Debug, Clone)]
pub struct ManifestSource {
    url: String,
    request: RequestConfig,
}

impl ManifestSource {
    /// Construct a source that fetches the manifest at `url`.
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            request: RequestConfig::default(),
        }
    }

    request_config_setters!(request);

    /// Fetch and parse the manifest (shared by the sync `ReleaseSource` and, indirectly, the async
    /// impl builds its own request the same way).
    fn resolved_request(&self) -> Result<RequestConfig> {
        // Materialize a client from any custom root CA certs (no-op if none / a client was
        // injected) and surface any deferred header/cert error, mirroring the builder's `build()`.
        let mut request = self.request.clone();
        request.build_client();
        request.check()?;
        Ok(request)
    }
}

impl ReleaseSource for ManifestSource {
    fn get_releases(&self) -> Result<Vec<Release>> {
        let request = self.resolved_request()?;
        let resp = send(&self.url, base_headers(), &request)?;
        let body = read_body(resp)?;
        parse_manifest(&body, &self.url)
    }
}

#[cfg(feature = "async")]
impl crate::update::AsyncReleaseSource for ManifestSource {
    async fn get_releases(&self) -> Result<Vec<Release>> {
        let request = self.resolved_request()?;
        let resp = send_async(&self.url, base_headers(), &request).await?;
        let body = resp.text().await?;
        parse_manifest(&body, &self.url)
    }
}

/// [`manifest::Update`](Update) builder.
///
/// Mirrors the [`custom`](crate::backends::custom) backend's builder (it wraps the same update
/// pipeline over a [`ManifestSource`]), adding the [`manifest_url`](Self::manifest_url) setter. The
/// shared transport setters ([`timeout`](Self::timeout), [`request_header`](Self::request_header),
/// [`retries`](Self::retries), an injected client, ...) apply to **both** the manifest fetch and the
/// crate-controlled asset download.
#[must_use]
#[derive(Clone, Debug, Default)]
pub struct UpdateBuilder {
    manifest_url: Option<String>,
    common: CommonBuilderConfig,
}

impl UpdateBuilder {
    /// Initialize a new builder.
    pub fn new() -> Self {
        Default::default()
    }

    /// Set the URL of the JSON release manifest. Required.
    pub fn manifest_url(&mut self, url: impl Into<String>) -> &mut Self {
        self.manifest_url = Some(url.into());
        self
    }

    impl_common_builder_setters!(no_auth_token);

    fn build_update(&self) -> Result<Update> {
        let url = self.manifest_url.clone().ok_or(Error::MissingField {
            field: "manifest_url",
        })?;
        let common = self.common.build()?;
        // Thread the same resolved transport config into the manifest fetch as the download uses,
        // so `.timeout()` / `.request_header()` / `.retries()` / an injected client apply to both.
        let source = ManifestSource {
            url,
            request: common.request.clone(),
        };
        Ok(Update {
            source: Arc::new(source),
            common,
        })
    }

    /// Confirm config and create a ready-to-use [`Update`].
    ///
    /// Returns the concrete [`Update`], which is `Send` and exposes the update verbs as inherent
    /// methods.
    ///
    /// * Errors:
    ///     * `MissingField` - no `manifest_url` was set, or an invalid `Update` configuration
    pub fn build(&self) -> Result<Update> {
        self.build_update()
    }

    /// Confirm config and create a ready-to-use [`AsyncUpdate`] for the async API
    /// (`update_async`).
    ///
    /// Unlike [`build`](Self::build) this returns the distinct [`AsyncUpdate`] newtype, which
    /// exposes only the inherent `*_async` verbs, so a stray blocking `.update()` on an async-built
    /// updater is a compile error rather than a silent block of the executor.
    #[cfg(feature = "async")]
    pub fn build_async(&self) -> Result<AsyncUpdate> {
        Ok(AsyncUpdate(self.build_update()?))
    }
}

/// Updates to a specified or latest release described by a JSON manifest.
#[derive(Debug)]
#[non_exhaustive]
pub struct Update {
    source: Arc<ManifestSource>,
    common: CommonConfig,
}

impl Update {
    /// Initialize a new `Update` builder.
    pub fn configure() -> UpdateBuilder {
        UpdateBuilder::new()
    }
}

impl crate::update::sealed::Sealed for Update {}

impl_update_config_accessors!(Update);

impl ReleaseUpdate for Update {
    fn get_latest_release(&self) -> Result<Releases> {
        let current_version = crate::update::UpdateConfig::current_version(self).to_owned();
        let release = self.source.get_latest_release()?;
        Ok(Releases::new(vec![release], current_version))
    }

    fn get_newer_releases(&self) -> Result<Releases> {
        let current_version = crate::update::UpdateConfig::current_version(self).to_owned();
        let releases = self
            .source
            .get_releases()?
            .into_iter()
            .filter(|r| {
                crate::version::bump_is_greater(&current_version, r.version()).unwrap_or(false)
            })
            .collect();
        Ok(Releases::new(releases, current_version))
    }

    fn get_release_version(&self, ver: &str) -> Result<Release> {
        self.source.get_release_version(ver)
    }
}

impl_sync_update_verbs!(Update);

/// Async-only updater returned by [`UpdateBuilder::build_async`].
///
/// A newtype over the blocking [`Update`] that exposes **only** the inherent `*_async` verbs, so a
/// blocking call on an async-built updater (e.g. `build_async()?.update()`) is a compile error.
#[cfg(feature = "async")]
#[derive(Debug)]
pub struct AsyncUpdate(Update);

#[cfg(feature = "async")]
impl_async_update_verbs!(AsyncUpdate);

#[cfg(feature = "async")]
impl crate::update::AsyncReleaseUpdate for Update {
    async fn get_latest_release_async(&self) -> Result<Releases> {
        let current_version = crate::update::UpdateConfig::current_version(self).to_owned();
        let release = crate::update::AsyncReleaseSource::get_latest_release(&*self.source).await?;
        Ok(Releases::new(vec![release], current_version))
    }

    async fn get_newer_releases_async(&self) -> Result<Releases> {
        let current_version = crate::update::UpdateConfig::current_version(self).to_owned();
        let releases = crate::update::AsyncReleaseSource::get_releases(&*self.source)
            .await?
            .into_iter()
            .filter(|r| {
                crate::version::bump_is_greater(&current_version, r.version()).unwrap_or(false)
            })
            .collect();
        Ok(Releases::new(releases, current_version))
    }

    async fn get_release_version_async(&self, ver: &str) -> Result<Release> {
        crate::update::AsyncReleaseSource::get_release_version(&*self.source, ver).await
    }
}

#[cfg(test)]
mod tests {
    use super::{ManifestSource, Update, parse_manifest, resolve_asset_url};
    use crate::update::ReleaseSource;

    const MANIFEST_URL: &str = "https://example.net/releases/manifest.json";

    // --- parse_manifest unit tests (transport-free) --------------------------------------------

    #[test]
    fn parse_manifest_schema_too_new_errors_naming_the_version() {
        // A manifest declaring a schema newer than supported must be refused (not silently
        // mis-parsed), and the error must name the version found.
        let body = r#"{ "schema": 2, "releases": [] }"#;
        let err = parse_manifest(body, MANIFEST_URL)
            .expect_err("schema 2 must be rejected by a schema-1 client");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "schema-too-new must be InvalidResponse, got {err:?}"
        );
        let shown = err.to_string();
        assert!(
            shown.contains("unsupported manifest schema version 2"),
            "the error must name the found schema version 2: {shown}"
        );
    }

    #[test]
    fn parse_manifest_missing_version_field_errors() {
        // A release object without the required `version` field is a parse error.
        let body = r#"{ "schema": 1, "releases": [ { "assets": [] } ] }"#;
        let err = parse_manifest(body, MANIFEST_URL).expect_err("missing `version` must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "a missing required field must be InvalidResponse, got {err:?}"
        );
    }

    #[test]
    fn parse_manifest_missing_asset_name_errors() {
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.0.0", "assets": [ { "url": "app.tar.gz" } ] } ] }"#;
        let err = parse_manifest(body, MANIFEST_URL).expect_err("missing asset `name` must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "a missing asset name must be InvalidResponse, got {err:?}"
        );
    }

    #[test]
    fn parse_manifest_missing_asset_url_errors() {
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.0.0", "assets": [ { "name": "app.tar.gz" } ] } ] }"#;
        let err = parse_manifest(body, MANIFEST_URL).expect_err("missing asset `url` must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "a missing asset url must be InvalidResponse, got {err:?}"
        );
    }

    #[test]
    fn parse_manifest_relative_asset_url_resolved_against_manifest_dir() {
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.2.3", "assets": [
                { "name": "app-1.2.3.tar.gz", "url": "app-1.2.3.tar.gz" } ] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(releases.len(), 1);
        let asset = &releases[0].assets()[0];
        assert_eq!(
            asset.download_url(),
            "https://example.net/releases/app-1.2.3.tar.gz",
            "a relative asset url must resolve against the manifest URL's directory"
        );
    }

    #[test]
    fn parse_manifest_absolute_asset_url_passes_through() {
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.2.3", "assets": [
                { "name": "app.tar.gz", "url": "https://cdn.example.com/app.tar.gz" } ] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(
            releases[0].assets()[0].download_url(),
            "https://cdn.example.com/app.tar.gz",
            "an absolute http(s) asset url must pass through verbatim"
        );
    }

    #[test]
    fn resolve_asset_url_handles_relative_absolute_and_no_slash() {
        assert_eq!(
            resolve_asset_url("https://h/a/b/manifest.json", "app.tar.gz"),
            "https://h/a/b/app.tar.gz"
        );
        assert_eq!(
            resolve_asset_url("https://h/a/manifest.json", "http://other/app.tar.gz"),
            "http://other/app.tar.gz"
        );
        assert_eq!(
            resolve_asset_url("https://h/a/manifest.json", "https://other/app.tar.gz"),
            "https://other/app.tar.gz"
        );
        // Degenerate: a manifest "url" with no slash falls back to the asset url as given.
        assert_eq!(
            resolve_asset_url("manifest.json", "app.tar.gz"),
            "app.tar.gz"
        );
    }

    #[test]
    fn parse_manifest_digest_mapped_through_to_asset() {
        let digest = "sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
        let body = format!(
            r#"{{ "schema": 1, "releases": [
                {{ "version": "1.0.0", "assets": [
                    {{ "name": "app.tar.gz", "url": "app.tar.gz", "digest": "{digest}" }} ] }} ] }}"#
        );
        let releases = parse_manifest(&body, MANIFEST_URL).unwrap();
        assert_eq!(
            releases[0].assets()[0].digest(),
            Some(digest),
            "an asset digest must be mapped onto the ReleaseAsset"
        );
    }

    #[test]
    fn parse_manifest_no_digest_leaves_asset_digest_none() {
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.0.0", "assets": [ { "name": "app.tar.gz", "url": "app.tar.gz" } ] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(releases[0].assets()[0].digest(), None);
    }

    #[test]
    fn parse_manifest_date_and_notes_url_mapped() {
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.0.0", "date": "2026-07-16T00:00:00Z",
              "notes_url": "https://example.net/releases/1.0.0", "assets": [] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(releases[0].date(), "2026-07-16T00:00:00Z");
        assert_eq!(
            releases[0].release_notes_url(),
            Some("https://example.net/releases/1.0.0")
        );
    }

    #[test]
    fn parse_manifest_non_semver_version_skipped_valid_siblings_survive() {
        // A non-semver `version` (a rolling `nightly` entry) is skipped, not an error; the valid
        // sibling release survives.
        let body = r#"{ "schema": 1, "releases": [
            { "version": "nightly", "assets": [] },
            { "version": "1.2.3", "assets": [] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(releases.len(), 1, "the non-semver release must be skipped");
        assert_eq!(releases[0].version(), "1.2.3");
    }

    #[test]
    fn parse_manifest_unknown_fields_ignored() {
        // Unknown fields at the top level, on a release, and on an asset must be ignored (forward
        // compat), not rejected.
        let body = r#"{
            "schema": 1,
            "generator": "some-tool",
            "releases": [
                { "version": "1.2.3", "channel": "stable", "assets": [
                    { "name": "app.tar.gz", "url": "app.tar.gz", "size": 12345 } ] } ]
        }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(releases.len(), 1);
        assert_eq!(releases[0].version(), "1.2.3");
        assert_eq!(releases[0].assets().len(), 1);
    }

    // --- resolve_asset_url edge cases (spec: deliberate string-join, no `..`/query handling) -----

    #[test]
    fn resolve_asset_url_trailing_slash_manifest_url_appends_directly() {
        // A manifest URL ending in `/` (a "directory" URL) resolves the asset right after it.
        assert_eq!(
            resolve_asset_url("https://h/a/b/", "app.tar.gz"),
            "https://h/a/b/app.tar.gz"
        );
    }

    #[test]
    fn resolve_asset_url_relative_asset_with_subpath_joins_verbatim() {
        // A relative asset url that itself contains `/` is appended verbatim onto the manifest dir.
        assert_eq!(
            resolve_asset_url("https://h/a/manifest.json", "sub/app.tar.gz"),
            "https://h/a/sub/app.tar.gz"
        );
    }

    #[test]
    fn resolve_asset_url_dotdot_segments_not_collapsed() {
        // Spec: `..` segments are NOT specially handled; they land in the resolved URL as-is.
        assert_eq!(
            resolve_asset_url("https://h/a/b/manifest.json", "../app.tar.gz"),
            "https://h/a/b/../app.tar.gz"
        );
    }

    #[test]
    fn resolve_asset_url_query_string_with_slash_splits_at_that_slash() {
        // Documented degenerate case: the join truncates at the LAST `/`, even one inside a query
        // string. A manifest URL whose query contains a `/` therefore resolves oddly -- this pins
        // that documented behavior so a "fix" that adds query awareness is a conscious change.
        assert_eq!(
            resolve_asset_url("https://h/a/manifest.json?path=/x", "app.tar.gz"),
            "https://h/a/manifest.json?path=/app.tar.gz"
        );
    }

    #[test]
    fn resolve_asset_url_query_string_without_slash_resolves_against_dir() {
        // A query string with no `/` in it does not move the split point: the last `/` is still the
        // one before the manifest file name.
        assert_eq!(
            resolve_asset_url("https://h/a/manifest.json?token=abc", "app.tar.gz"),
            "https://h/a/app.tar.gz"
        );
    }

    // --- parse_manifest schema / body edge cases -----------------------------------------------

    #[test]
    fn parse_manifest_missing_schema_field_errors() {
        // `schema` is a required field (no serde default): a manifest that omits it entirely is a
        // parse error, not silently treated as schema 0/1.
        let body = r#"{ "releases": [] }"#;
        let err = parse_manifest(body, MANIFEST_URL).expect_err("absent `schema` must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "an absent schema must be InvalidResponse, got {err:?}"
        );
    }

    #[test]
    fn parse_manifest_schema_wrong_type_errors() {
        // A `schema` of the wrong JSON type (string instead of integer) is a parse error.
        let body = r#"{ "schema": "1", "releases": [] }"#;
        let err = parse_manifest(body, MANIFEST_URL).expect_err("string `schema` must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "a wrong-typed schema must be InvalidResponse, got {err:?}"
        );
    }

    #[test]
    fn parse_manifest_garbage_body_errors() {
        // A body that is not JSON at all surfaces as InvalidResponse, never a panic or empty list.
        let err =
            parse_manifest("this is not json", MANIFEST_URL).expect_err("non-JSON body must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "a non-JSON body must be InvalidResponse, got {err:?}"
        );
    }

    #[test]
    fn parse_manifest_empty_releases_yields_empty_vec() {
        // An explicitly empty releases array parses cleanly to zero releases (the NoReleaseFound
        // surfaces later, from the update-selection helpers -- see the stub test below).
        let releases = parse_manifest(r#"{ "schema": 1, "releases": [] }"#, MANIFEST_URL).unwrap();
        assert!(releases.is_empty());
    }

    #[test]
    fn parse_manifest_release_with_empty_assets_is_kept_with_zero_assets() {
        // A valid-version release carrying zero assets is a real release (asset selection fails
        // later, but parsing keeps it); the entry must survive with an empty asset list.
        let body = r#"{ "schema": 1, "releases": [ { "version": "1.2.3", "assets": [] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(releases.len(), 1);
        assert_eq!(releases[0].version(), "1.2.3");
        assert!(releases[0].assets().is_empty());
    }

    #[test]
    fn parse_manifest_duplicate_versions_are_all_kept() {
        // The parser does not de-duplicate: two entries with the same version both survive, in
        // document order. (Selection later picks by semver; a tie takes the earliest-positioned.)
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.2.3", "assets": [ { "name": "a.tgz", "url": "a.tgz" } ] },
            { "version": "1.2.3", "assets": [ { "name": "b.tgz", "url": "b.tgz" } ] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(
            releases.len(),
            2,
            "duplicate versions must not be collapsed"
        );
        assert_eq!(releases[0].assets()[0].name(), "a.tgz");
        assert_eq!(releases[1].assets()[0].name(), "b.tgz");
    }

    #[test]
    fn parse_manifest_non_sha256_digest_prefix_is_mapped_verbatim() {
        // Any `digest` string is mapped verbatim through `ReleaseAsset::with_digest`: an
        // unsupported algorithm (e.g. `md5:`) then hard-errors at verify time under the
        // `checksums` feature rather than being silently dropped. Silently ignoring a digest the
        // manifest author supplied would skip verification the user believes is happening.
        let body = r#"{ "schema": 1, "releases": [
            { "version": "1.0.0", "assets": [
                { "name": "app.tar.gz", "url": "app.tar.gz", "digest": "md5:abc123" } ] } ] }"#;
        let releases = parse_manifest(body, MANIFEST_URL).unwrap();
        assert_eq!(
            releases[0].assets()[0].digest(),
            Some("md5:abc123"),
            "current impl maps a non-sha256 digest verbatim (diverges from the spec's \
             treat-as-absent contract)"
        );
    }

    #[test]
    fn parse_manifest_schema_zero_is_rejected() {
        // Only `schema: 1` is accepted: any other value (0 included) is refused with an error
        // naming the received version, per the spec's invariant checklist.
        let err = parse_manifest(r#"{ "schema": 0, "releases": [] }"#, MANIFEST_URL)
            .expect_err("schema 0 must be rejected; only schema 1 is supported");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "schema 0 must be InvalidResponse, got {err:?}"
        );
        assert!(
            err.to_string()
                .contains("unsupported manifest schema version 0"),
            "the error must name the found schema version 0: {err}"
        );
    }

    #[test]
    fn build_requires_a_manifest_url() {
        // Absent `manifest_url` must be the specific `MissingField { field: "manifest_url" }`, and
        // it must take precedence even when the common fields (bin_name/current_version) are set.
        let err = Update::configure()
            .bin_name("app")
            .current_version("1.0.0")
            .build()
            .expect_err("build must fail without a manifest_url");
        assert!(
            matches!(
                err,
                crate::errors::Error::MissingField {
                    field: "manifest_url"
                }
            ),
            "a missing manifest_url must be MissingField {{ field: \"manifest_url\" }}, got {err:?}"
        );
    }

    // --- Loopback stub end-to-end tests --------------------------------------------------------

    use std::io::{Read as _, Write as _};
    use std::net::TcpListener;

    /// Serve a sequence of raw HTTP responses over a loopback listener, one connection per entry.
    /// Each entry is `(content_type, body_bytes)`. Returns the base URL (`http://127.0.0.1:<port>`).
    fn stub(responses: Vec<(&'static str, Vec<u8>)>) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        let owned: Vec<(String, Vec<u8>)> = responses
            .into_iter()
            .map(|(ct, body)| (ct.to_string(), body))
            .collect();
        std::thread::spawn(move || {
            for (content_type, body) in owned {
                let (mut stream, _) = match listener.accept() {
                    Ok(c) => c,
                    Err(_) => return,
                };
                let mut buf = [0u8; 4096];
                let _ = stream.read(&mut buf);
                let header = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    content_type,
                    body.len()
                );
                let _ = stream.write_all(header.as_bytes());
                let _ = stream.write_all(&body);
                let _ = stream.flush();
            }
        });
        base
    }

    #[test]
    fn get_releases_over_the_stub_parses_the_manifest() {
        let manifest = r#"{ "schema": 1, "releases": [
            { "version": "2.0.0", "assets": [
                { "name": "app-2.0.0.tar.gz", "url": "app-2.0.0.tar.gz" } ] },
            { "version": "1.0.0", "assets": [] } ] }"#;
        let base = stub(vec![("application/json", manifest.as_bytes().to_vec())]);
        let source = ManifestSource::new(format!("{base}/releases/manifest.json"));
        let releases = source.get_releases().unwrap();
        let versions: Vec<&str> = releases.iter().map(|r| r.version()).collect();
        assert_eq!(versions, vec!["2.0.0", "1.0.0"]);
        // The relative asset url resolved against the manifest URL's directory (same stub host).
        assert_eq!(
            releases[0].assets()[0].download_url(),
            format!("{base}/releases/app-2.0.0.tar.gz")
        );
    }

    #[test]
    fn get_releases_over_the_stub_propagates_http_error() {
        // A non-2xx manifest response must surface as a structured error, before any parse.
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 4096];
                let _ = stream.read(&mut buf);
                let out = "HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
                let _ = stream.write_all(out.as_bytes());
                let _ = stream.flush();
            }
        });
        let source = ManifestSource::new(format!("{base}/manifest.json"));
        let err = source.get_releases().expect_err("503 must error");
        assert!(
            matches!(err, crate::errors::Error::HttpStatus { status: 503, .. }),
            "a 503 manifest response must surface as HttpStatus, got {err:?}"
        );
    }

    /// Serve one raw HTTP response with an explicit status line and empty body, over a loopback
    /// listener. Used to check that a non-2xx manifest fetch maps to the right structured error.
    fn stub_status(status_line: &'static str) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 4096];
                let _ = stream.read(&mut buf);
                let out = format!(
                    "HTTP/1.1 {status_line}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
                );
                let _ = stream.write_all(out.as_bytes());
                let _ = stream.flush();
            }
        });
        base
    }

    #[test]
    fn get_releases_over_the_stub_maps_404_to_not_found() {
        let base = stub_status("404 Not Found");
        let source = ManifestSource::new(format!("{base}/manifest.json"));
        let err = source.get_releases().expect_err("404 must error");
        assert!(
            matches!(err, crate::errors::Error::NotFound { .. }),
            "a 404 manifest response must surface as NotFound, got {err:?}"
        );
    }

    #[test]
    fn get_releases_over_the_stub_maps_401_to_unauthorized() {
        let base = stub_status("401 Unauthorized");
        let source = ManifestSource::new(format!("{base}/manifest.json"));
        let err = source.get_releases().expect_err("401 must error");
        assert!(
            matches!(err, crate::errors::Error::Unauthorized { status: 401, .. }),
            "a 401 manifest response must surface as Unauthorized, got {err:?}"
        );
    }

    #[test]
    fn get_releases_over_the_stub_maps_403_to_unauthorized() {
        let base = stub_status("403 Forbidden");
        let source = ManifestSource::new(format!("{base}/manifest.json"));
        let err = source.get_releases().expect_err("403 must error");
        assert!(
            matches!(err, crate::errors::Error::Unauthorized { status: 403, .. }),
            "a 403 manifest response must surface as Unauthorized, got {err:?}"
        );
    }

    #[test]
    fn get_releases_over_the_stub_empty_releases_yields_no_release_found() {
        // An empty (but valid) manifest parses fine, but the latest-release selection has nothing
        // to pick -> Error::NoReleaseFound. This is the update-path surface of an empty manifest.
        let base = stub(vec![(
            "application/json",
            br#"{ "schema": 1, "releases": [] }"#.to_vec(),
        )]);
        let source = ManifestSource::new(format!("{base}/manifest.json"));
        let err = source
            .get_latest_release()
            .expect_err("an empty manifest must have no latest release");
        assert!(
            matches!(err, crate::errors::Error::NoReleaseFound { .. }),
            "an empty releases list must yield NoReleaseFound, got {err:?}"
        );
    }

    #[test]
    fn get_releases_over_the_stub_non_utf8_body_errors() {
        // A manifest body that is not valid UTF-8 must be a structured InvalidResponse (from
        // `read_body`), never a panic. `0xff 0xfe` is not valid UTF-8.
        let base = stub(vec![("application/json", vec![0xff, 0xfe, 0x00, 0x01])]);
        let source = ManifestSource::new(format!("{base}/manifest.json"));
        let err = source
            .get_releases()
            .expect_err("a non-UTF-8 manifest body must error");
        assert!(
            matches!(err, crate::errors::Error::InvalidResponse { .. }),
            "a non-UTF-8 body must be InvalidResponse, got {err:?}"
        );
    }

    /// Serve one JSON response but capture the raw request bytes the client sent, so a test can
    /// assert which headers actually reached the manifest fetch. Returns `(base_url, captured)`.
    fn stub_capturing_request(
        body: Vec<u8>,
    ) -> (String, std::sync::Arc<std::sync::Mutex<Vec<u8>>>) {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let captured_thread = std::sync::Arc::clone(&captured);
        std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 4096];
                let n = stream.read(&mut buf).unwrap_or(0);
                *captured_thread.lock().unwrap() = buf[..n].to_vec();
                let header = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body.len()
                );
                let _ = stream.write_all(header.as_bytes());
                let _ = stream.write_all(&body);
                let _ = stream.flush();
            }
        });
        (base, captured)
    }

    #[test]
    fn manifest_fetch_sends_accept_json_and_custom_request_header() {
        // Transport-control: the base `Accept: application/json` header and a user-supplied
        // `request_header(..)` must both reach the manifest fetch. A loopback stub captures the
        // raw request line + headers so we can assert the bytes actually went out.
        let (base, captured) =
            stub_capturing_request(br#"{ "schema": 1, "releases": [] }"#.to_vec());
        let mut source = ManifestSource::new(format!("{base}/manifest.json"));
        source.request_header("X-Manifest-Probe", "sentinel-value");
        // Empty manifest -> NoReleaseFound is fine; we only care that the request went out.
        let _ = source.get_releases();

        let raw = captured.lock().unwrap().clone();
        let text = String::from_utf8_lossy(&raw);
        let lower = text.to_ascii_lowercase();
        assert!(
            lower.contains("accept: application/json"),
            "the manifest fetch must send `Accept: application/json`; raw request was:\n{text}"
        );
        assert!(
            text.contains("sentinel-value") && lower.contains("x-manifest-probe:"),
            "a custom request_header(..) must reach the manifest fetch; raw request was:\n{text}"
        );
    }

    /// Build a tiny tar.gz in memory containing the single file the updater will look for: the
    /// default `bin_path_in_archive` derived from `bin_name("app")`, which carries the platform exe
    /// suffix (`app.exe` on windows, `app` where EXE_SUFFIX is empty).
    #[cfg(all(feature = "archive-tar", feature = "compression-tar-gz"))]
    fn app_tar_gz(payload: &[u8]) -> Vec<u8> {
        let mut tar = tar::Builder::new(Vec::new());
        let mut header = tar::Header::new_gnu();
        header
            .set_path(format!("app{}", std::env::consts::EXE_SUFFIX))
            .unwrap();
        header.set_size(payload.len() as u64);
        header.set_mode(0o755);
        header.set_cksum();
        tar.append(&header, payload).unwrap();
        let tar_bytes = tar.into_inner().unwrap();
        let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
        enc.write_all(&tar_bytes).unwrap();
        enc.finish().unwrap()
    }

    #[cfg(all(feature = "archive-tar", feature = "compression-tar-gz"))]
    #[test]
    fn update_downloads_and_installs_from_a_manifest() {
        // Full sync flow: the stub serves the manifest (connection 1) then the tar.gz asset
        // (connection 2). The updater compares versions, selects the asset, downloads it, extracts
        // `app`, and installs it to a temp path. A successful `Updated` status plus the installed
        // file proves the whole pipeline ran over the manifest backend.
        let payload = b"installed-binary-payload";
        let archive = app_tar_gz(payload);

        // Bind first so we know the base, then build the manifest referencing a sibling asset URL.
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        let manifest = r#"{ "schema": 1, "releases": [
            { "version": "2.0.0", "assets": [
                { "name": "app.tar.gz", "url": "app.tar.gz" } ] } ] }"#;
        let manifest_bytes = manifest.as_bytes().to_vec();
        std::thread::spawn(move || {
            let responses: Vec<(&str, Vec<u8>)> = vec![
                ("application/json", manifest_bytes),
                ("application/octet-stream", archive),
            ];
            for (content_type, body) in responses {
                let (mut stream, _) = match listener.accept() {
                    Ok(c) => c,
                    Err(_) => return,
                };
                let mut buf = [0u8; 4096];
                let _ = stream.read(&mut buf);
                let header = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    content_type,
                    body.len()
                );
                let _ = stream.write_all(header.as_bytes());
                let _ = stream.write_all(&body);
                let _ = stream.flush();
            }
        });

        let install_dir = tempfile::tempdir().unwrap();
        let install_path = install_dir.path().join("installed-app");

        let status = Update::configure()
            .manifest_url(format!("{base}/releases/manifest.json"))
            .bin_name("app")
            .target("x86_64-unknown-linux-gnu")
            .current_version("1.0.0")
            .bin_install_path(&install_path)
            .no_confirm(true)
            .show_output(false)
            // Pick the single served asset directly, sidestepping target-name matching.
            .asset_matcher(|assets| assets.first().cloned())
            .build()
            .unwrap()
            .update_extended()
            .expect("the update must download and install from the manifest");

        assert!(
            status.is_updated(),
            "a newer manifest release served as a real tar.gz must install -> Updated, got {status:?}"
        );
        assert_eq!(status.version(), Some("2.0.0"));
        assert!(
            install_path.exists(),
            "the pipeline must install the extracted binary to {install_path:?}"
        );
        assert_eq!(std::fs::read(&install_path).unwrap(), payload);
    }

    #[cfg(feature = "async")]
    mod async_tests {
        use super::super::{ManifestSource, Update};
        use crate::update::AsyncReleaseSource;
        use std::io::{Read as _, Write as _};
        use std::net::TcpListener;

        fn stub(responses: Vec<(&'static str, Vec<u8>)>) -> String {
            super::stub(responses)
        }

        #[tokio::test]
        async fn get_releases_async_over_the_stub_parses_the_manifest() {
            let manifest = r#"{ "schema": 1, "releases": [
                { "version": "2.0.0", "assets": [
                    { "name": "app-2.0.0.tar.gz", "url": "app-2.0.0.tar.gz" } ] } ] }"#;
            let base = stub(vec![("application/json", manifest.as_bytes().to_vec())]);
            let source = ManifestSource::new(format!("{base}/releases/manifest.json"));
            let releases = source.get_releases().await.unwrap();
            assert_eq!(releases.len(), 1);
            assert_eq!(releases[0].version(), "2.0.0");
            assert_eq!(
                releases[0].assets()[0].download_url(),
                format!("{base}/releases/app-2.0.0.tar.gz")
            );
        }

        #[tokio::test]
        async fn get_releases_async_empty_releases_yields_no_release_found() {
            // Async parity: an empty manifest over the async transport has no latest release.
            let base = super::stub(vec![(
                "application/json",
                br#"{ "schema": 1, "releases": [] }"#.to_vec(),
            )]);
            let source = ManifestSource::new(format!("{base}/manifest.json"));
            let err = source
                .get_latest_release()
                .await
                .expect_err("an empty manifest must have no latest release");
            assert!(
                matches!(err, crate::errors::Error::NoReleaseFound { .. }),
                "async empty releases must yield NoReleaseFound, got {err:?}"
            );
        }

        #[tokio::test]
        async fn get_releases_async_maps_404_to_not_found() {
            // Async parity: a non-2xx manifest fetch surfaces as the same structured status error.
            let base = super::stub_status("404 Not Found");
            let source = ManifestSource::new(format!("{base}/manifest.json"));
            let err = source.get_releases().await.expect_err("404 must error");
            assert!(
                matches!(err, crate::errors::Error::NotFound { .. }),
                "async 404 must surface as NotFound, got {err:?}"
            );
        }

        #[tokio::test]
        async fn manifest_fetch_async_sends_accept_json_and_custom_request_header() {
            // Async parity: the base Accept header and a custom request_header both reach the
            // async manifest fetch.
            let (base, captured) =
                super::stub_capturing_request(br#"{ "schema": 1, "releases": [] }"#.to_vec());
            let mut source = ManifestSource::new(format!("{base}/manifest.json"));
            source.request_header("X-Manifest-Probe", "sentinel-value");
            let _ = source.get_releases().await;

            let raw = captured.lock().unwrap().clone();
            let text = String::from_utf8_lossy(&raw);
            let lower = text.to_ascii_lowercase();
            assert!(
                lower.contains("accept: application/json"),
                "async manifest fetch must send Accept: application/json; raw:\n{text}"
            );
            assert!(
                text.contains("sentinel-value") && lower.contains("x-manifest-probe:"),
                "async custom request_header must reach the fetch; raw:\n{text}"
            );
        }

        #[cfg(all(feature = "archive-tar", feature = "compression-tar-gz"))]
        #[tokio::test]
        async fn update_async_downloads_and_installs_from_a_manifest() {
            // Async sibling of the sync end-to-end flow: the manifest is fetched over the async
            // transport, then the archive is downloaded and installed through the spawn_blocking
            // finish tail.
            let payload = b"async-installed-binary-payload";
            let archive = super::app_tar_gz(payload);

            let listener = TcpListener::bind("127.0.0.1:0").unwrap();
            let base = format!("http://{}", listener.local_addr().unwrap());
            let manifest = r#"{ "schema": 1, "releases": [
                { "version": "2.0.0", "assets": [
                    { "name": "app.tar.gz", "url": "app.tar.gz" } ] } ] }"#;
            let manifest_bytes = manifest.as_bytes().to_vec();
            std::thread::spawn(move || {
                let responses: Vec<(&str, Vec<u8>)> = vec![
                    ("application/json", manifest_bytes),
                    ("application/octet-stream", archive),
                ];
                for (content_type, body) in responses {
                    let (mut stream, _) = match listener.accept() {
                        Ok(c) => c,
                        Err(_) => return,
                    };
                    let mut buf = [0u8; 4096];
                    let _ = stream.read(&mut buf);
                    let header = format!(
                        "HTTP/1.1 200 OK\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                        content_type,
                        body.len()
                    );
                    let _ = stream.write_all(header.as_bytes());
                    let _ = stream.write_all(&body);
                    let _ = stream.flush();
                }
            });

            let install_dir = tempfile::tempdir().unwrap();
            let install_path = install_dir.path().join("installed-app");

            let status = Update::configure()
                .manifest_url(format!("{base}/releases/manifest.json"))
                .bin_name("app")
                .target("x86_64-unknown-linux-gnu")
                .current_version("1.0.0")
                .bin_install_path(&install_path)
                .no_confirm(true)
                .show_output(false)
                .asset_matcher(|assets| assets.first().cloned())
                .build_async()
                .unwrap()
                .update_extended_async()
                .await
                .expect("the async update must download and install from the manifest");

            assert!(
                status.is_updated(),
                "async update must install -> Updated, got {status:?}"
            );
            assert_eq!(status.version(), Some("2.0.0"));
            assert!(install_path.exists());
            assert_eq!(std::fs::read(&install_path).unwrap(), payload);
        }
    }
}