mod common;
use assert_cmd::Command;
use common::TestHomeGuard;
#[test]
fn test_cache_search_lts_only_filter() {
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", "refresh"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
if !output.status.success() {
eprintln!(
"Cache refresh failed: {}",
String::from_utf8_lossy(&output.stderr)
);
return;
}
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "java", "--lts-only"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
if output.status.success() && stdout.contains("Available LTS Java versions") {
assert!(stdout.contains("LTS"));
assert!(stdout.contains("Available LTS Java versions matching 'java':"));
assert!(!stdout.contains("STS"));
}
}
#[test]
fn test_cache_search_lts_only_with_json() {
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", "refresh"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
if !output.status.success() {
return; }
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "21", "--lts-only", "--json"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let result: serde_json::Result<serde_json::Value> = serde_json::from_str(&stdout);
assert!(result.is_ok(), "Output should be valid JSON");
if let Ok(json) = result
&& let Some(array) = json.as_array()
{
for item in array {
if let Some(package) = item.get("package")
&& let Some(tos) = package.get("term_of_support")
{
assert_eq!(tos.as_str(), Some("lts"));
}
}
}
}
}
#[test]
fn test_cache_list_distributions() {
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", "refresh"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
if !output.status.success() {
return; }
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "list-distributions"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("Available distributions in cache:"));
assert!(stdout.contains("Distribution"));
assert!(stdout.contains("Display Name"));
assert!(stdout.contains("Versions"));
assert!(stdout.contains("Total:"));
assert!(
stdout.contains("temurin") || stdout.contains("corretto") || stdout.contains("zulu"),
"Expected at least one known distribution in output"
);
}
#[test]
fn test_cache_list_distributions_no_cache() {
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", "list-distributions"])
.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!(
output.status.success(),
"Command failed. stdout: {stdout}, stderr: {stderr}"
);
assert!(
stdout.contains("No cache found")
|| stdout.contains("Fetching")
|| stdout.contains("Available distributions")
|| stdout.contains("Distribution"),
"Unexpected output. stdout: {stdout}"
);
}
#[test]
fn test_cache_search_no_lts_results() {
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", "refresh"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
if !output.status.success() {
return; }
let output = Command::cargo_bin("kopi")
.unwrap()
.args(["cache", "search", "99", "--lts-only"])
.env("KOPI_HOME", kopi_home)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
if !stdout.contains("Available LTS Java versions") {
assert!(stdout.contains("No matching LTS Java versions found"));
}
}