use crate::cli::arguments::Diagnostic;
use crate::commands::doctor::{run, should_run};
use acorn::doctor::{MemoryInformation, NetworkInformation, SystemInformation, TableFormatPrint};
use color_eyre::eyre::Result;
const OFFLINE: bool = false;
#[test]
fn test_should_run() {
assert!(should_run(&[Diagnostic::Software], Diagnostic::Software));
assert!(!should_run(&[Diagnostic::Memory], Diagnostic::System));
assert!(!should_run(&[Diagnostic::Network], Diagnostic::Gpu));
assert!(should_run(&[Diagnostic::All], Diagnostic::Memory));
assert!(should_run(&[Diagnostic::All], Diagnostic::System));
assert!(should_run(&[Diagnostic::All], Diagnostic::Network));
assert!(should_run(&[Diagnostic::All], Diagnostic::Gpu));
assert!(should_run(&[Diagnostic::All], Diagnostic::Software));
assert!(!should_run(&[], Diagnostic::Memory));
assert!(!should_run(&[], Diagnostic::System));
assert!(should_run(&[Diagnostic::Memory, Diagnostic::System], Diagnostic::Memory));
assert!(should_run(&[Diagnostic::Memory, Diagnostic::System], Diagnostic::System));
assert!(!should_run(&[Diagnostic::Memory, Diagnostic::System], Diagnostic::Network));
}
#[test]
fn test_run_with_valid_diagnostics() -> Result<()> {
let result = run(&false, &false, &[Diagnostic::System], &OFFLINE);
assert!(result.is_ok());
let result = run(&false, &false, &[Diagnostic::Memory], &OFFLINE);
assert!(result.is_ok());
let result = run(&false, &false, &[Diagnostic::Network], &OFFLINE);
assert!(result.is_ok());
let result = run(&false, &false, &[Diagnostic::System, Diagnostic::Memory], &OFFLINE);
assert!(result.is_ok());
let result = run(&false, &false, &[Diagnostic::All], &OFFLINE);
assert!(result.is_ok());
Ok(())
}
#[test]
fn test_gpu_diagnostic_warning() -> Result<()> {
let result = run(&false, &false, &[Diagnostic::Gpu], &OFFLINE);
assert!(result.is_ok());
Ok(())
}
#[test]
fn test_run_with_empty_diagnostics() -> Result<()> {
let result = run(&false, &false, &[], &OFFLINE);
assert!(result.is_ok());
Ok(())
}
#[test]
fn test_system_information_init() {
let system_info = SystemInformation::init();
assert!(!system_info.name.is_empty());
assert!(!system_info.kernel_version.is_empty());
assert!(!system_info.os_version.is_empty());
assert!(!system_info.host_name.is_empty());
assert!(!system_info.cpu_arch.is_empty());
assert!(!system_info.cpu_count.is_empty());
let cpu_count = system_info.cpu_count.parse::<usize>();
assert!(cpu_count.is_ok());
assert!(cpu_count.unwrap() > 0);
}
#[test]
fn test_memory_information_init() {
let memory_info = MemoryInformation::init();
assert!(!memory_info.total.is_empty());
assert!(!memory_info.available.is_empty());
assert!(!memory_info.used.is_empty());
assert!(!memory_info.swap.is_empty());
assert!(memory_info.total.contains("B")); assert!(memory_info.used.contains("B"));
}
#[test]
fn test_network_information_init() {
let network_info = NetworkInformation::init();
if !network_info.networks.is_empty() {
let network = &network_info.networks[0];
if !network.mac_address.is_empty() {
assert!(network.mac_address.contains(':') || network.mac_address.contains('-'));
}
let mtu = network.mtu.parse::<usize>();
assert!(mtu.is_ok() || network.mtu.is_empty());
if !network.ip_address.is_empty() {
let ip = &network.ip_address[0];
assert!(ip.contains('.') || ip.contains(':'));
}
}
}
#[test]
fn test_diagnostic_combinations() -> Result<()> {
let test_cases = vec![
vec![Diagnostic::System, Diagnostic::Memory],
vec![Diagnostic::Memory, Diagnostic::Network],
vec![Diagnostic::System, Diagnostic::Network],
vec![Diagnostic::System, Diagnostic::Memory, Diagnostic::Network],
vec![Diagnostic::All],
];
for diagnostics in test_cases {
let result = run(&false, &false, &diagnostics, &OFFLINE);
assert!(result.is_ok(), "Failed with diagnostics: {diagnostics:?}");
}
Ok(())
}
#[test]
fn test_edge_case_invalid_combination() -> Result<()> {
let result = run(&false, &false, &[Diagnostic::System, Diagnostic::Gpu], &OFFLINE);
assert!(result.is_ok());
let result = run(&false, &false, &[Diagnostic::Software], &OFFLINE);
assert!(result.is_ok());
Ok(())
}