rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::path::PathBuf;

use pretty_assertions::assert_eq;
use serde_json::json;

use super::{InstallMethod, Lookups, UpdateCheck, UpdateInfo};

// Covers: the `rho update --check --json` object rho-ui parses: field and
// method names, `update_available` from the version order, the command
// pinned to the latest release tag (and none, so no lookup, without an
// update), and which methods need a terminal: on Windows, every method but
// the install script's, whose rho `rho update` replaces itself.
// Owner: update check report
#[test]
fn check_report_is_the_client_contract() {
    const LATEST_TAG: &str = "rho-coding-agent-v3.1.1";
    let update = |current: &str| UpdateInfo {
        latest_tag: LATEST_TAG.into(),
        latest_version: "3.1.1".into(),
        current_version: current.into(),
    };
    let script_command = InstallMethod::Script
        .update_command(LATEST_TAG)
        .expect("the script command needs no lookup");
    let cases = [
        (
            update("3.1.0"),
            InstallMethod::Pacman,
            json!({
                "current": "3.1.0",
                "latest": "3.1.1",
                "update_available": true,
                "method": "pacman",
                "command": "sudo pacman -Sy mjiang-extras/rho-coding-agent",
                "needs_terminal": true,
            }),
        ),
        (
            update("3.1.0"),
            InstallMethod::Dnf,
            json!({
                "current": "3.1.0",
                "latest": "3.1.1",
                "update_available": true,
                "method": "dnf",
                "command": "sudo dnf upgrade --refresh rho-coding-agent",
                "needs_terminal": true,
            }),
        ),
        (
            update("3.1.1"),
            InstallMethod::ScoopGlobal,
            json!({
                "current": "3.1.1",
                "latest": "3.1.1",
                "update_available": false,
                "method": "scoop-global",
                "command": null,
                "needs_terminal": cfg!(windows),
            }),
        ),
        (
            update("3.1.0"),
            InstallMethod::Script,
            json!({
                "current": "3.1.0",
                "latest": "3.1.1",
                "update_available": true,
                "method": "script",
                "command": script_command,
                // On Windows `rho update` replaces the install script's
                // rho itself.
                "needs_terminal": false,
            }),
        ),
    ];
    for (update, method, expected) in cases {
        let check = UpdateCheck::new(update, method, &Lookups::OfThisProcess)
            .expect("the command needs no lookup");
        assert_eq!(
            serde_json::to_value(&check).expect("the report serializes"),
            expected,
            "{method:?}"
        );
    }
}

// Covers: the command names what lookups found, as the daemon finds them in
// the login environment, and a lookup that failed fails only a check whose
// update needs its command, as `rho update --check` resolves the command
// only then.
// Owner: update check report
#[test]
fn check_command_names_what_the_lookups_found() {
    let update = |current: &str| UpdateInfo {
        latest_tag: "rho-coding-agent-v3.1.1".into(),
        latest_version: "3.1.1".into(),
        current_version: current.into(),
    };
    let found = |cargo_root: Option<&str>, mise_tool: Result<&str, &str>| Lookups::Found {
        cargo_root: cargo_root.map(PathBuf::from),
        mise_tool: mise_tool.map(str::to_owned).map_err(str::to_owned),
    };
    let not_found = Err("`mise` is not on PATH");
    let cases = [
        (
            update("3.1.0"),
            InstallMethod::Cargo,
            found(Some("/opt/cargo"), not_found),
            Some(Some(
                "cargo install rho-coding-agent --locked --root /opt/cargo",
            )),
        ),
        (
            update("3.1.0"),
            InstallMethod::Mise,
            found(None, Ok("rho")),
            Some(Some("mise upgrade --global rho")),
        ),
        (
            update("3.1.1"),
            InstallMethod::Mise,
            found(None, not_found),
            Some(None),
        ),
        (
            update("3.1.0"),
            InstallMethod::Mise,
            found(None, not_found),
            None,
        ),
    ];
    for (update, method, lookups, expected) in cases {
        let command = UpdateCheck::new(update, method, &lookups)
            .ok()
            .map(|check| check.command);
        assert_eq!(
            command,
            expected.map(|command| command.map(str::to_owned)),
            "{method:?} {lookups:?}"
        );
    }
}