tessera-command 0.6.0

The tesserax command: opens a recipe, then serves, fetches or runs tasks
Documentation
use crate::recipe::render;
use crate::source::{Origin, open_recipe, split};
use std::path::Path;
use tessera_frontend::recipe::{Host, Recipe};

const SAMPLE: &str = r#"
name = "demo-stack"
about = "Base stack"

[weights]
hf = "org/model@0123456789abcdef0123456789abcdef01234567"

[lower]
compute = "bf16"

[serve]
task = "generate"
weight_dtype = "mxfp8"
mtp = 3

[profile.p1]
about = "1x Blackwell"
filter = { backend = "cuda", arch = "sm_120*", gpus = 1, min_vram_gib = 80 }

[profile.p1.serve]
slots = 4

[profile.p2]
about = "2x GB10"
filter = { backend = "cuda", arch = "sm_121*", gpus = 2, min_vram_gib = 110, integrated = true }
unset = ["mtp"]

[profile.p2.serve]
slots = 11
moe = "tp"
sums = "arx"
"#;

#[test]
fn split_parses_profile_flag() {
    let given: Vec<String> = [
        "--recipe",
        "gemma-4-e4b",
        "--profile",
        "fast",
        "recipe",
        "--json",
    ]
    .into_iter()
    .map(String::from)
    .collect();
    let (origin, rest) = split(&given).unwrap();
    assert!(matches!(origin, Some(Origin::Profiled(_, _))));
    assert_eq!(rest, vec!["recipe", "--json"]);

    let bare: Vec<String> = ["--profile", "fast", "serve"]
        .into_iter()
        .map(String::from)
        .collect();
    assert!(split(&bare).is_err());

    let (_, profile) = open_recipe(&origin.unwrap()).unwrap();
    assert_eq!(profile.as_deref(), Some("fast"));
}

#[test]
fn renders_effective() {
    let raw = Recipe::parse(SAMPLE, Path::new("/demo/recipe.toml")).unwrap();
    let host_p1 = Host {
        backend: "cuda".into(),
        arch: "sm_120a".into(),
        device: "NVIDIA RTX PRO 6000".into(),
        gpus: 1,
        vram_bytes: 96u64 << 30,
        host_bytes: 256u64 << 30,
        integrated: false,
    };

    let toml_out = render(&raw, None, &host_p1, false, false).unwrap();
    assert!(toml_out.contains("# profile: p1 (auto)"));
    assert!(toml_out.contains("# filter: matched"));
    assert!(toml_out.contains("slots = 4"));
    assert!(toml_out.contains("mtp = 3"));

    let json_out = render(&raw, Some("p2"), &host_p1, false, true).unwrap();
    let v: serde_json::Value = serde_json::from_str(&json_out).unwrap();
    assert_eq!(v["profile"], "p2");
    assert_eq!(v["selection"], "explicit");
    assert_eq!(v["filter_matches"], false);
    assert_eq!(v["serve"]["slots"], 11);
    assert!(v["serve"].get("mtp").is_none());

    let list_out = render(&raw, None, &host_p1, true, false).unwrap();
    assert!(list_out.contains("* p1 [match]"));
    assert!(list_out.contains("  p2 [no match:"));
}

/// A recipe `mtp = "auto"` reaches `--mtp` as a valid value.
#[test]
fn mtp_auto_parses() {
    let text = SAMPLE.replace("mtp = 3", "mtp = \"auto\"");
    let recipe = Recipe::parse(&text, Path::new("/demo/recipe.toml")).unwrap();
    let flags = recipe.flags();
    let (flag, value) = flags.iter().find(|(f, _)| f == "--mtp").unwrap();
    let mut o = tessera_frontend::serve::start::Options::default();
    assert!(o.flag(flag, value.clone().unwrap()).is_some());
    assert!(o.flag("--mtp", "0".into()).is_none());
    assert!(o.flag("--mtp", "bogus".into()).is_none());
}