rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;

use super::{
    install_script_ref, latest_app_release_tag, release_tag_to_version,
    scoop_update_command_display, version_is_newer, InstallMethod, Plan, Release, Report,
    ScoopInstallScope,
};

/// What an update said, in order, and where `rho update` prints it.
#[derive(Debug, Default)]
pub(super) struct Recorded(pub(super) Vec<Said>);

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum Said {
    Stdout(String),
    Stderr(String),
}

impl Report for Recorded {
    fn say(&mut self, line: &str) {
        self.0.push(Said::Stdout(line.to_owned()));
    }

    fn warn(&mut self, line: &str) {
        self.0.push(Said::Stderr(line.to_owned()));
    }
}

// Covers: with the planning moved behind a report, a failed release lookup
// still warns on stderr, then says on stdout that the command goes on,
// pinned to `main`, as `rho update` printed before the daemon shared it. A
// warning moved to stdout would land in what scripts read of `rho update`.
// Owner: update planning
#[test]
fn a_failed_lookup_warns_and_goes_on_pinned_to_main() {
    let mut recorded = Recorded::default();

    let plan = Plan::new(
        InstallMethod::Cargo,
        Err(anyhow::anyhow!("offline")),
        &mut recorded,
    )
    .unwrap();

    let git_ref = "main".to_owned();
    let expected = if cfg!(windows) {
        Plan::Print { git_ref }
    } else {
        Plan::Run { git_ref }
    };
    assert_eq!(plan, expected);
    assert_eq!(
        recorded.0,
        [
            Said::Stderr("warning: could not check latest release: offline".to_owned()),
            Said::Stdout("continuing with Cargo update command.".to_owned()),
        ]
    );
}

#[test]
fn install_script_ref_accepts_release_tags_and_rejects_shell_metacharacters() {
    assert_eq!(
        install_script_ref("rho-coding-agent-v1.13.0".into()),
        "rho-coding-agent-v1.13.0"
    );
    // A tag that would inject into `sh -c` falls back to the safe `main` ref.
    assert_eq!(install_script_ref("$(rm -rf ~)v1.0.0".into()), "main");
    assert_eq!(install_script_ref("v1.0.0; echo pwned".into()), "main");
    assert_eq!(install_script_ref(String::new()), "main");
}

// Covers: another workspace package's major release must not become a rho update.
// Owner: update release selection
#[test]
fn selects_latest_published_app_release() {
    let releases = [
        Release {
            tag_name: "rho-providers-v3.0.0".into(),
            draft: false,
            prerelease: false,
        },
        Release {
            tag_name: "rho-coding-agent-v2.3.0".into(),
            draft: true,
            prerelease: false,
        },
        Release {
            tag_name: "rho-coding-agent-vbroken".into(),
            draft: false,
            prerelease: false,
        },
        Release {
            tag_name: "rho-coding-agent-v2.2.0".into(),
            draft: false,
            prerelease: false,
        },
    ];

    assert_eq!(
        latest_app_release_tag(&releases),
        Some("rho-coding-agent-v2.2.0")
    );
    assert_eq!(
        release_tag_to_version("rho-coding-agent-v2.2.0").as_deref(),
        Some("2.2.0")
    );
    assert_eq!(release_tag_to_version("rho-providers-v3.0.0"), None);
}

// Covers: a development build such as 3.0.0-alpha.dev is not offered
// the last 2.x release as an update, is offered the 3.0.0 betas and
// 3.0.0 once they ship, and is not newer or older than another build of
// the same version.
// Owner: pure unit
#[test]
fn orders_versions_by_semantic_precedence() {
    let cases = [
        ("0.12.3", "0.12.1", true),
        ("0.13.0", "0.12.9", true),
        ("0.12.1", "0.12.1", false),
        ("0.12.0", "0.12.1", false),
        ("2.33.1", "3.0.0-alpha.dev", false),
        ("3.0.0-alpha.dev", "2.33.1", true),
        ("3.0.0-alpha.dev", "3.0.0-alpha.dev", false),
        ("3.0.0-beta", "3.0.0-alpha.dev", true),
        ("3.0.0-beta.1", "3.0.0-alpha.dev", true),
        ("3.0.0-alpha.dev", "3.0.0-beta.1", false),
        ("3.0.0", "3.0.0-alpha.dev", true),
        ("3.0.0-alpha.dev", "3.0.0", false),
        ("3.0.0-beta.10", "3.0.0-beta.2", true),
        ("3.0.0-beta.1", "3.0.0-beta", true),
        ("3.0.0-beta.1", "3.0.0-1", true),
        ("3.0.0-beta+build.2", "3.0.0-beta+build.1", false),
        ("not-a-version", "3.0.0-alpha.dev", false),
        ("3.0.0-alpha.dev", "not-a-version", false),
    ];
    for (candidate, current, newer) in cases {
        assert_eq!(
            version_is_newer(candidate, current),
            newer,
            "{candidate} newer than {current}"
        );
    }
}

#[test]
fn script_update_command_display_uses_platform_installer() {
    let command = InstallMethod::Script
        .update_command("rho-coding-agent-v1.2.3")
        .unwrap();

    // The install script is fetched from the targeted release tag, never
    // from mutable `main`.
    assert!(command.contains("rho-coding-agent-v1.2.3/scripts/install"));
    assert!(!command.contains("/main/scripts/install"));

    #[cfg(windows)]
    {
        assert!(command.contains("powershell"));
        assert!(command.contains("install.ps1"));
        assert!(!command.contains("install.sh"));
    }

    #[cfg(not(windows))]
    {
        assert!(command.contains("sh -c"));
        assert!(command.contains("install.sh"));
        assert!(!command.contains("install.ps1"));
    }
}

#[cfg(not(windows))]
#[test]
fn script_update_command_display_preserves_curl_failure_status() {
    let command = InstallMethod::Script
        .update_command("rho-coding-agent-v1.2.3")
        .unwrap();

    assert!(command.contains("curl"));
    assert!(command.contains("--proto"));
    assert!(command.contains("-o"));
    assert!(command.contains("$tmp"));
    assert!(command.contains("exit $status"));
    assert!(!command.contains("| sh"));
}

#[test]
fn scoop_update_command_refreshes_buckets_then_updates_rho() {
    for (scope, method, expected) in [
        (
            ScoopInstallScope::User,
            InstallMethod::Scoop,
            "scoop update; scoop update rho",
        ),
        (
            ScoopInstallScope::Global,
            InstallMethod::ScoopGlobal,
            "scoop update; scoop update -g rho",
        ),
    ] {
        assert_eq!(scoop_update_command_display(scope), expected);
        assert_eq!(method.update_command("main").unwrap(), expected);
    }
}