terraphim_update 1.16.33

Shared auto-update functionality for Terraphim AI binaries
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
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
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
//! Shared auto-update functionality for Terraphim AI binaries
//!
//! This crate provides a unified interface for self-updating Terraphim AI CLI tools
//! using GitHub Releases as a distribution channel.

pub mod config;
pub mod downloader;
pub mod notification;
pub mod platform;
pub mod rollback;
pub mod scheduler;
pub mod signature;
pub mod state;

use anyhow::{Context, Result, anyhow};
use base64::Engine;
use self_update::cargo_crate_version;
use self_update::version::bump_is_greater;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::NamedTempFile;
use tracing::{error, info, warn};

/// Represents the status of an update operation
#[derive(Debug, Clone)]
pub enum UpdateStatus {
    /// No update available - already running latest version
    UpToDate(String),
    /// Update available and successfully installed
    Updated {
        from_version: String,
        to_version: String,
    },
    /// Update available but not installed
    Available {
        current_version: String,
        latest_version: String,
    },
    /// Update failed with error
    Failed(String),
}

/// Compare two version strings to determine if the first is newer than the second
/// Static version that can be called from blocking contexts
/// Uses semver crate for proper semantic versioning comparison
fn is_newer_version_static(version1: &str, version2: &str) -> Result<bool, anyhow::Error> {
    use semver::Version;

    let v1 = Version::parse(version1.trim_start_matches('v'))
        .map_err(|e| anyhow::anyhow!("Invalid version '{}': {}", version1, e))?;

    let v2 = Version::parse(version2.trim_start_matches('v'))
        .map_err(|e| anyhow::anyhow!("Invalid version '{}': {}", version2, e))?;

    Ok(v1 > v2)
}

impl fmt::Display for UpdateStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UpdateStatus::UpToDate(version) => {
                write!(f, "[OK] Already running latest version: {}", version)
            }
            UpdateStatus::Updated {
                from_version,
                to_version,
            } => {
                write!(f, "Updated: from {} to {}", from_version, to_version)
            }
            UpdateStatus::Available {
                current_version,
                latest_version,
            } => {
                write!(
                    f,
                    "Update available: {} → {}",
                    current_version, latest_version
                )
            }
            UpdateStatus::Failed(error) => {
                write!(f, "[ERROR] Update failed: {}", error)
            }
        }
    }
}

/// Configuration for the updater
#[derive(Debug, Clone)]
pub struct UpdaterConfig {
    /// Name of the binary (e.g., "terraphim_server")
    pub bin_name: String,
    /// GitHub repository owner (e.g., "terraphim")
    pub repo_owner: String,
    /// GitHub repository name (e.g., "terraphim-ai")
    pub repo_name: String,
    /// Current version of the binary
    pub current_version: String,
    /// Whether to show download progress
    pub show_progress: bool,
}

impl UpdaterConfig {
    /// Create a new updater config for Terraphim AI binaries
    pub fn new(bin_name: impl Into<String>) -> Self {
        Self {
            bin_name: bin_name.into(),
            repo_owner: "terraphim".to_string(),
            repo_name: "terraphim-ai".to_string(),
            current_version: cargo_crate_version!().to_string(),
            show_progress: true,
        }
    }

    /// Set a custom current version (useful for testing)
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.current_version = version.into();
        self
    }

    /// Enable or disable progress display
    pub fn with_progress(mut self, show: bool) -> Self {
        self.show_progress = show;
        self
    }
}

/// Updater client for Terraphim AI binaries
pub struct TerraphimUpdater {
    config: UpdaterConfig,
}

impl TerraphimUpdater {
    /// Create a new updater instance
    pub fn new(config: UpdaterConfig) -> Self {
        Self { config }
    }

    /// Check if an update is available without installing
    pub async fn check_update(&self) -> Result<UpdateStatus> {
        info!(
            "Checking for updates: {} v{}",
            self.config.bin_name, self.config.current_version
        );

        // Clone data for the blocking task
        let repo_owner = self.config.repo_owner.clone();
        let repo_name = self.config.repo_name.clone();
        let bin_name = self.config.bin_name.clone();
        let current_version = self.config.current_version.clone();
        let show_progress = self.config.show_progress;

        // Move self_update operations to a blocking task to avoid runtime conflicts
        let result = tokio::task::spawn_blocking(move || {
            // Normalize binary name for asset lookup (underscores to hyphens)
            let bin_name_for_asset = bin_name.replace('_', "-");

            // Check if update is available
            let mut builder = self_update::backends::github::Update::configure();
            builder.repo_owner(&repo_owner);
            builder.repo_name(&repo_name);
            builder.bin_name(&bin_name_for_asset); // Use hyphenated name for asset lookup
            builder.current_version(&current_version);
            builder.show_download_progress(show_progress);

            // Set custom install path to preserve underscore naming
            builder.bin_install_path(format!("/usr/local/bin/{}", bin_name));

            match builder.build() {
                Ok(updater) => {
                    // This will check without updating
                    match updater.get_latest_release() {
                        Ok(release) => {
                            let latest_version = release.version.clone();

                            // Compare versions using semver
                            match is_newer_version_static(&latest_version, &current_version) {
                                Ok(true) => {
                                    Ok::<UpdateStatus, anyhow::Error>(UpdateStatus::Available {
                                        current_version,
                                        latest_version,
                                    })
                                }
                                Ok(false) => Ok::<UpdateStatus, anyhow::Error>(
                                    UpdateStatus::UpToDate(current_version),
                                ),
                                Err(e) => Err(e),
                            }
                        }
                        Err(e) => Ok(UpdateStatus::Failed(format!("Check failed: {}", e))),
                    }
                }
                Err(e) => Ok(UpdateStatus::Failed(format!("Configuration error: {}", e))),
            }
        })
        .await;

        match result {
            Ok(update_result) => {
                match update_result {
                    Ok(status) => {
                        // Log the result for debugging
                        match &status {
                            UpdateStatus::Available {
                                current_version,
                                latest_version,
                            } => {
                                info!(
                                    "Update available: {} -> {}",
                                    current_version, latest_version
                                );
                            }
                            UpdateStatus::UpToDate(version) => {
                                info!("Already up to date: {}", version);
                            }
                            UpdateStatus::Updated {
                                from_version,
                                to_version,
                            } => {
                                info!(
                                    "Successfully updated from {} to {}",
                                    from_version, to_version
                                );
                            }
                            UpdateStatus::Failed(error) => {
                                error!("Update check failed: {}", error);
                            }
                        }
                        Ok(status)
                    }
                    Err(e) => {
                        error!("Blocking task failed: {}", e);
                        Ok(UpdateStatus::Failed(format!("Blocking task error: {}", e)))
                    }
                }
            }
            Err(e) => {
                error!("Failed to spawn blocking task: {}", e);
                Ok(UpdateStatus::Failed(format!("Task spawn error: {}", e)))
            }
        }
    }

