purple-ssh 1.27.0

Manage SSH configs and launch connections from the terminal. TUI host manager with search, tags, tunnels, command snippets, password management (keychain, 1Password, Bitwarden, pass, Vault), cloud sync (AWS EC2, DigitalOcean, Vultr, Linode, Hetzner, UpCloud, Proxmox VE, Scaleway, GCP), self-update and round-trip fidelity for ~/.ssh/config.
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
use std::io::Read;
use std::path::Path;
use std::sync::mpsc;

use anyhow::{Context, Result};

use crate::event::AppEvent;

/// Current compiled-in version from Cargo.toml.
pub fn current_version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Parse a semver string "X.Y.Z" into a tuple.
fn parse_version(v: &str) -> Option<(u32, u32, u32)> {
    let mut parts = v.splitn(3, '.');
    let major = parts.next()?.parse().ok()?;
    let minor = parts.next()?.parse().ok()?;
    let patch = parts.next()?.parse().ok()?;
    Some((major, minor, patch))
}

/// Returns true if `latest` is strictly newer than `current`.
fn is_newer(current: &str, latest: &str) -> bool {
    match (parse_version(current), parse_version(latest)) {
        (Some(c), Some(l)) => l > c,
        _ => false,
    }
}

/// Extract version string from GitHub release JSON.
fn extract_version(json: &serde_json::Value) -> Result<String> {
    let tag = json["tag_name"]
        .as_str()
        .context("Missing tag_name in release")?;

    let version = tag.strip_prefix('v').unwrap_or(tag);

    if parse_version(version).is_none() {
        anyhow::bail!("Invalid version format: {}", version);
    }

    Ok(version.to_string())
}

/// Fetch the latest release version from GitHub.
fn check_latest_version(agent: &ureq::Agent) -> Result<String> {
    let resp = agent
        .get("https://api.github.com/repos/erickochen/purple/releases/latest")
        .set("Accept", "application/vnd.github+json")
        .set("User-Agent", &format!("purple-ssh/{}", current_version()))
        .call()
        .context("Failed to fetch latest release. GitHub may be rate-limited.")?;

    let mut body = Vec::new();
    resp.into_reader()
        .take(1_048_576) // 1 MB limit for API response
        .read_to_end(&mut body)
        .context("Failed to read release JSON")?;

    let json: serde_json::Value =
        serde_json::from_slice(&body).context("Failed to parse release JSON")?;

    extract_version(&json)
}

/// TTL for version check cache (24 hours).
const VERSION_CHECK_TTL: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60);

/// Parse cache file content and determine if a newer version is available.
/// Returns `Some(Some(version))` if cache is fresh and a newer version exists,
/// `Some(None)` if cache is fresh and we are up-to-date,
/// `None` if cache content is corrupt, expired or unparseable.
fn parse_version_cache(content: &str, now_secs: u64, current: &str) -> Option<Option<String>> {
    let mut lines = content.lines();
    let timestamp: u64 = lines.next()?.parse().ok()?;
    let version = lines.next()?.to_string();

    if version.is_empty() || parse_version(&version).is_none() {
        return None; // Corrupt version string
    }

    if now_secs.saturating_sub(timestamp) > VERSION_CHECK_TTL.as_secs() {
        return None; // Cache expired
    }

    if is_newer(current, &version) {
        Some(Some(version))
    } else {
        Some(None) // Up-to-date, no API call needed
    }
}

/// Read cached version check result from ~/.purple/last_version_check.
/// Returns `Some(Some(version))` if cache is fresh and a newer version exists,
/// `Some(None)` if cache is fresh and we are up-to-date,
/// `None` if cache is missing, corrupt or expired.
fn read_cached_version() -> Option<Option<String>> {
    let path = dirs::home_dir()?.join(".purple").join("last_version_check");
    let content = std::fs::read_to_string(&path).ok()?;
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs();
    parse_version_cache(&content, now, current_version())
}

/// Write version check result to ~/.purple/last_version_check.
fn write_version_cache(version: &str) {
    let Some(dir) = dirs::home_dir().map(|h| h.join(".purple")) else {
        return;
    };
    let _ = std::fs::create_dir_all(&dir);
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let _ = std::fs::write(dir.join("last_version_check"), format!("{}\n{}\n", now, version));
}

