mod common;
use assert_cmd::Command;
use common::TestHomeGuard;
#[test]
fn test_cache_search_auto_fetch_distribution() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home_path = test_home.kopi_home();
let kopi_home = kopi_home_path.to_str().unwrap();
Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "clear"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "corretto"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
if output.status.success() {
assert!(
stdout.contains("corretto")
|| stdout.contains("Corretto")
|| stdout.contains("Distribution")
|| stdout.contains("Failed")
);
} else {
assert!(
stdout.contains("Failed to fetch distribution") || stderr.contains("Failed to fetch")
);
}
}
#[test]
fn test_cache_search_specific_distribution_version() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home_path = test_home.kopi_home();
let kopi_home = kopi_home_path.to_str().unwrap();
let _ = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "clear"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "temurin@21"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
if output.status.success() {
assert!(
stdout.contains("temurin")
|| stdout.contains("Temurin")
|| stdout.contains("21")
|| stdout.contains("No matching")
);
}
}
#[test]
fn test_cache_search_json_with_auto_fetch() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home_path = test_home.kopi_home();
let kopi_home = kopi_home_path.to_str().unwrap();
let _ = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "clear"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "corretto", "--json"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
if output.status.success() {
let result: serde_json::Result<serde_json::Value> = serde_json::from_str(&stdout);
assert!(result.is_ok(), "Output should be valid JSON");
}
}
#[test]
fn test_cache_search_invalid_distribution() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home_path = test_home.kopi_home();
let kopi_home = kopi_home_path.to_str().unwrap();
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "notarealdistribution"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stdout.contains("Failed")
|| stdout.contains("No matching")
|| stdout.contains("notarealdistribution")
|| stderr.contains("Error")
|| stderr.contains("InvalidVersionFormat")
);
}
#[test]
fn test_cache_persists_after_fetch() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home_path = test_home.kopi_home();
let kopi_home = kopi_home_path.to_str().unwrap();
let refresh_output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "refresh"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
assert!(refresh_output.status.success(), "Cache refresh failed");
let output1 = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "corretto"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout1 = String::from_utf8_lossy(&output1.stdout);
let stderr1 = String::from_utf8_lossy(&output1.stderr);
assert!(
output1.status.success(),
"First search failed: stdout={stdout1}, stderr={stderr1}"
);
std::thread::sleep(std::time::Duration::from_millis(100));
let output2 = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "corretto"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout2 = String::from_utf8_lossy(&output2.stdout);
let stderr2 = String::from_utf8_lossy(&output2.stderr);
assert!(
output2.status.success(),
"Second search failed: stdout={stdout2}, stderr={stderr2}"
);
assert!(stdout1.contains("Found") && stdout1.contains("corretto"));
assert!(stdout2.contains("Found") && stdout2.contains("corretto"));
}