    /// Update the binary to the latest version
    pub async fn update(&self) -> Result<UpdateStatus> {
        info!(
            "Updating {} from version {}",
            self.config.bin_name, self.config.current_version
        );

        // Clone data for the blocking task
        let repo_owner = self.config.repo_owner.clone();
        let repo_name = self.config.repo_name.clone();
        let bin_name = self.config.bin_name.clone();
        let current_version = self.config.current_version.clone();
        let show_progress = self.config.show_progress;

        // Decode the embedded public key for signature verification
        let key_bytes = base64::engine::general_purpose::STANDARD
            .decode(signature::get_embedded_public_key())
            .context("Failed to decode public key")?;

        // Convert to array (must be exactly 32 bytes for Ed25519)
        if key_bytes.len() != 32 {
            return Err(anyhow!(
                "Invalid public key length: {} bytes (expected 32)",
                key_bytes.len()
            ));
        }
        let mut key_array = [0u8; 32];
        key_array.copy_from_slice(&key_bytes);

        // Move self_update operations to a blocking task to avoid runtime conflicts
        let result = tokio::task::spawn_blocking(move || {
            // Normalize binary name for asset lookup (underscores to hyphens)
            let bin_name_for_asset = bin_name.replace('_', "-");

            // Build the updater with signature verification enabled
            let mut builder = self_update::backends::github::Update::configure();
            builder.repo_owner(&repo_owner);
            builder.repo_name(&repo_name);
            builder.bin_name(&bin_name_for_asset); // Use hyphenated name for asset lookup
            builder.current_version(&current_version);
            builder.show_download_progress(show_progress);
            builder.verifying_keys(vec![key_array]); // Enable signature verification

            // Set custom install path to preserve underscore naming
            builder.bin_install_path(format!("/usr/local/bin/{}", bin_name));

            match builder.build() {
                Ok(updater) => match updater.update() {
                    Ok(status) => match status {
                        self_update::Status::UpToDate(version) => {
                            Ok::<UpdateStatus, anyhow::Error>(UpdateStatus::UpToDate(version))
                        }
                        self_update::Status::Updated(version) => {
                            Ok::<UpdateStatus, anyhow::Error>(UpdateStatus::Updated {
                                from_version: current_version,
                                to_version: version,
                            })
                        }
                    },
                    Err(e) => Ok(UpdateStatus::Failed(format!("Update failed: {}", e))),
                },
                Err(e) => Ok(UpdateStatus::Failed(format!("Configuration error: {}", e))),
            }
        })
        .await;

        match result {
            Ok(update_result) => {
                match update_result {
                    Ok(status) => {
                        // Log the result for debugging
                        match &status {
                            UpdateStatus::Updated {
                                from_version,
                                to_version,
                            } => {
                                info!(
                                    "Successfully updated from {} to {}",
                                    from_version, to_version
                                );
                            }
                            UpdateStatus::UpToDate(version) => {
                                info!("Already up to date: {}", version);
                            }
                            UpdateStatus::Available {
                                current_version,
                                latest_version,
                            } => {
                                info!(
                                    "Update available: {} -> {}",
                                    current_version, latest_version
                                );
                            }
                            UpdateStatus::Failed(error) => {
                                error!("Update failed: {}", error);
                            }
                        }
                        Ok(status)
                    }
                    Err(e) => {
                        error!("Blocking task failed: {}", e);
                        Ok(UpdateStatus::Failed(format!("Blocking task error: {}", e)))
                    }
                }
            }
            Err(e) => {
                error!("Failed to spawn blocking task: {}", e);
                Ok(UpdateStatus::Failed(format!("Task spawn error: {}", e)))
            }
        }
    }