/// Spawn a background thread to check for updates. Sends an event if a newer version exists.
/// Uses a local cache (~/.purple/last_version_check) with a 24h TTL to avoid unnecessary
/// GitHub API calls on frequent startup. Silently does nothing on any error.
pub fn spawn_version_check(tx: mpsc::Sender<AppEvent>) {
    let _ = std::thread::Builder::new()
        .name("version-check".to_string())
        .spawn(move || {
            // Check cache first — skip API call if fresh result exists
            match read_cached_version() {
                Some(Some(version)) => {
                    let _ = tx.send(AppEvent::UpdateAvailable { version });
                    return;
                }
                Some(None) => return, // Up-to-date, cache still fresh
                None => {}            // Cache missing or expired, fetch
            }

            // Short timeout: fire-and-forget background check,
            // don't tie up thread resources for 30s like the provider agent
            let agent = ureq::AgentBuilder::new()
                .timeout(std::time::Duration::from_secs(5))
                .build();

            if let Ok(latest) = check_latest_version(&agent) {
                write_version_cache(&latest);
                if is_newer(current_version(), &latest) {
                    let _ = tx.send(AppEvent::UpdateAvailable { version: latest });
                }
            }
        });
}

/// Format text as bold, respecting NO_COLOR.
fn bold(text: &str) -> String {
    if std::env::var_os("NO_COLOR").is_some() {
        text.to_string()
    } else {
        format!("\x1b[1m{}\x1b[0m", text)
    }
}

/// Format text as bold purple, respecting NO_COLOR.
fn bold_purple(text: &str) -> String {
    if std::env::var_os("NO_COLOR").is_some() {
        text.to_string()
    } else {
        format!("\x1b[1;35m{}\x1b[0m", text)
    }
}

/// Install method detected from binary path.
enum InstallMethod {
    Homebrew,
    Cargo,
    CurlOrManual,
}

/// Check if exe_path is under a Homebrew Cellar directory.
/// Validates that the Cellar path ends with a "Cellar" component and
/// that the binary sits in the expected .../Cellar/<formula>/.../ structure.
fn is_homebrew_path(exe_path: &Path, cellar: &Path) -> bool {
    // Cellar dir must end with "Cellar" component
    if cellar.file_name().and_then(|n| n.to_str()) != Some("Cellar") {
        return false;
    }
    // Path::starts_with is component-aware: /usr/local won't match /usr/local-bin
    if !exe_path.starts_with(cellar) {
        return false;
    }
    // Must have at least one component after Cellar (the formula name)
    exe_path
        .strip_prefix(cellar)
        .is_ok_and(|rest| rest.components().count() >= 1)
}

/// Check if exe_path's parent is exactly <cargo_home>/bin.
fn is_cargo_path(exe_path: &Path, cargo_home: &Path) -> bool {
    let cargo_bin = cargo_home.join("bin");
    exe_path.parent() == Some(cargo_bin.as_path())
}

/// Detect how purple was installed by checking the binary path against
/// known package manager directories. Uses Path::starts_with for
/// component-aware comparison (prevents /usr/local matching /usr/local-bin).
/// Env vars (HOMEBREW_CELLAR, HOMEBREW_PREFIX, CARGO_HOME) are treated
/// as hints and validated structurally before trusting. Falls back to
/// well-known default paths. Fails open to CurlOrManual when uncertain.
fn detect_install_method(exe_path: &Path) -> InstallMethod {
    // Homebrew: check HOMEBREW_CELLAR env var first (most specific),
    // then derive Cellar from HOMEBREW_PREFIX, then fall back to
    // well-known default Cellar locations
    if let Ok(cellar) = std::env::var("HOMEBREW_CELLAR") {
        if is_homebrew_path(exe_path, Path::new(&cellar)) {
            return InstallMethod::Homebrew;
        }
    }
    if let Ok(prefix) = std::env::var("HOMEBREW_PREFIX") {
        let cellar = std::path::PathBuf::from(&prefix).join("Cellar");
        if is_homebrew_path(exe_path, &cellar) {
            return InstallMethod::Homebrew;
        }
    }
    // Default Cellar locations (Apple Silicon + Intel)
    for cellar in ["/opt/homebrew/Cellar", "/usr/local/Cellar"] {
        if is_homebrew_path(exe_path, Path::new(cellar)) {
            return InstallMethod::Homebrew;
        }
    }

    // Cargo: check CARGO_HOME env var first, then check if parent
    // is a "bin" dir inside a ".cargo" dir (component-aware fallback)
    if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
        if is_cargo_path(exe_path, Path::new(&cargo_home)) {
            return InstallMethod::Cargo;
        }
    }
    if let Some(parent) = exe_path.parent() {
        if parent.file_name().and_then(|n| n.to_str()) == Some("bin") {
            if let Some(grandparent) = parent.parent() {
                if grandparent.file_name().and_then(|n| n.to_str()) == Some(".cargo") {
                    return InstallMethod::Cargo;
                }
            }
        }
    }

    InstallMethod::CurlOrManual
}

