use std::path::PathBuf;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::{InstallMethod, Lookups, UpdateCheck, UpdateInfo};
#[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,
"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:?}"
);
}
}
#[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:?}"
);
}
}