    /// Update the binary with signature verification
    ///
    /// This method implements a manual download, verify, and install flow
    /// to ensure that only signed and verified binaries are installed.
    ///
    /// # Returns
    /// * `Ok(UpdateStatus)` - Status of the update operation
    /// * `Err(anyhow::Error)` - Error if update fails
    ///
    /// # Process
    /// 1. Get latest release info from GitHub
    /// 2. Download the release archive to a temp location
    /// 3. Verify the Ed25519 signature using zipsign-api
    /// 4. Install if valid, reject if invalid/missing signature
    ///
    /// # Security
    /// - Rejects updates with invalid signatures
    /// - Rejects updates with missing signatures
    /// - Only installs verified binaries
    pub async fn update_with_verification(&self) -> Result<UpdateStatus> {
        info!(
            "Updating {} from version {} with signature verification",
            self.config.bin_name, self.config.current_version
        );

        // Clone data for the blocking task
        let repo_owner = self.config.repo_owner.clone();
        let repo_name = self.config.repo_name.clone();
        let bin_name = self.config.bin_name.clone();
        let current_version = self.config.current_version.clone();
        let show_progress = self.config.show_progress;

        // Move self_update operations to a blocking task
        let result = tokio::task::spawn_blocking(move || {
            Self::update_with_verification_blocking(
                &repo_owner,
                &repo_name,
                &bin_name,
                &current_version,
                show_progress,
            )
        })
        .await;

        match result {
            Ok(Ok(update_status)) => {
                match &update_status {
                    UpdateStatus::Updated {
                        from_version,
                        to_version,
                    } => {
                        info!(
                            "Successfully updated from {} to {} with verified signature",
                            from_version, to_version
                        );
                    }
                    UpdateStatus::UpToDate(version) => {
                        info!("Already up to date: {}", version);
                    }
                    UpdateStatus::Failed(error) => {
                        error!("Update with verification failed: {}", error);
                    }
                    _ => {}
                }
                Ok(update_status)
            }
            Ok(Err(e)) => {
                error!("Blocking task returned error: {}", e);
                Ok(UpdateStatus::Failed(format!("Update error: {}", e)))
            }
            Err(e) => {
                error!("Blocking task failed: {}", e);
                Ok(UpdateStatus::Failed(format!("Task spawn error: {}", e)))
            }
        }
    }

    /// Blocking version of update_with_verification for use in spawn_blocking
    fn update_with_verification_blocking(
        repo_owner: &str,
        repo_name: &str,
        bin_name: &str,
        current_version: &str,
        show_progress: bool,
    ) -> Result<UpdateStatus> {
        info!(
            "Starting verified update flow for {} v{}",
            bin_name, current_version
        );

        // Step 1: Get latest release info from GitHub
        let release =
            match Self::get_latest_release_info(repo_owner, repo_name, bin_name, current_version) {
                Ok(release) => release,
                Err(e) => {
                    return Ok(UpdateStatus::Failed(format!(
                        "Failed to get release info: {}",
                        e
                    )));
                }
            };

        let latest_version = &release.version;

        // Step 2: Download archive to temp location
        let temp_archive = match Self::download_release_archive(
            repo_owner,
            repo_name,
            bin_name,
            latest_version,
            show_progress,
        ) {
            Ok(archive) => archive,
            Err(e) => {
                return Ok(UpdateStatus::Failed(format!(
                    "Failed to download archive: {}",
                    e
                )));
            }
        };

        let archive_path = temp_archive.path().to_path_buf();

        // Step 3: Verify signature BEFORE installation
        info!("Verifying signature for archive {:?}", archive_path);
        let verification_result =
            match crate::signature::verify_archive_signature(&archive_path, None) {
                Ok(result) => result,
                Err(e) => return Ok(UpdateStatus::Failed(format!("Verification error: {}", e))),
            };

        match verification_result {
            crate::signature::VerificationResult::Valid => {
                info!("Signature verification passed - proceeding with installation");
            }
            crate::signature::VerificationResult::Invalid { reason } => {
                let error_msg = format!("Signature verification failed: {}", reason);
                error!("{}", error_msg);
                return Ok(UpdateStatus::Failed(error_msg));
            }
            crate::signature::VerificationResult::MissingSignature => {
                warn!(
                    "No signature found in archive - proceeding without verification. \
                       Archives will be signed in a future release."
                );
            }
            crate::signature::VerificationResult::Error(msg) => {
                let error_msg = format!("Verification error: {}", msg);
                error!("{}", error_msg);
                return Ok(UpdateStatus::Failed(error_msg));
            }
        }

        // Step 4: Install the verified archive
        match Self::install_verified_archive(&archive_path, bin_name) {
            Ok(_) => {
                info!("Successfully installed verified update");
                Ok(UpdateStatus::Updated {
                    from_version: current_version.to_string(),
                    to_version: latest_version.clone(),
                })
            }
            Err(e) => Ok(UpdateStatus::Failed(format!("Installation failed: {}", e))),
        }
    }

    /// Get latest release info from GitHub
    fn get_latest_release_info(
        repo_owner: &str,
        repo_name: &str,
        bin_name: &str,
        current_version: &str,
    ) -> Result<self_update::update::Release> {
        info!(
            "Fetching latest release info for {}/{}",
            repo_owner, repo_name
        );

        // Normalize binary name for asset lookup (underscores to hyphens)
        let bin_name_for_asset = bin_name.replace('_', "-");

        let mut builder = self_update::backends::github::Update::configure();
        builder.repo_owner(repo_owner);
        builder.repo_name(repo_name);
        builder.bin_name(&bin_name_for_asset); // Use hyphenated name for asset lookup
        builder.current_version(current_version);

        // Set custom install path to preserve underscore naming
        builder.bin_install_path(format!("/usr/local/bin/{}", bin_name));

        let updater = builder.build()?;

        let release = updater.get_latest_release()?;

        // Check if the latest version is actually newer
        #[allow(clippy::needless_borrow)]
        if !bump_is_greater(&current_version, &release.version)? {
            return Err(anyhow!(
                "Current version {} is up to date with {}",
                current_version,
                release.version
            ));
        }

        info!("Latest version: {}", release.version);
        Ok(release)
    }