/// Detect the update command appropriate for how purple was installed.
pub fn update_hint() -> &'static str {
    if std::env::consts::OS != "macos" {
        return "cargo install purple-ssh";
    }
    if let Ok(exe) = std::env::current_exe() {
        let path = std::fs::canonicalize(&exe).unwrap_or(exe);
        return match detect_install_method(&path) {
            InstallMethod::Homebrew => "brew upgrade erickochen/purple/purple",
            InstallMethod::Cargo => "cargo install purple-ssh",
            InstallMethod::CurlOrManual => "purple update",
        };
    }
    "purple update"
}

/// Self-update the purple binary to the latest release.
pub fn self_update() -> Result<()> {
    // macOS only
    if std::env::consts::OS != "macos" {
        anyhow::bail!(
            "Self-update is available on macOS only.\n  \
             Update via: cargo install purple-ssh"
        );
    }

    println!("\n  {} updater\n", bold("purple."));

    // Resolve current binary path
    let exe_path = std::env::current_exe().context("Failed to detect binary path")?;
    let exe_path = std::fs::canonicalize(&exe_path).unwrap_or(exe_path);
    println!("  Binary: {}", exe_path.display());

    // Detect package manager installations
    match detect_install_method(&exe_path) {
        InstallMethod::Homebrew => {
            anyhow::bail!(
                "purple appears to be installed via Homebrew.\n  \
                 Update with: brew upgrade erickochen/purple/purple"
            );
        }
        InstallMethod::Cargo => {
            anyhow::bail!(
                "purple appears to be installed via cargo.\n  \
                 Update with: cargo install purple-ssh"
            );
        }
        InstallMethod::CurlOrManual => {}
    }

    // Fetch latest version (needs redirects for GitHub release asset downloads)
    print!("  Checking for updates... ");
    let agent = ureq::AgentBuilder::new()
        .timeout(std::time::Duration::from_secs(30))
        .build();
    let latest = check_latest_version(&agent)?;
    let current = current_version();

    if !is_newer(current, &latest) {
        println!("already on v{} (latest).", current);
        return Ok(());
    }

    println!("v{} available (current: v{}).", latest, current);

    // Detect target
    let target = match std::env::consts::ARCH {
        "aarch64" => "aarch64-apple-darwin",
        "x86_64" => "x86_64-apple-darwin",
        arch => anyhow::bail!("Unsupported architecture: {}", arch),
    };

    // Check we can write to the binary location
    let parent = exe_path
        .parent()
        .context("Binary has no parent directory")?;

    // Warn when running via sudo — creates root-owned cache files
    if std::env::var_os("SUDO_USER").is_some() {
        eprintln!(
            "  {} Running via sudo. Consider fixing directory permissions instead.",
            bold("!"),
        );
    }

    if !is_writable(parent) {
        anyhow::bail!(
            "No write permission to {}.\n  Check directory permissions or run with elevated privileges.",
            parent.display()
        );
    }

    // Clean up stale staged binaries from interrupted previous updates
    clean_stale_staged(parent);

    // Set up temp directory (create_dir fails if path exists, preventing symlink attacks)
    let tmp_dir = std::env::temp_dir().join(format!(
        "purple_update_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos()
    ));
    std::fs::create_dir(&tmp_dir).context("Failed to create temp directory")?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&tmp_dir, std::fs::Permissions::from_mode(0o700))
            .context("Failed to set temp directory permissions")?;
    }

    // Ensure cleanup on any exit path
    let _cleanup = TempCleanup(&tmp_dir);

    let tarball_name = format!("purple-{}-{}.tar.gz", latest, target);
    let base_url = format!(
        "https://github.com/erickochen/purple/releases/download/v{}",
        latest
    );

    // Download tarball
    print!("  Downloading v{}... ", latest);
    let tarball_path = tmp_dir.join(&tarball_name);
    download_file(
        &agent,
        &format!("{}/{}", base_url, tarball_name),
        &tarball_path,
    )?;

    // Download checksum
    let sha_path = tmp_dir.join(format!("{}.sha256", tarball_name));
    download_file(
        &agent,
        &format!("{}/{}.sha256", base_url, tarball_name),
        &sha_path,
    )?;
    println!("done.");

    // Verify checksum
    print!("  Verifying checksum... ");
    verify_checksum(&tarball_path, &sha_path)?;
    println!("ok.");

    // Extract
    print!("  Installing... ");
    let status = std::process::Command::new("tar")
        .arg("-xzf")
        .arg(&tarball_path)
        .arg("-C")
        .arg(&tmp_dir)
        .status()
        .context("Failed to run tar")?;
    if !status.success() {
        anyhow::bail!("tar extraction failed");
    }

    let new_binary = tmp_dir.join("purple");
    if !new_binary.exists() {
        anyhow::bail!("Binary not found in archive");
    }

    // Atomic replacement: stage new binary in the same directory via O_EXCL
    // (prevents symlink attacks), then rename over the target (atomic within
    // the same filesystem)
    let staged_path = parent.join(format!(".purple_new_{}", std::process::id()));
    {
        use std::io::Write;
        let source = std::fs::read(&new_binary).context("Failed to read new binary")?;
        let mut dest = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true) // O_EXCL: fails if path exists (prevents symlink following)
            .open(&staged_path)
            .context("Failed to create staged binary")?;
        dest.write_all(&source)
            .context("Failed to write staged binary")?;
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&staged_path, std::fs::Permissions::from_mode(0o755))
            .context("Failed to set permissions")?;
    }

    if let Err(e) = std::fs::rename(&staged_path, &exe_path) {
        // Clean up staged file on failure
        let _ = std::fs::remove_file(&staged_path);
        return Err(e).context("Failed to replace binary");
    }

    println!("done.");
    println!(
        "\n  {} installed at {}.\n",
        bold_purple(&format!("purple v{}", latest)),
        exe_path.display()
    );

    Ok(())
}

