dalo 1.11.1

Your team's agent skills, versioned like code. Review, pin, and sync skills, instructions, and hooks into every agent.
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
//! Passive version checks through the shared app-tracking client, with Dalo-owned guidance.

use std::env;
use std::ffi::OsStr;
use std::fs;
use std::io::{IsTerminal, Write};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use app_tracking::{Mode, Tool, UpdateClient};

// Public, update-only service origin. The request carries no installed version or identity.
const VERSION_ORIGIN: &str = "https://sebastian-app-tracking.b-cdn.net";
const INSTALL_RECEIPT: &str = ".dalo-install-channel";

/// Check and print an advisory notice after an eligible successful command.
/// The shared client completes a due request synchronously with a 200 ms network
/// deadline. It owns hourly caching, bounded retry backoff, and notice throttling.
pub fn print_notice() {
    if !update_checks_enabled() {
        return;
    }
    let Some(cache_dir) = update_notice_cache_dir() else {
        return;
    };
    let executable = env::current_exe().ok();
    let channel = detect_install_channel(executable.as_deref());
    if let Some(version) = check_notice_with_metrics(
        VERSION_ORIGIN,
        &cache_dir,
        env!("CARGO_PKG_VERSION"),
        Mode::Enabled,
        !env_truthy("DO_NOT_TRACK"),
    ) {
        let _ = writeln!(
            std::io::stderr(),
            "\n{}",
            render_notice(
                &version,
                env!("CARGO_PKG_VERSION"),
                channel,
                executable.as_deref()
            )
        );
    }
}

fn check_notice_with_metrics(
    origin: &str,
    cache_dir: &Path,
    installed: &str,
    mode: Mode,
    service_metrics: bool,
) -> Option<String> {
    let tool = Tool::new("dalo", installed, "github", "stable").ok()?;
    UpdateClient::new(origin, cache_dir, tool)
        .ok()?
        .with_service_metrics(service_metrics)
        .check(mode)
        .ok()
        .flatten()
        .map(|update| update.latest_version)
}

fn render_notice(
    latest_version: &str,
    installed_version: &str,
    channel: InstallChannel,
    executable: Option<&Path>,
) -> String {
    let mut notice = format!(
        "update available: dalo v{latest_version} (installed v{installed_version} via {})",
        channel.label()
    );
    if let Some(command) = channel.upgrade_command(executable) {
        notice.push_str(&format!("\nupgrade with: {command}"));
    } else {
        let guide = if channel == InstallChannel::DebianPackage {
            "https://dalo.sh/install.md#debian-packages"
        } else {
            "https://dalo.sh/install.md"
        };
        notice.push_str(&format!("\nupgrade guide: {guide}"));
    }
    notice
}

fn update_checks_enabled() -> bool {
    update_checks_enabled_for(
        env::var("DALO_UPDATE_CHECK").ok().as_deref(),
        std::io::stderr().is_terminal(),
        env_truthy("CI"),
        env_truthy("DALO_OFFLINE"),
    )
}

fn update_checks_enabled_for(
    setting: Option<&str>,
    stderr_is_terminal: bool,
    ci: bool,
    offline: bool,
) -> bool {
    let disabled = setting.is_some_and(|value| {
        matches!(
            value.to_ascii_lowercase().as_str(),
            "never" | "off" | "false" | "0"
        )
    });
    !disabled && stderr_is_terminal && !ci && !offline
}

fn env_truthy(name: &str) -> bool {
    env::var(name)
        .map(|value| {
            !matches!(
                value.to_ascii_lowercase().as_str(),
                "" | "0" | "false" | "off"
            )
        })
        .unwrap_or(false)
}