    /// Download release archive to a temporary file with fallback support
    ///
    /// For Linux x86_64, tries GNU first, then falls back to MUSL if GNU is not available
    fn download_release_archive(
        repo_owner: &str,
        repo_name: &str,
        bin_name: &str,
        version: &str,
        show_progress: bool,
    ) -> Result<NamedTempFile> {
        // Normalize binary name (replace underscores with hyphens for GitHub releases)
        let bin_name_in_asset = bin_name.replace('_', "-");

        // Determine current platform and get list of targets to try (for fallback)
        let targets = Self::get_target_triples_with_fallback()?;

        // Try each target in order
        let mut last_error = None;

        for target in targets {
            // Try both raw binary and archive formats
            let asset_names = Self::get_asset_names(&bin_name_in_asset, &target, version);

            for asset_name in asset_names {
                // Construct download URL
                let version_tag = if version.starts_with('v') {
                    version.to_string()
                } else {
                    format!("v{}", version)
                };
                let download_url = format!(
                    "https://github.com/{}/{}/releases/download/{}/{}",
                    repo_owner, repo_name, version_tag, asset_name
                );

                info!("Trying to download from: {}", download_url);

                // Create temp file for download
                let temp_file = NamedTempFile::new()?;
                let download_config = crate::downloader::DownloadConfig {
                    show_progress,
                    ..Default::default()
                };

                match crate::downloader::download_with_retry(
                    &download_url,
                    temp_file.path(),
                    Some(download_config),
                ) {
                    Ok(_) => {
                        info!(
                            "Successfully downloaded {} to: {:?}",
                            asset_name,
                            temp_file.path()
                        );
                        return Ok(temp_file);
                    }
                    Err(e) => {
                        info!("Failed to download {}: {}", asset_name, e);
                        last_error = Some((asset_name, e));
                        // Continue to next asset/target
                    }
                }
            }
        }

        // All attempts failed
        let error_msg = if let Some((asset_name, e)) = last_error {
            format!(
                "Failed to download any asset. Last attempt '{}'. Error: {}. Available assets can be listed at: https://github.com/{}/{}/releases/tag/{}",
                asset_name, e, repo_owner, repo_name, version
            )
        } else {
            format!(
                "Failed to determine assets to download. Available assets can be listed at: https://github.com/{}/{}/releases/tag/{}",
                repo_owner, repo_name, version
            )
        };

        Err(anyhow!("{}", error_msg))
    }

    /// Get asset names to try for a given target
    /// Returns asset names in order of preference (archives with signatures first, then raw binaries)
    fn get_asset_names(bin_name: &str, target: &str, version: &str) -> Vec<String> {
        let mut assets = Vec::new();

        // Archive name with version comes FIRST (terraphim-agent-1.16.31-x86_64-unknown-linux-gnu.tar.gz)
        // Archives are signed and preferred for security verification
        let version_clean = version.trim_start_matches('v');
        let archive_name = format!("{}-{}-{}.tar.gz", bin_name, version_clean, target);
        assets.push(archive_name);

        // Raw binary name second (terraphim-agent-x86_64-unknown-linux-gnu)
        // Raw binaries may not have embedded signatures
        let raw_name = if cfg!(windows) {
            format!("{}.exe", target)
        } else {
            target.to_string()
        };
        assets.push(raw_name);

        // Also try zip for Windows
        if cfg!(windows) {
            let zip_name = format!("{}-{}-{}.zip", bin_name, version_clean, target);
            assets.push(zip_name);
        }

        assets
    }

    /// Get the target triples to try for the current platform
    ///
    /// For Linux x86_64, returns both GNU and MUSL variants (GNU first, MUSL fallback)
    fn get_target_triples_with_fallback() -> Result<Vec<String>> {
        use std::env::consts::{ARCH, OS};

        let target = format!("{}-{}", ARCH, OS);

        // Map Rust targets to common release naming conventions
        // For Linux x86_64, try GNU first, then MUSL as fallback
        let targets = match target.as_str() {
            "x86_64-linux" => vec![
                "x86_64-unknown-linux-gnu".to_string(),
                "x86_64-unknown-linux-musl".to_string(),
            ],
            "aarch64-linux" => vec![
                "aarch64-unknown-linux-gnu".to_string(),
                "aarch64-unknown-linux-musl".to_string(),
            ],
            "x86_64-windows" => vec!["x86_64-pc-windows-msvc".to_string()],
            "x86_64-macos" => vec!["x86_64-apple-darwin".to_string()],
            "aarch64-macos" => vec!["aarch64-apple-darwin".to_string()],
            _ => vec![target],
        };

        Ok(targets)
    }