/// Download a file from a URL.
fn download_file(agent: &ureq::Agent, url: &str, dest: &Path) -> Result<()> {
    let resp = agent.get(url).call().with_context(|| {
        format!("Failed to download {}", url)
    })?;

    let mut bytes = Vec::new();
    resp.into_reader()
        .take(100 * 1024 * 1024) // 100 MB limit
        .read_to_end(&mut bytes)
        .context("Failed to read download")?;

    if bytes.is_empty() {
        anyhow::bail!("Empty response from {}", url);
    }

    std::fs::write(dest, bytes).context("Failed to write file")?;
    Ok(())
}

/// Verify SHA256 checksum of a file.
fn verify_checksum(file: &Path, sha_file: &Path) -> Result<()> {
    let expected = std::fs::read_to_string(sha_file)
        .context("Failed to read checksum file")?;
    let expected = expected
        .split_whitespace()
        .next()
        .context("Empty checksum file")?;

    let output = std::process::Command::new("shasum")
        .args(["-a", "256"])
        .arg(file)
        .output()
        .context("Failed to run shasum")?;

    if !output.status.success() {
        anyhow::bail!("shasum failed");
    }

    let actual = String::from_utf8_lossy(&output.stdout);
    let actual = actual
        .split_whitespace()
        .next()
        .context("Empty shasum output")?;

    if expected != actual {
        anyhow::bail!(
            "Checksum mismatch.\n    Expected: {}\n    Got:      {}",
            expected,
            actual
        );
    }

    Ok(())
}

/// Remove stale `.purple_new_*` files from previous interrupted updates.
fn clean_stale_staged(dir: &Path) {
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            if let Some(name) = entry.file_name().to_str() {
                if name.starts_with(".purple_new_") {
                    let _ = std::fs::remove_file(entry.path());
                }
            }
        }
    }
}

/// Check if a directory is writable.
fn is_writable(path: &Path) -> bool {
    let probe = path.join(format!(".purple_write_test_{}", std::process::id()));
    if std::fs::File::create(&probe).is_ok() {
        let _ = std::fs::remove_file(&probe);
        true
    } else {
        false
    }
}

/// RAII guard that removes a temp directory on drop.
struct TempCleanup<'a>(&'a Path);

