use super::{PMSET_ARGUMENTS, PS_SELECTOR};
use crate::compat::{String, ToString};
#[test]
fn total_memory_is_measured_and_plausible() {
let mb = super::total_memory_mb().expect(
"sysconf(_SC_PHYS_PAGES) must yield installed memory on a Darwin host; a None here \
means the probe regressed to an OS source this platform does not have",
);
assert!(mb >= 256, "installed memory {mb} MiB is implausibly small");
assert!(mb <= 4 * 1024 * 1024, "installed memory {mb} MiB exceeds any plausible host");
}
#[test]
fn process_memory_utilization_is_a_fraction() {
let ratio = super::process_memory_utilization().expect(
"ps -o rss=,vsz= must answer for the current pid; a None here means the selector \
or the output parsing regressed",
);
assert!((0.0..=1.0).contains(&ratio), "memory utilization {ratio} is outside [0, 1]");
assert!(ratio > 0.0, "a running process must have a non-zero resident share");
}
#[test]
fn battery_answer_matches_the_pmset_banner() {
let Ok(output) = std::process::Command::new("pmset").args(PMSET_ARGUMENTS).output() else {
assert!(!super::is_on_battery(), "without pmset the probe must answer false");
return;
};
let text = String::from_utf8_lossy(&output.stdout);
let expected = text.contains("'Battery Power'");
assert_eq!(
super::is_on_battery(),
expected,
"probe disagreed with the pmset banner it is documented to read"
);
}
#[test]
fn ps_selector_is_bsd_spelled() {
assert_eq!(PS_SELECTOR, ["-o", "rss=,vsz="]);
let output = std::process::Command::new("ps")
.args(PS_SELECTOR)
.arg("-p")
.arg(std::process::id().to_string())
.output()
.expect("ps must be spawnable on Darwin");
assert!(output.status.success(), "ps rejected {PS_SELECTOR:?}");
let text = String::from_utf8_lossy(&output.stdout);
let first = text.split_whitespace().next().expect("ps printed a figure for a live pid");
assert!(
first.parse::<u64>().is_ok(),
"first token '{first}' is not a figure — the header suppression `=` was lost"
);
}
#[test]
fn parse_error_message_names_the_offending_output() {
let message = super::ps_parse_error(" RSS VSZ\n");
assert!(message.contains("RSS"), "message dropped the raw output: {message}");
assert!(message.contains("rss vsz"), "message does not name the expected pair: {message}");
}