    /// Install a verified archive to the current binary location
    fn install_verified_archive(archive_path: &Path, bin_name: &str) -> Result<()> {
        info!("Installing verified archive {:?}", archive_path);

        // Get current executable path
        let current_exe = std::env::current_exe()?;
        let install_dir = current_exe
            .parent()
            .ok_or_else(|| anyhow!("Cannot determine install directory"))?;

        info!("Installing to directory: {:?}", install_dir);

        // Check if the downloaded file is an archive or a raw binary
        let file_magic = std::fs::File::open(archive_path)?;
        let first_bytes = Self::read_file_magic(&file_magic)?;

        // Check file magic to determine if it's an archive or raw binary
        if Self::is_archive(&first_bytes) {
            info!("Detected archive format, extracting...");
            // Extract and install using self_update's extract functionality
            if cfg!(windows) {
                // Windows: extract ZIP
                Self::extract_zip(archive_path, install_dir)?;
            } else {
                // Unix: extract tar.gz
                Self::extract_tarball(archive_path, install_dir, bin_name)?;
            }
        } else {
            info!("Detected raw binary, copying directly...");
            // It's a raw binary, just copy it to the install location
            let normalized_bin_name = bin_name.replace('_', "-");
            let target_path = install_dir.join(&normalized_bin_name);

            info!("Copying binary to {:?}", target_path);
            std::fs::copy(archive_path, &target_path)?;

            // Also try the original bin_name in case the release uses underscores
            if normalized_bin_name != bin_name {
                let alt_path = install_dir.join(bin_name);
                if !alt_path.exists() {
                    std::fs::copy(archive_path, &alt_path)?;
                    info!("Also copied to {:?}", alt_path);
                }
            }
        }

        // Make executable on Unix
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            for name in &[bin_name, &bin_name.replace('_', "-")] {
                let bin_path = install_dir.join(name);
                if bin_path.exists() {
                    let mut perms = fs::metadata(&bin_path)?.permissions();
                    perms.set_mode(0o755);
                    fs::set_permissions(&bin_path, perms)?;
                    info!("Made executable: {:?}", bin_path);
                }
            }
        }

        Ok(())
    }

    /// Read first few bytes of a file to detect format
    fn read_file_magic(file: &std::fs::File) -> Result<Vec<u8>> {
        use std::io::Read;
        let mut buffer = [0u8; 16];
        let mut handle = file.try_clone()?;
        handle.read_exact(&mut buffer)?;
        Ok(buffer.to_vec())
    }

    /// Check if bytes indicate an archive format
    fn is_archive(bytes: &[u8]) -> bool {
        // ZIP magic number: PK\x03\x04
        if bytes.starts_with(&[0x50, 0x4B, 0x03, 0x04]) {
            return true;
        }

        // gzip magic number: \x1f\x8b
        if bytes.starts_with(&[0x1F, 0x8B]) {
            return true;
        }

        false
    }

    /// Extract a ZIP archive to the target directory
    fn extract_zip(archive_path: &Path, target_dir: &Path) -> Result<()> {
        use zip::ZipArchive;

        let file = fs::File::open(archive_path)?;
        let mut archive = ZipArchive::new(file)?;

        for i in 0..archive.len() {
            let mut file = archive.by_index(i)?;
            #[allow(clippy::needless_borrows_for_generic_args)]
            let outpath = target_dir.join(file.mangled_name());

            if file.name().ends_with('/') {
                fs::create_dir_all(&outpath)?;
            } else {
                if let Some(parent) = outpath.parent() {
                    fs::create_dir_all(parent)?;
                }
                let mut outfile = fs::File::create(&outpath)?;
                std::io::copy(&mut file, &mut outfile)?;
            }
        }

        Ok(())
    }

    /// Extract a tar.gz archive to the target directory
    fn extract_tarball(archive_path: &Path, target_dir: &Path, bin_name: &str) -> Result<()> {
        use flate2::read::GzDecoder;
        use tar::Archive;

        let file = fs::File::open(archive_path)?;
        let decoder = GzDecoder::new(file);
        let mut archive = Archive::new(decoder);

        // Extract the binary from the archive
        // The binary should be at the root of the archive
        for entry in archive.entries()? {
            let mut entry = entry?;
            let path = entry.path()?;

            // Only extract the main binary, not directory structure
            if let Some(file_name) = path.file_name() {
                if file_name.to_str() == Some(bin_name) {
                    let outpath = target_dir.join(bin_name);
                    let mut outfile = fs::File::create(&outpath)?;
                    std::io::copy(&mut entry, &mut outfile)?;
                    info!("Extracted binary to {:?}", outpath);
                    break;
                }
            }
        }

        Ok(())
    }

    /// Check for update and install if available with signature verification
    pub async fn check_and_update(&self) -> Result<UpdateStatus> {
        match self.check_update().await? {
            UpdateStatus::Available {
                current_version,
                latest_version,
            } => {
                info!(
                    "Update available: {} → {}, installing...",
                    current_version, latest_version
                );
                self.update_with_verification().await
            }
            status => Ok(status),
        }
    }

    /// Compare two version strings to determine if the first is newer than the second
    #[allow(dead_code)]
    fn is_newer_version(&self, version1: &str, version2: &str) -> Result<bool> {
        // Simple version comparison - in production you might want to use semver crate
        let v1_parts: Vec<u32> = version1
            .trim_start_matches('v')
            .split('.')
            .take(3)
            .map(|s| s.parse().unwrap_or(0))
            .collect();

        let v2_parts: Vec<u32> = version2
            .trim_start_matches('v')
            .split('.')
            .take(3)
            .map(|s| s.parse().unwrap_or(0))
            .collect();

        // Pad with zeros if needed
        let v1 = [
            v1_parts.first().copied().unwrap_or(0),
            v1_parts.get(1).copied().unwrap_or(0),
            v1_parts.get(2).copied().unwrap_or(0),
        ];

        let v2 = [
            v2_parts.first().copied().unwrap_or(0),
            v2_parts.get(1).copied().unwrap_or(0),
            v2_parts.get(2).copied().unwrap_or(0),
        ];

        Ok(v1 > v2)
    }
}

/// Convenience function to create an updater and check for updates
pub async fn check_for_updates(bin_name: impl Into<String>) -> Result<UpdateStatus> {
    let config = UpdaterConfig::new(bin_name);
    let updater = TerraphimUpdater::new(config);
    updater.check_update().await
}