fn update_notice_cache_dir() -> Option<PathBuf> {
    env::var_os("XDG_CACHE_HOME")
        .filter(|path| Path::new(path).is_absolute())
        .map(PathBuf::from)
        .or_else(|| env::var_os("HOME").map(|home| PathBuf::from(home).join(".cache")))
        .map(|cache| cache.join("dalo/update-notices/app-tracking-v1"))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InstallChannel {
    DebianPackage,
    Homebrew,
    Npm,
    Npx,
    Mise,
    MiseUbi,
    Nix,
    Cargo,
    Standalone,
    Unknown,
}

impl InstallChannel {
    fn label(self) -> &'static str {
        match self {
            Self::DebianPackage => "Debian package",
            Self::Homebrew => "Homebrew",
            Self::Npm => "npm",
            Self::Npx => "npx",
            Self::Mise | Self::MiseUbi => "mise",
            Self::Nix => "Nix",
            Self::Cargo => "Cargo",
            Self::Standalone => "the hosted installer",
            Self::Unknown => "an unknown installation method",
        }
    }

    fn upgrade_command(self, executable: Option<&Path>) -> Option<String> {
        match self {
            Self::DebianPackage => None,
            Self::Homebrew => Some("brew upgrade sebastian-software/tap/dalo".to_owned()),
            Self::Npm => Some("npm install --global getdalo@latest".to_owned()),
            Self::Npx => Some("npx getdalo@latest".to_owned()),
            Self::Mise => Some("mise upgrade github:sebastian-software/dalo".to_owned()),
            Self::MiseUbi => Some("mise upgrade ubi:sebastian-software/dalo".to_owned()),
            Self::Nix => Some("nix profile upgrade dalo".to_owned()),
            Self::Cargo => Some(cargo_upgrade_command()),
            Self::Standalone => standalone_upgrade_command(executable),
            Self::Unknown => None,
        }
    }
}

fn detect_install_channel(executable: Option<&Path>) -> InstallChannel {
    detect_install_channel_from_with_debian_marker(
        env::var("DALO_INSTALL_CHANNEL").ok().as_deref(),
        executable,
        env::var_os("HOME").as_deref().map(Path::new),
        env::var_os("CARGO_HOME").as_deref().map(Path::new),
        Some(Path::new("/usr/share/dalo/.dalo-install-channel")),
    )
}

#[cfg(test)]
fn detect_install_channel_from(
    explicit: Option<&str>,
    executable: Option<&Path>,
    home: Option<&Path>,
    cargo_home: Option<&Path>,
) -> InstallChannel {
    detect_install_channel_from_with_debian_marker(explicit, executable, home, cargo_home, None)
}

fn detect_install_channel_from_with_debian_marker(
    explicit: Option<&str>,
    executable: Option<&Path>,
    home: Option<&Path>,
    cargo_home: Option<&Path>,
    debian_marker: Option<&Path>,
) -> InstallChannel {
    if let Some(channel) = explicit.and_then(parse_install_channel) {
        return channel;
    }

    let Some(executable) = executable else {
        return InstallChannel::Unknown;
    };
    let components = executable
        .components()
        .map(|part| part.as_os_str())
        .collect::<Vec<_>>();

    if components
        .windows(2)
        .any(|parts| parts[0] == OsStr::new("Cellar") && parts[1] == OsStr::new("dalo"))
    {
        return InstallChannel::Homebrew;
    }

    if is_debian_package(executable, debian_marker) {
        return InstallChannel::DebianPackage;
    }

    if let Some(install_id) = components.windows(3).find_map(|parts| {
        (parts[0] == OsStr::new("mise") && parts[1] == OsStr::new("installs"))
            .then(|| parts[2].to_string_lossy().to_ascii_lowercase())
    }) {
        return if install_id == "ubi" || install_id.starts_with("ubi-") {
            InstallChannel::MiseUbi
        } else {
            InstallChannel::Mise
        };
    }

    let cargo_root = cargo_home
        .map(Path::to_path_buf)
        .or_else(|| home.map(|home| home.join(".cargo")));
    if cargo_root
        .as_deref()
        .is_some_and(|root| executable == root.join("bin/dalo"))
        || executable
            .parent()
            .and_then(Path::parent)
            .is_some_and(|root| root.join(".crates2.json").is_file())
    {
        return InstallChannel::Cargo;
    }

    if let Some(channel) = read_install_receipt(executable) {
        return channel;
    }

    InstallChannel::Unknown
}

