use cpufetch_rs::cpu::CpuInfo;
#[test]
fn test_full_pipeline_no_panic() {
let cpu_info = CpuInfo::new().expect("CpuInfo::new() should succeed on a real machine");
assert!(cpu_info.physical_cores > 0, "physical_cores must be > 0");
assert!(cpu_info.logical_cores > 0, "logical_cores must be > 0");
assert!(!cpu_info.brand_string.is_empty(), "brand_string must not be empty");
#[cfg(feature = "display")]
{
use cpufetch_rs::cli::Args;
use cpufetch_rs::printer::print_cpu_info;
let args = Args {
cache: true,
no_logo: true, no_color: true,
..Args::default()
};
print_cpu_info(&cpu_info, &args).expect("print_cpu_info should not fail");
}
}
#[test]
fn test_cpu_info_get_singleton() {
let a = CpuInfo::get();
let b = CpuInfo::get();
assert!(std::ptr::eq(a, b), "CpuInfo::get() must return the same instance");
}
#[test]
#[cfg(feature = "json")]
fn test_json_roundtrip() {
let cpu_info = CpuInfo::new().expect("CpuInfo::new() should succeed");
let json = serde_json::to_string_pretty(&cpu_info).expect("serialisation must not fail");
assert!(!json.is_empty());
let restored: CpuInfo = serde_json::from_str(&json).expect("deserialisation must not fail");
assert_eq!(cpu_info.vendor, restored.vendor);
assert_eq!(cpu_info.brand_string, restored.brand_string);
assert_eq!(cpu_info.physical_cores, restored.physical_cores);
assert_eq!(cpu_info.logical_cores, restored.logical_cores);
assert_eq!(cpu_info.cache_sizes, restored.cache_sizes);
}
#[test]
#[cfg(feature = "cli")]
fn test_cli_help_exits_zero() {
use assert_cmd::Command;
Command::cargo_bin("cpufetch").unwrap().arg("--help").assert().success();
}
#[test]
#[cfg(feature = "cli")]
fn test_cli_version_exits_zero() {
use assert_cmd::Command;
Command::cargo_bin("cpufetch")
.unwrap()
.arg("--version")
.assert()
.success();
}
#[test]
#[cfg(feature = "cli")]
fn test_cli_json_output_is_valid() {
use assert_cmd::Command;
use predicates::str::contains;
Command::cargo_bin("cpufetch")
.unwrap()
.args(["--json", "--no-color"])
.assert()
.success()
.stdout(contains("vendor"))
.stdout(contains("brand_string"));
}
#[test]
#[cfg(all(
target_arch = "x86_64",
any(target_os = "linux", target_os = "macos"),
feature = "frequency"
))]
fn test_frequency_populated_on_x86_64() {
let cpu = CpuInfo::new().expect("CpuInfo::new() should succeed");
assert!(
cpu.frequency.base.is_some() || cpu.frequency.max.is_some() || cpu.frequency.current.is_some(),
"At least one frequency field must be populated on Linux/macOS x86_64"
);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_microarch_detected_on_x86_64() {
use cpufetch_rs::cpu::Vendor;
let cpu = CpuInfo::new().expect("CpuInfo::new() should succeed");
match cpu.vendor {
Vendor::Intel | Vendor::AMD if cpu.microarch.is_none() => {
eprintln!(
"WARN: microarchitecture not detected for Intel/AMD CPU (family={}, model={}) — add to uarch table",
cpu.version.family, cpu.version.model
);
},
_ => {
},
}
}
#[test]
#[cfg(all(target_arch = "x86_64", feature = "frequency"))]
fn test_peak_flops_positive_when_freq_available() {
let cpu = CpuInfo::new().expect("CpuInfo::new() should succeed");
let has_freq = cpu.frequency.max.is_some() || cpu.frequency.base.is_some();
if has_freq {
assert!(
cpu.peak_flops.is_some_and(|f| f > 0.0),
"peak_flops should be positive when frequency is available"
);
}
}