/// Convenience function to create an updater and install updates
pub async fn update_binary(bin_name: impl Into<String>) -> Result<UpdateStatus> {
    let config = UpdaterConfig::new(bin_name);
    let updater = TerraphimUpdater::new(config);
    updater.check_and_update().await
}

/// Convenience function with progress disabled (useful for automated environments)
pub async fn update_binary_silent(bin_name: impl Into<String>) -> Result<UpdateStatus> {
    let config = UpdaterConfig::new(bin_name).with_progress(false);
    let updater = TerraphimUpdater::new(config);
    updater.check_and_update().await
}

/// Check for updates automatically using self_update backend
///
/// This is a simplified function that leverages self_update's GitHub backend
/// to check for available updates without installing them.
///
/// # Arguments
/// * `bin_name` - Name of the binary (e.g., "terraphim")
/// * `current_version` - Current version of the binary (e.g., "1.0.0")
///
/// # Returns
/// * `Ok(UpdateStatus)` - Status indicating if an update is available
/// * `Err(anyhow::Error)` - Error if the check fails
///
/// # Example
/// ```no_run
/// use terraphim_update::check_for_updates_auto;
///
/// async {
///     let status = check_for_updates_auto("terraphim", "1.0.0").await?;
///     println!("Update status: {}", status);
///     Ok::<(), anyhow::Error>(())
/// };
/// ```
pub async fn check_for_updates_auto(bin_name: &str, current_version: &str) -> Result<UpdateStatus> {
    info!("Checking for updates: {} v{}", bin_name, current_version);

    let bin_name = bin_name.to_string();
    let current_version = current_version.to_string();

    let result = tokio::task::spawn_blocking(move || {
        // Normalize binary name for asset lookup (underscores to hyphens)
        let bin_name_for_asset = bin_name.replace('_', "-");

        let mut builder = self_update::backends::github::Update::configure();
        builder.repo_owner("terraphim");
        builder.repo_name("terraphim-ai");
        builder.bin_name(&bin_name_for_asset); // Use hyphenated name for asset lookup
        builder.current_version(&current_version);

        // Set custom install path to preserve underscore naming
        builder.bin_install_path(format!("/usr/local/bin/{}", bin_name));

        match builder.build() {
            Ok(updater) => match updater.get_latest_release() {
                Ok(release) => {
                    let latest_version = release.version.clone();

                    match is_newer_version_static(&latest_version, &current_version) {
                        Ok(true) => Ok::<UpdateStatus, anyhow::Error>(UpdateStatus::Available {
                            current_version,
                            latest_version,
                        }),
                        Ok(false) => Ok::<UpdateStatus, anyhow::Error>(UpdateStatus::UpToDate(
                            current_version,
                        )),
                        Err(e) => Err(e),
                    }
                }
                Err(e) => Ok(UpdateStatus::Failed(format!("Check failed: {}", e))),
            },
            Err(e) => Ok(UpdateStatus::Failed(format!("Configuration error: {}", e))),
        }
    })
    .await;

    match result {
        Ok(update_result) => update_result,
        Err(e) => {
            error!("Failed to spawn blocking task: {}", e);
            Ok(UpdateStatus::Failed(format!("Task spawn error: {}", e)))
        }
    }
}

/// Check for updates on application startup
///
/// This function performs a non-blocking update check on startup
/// and logs a warning if the check fails (doesn't interrupt startup).
///
/// # Arguments
/// * `bin_name` - Name of the binary (e.g., "terraphim-agent")
///
/// # Returns
/// * `Ok(UpdateStatus)` - Status of update check
/// * `Err(anyhow::Error)` - Error if check fails
///
/// # Example
/// ```no_run
/// use terraphim_update::check_for_updates_startup;
///
/// async {
///     if let Err(e) = check_for_updates_startup("terraphim-agent").await {
///         eprintln!("Update check failed: {}", e);
///     }
///     Ok::<(), anyhow::Error>(())
/// };
/// ```
pub async fn check_for_updates_startup(bin_name: &str) -> Result<UpdateStatus> {
    let current_version = env!("CARGO_PKG_VERSION");
    check_for_updates_auto(bin_name, current_version).await
}

