haest-cli 0.4.1

haest, the command line agent
//! The `haest` binary as a human launches it: what a run that cannot start
//! leaves behind, and what an odd environment does to a launch.

mod support;

use std::path::PathBuf;

use support::{Home, stderr};

/// A run that fails before its first turn leaves no new, empty session for
/// `haest -r` to resume instead of the real previous one.
#[test]
fn a_run_that_cannot_start_leaves_no_session_behind() {
    let home = Home::new("cannot-start");
    let no_key = home.haest(&["-p", "hi"], &[]);
    assert_eq!(no_key.status.code(), Some(2), "{}", stderr(&no_key));
    assert_eq!(home.sessions(), Vec::<PathBuf>::new(), "no key");

    let nothing_asked = home.haest(&["-p"], &[("OPENROUTER_API_KEY", "k")]);
    assert_eq!(
        nothing_asked.status.code(),
        Some(2),
        "{}",
        stderr(&nothing_asked)
    );
    assert_eq!(home.sessions(), Vec::<PathBuf>::new(), "nothing to ask");
}

/// A setting no one has stops the launch, and the `haest` command names one
/// of its own that lists the settings there are (the library's message names
/// no command). That command works with the same broken config, and its list
/// has the setting meant.
#[test]
fn an_unknown_setting_points_at_a_command_that_lists_them() {
    let home = Home::new("unknown-setting");
    let dir = home.0.join(".config/haest");
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("config.toml"), "tool = [\"read\"]\n").unwrap();
    let out = home.haest(&["-p", "hi"], &[]);
    assert_eq!(out.status.code(), Some(2), "{}", stderr(&out));
    let said = stderr(&out);
    let hint = said
        .split_once("(haest ")
        .and_then(|(_, rest)| rest.split_once(" lists them)"))
        .map(|(args, _)| args.split_whitespace().collect::<Vec<_>>());
    let Some(hint) = hint else {
        panic!("no command named: {said}")
    };
    let listed = home.haest(&hint, &[]);
    assert_eq!(
        listed.status.code(),
        Some(0),
        "{hint:?}: {}",
        stderr(&listed)
    );
    let shown = String::from_utf8_lossy(&listed.stdout);
    assert!(shown.contains("tools"), "{hint:?}: {shown}");
}

/// A variable that is not Unicode, in its name or its value, is skipped: the
/// launch goes on, and the other variables still count.
#[test]
fn a_variable_that_is_not_unicode_is_skipped_never_a_crash() {
    use std::{ffi::OsStr, os::unix::ffi::OsStrExt};

    let home = Home::new("not-unicode");
    let garbage = OsStr::from_bytes(b"\xff\xfe");
    let out = home
        .command(&["settings"])
        .env(OsStr::from_bytes(b"BAD_\xff"), "x")
        .env("HAEST_EFFORT", garbage)
        .env("HAEST_MODEL", "a:b")
        .output()
        .unwrap();
    assert_eq!(out.status.code(), Some(0), "{}", stderr(&out));
    let shown = String::from_utf8_lossy(&out.stdout);
    let model = shown.lines().find(|l| l.starts_with("model ")).unwrap();
    assert!(
        model.contains("a:b") && model.contains("HAEST_MODEL"),
        "{model}"
    );
}