fn parse_install_channel(channel: &str) -> Option<InstallChannel> {
    match channel.to_ascii_lowercase().as_str() {
        "deb" | "debian" | "apt" => Some(InstallChannel::DebianPackage),
        "homebrew" | "brew" => Some(InstallChannel::Homebrew),
        "npm" => Some(InstallChannel::Npm),
        "npx" => Some(InstallChannel::Npx),
        "mise" | "mise-github" => Some(InstallChannel::Mise),
        "mise-ubi" => Some(InstallChannel::MiseUbi),
        "nix" => Some(InstallChannel::Nix),
        "cargo" | "cargo-binstall" => Some(InstallChannel::Cargo),
        "standalone" | "installer" => Some(InstallChannel::Standalone),
        _ => None,
    }
}

fn read_install_receipt(executable: &Path) -> Option<InstallChannel> {
    executable
        .parent()
        .and_then(|parent| fs::read_to_string(parent.join(INSTALL_RECEIPT)).ok())
        .and_then(|receipt| match receipt.trim() {
            "standalone" => Some(InstallChannel::Standalone),
            "nix" => Some(InstallChannel::Nix),
            _ => None,
        })
}

fn has_debian_install_marker(path: &Path) -> bool {
    fs::read_to_string(path).is_ok_and(|receipt| receipt.trim() == "debian")
}

fn is_debian_package(executable: &Path, marker: Option<&Path>) -> bool {
    executable == Path::new("/usr/bin/dalo") && marker.is_some_and(has_debian_install_marker)
}

fn cargo_upgrade_command() -> String {
    if command_exists("cargo-binstall") {
        "cargo binstall dalo".to_owned()
    } else {
        "cargo install dalo --locked --force".to_owned()
    }
}

fn command_exists(program: &str) -> bool {
    env::var_os("PATH").is_some_and(|path| {
        env::split_paths(&path).any(|directory| is_executable_file(&directory.join(program)))
    })
}

#[cfg(unix)]
fn is_executable_file(path: &Path) -> bool {
    fs::metadata(path)
        .is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
}

#[cfg(not(unix))]
fn is_executable_file(path: &Path) -> bool {
    path.is_file()
}

fn standalone_upgrade_command(executable: Option<&Path>) -> Option<String> {
    let install_dir = executable?.parent()?;
    let default_dir = env::var_os("HOME")
        .map(PathBuf::from)
        .map(|home| home.join(".local/bin"));
    if default_dir.as_deref() == Some(install_dir) {
        return Some("curl -fsSL https://dalo.sh/install.sh | sh".to_owned());
    }

    Some(format!(
        "curl -fsSL https://dalo.sh/install.sh | DALO_INSTALL_DIR={} sh",
        shell_quote(install_dir)
    ))
}