/// Start the update scheduler
///
/// This function starts a background task that periodically checks for updates
/// and sends notifications through a callback when updates are available.
///
/// # Arguments
/// * `bin_name` - Name of the binary (e.g., "terraphim-agent")
/// * `current_version` - Current version of the binary
/// * `callback` - Function to call when an update is available
///
/// # Returns
/// * `Ok(JoinHandle<()>)` - Handle to the scheduler task (can be used to abort)
/// * `Err(anyhow::Error)` - Error if scheduler fails to start
///
/// # Example
/// ```no_run
/// use terraphim_update::start_update_scheduler;
///
/// async {
///     let handle = start_update_scheduler(
///         "terraphim-agent",
///         "1.0.0",
///         Box::new(|update_info| {
///             println!("Update available: {}", update_info.latest_version);
///         })
///     ).await?;
///     # Ok::<(), anyhow::Error>(())
/// };
/// ```
pub async fn start_update_scheduler(
    bin_name: &str,
    current_version: &str,
    callback: Box<dyn Fn(UpdateAvailableInfo) + Send + Sync>,
) -> Result<tokio::task::JoinHandle<()>> {
    use crate::config::UpdateConfig;
    use crate::scheduler::{UpdateCheckResult, UpdateScheduler};
    use std::sync::Arc;

    let config = UpdateConfig::default();

    let bin_name_clone = bin_name.to_string();
    let current_version_clone = current_version.to_string();

    let check_fn = Arc::new(move || -> anyhow::Result<UpdateCheckResult> {
        let status = {
            let bin_name = bin_name_clone.clone();
            let current_version = current_version_clone.clone();

            tokio::task::block_in_place(|| {
                let rt = tokio::runtime::Runtime::new()?;
                rt.block_on(async { check_for_updates_auto(&bin_name, &current_version).await })
            })
        }?;

        match status {
            UpdateStatus::Available {
                current_version,
                latest_version,
            } => Ok(UpdateCheckResult::UpdateAvailable {
                current_version,
                latest_version,
            }),
            UpdateStatus::UpToDate(_) => Ok(UpdateCheckResult::UpToDate),
            UpdateStatus::Failed(error) => Ok(UpdateCheckResult::Failed { error }),
            _ => Ok(UpdateCheckResult::UpToDate),
        }
    });

    let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);
    let mut receiver = scheduler.create_notification_channel()?;

    scheduler.start().await?;

    let callback = Arc::new(callback);

    let handle = tokio::spawn(async move {
        while let Some(notification) = receiver.recv().await {
            match notification {
                crate::scheduler::UpdateNotification::UpdateAvailable {
                    current_version,
                    latest_version,
                } => {
                    callback(UpdateAvailableInfo {
                        current_version: current_version.clone(),
                        latest_version: latest_version.clone(),
                    });
                }
                crate::scheduler::UpdateNotification::CheckFailed { error } => {
                    tracing::warn!("Update check failed: {}", error);
                }
                crate::scheduler::UpdateNotification::Stopped => {
                    break;
                }
            }
        }
    });

    Ok(handle)
}

/// Information about an available update (for callback)
#[derive(Debug, Clone)]
pub struct UpdateAvailableInfo {
    pub current_version: String,
    pub latest_version: String,
}

/// Backup the current binary with a version suffix
///
/// Creates a backup of the binary before updating, allowing rollback
/// if the update fails.
///
/// # Arguments
/// * `binary_path` - Path to the binary to backup
/// * `version` - Version string to use in backup filename
///
/// # Returns
/// * `Ok(PathBuf)` - Path to the backup file
/// * `Err(anyhow::Error)` - Error if backup fails
///
/// # Example
/// ```no_run
/// use terraphim_update::backup_binary;
/// use std::path::Path;
///
/// let backup = backup_binary(Path::new("/usr/local/bin/terraphim"), "1.0.0")?;
/// println!("Backup created at: {:?}", backup);
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn backup_binary(binary_path: &Path, version: &str) -> Result<PathBuf> {
    info!(
        "Backing up binary at {:?} with version {}",
        binary_path, version
    );

    if !binary_path.exists() {
        anyhow::bail!("Binary not found at {:?}", binary_path);
    }

    let backup_path = binary_path.with_extension(format!("bak-{}", version));

    fs::copy(binary_path, &backup_path)?;

    info!("Backup created at {:?}", backup_path);
    Ok(backup_path)
}

