oomtop-cli 0.2.3

oomtop: see the OOM coming. Terminal resource monitor for local AI work.
Documentation
//! `oomtop sandboxes` and `oomtop ns` (SPEC ยง11) against the recorded fixtures. Private XDG dirs: nothing
//! touches the real config or state.

use serde_json::Value;
use std::path::PathBuf;
use std::process::{Command, Stdio};

fn run(fixture_rel: &str, args: &[&str]) -> (i32, String) {
    let fixture = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .join(fixture_rel);
    let dir = tempfile::tempdir().unwrap();
    let mut c = Command::new(env!("CARGO_BIN_EXE_oomtop"));
    c.env("XDG_CONFIG_HOME", dir.path().join("config"))
        .env("XDG_STATE_HOME", dir.path().join("state"))
        .env("TERM", "dumb")
        .stdin(Stdio::null())
        .args(["--replay", &fixture.to_string_lossy(), "--offline"])
        .args(args);
    for (k, _) in std::env::vars() {
        if k.starts_with("OOMTOP_") {
            c.env_remove(k);
        }
    }
    let o = c.output().unwrap();
    (
        o.status.code().unwrap_or(-1),
        String::from_utf8_lossy(&o.stdout).into_owned(),
    )
}

#[test]
fn ns_on_macos_explains_and_points_to_sandboxes() {
    let (code, out) = run("fixtures/macos/m5-air-agents.json", &["ns"]);
    assert_eq!(code, 0);
    assert!(
        out.contains("macOS has none") && out.contains("oomtop sandboxes"),
        "{out}"
    );
    let (code, out) = run("fixtures/macos/m5-air-agents.json", &["ns", "--json"]);
    assert_eq!(code, 0);
    let v: Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["supported"], false);
    assert!(v["sets"].as_array().unwrap().is_empty());
}

#[test]
fn ns_on_an_old_linux_recording_says_why_there_is_no_data() {
    let (code, out) = run("fixtures/linux/nvidia-oom-trend.json", &["ns"]);
    assert_eq!(code, 0);
    assert!(out.starts_with("No namespace data"), "{out}");
}

#[test]
fn sandboxes_lists_the_vm_and_prints_json() {
    let (code, out) = run("fixtures/macos/m5-air-agents.json", &["sandboxes"]);
    assert_eq!(code, 0);
    assert!(
        out.contains("Sandboxes") && out.contains("virtualization.framework"),
        "{out}"
    );
    let (code, out) = run("fixtures/macos/m5-air-agents.json", &["sandboxes", "--json"]);
    assert_eq!(code, 0);
    let v: Value = serde_json::from_str(&out).unwrap();
    assert!(v["sandboxes"].as_array().is_some_and(|a| !a.is_empty()));
    assert!(v["schema_version"].is_u64());
    // Offline replay: no Kubernetes calls, no contexts.
    assert!(v.get("kube_contexts").is_none());
    let (code, out) = run("fixtures/linux/nvidia-oom-trend.json", &["sandboxes"]);
    assert_eq!(code, 0);
    assert!(
        out.contains("No containers, VMs or local clusters found."),
        "{out}"
    );
}