fn shell_quote(path: &Path) -> String {
    let value = path.to_string_lossy();
    format!("'{}'", value.replace('\'', "'\"'\"'"))
}

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

    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::mpsc;
    use std::thread;
    use std::time::{Duration, Instant};
    use time::{OffsetDateTime, format_description::well_known::Rfc3339};

    fn check_notice_in(
        origin: &str,
        cache_dir: &Path,
        installed: &str,
        mode: Mode,
    ) -> Option<String> {
        check_notice_with_metrics(origin, cache_dir, installed, mode, true)
    }

    fn payload(distribution: &str, age: i64, stale: bool) -> String {
        let checked = OffsetDateTime::now_utc() - time::Duration::seconds(age);
        serde_json::json!({
            "schema_version": 1, "tool_id": "dalo", "distribution": distribution,
            "channel": "stable", "latest_version": "99.0.0",
            "published_at": (checked - time::Duration::days(1)).format(&Rfc3339).unwrap(),
            "checked_at": checked.format(&Rfc3339).unwrap(),
            "release_url": "https://example.com/releases/99.0.0", "stale": stale,
        })
        .to_string()
    }

    fn endpoint(
        status: u16,
        body: String,
        delay: Duration,
    ) -> (String, mpsc::Receiver<String>, thread::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let origin = format!("http://{}", listener.local_addr().unwrap());
        let (tx, rx) = mpsc::channel();
        let task = thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            stream
                .set_read_timeout(Some(Duration::from_secs(2)))
                .unwrap();
            let mut request = Vec::new();
            let mut byte = [0];
            while !request.ends_with(b"\r\n\r\n") {
                stream.read_exact(&mut byte).unwrap();
                request.push(byte[0]);
            }
            let _ = tx.send(String::from_utf8(request).unwrap());
            thread::sleep(delay);
            let response = format!(
                "HTTP/1.1 {status} Test\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            );
            let _ = stream.write_all(response.as_bytes());
        });
        (origin, rx, task)
    }

    #[test]
    fn update_policy_preserves_interactive_online_non_ci_eligibility() {
        assert!(update_checks_enabled_for(None, true, false, false));
        for setting in ["never", "NEVER", "off", "FALSE", "0"] {
            assert!(!update_checks_enabled_for(
                Some(setting),
                true,
                false,
                false
            ));
        }
        assert!(!update_checks_enabled_for(None, false, false, false));
        assert!(!update_checks_enabled_for(None, true, true, false));
        assert!(!update_checks_enabled_for(None, true, false, true));
    }

    #[test]
    fn all_installations_share_the_release_feed_and_keep_host_guidance() {
        for (channel, guidance) in [
            (InstallChannel::Npm, "npm install --global getdalo@latest"),
            (InstallChannel::Npx, "npx getdalo@latest"),
            (
                InstallChannel::Mise,
                "mise upgrade github:sebastian-software/dalo",
            ),
            (
                InstallChannel::MiseUbi,
                "mise upgrade ubi:sebastian-software/dalo",
            ),
            (
                InstallChannel::Homebrew,
                "brew upgrade sebastian-software/tap/dalo",
            ),
            (InstallChannel::Cargo, "cargo install dalo --locked"),
            (InstallChannel::Nix, "nix profile upgrade"),
            (InstallChannel::DebianPackage, "https://dalo.sh/install.md"),
            (InstallChannel::Standalone, "https://dalo.sh/install.md"),
            (InstallChannel::Unknown, "https://dalo.sh/install.md"),
        ] {
            let temp = tempdir().unwrap();
            let (origin, request, task) =
                endpoint(200, payload("github", 0, false), Duration::ZERO);
            let latest = check_notice_in(&origin, temp.path(), "1.0.0", Mode::Enabled).unwrap();
            assert_eq!(latest, "99.0.0");
            assert!(render_notice(&latest, "1.0.0", channel, None).contains(guidance));
            let request = request.recv().unwrap();
            assert!(request.starts_with("GET /v1/versions/dalo/github/stable HTTP/1.1\r\n"));
            assert!(!request.contains("1.0.0"));
            assert!(!request.to_ascii_lowercase().contains("authorization:"));
            task.join().unwrap();
            // The now-closed endpoint cannot receive a second request; installation
            // detection does not partition the shared cache or notice throttle.
            assert!(check_notice_in(&origin, temp.path(), "1.0.0", Mode::Enabled).is_none());
        }
    }

    #[test]
    fn service_metrics_opt_out_preserves_update_checks_without_usage_state() {
        for enabled in [true, false] {
            let temp = tempdir().unwrap();
            let (origin, request, task) =
                endpoint(200, payload("github", 0, false), Duration::ZERO);
            assert_eq!(
                check_notice_with_metrics(&origin, temp.path(), "1.0.0", Mode::Enabled, enabled)
                    .as_deref(),
                Some("99.0.0")
            );
            let request = request.recv().unwrap().to_ascii_lowercase();
            assert_eq!(request.contains("dnt: 1\r\n"), !enabled);
            assert!(request.starts_with("get /v1/versions/dalo/github/stable http/1.1\r\n"));
            assert!(!request.contains("1.0.0"));
            assert!(!request.contains("authorization:"));
            task.join().unwrap();
            let mut files = fs::read_dir(temp.path())
                .unwrap()
                .map(|entry| entry.unwrap().file_name())
                .collect::<Vec<_>>();
            files.sort();
            assert_eq!(
                files,
                ["dalo.github.stable.json", "dalo.github.stable.lock"]
            );
        }
    }

    #[test]
    fn sdk_upgrade_preserves_the_v1_update_cache_and_notice_throttle() {
        let temp = tempdir().unwrap();
        let origin = "http://127.0.0.1:1";
        let now = OffsetDateTime::now_utc().unix_timestamp();
        // This is the serialized state schema emitted by app-tracking 0.1.0.
        let state = serde_json::json!({
            "schema_version": 1,
            "resource": format!("{origin}/v1/versions/dalo/github/stable"),
            "next_check": now + 3600, "failures": 0,
            "release": serde_json::from_str::<serde_json::Value>(&payload("github", 0, false)).unwrap(),
            "etag": "\"cached\"", "noticed_version": "99.0.0", "noticed_at": now,
        });
        let file = temp.path().join("dalo.github.stable.json");
        fs::write(&file, serde_json::to_vec(&state).unwrap()).unwrap();
        assert!(check_notice_in(origin, temp.path(), "1.0.0", Mode::Enabled).is_none());
        assert_eq!(
            serde_json::from_slice::<serde_json::Value>(&fs::read(file).unwrap()).unwrap(),
            state
        );
    }

    #[test]
    fn disabled_and_offline_checks_create_no_update_state() {
        let temp = tempdir().unwrap();
        let absent = temp.path().join("absent");
        for mode in [Mode::Disabled, Mode::Offline] {
            assert!(check_notice_in("http://127.0.0.1:1", &absent, "1.0.0", mode).is_none());
        }
        assert!(!absent.exists());
    }

    #[test]
    fn stale_data_and_network_failures_remain_advisory_and_bounded() {
        let temp = tempdir().unwrap();
        let (origin, _, task) = endpoint(200, payload("github", 7200, true), Duration::ZERO);
        assert_eq!(
            check_notice_in(&origin, temp.path(), "1.0.0", Mode::Enabled).as_deref(),
            Some("99.0.0")
        );
        task.join().unwrap();
        let state: serde_json::Value =
            serde_json::from_slice(&fs::read(temp.path().join("dalo.github.stable.json")).unwrap())
                .unwrap();
        assert_eq!(state["failures"], 1);
        for (status, body, delay) in [
            (503, "not JSON".to_owned(), Duration::ZERO),
            (200, payload("github", 86400, true), Duration::ZERO),
            (200, payload("github", 0, false), Duration::from_millis(400)),
        ] {
            let temp = tempdir().unwrap();
            let (origin, _, task) = endpoint(status, body, delay);
            let start = Instant::now();
            assert!(check_notice_in(&origin, temp.path(), "1.0.0", Mode::Enabled).is_none());
            assert!(start.elapsed() < Duration::from_secs(2));
            task.join().unwrap();
            assert!(check_notice_in(&origin, temp.path(), "1.0.0", Mode::Enabled).is_none());
        }
    }

    #[test]
    fn legacy_records_are_left_untouched_and_do_not_drive_the_new_client() {
        let temp = tempdir().unwrap();
        let legacy = temp.path().join("last-check");
        fs::write(
            &legacy,
            "schema=1\nstate=complete\ninstalled=1.0.0\nlatest=100.0.0\n",
        )
        .unwrap();
        let (origin, _, task) = endpoint(200, payload("github", 0, false), Duration::ZERO);
        assert_eq!(
            check_notice_in(
                &origin,
                &temp.path().join("app-tracking-v1"),
                "1.0.0",
                Mode::Enabled
            )
            .as_deref(),
            Some("99.0.0")
        );
        task.join().unwrap();
        assert!(
            fs::read_to_string(legacy)
                .unwrap()
                .contains("latest=100.0.0")
        );
    }

    #[test]
    #[ignore = "opt-in read-only smoke against the public Bunny version service"]
    fn live_bunny_version_client_smoke() {
        for channel in [
            InstallChannel::Npm,
            InstallChannel::Homebrew,
            InstallChannel::Standalone,
        ] {
            let distribution = "github";
            // Warm public metadata before exercising the CLI's stricter deadline.
            // No service state is changed and no fake release is published.
            let preflight: ureq::Agent = ureq::Agent::config_builder()
                .timeout_global(Some(Duration::from_secs(5)))
                .build()
                .into();
            let response = preflight
                .get(format!(
                    "{VERSION_ORIGIN}/v1/versions/dalo/{distribution}/stable"
                ))
                .call()
                .unwrap();
            assert_eq!(response.status(), 200);
            let temp = tempdir().unwrap();
            let latest = check_notice_in(
                VERSION_ORIGIN,
                temp.path(),
                "0.0.0",
                Mode::Enabled,
            )
            .expect(
                "live metadata must identify a newer release for the synthetic installed version",
            );
            assert_ne!(latest, "0.0.0");
            let notice = render_notice(
                &latest,
                "0.0.0",
                channel,
                Some(Path::new("/tmp/dalo-smoke/bin/dalo")),
            );
            assert!(notice.contains("update available: dalo v"));
            assert!(notice.contains("upgrade with:"));
            println!("Bunny {distribution}: version comparison and host guidance passed");
        }
    }

    #[test]
    fn explicit_launcher_channel_should_win_over_path_detection() {
        let channel = detect_install_channel_from(
            Some("npx"),
            Some(Path::new("/home/user/.cache/dalo/1.2.3/dalo")),
            Some(Path::new("/home/user")),
            None,
        );

        assert_eq!(channel, InstallChannel::Npx);
        assert_eq!(
            channel.upgrade_command(None).as_deref(),
            Some("npx getdalo@latest")
        );
    }

    #[test]
    fn managed_install_paths_should_be_detected_conservatively() {
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new("/opt/homebrew/Cellar/dalo/1.2.3/bin/dalo")),
                Some(Path::new("/Users/user")),
                None,
            ),
            InstallChannel::Homebrew
        );
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new(
                    "/home/rubicon/.local/share/mise/installs/github-sebastian-software-dalo/latest/bin/dalo"
                )),
                Some(Path::new("/home/rubicon")),
                None,
            ),
            InstallChannel::Mise
        );
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new(
                    "/home/user/.local/share/mise/installs/ubi-sebastian-software-dalo/latest/bin/dalo"
                )),
                Some(Path::new("/home/user")),
                None,
            ),
            InstallChannel::MiseUbi
        );
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new("/home/user/.cargo/bin/dalo")),
                Some(Path::new("/home/user")),
                None,
            ),
            InstallChannel::Cargo
        );
    }

    #[test]
    fn only_a_marked_system_binary_should_be_detected_as_a_debian_package() {
        let temp = tempdir().expect("tempdir");
        let marker = temp.path().join(".dalo-install-channel");
        fs::write(&marker, "debian\n").expect("write Debian marker");

        assert!(is_debian_package(Path::new("/usr/bin/dalo"), Some(&marker)));
        assert!(!is_debian_package(Path::new("/usr/bin/dalo"), None));
        assert!(!is_debian_package(
            Path::new("/usr/local/bin/dalo"),
            Some(&marker)
        ));
        assert!(!is_debian_package(
            Path::new("/usr/bin/dalo"),
            Some(&temp.path().join("missing-marker"))
        ));
    }

    #[test]
    fn debian_install_marker_should_be_required_and_recognized() {
        let temp = tempdir().expect("tempdir");
        let marker = temp.path().join(".dalo-install-channel");

        assert!(!has_debian_install_marker(&marker));
        fs::write(&marker, "debian\n").expect("write Debian marker");
        assert!(has_debian_install_marker(&marker));
        fs::write(&marker, "standalone\n").expect("replace marker");
        assert!(!has_debian_install_marker(&marker));
    }

    #[test]
    fn installer_receipt_should_preserve_custom_install_directory() {
        let temp = tempdir().expect("tempdir");
        let executable = temp.path().join("custom bin/dalo");
        fs::create_dir_all(executable.parent().expect("parent")).expect("create bin dir");
        fs::write(
            executable.parent().expect("parent").join(INSTALL_RECEIPT),
            "standalone\n",
        )
        .expect("write receipt");

        let channel = detect_install_channel_from(None, Some(&executable), None, None);
        assert_eq!(channel, InstallChannel::Standalone);
        let expected = format!(
            "curl -fsSL https://dalo.sh/install.sh | DALO_INSTALL_DIR={} sh",
            shell_quote(executable.parent().expect("parent"))
        );
        assert_eq!(channel.upgrade_command(Some(&executable)), Some(expected));
    }

    #[test]
    fn nix_receipt_should_select_nix_profile_upgrade() {
        let temp = tempdir().expect("tempdir");
        let executable = temp.path().join("bin/.dalo-wrapped");
        fs::create_dir_all(executable.parent().expect("parent")).expect("create bin dir");
        fs::write(
            executable.parent().expect("parent").join(INSTALL_RECEIPT),
            "nix\n",
        )
        .expect("write receipt");

        let channel = detect_install_channel_from(None, Some(&executable), None, None);
        assert_eq!(channel, InstallChannel::Nix);
        assert_eq!(
            channel.upgrade_command(Some(&executable)).as_deref(),
            Some("nix profile upgrade dalo")
        );
    }

    #[test]
    fn bare_default_install_path_should_not_guess_standalone() {
        let channel = detect_install_channel_from(
            None,
            Some(Path::new("/home/user/.local/bin/dalo")),
            Some(Path::new("/home/user")),
            None,
        );

        assert_eq!(channel, InstallChannel::Unknown);
        assert_eq!(channel.upgrade_command(None), None);
    }

    #[test]
    fn unknown_installation_should_not_guess_an_upgrade_command() {
        let temp = tempdir().expect("tempdir");
        let executable = temp.path().join("bin/dalo");
        fs::create_dir_all(executable.parent().expect("parent")).expect("create bin dir");

        let channel = detect_install_channel_from(
            Some("not-a-channel"),
            Some(&executable),
            Some(temp.path()),
            None,
        );

        assert_eq!(channel, InstallChannel::Unknown);
        assert_eq!(channel.upgrade_command(None), None);
    }

    #[test]
    fn notice_should_include_channel_specific_or_generic_guidance() {
        let latest_version = "1.2.4";

        assert_eq!(
            render_notice(latest_version, "1.2.3", InstallChannel::Homebrew, None,),
            "update available: dalo v1.2.4 (installed v1.2.3 via Homebrew)\n\
             upgrade with: brew upgrade sebastian-software/tap/dalo"
        );
        assert_eq!(
            render_notice(latest_version, "1.2.3", InstallChannel::Unknown, None,),
            "update available: dalo v1.2.4 (installed v1.2.3 via an unknown installation method)\n\
             upgrade guide: https://dalo.sh/install.md"
        );

        assert_eq!(
            render_notice(latest_version, "1.2.3", InstallChannel::DebianPackage, None,),
            "update available: dalo v1.2.4 (installed v1.2.3 via Debian package)\n\
             upgrade guide: https://dalo.sh/install.md#debian-packages"
        );
    }

    #[test]
    fn major_release_notice_should_keep_a_single_version_prefix() {
        let latest_version = "1.0.0";

        assert_eq!(
            render_notice(latest_version, "0.16.0", InstallChannel::Npm, None,),
            "update available: dalo v1.0.0 (installed v0.16.0 via npm)\n\
             upgrade with: npm install --global getdalo@latest"
        );
    }

    #[test]
    fn shell_quote_should_handle_single_quotes() {
        assert_eq!(
            shell_quote(Path::new("/tmp/dalo's bin")),
            "'/tmp/dalo'\"'\"'s bin'"
        );
    }

    #[cfg(unix)]
    #[test]
    fn command_detection_should_require_an_executable_file() {
        let temp = tempdir().expect("tempdir");
        let command = temp.path().join("cargo-binstall");
        fs::write(&command, "not really a binary").expect("write command");

        let mut permissions = fs::metadata(&command).expect("metadata").permissions();
        permissions.set_mode(0o644);
        fs::set_permissions(&command, permissions).expect("remove execute permission");
        assert!(!is_executable_file(&command));

        let mut permissions = fs::metadata(&command).expect("metadata").permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&command, permissions).expect("add execute permission");
        assert!(is_executable_file(&command));
    }
}