impl Drop for TempCleanup<'_> {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(self.0);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_version() {
        assert_eq!(parse_version("1.5.0"), Some((1, 5, 0)));
        assert_eq!(parse_version("0.1.2"), Some((0, 1, 2)));
        assert_eq!(parse_version("10.20.30"), Some((10, 20, 30)));
    }

    #[test]
    fn test_parse_version_invalid() {
        assert_eq!(parse_version(""), None);
        assert_eq!(parse_version("1.2"), None);
        assert_eq!(parse_version("abc"), None);
        assert_eq!(parse_version("1.2.x"), None);
        assert_eq!(parse_version("1.5.0-rc1"), None);
    }

    #[test]
    fn test_is_newer_patch() {
        assert!(is_newer("1.5.0", "1.5.1"));
        assert!(!is_newer("1.5.1", "1.5.0"));
    }

    #[test]
    fn test_is_newer_minor() {
        assert!(is_newer("1.5.0", "1.6.0"));
        assert!(!is_newer("1.6.0", "1.5.0"));
    }

    #[test]
    fn test_is_newer_major() {
        assert!(is_newer("1.5.0", "2.0.0"));
        assert!(!is_newer("2.0.0", "1.5.0"));
    }

    #[test]
    fn test_is_newer_equal() {
        assert!(!is_newer("1.5.0", "1.5.0"));
    }

    #[test]
    fn test_is_newer_invalid() {
        assert!(!is_newer("1.5.0", "bad"));
        assert!(!is_newer("bad", "1.5.0"));
    }

    #[test]
    fn test_extract_version_with_v_prefix() {
        let json = serde_json::json!({"tag_name": "v1.6.0"});
        assert_eq!(extract_version(&json).unwrap(), "1.6.0");
    }

    #[test]
    fn test_extract_version_without_prefix() {
        let json = serde_json::json!({"tag_name": "1.6.0"});
        assert_eq!(extract_version(&json).unwrap(), "1.6.0");
    }

    #[test]
    fn test_extract_version_missing_tag() {
        let json = serde_json::json!({"name": "Release"});
        assert!(extract_version(&json).is_err());
    }

    #[test]
    fn test_extract_version_invalid_format() {
        let json = serde_json::json!({"tag_name": "v1.2.3-rc1"});
        assert!(extract_version(&json).is_err());
    }

    #[test]
    fn test_current_version_is_valid() {
        assert!(parse_version(current_version()).is_some());
    }

    // --- is_homebrew_path tests ---

    #[test]
    fn test_homebrew_cellar_apple_silicon() {
        let path = Path::new("/opt/homebrew/Cellar/purple/1.5.0/bin/purple");
        assert!(is_homebrew_path(path, Path::new("/opt/homebrew/Cellar")));
    }

    #[test]
    fn test_homebrew_cellar_intel() {
        let path = Path::new("/usr/local/Cellar/purple/1.5.0/bin/purple");
        assert!(is_homebrew_path(path, Path::new("/usr/local/Cellar")));
    }

    #[test]
    fn test_homebrew_cellar_rejects_non_cellar_suffix() {
        // Env var points to a dir that doesn't end in "Cellar"
        let path = Path::new("/opt/homebrew/lib/purple");
        assert!(!is_homebrew_path(path, Path::new("/opt/homebrew/lib")));
    }

    #[test]
    fn test_homebrew_cellar_rejects_bare_cellar() {
        // Binary directly inside Cellar with no formula subdirectory
        let path = Path::new("/opt/homebrew/Cellar");
        assert!(!is_homebrew_path(path, Path::new("/opt/homebrew/Cellar")));
    }

    #[test]
    fn test_homebrew_cellar_rejects_prefix_overlap() {
        // /usr/local/Cellar-custom is not /usr/local/Cellar
        // Path::starts_with is component-aware so this must not match
        let path = Path::new("/usr/local/Cellar-custom/purple/bin/purple");
        assert!(!is_homebrew_path(path, Path::new("/usr/local/Cellar")));
    }

    // --- is_cargo_path tests ---

    #[test]
    fn test_cargo_default_path() {
        let path = Path::new("/Users/user/.cargo/bin/purple");
        assert!(is_cargo_path(path, Path::new("/Users/user/.cargo")));
    }

    #[test]
    fn test_cargo_custom_home() {
        let path = Path::new("/data/rust/cargo/bin/purple");
        assert!(is_cargo_path(path, Path::new("/data/rust/cargo")));
    }

    #[test]
    fn test_cargo_rejects_nested_bin() {
        // Binary in a subdir of bin — not a direct cargo install
        let path = Path::new("/Users/user/.cargo/bin/subdir/purple");
        assert!(!is_cargo_path(path, Path::new("/Users/user/.cargo")));
    }

    #[test]
    fn test_cargo_rejects_prefix_overlap() {
        // /.cargo-custom/bin is not /.cargo/bin
        let path = Path::new("/Users/user/.cargo-custom/bin/purple");
        assert!(!is_cargo_path(path, Path::new("/Users/user/.cargo")));
    }

    // --- detect_install_method tests (path-only, no env vars) ---

    #[test]
    fn test_detect_homebrew_cellar() {
        let path = Path::new("/opt/homebrew/Cellar/purple/1.5.0/bin/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::Homebrew));
    }

    #[test]
    fn test_detect_homebrew_default_intel() {
        let path = Path::new("/usr/local/Cellar/purple/1.5.0/bin/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::Homebrew));
    }

    #[test]
    fn test_detect_cargo_default() {
        let path = Path::new("/Users/user/.cargo/bin/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::Cargo));
    }

    #[test]
    fn test_detect_curl_usr_local_bin() {
        let path = Path::new("/usr/local/bin/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::CurlOrManual));
    }

    #[test]
    fn test_detect_curl_local_bin() {
        let path = Path::new("/Users/user/.local/bin/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::CurlOrManual));
    }

    #[test]
    fn test_detect_no_false_positive_homebrew_in_name() {
        let path = Path::new("/Users/user/homebrew-tools/bin/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::CurlOrManual));
    }

    // --- fail-open: ambiguous paths default to CurlOrManual ---

    #[test]
    fn test_detect_unknown_path() {
        let path = Path::new("/some/random/path/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::CurlOrManual));
    }

    #[test]
    fn test_detect_root_path() {
        let path = Path::new("/purple");
        assert!(matches!(detect_install_method(path), InstallMethod::CurlOrManual));
    }

    // --- parse_version_cache tests ---

    fn now_secs() -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }

    #[test]
    fn test_cache_fresh_newer_version() {
        let now = now_secs();
        let content = format!("{}\n99.0.0\n", now);
        // 99.0.0 is newer than any current version
        assert_eq!(
            parse_version_cache(&content, now, "1.5.0"),
            Some(Some("99.0.0".to_string()))
        );
    }

    #[test]
    fn test_cache_fresh_up_to_date() {
        let now = now_secs();
        let content = format!("{}\n1.5.0\n", now);
        // Same version: up-to-date
        assert_eq!(parse_version_cache(&content, now, "1.5.0"), Some(None));
    }

    #[test]
    fn test_cache_fresh_older_version() {
        let now = now_secs();
        let content = format!("{}\n1.0.0\n", now);
        // Cached version is older than current: up-to-date
        assert_eq!(parse_version_cache(&content, now, "1.5.0"), Some(None));
    }

    #[test]
    fn test_cache_expired() {
        let now = now_secs();
        let old = now - VERSION_CHECK_TTL.as_secs() - 1;
        let content = format!("{}\n99.0.0\n", old);
        assert_eq!(parse_version_cache(&content, now, "1.5.0"), None);
    }

    #[test]
    fn test_cache_exactly_at_ttl() {
        let now = now_secs();
        let at_ttl = now - VERSION_CHECK_TTL.as_secs();
        let content = format!("{}\n99.0.0\n", at_ttl);
        // At exactly TTL boundary: still valid (saturating_sub > TTL, not >=)
        assert_eq!(
            parse_version_cache(&content, now, "1.5.0"),
            Some(Some("99.0.0".to_string()))
        );
    }

    #[test]
    fn test_cache_empty_content() {
        assert_eq!(parse_version_cache("", now_secs(), "1.5.0"), None);
    }

    #[test]
    fn test_cache_missing_version_line() {
        let content = format!("{}\n", now_secs());
        assert_eq!(parse_version_cache(&content, now_secs(), "1.5.0"), None);
    }

    #[test]
    fn test_cache_non_numeric_timestamp() {
        assert_eq!(
            parse_version_cache("abc\n99.0.0\n", now_secs(), "1.5.0"),
            None
        );
    }

    #[test]
    fn test_cache_invalid_version_format() {
        let now = now_secs();
        let content = format!("{}\nnot-a-version\n", now);
        assert_eq!(parse_version_cache(&content, now, "1.5.0"), None);
    }

    #[test]
    fn test_cache_empty_version() {
        let now = now_secs();
        // Second line is empty
        let content = format!("{}\n\n", now);
        assert_eq!(parse_version_cache(&content, now, "1.5.0"), None);
    }

    #[test]
    fn test_cache_only_timestamp() {
        let content = format!("{}", now_secs());
        assert_eq!(parse_version_cache(&content, now_secs(), "1.5.0"), None);
    }

    #[test]
    fn test_cache_garbage() {
        assert_eq!(parse_version_cache("garbage", now_secs(), "1.5.0"), None);
    }
}