/// Rollback to a previous version from backup
///
/// Restores a backed-up binary to the original location.
///
/// # Arguments
/// * `backup_path` - Path to the backup file
/// * `target_path` - Path where to restore the binary
///
/// # Returns
/// * `Ok(())` - Success
/// * `Err(anyhow::Error)` - Error if rollback fails
///
/// # Example
/// ```no_run
/// use terraphim_update::rollback;
/// use std::path::Path;
///
/// rollback(
///     Path::new("/usr/local/bin/terraphim.bak-1.0.0"),
///     Path::new("/usr/local/bin/terraphim")
/// )?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn rollback(backup_path: &Path, target_path: &Path) -> Result<()> {
    info!("Rolling back from {:?} to {:?}", backup_path, target_path);

    if !backup_path.exists() {
        anyhow::bail!("Backup not found at {:?}", backup_path);
    }

    fs::copy(backup_path, target_path)?;

    info!("Rollback completed successfully");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_version_comparison() {
        let config = UpdaterConfig::new("test");
        let updater = TerraphimUpdater::new(config);

        // Test basic version comparisons
        assert!(updater.is_newer_version("1.1.0", "1.0.0").unwrap());
        assert!(updater.is_newer_version("2.0.0", "1.9.9").unwrap());
        assert!(updater.is_newer_version("1.0.1", "1.0.0").unwrap());

        // Test equal versions
        assert!(!updater.is_newer_version("1.0.0", "1.0.0").unwrap());

        // Test older versions
        assert!(!updater.is_newer_version("1.0.0", "1.1.0").unwrap());
        assert!(!updater.is_newer_version("1.9.9", "2.0.0").unwrap());

        // Test with v prefix
        assert!(updater.is_newer_version("v1.1.0", "v1.0.0").unwrap());
        assert!(updater.is_newer_version("1.1.0", "v1.0.0").unwrap());
        assert!(updater.is_newer_version("v1.1.0", "1.0.0").unwrap());
    }

    #[tokio::test]
    async fn test_updater_config() {
        let config = UpdaterConfig::new("test-binary")
            .with_version("1.0.0")
            .with_progress(false);

        assert_eq!(config.bin_name, "test-binary");
        assert_eq!(config.current_version, "1.0.0");
        assert!(!config.show_progress);
        assert_eq!(config.repo_owner, "terraphim");
        assert_eq!(config.repo_name, "terraphim-ai");
    }

    #[test]
    fn test_backup_binary() {
        // Create a temporary file to simulate a binary
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test binary content").unwrap();

        let binary_path = temp_file.path();
        let version = "1.0.0";

        let backup_path = backup_binary(binary_path, version).unwrap();

        // Verify backup was created
        assert!(backup_path.exists());
        assert!(backup_path.to_string_lossy().contains("bak-1.0.0"));

        // Verify backup has same content
        let original_content = fs::read_to_string(binary_path).unwrap();
        let backup_content = fs::read_to_string(&backup_path).unwrap();
        assert_eq!(original_content, backup_content);

        // Clean up backup
        fs::remove_file(&backup_path).unwrap();
    }

    #[test]
    fn test_backup_binary_nonexistent() {
        let nonexistent_path = Path::new("/nonexistent/path/to/binary");

        let result = backup_binary(nonexistent_path, "1.0.0");
        assert!(result.is_err());
    }

    #[test]
    fn test_rollback() {
        // Create a temporary file to simulate a backup
        let mut backup_file = NamedTempFile::new().unwrap();
        writeln!(backup_file, "backup content").unwrap();

        let backup_path = backup_file.path();

        // Create target path
        let mut target_file = NamedTempFile::new().unwrap();
        writeln!(target_file, "original content").unwrap();
        let target_path = target_file.path();

        // Perform rollback
        rollback(backup_path, target_path).unwrap();

        // Verify target now has backup content
        let target_content = fs::read_to_string(target_path).unwrap();
        assert_eq!(target_content, "backup content\n");
    }

    #[test]
    fn test_rollback_nonexistent() {
        let nonexistent_backup = Path::new("/nonexistent/backup.bak");
        let temp_file = NamedTempFile::new().unwrap();
        let target_path = temp_file.path();

        let result = rollback(nonexistent_backup, target_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_backup_and_rollback_roundtrip() {
        // Create original binary
        let mut original_file = NamedTempFile::new().unwrap();
        writeln!(original_file, "original binary v1.0.0").unwrap();
        let original_path = original_file.path();

        // Create backup
        let backup_path = backup_binary(original_path, "1.0.0").unwrap();

        // Modify original (simulate update)
        fs::write(original_path, "updated binary v1.1.0").unwrap();

        // Verify original changed
        assert_eq!(
            fs::read_to_string(original_path).unwrap(),
            "updated binary v1.1.0"
        );

        // Rollback
        rollback(&backup_path, original_path).unwrap();

        // Verify original restored
        assert_eq!(
            fs::read_to_string(original_path).unwrap(),
            "original binary v1.0.0\n"
        );

        // Clean up backup
        fs::remove_file(&backup_path).unwrap();
    }

    #[tokio::test]
    async fn test_check_for_updates_auto() {
        // This test will make actual API calls to GitHub
        // It's useful for manual testing but may be flaky in CI
        let status = check_for_updates_auto("terraphim", "0.0.1").await;

        match status {
            Ok(UpdateStatus::Available {
                current_version,
                latest_version,
            }) => {
                assert_eq!(current_version, "0.0.1");
                assert_ne!(current_version, latest_version);
            }
            Ok(UpdateStatus::UpToDate(version)) => {
                assert_eq!(version, "0.0.1");
            }
            Ok(UpdateStatus::Failed(_)) => {
                // This is acceptable if GitHub API is unavailable
            }
            _ => {}
        }
    }

    #[test]
    fn test_is_newer_version_static() {
        // Test basic comparisons
        assert!(is_newer_version_static("2.0.0", "1.0.0").unwrap());
        assert!(is_newer_version_static("1.1.0", "1.0.0").unwrap());
        assert!(is_newer_version_static("1.0.1", "1.0.0").unwrap());

        // Test equal versions
        assert!(!is_newer_version_static("1.0.0", "1.0.0").unwrap());

        // Test older versions
        assert!(!is_newer_version_static("1.0.0", "2.0.0").unwrap());
        assert!(!is_newer_version_static("1.0.0", "1.1.0").unwrap());

        // Test with v prefix
        assert!(is_newer_version_static("v2.0.0", "v1.0.0").unwrap());
        assert!(!is_newer_version_static("v1.0.0", "v2.0.0").unwrap());
    }

    #[test]
    fn test_update_status_display() {
        let up_to_date = UpdateStatus::UpToDate("1.0.0".to_string());
        assert!(up_to_date.to_string().contains("1.0.0"));

        let updated = UpdateStatus::Updated {
            from_version: "1.0.0".to_string(),
            to_version: "2.0.0".to_string(),
        };
        assert!(updated.to_string().contains("1.0.0"));
        assert!(updated.to_string().contains("2.0.0"));

        let available = UpdateStatus::Available {
            current_version: "1.0.0".to_string(),
            latest_version: "2.0.0".to_string(),
        };
        assert!(available.to_string().contains("1.0.0"));
        assert!(available.to_string().contains("2.0.0"));

        let failed = UpdateStatus::Failed("test error".to_string());
        assert!(failed.to_string().contains("test error"));
    }

    #[test]
    fn test_version_prefix_for_github_releases() {
        // GitHub release tags use "v" prefix but self_update strips it
        // This test verifies our version tag construction logic
        let version_without_v = "1.5.2";
        let version_with_v = "v1.5.2";

        // Version without v should get v prepended
        let version_tag_1 = if version_without_v.starts_with('v') {
            version_without_v.to_string()
        } else {
            format!("v{}", version_without_v)
        };
        assert_eq!(version_tag_1, "v1.5.2");

        // Version with v should remain unchanged
        let version_tag_2 = if version_with_v.starts_with('v') {
            version_with_v.to_string()
        } else {
            format!("v{}", version_with_v)
        };
        assert_eq!(version_tag_2, "v1.5.2");
    }
}