openmoat 0.22.1

Policy checks for AI coding agents: decides allow, defer, review or deny for each tool call the hook of Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot CLI and seven more agents reports, configures Claude Code's, Codex's and Cursor's own OS sandboxes from the same policy, and keeps a local audit log.
use std::ffi::OsString;
use std::path::PathBuf;

use openmoat_core::ProjectHookOff;
use openmoat_hosts::{Host, SessionOverride};

use super::{Off, Preflight, clashing, decide, notice, pre_flighted};

/// A host that runs another host's hook has no hook of its own to lose, so
/// the run does not check it (nor refuse it for a hook file it has none
/// of); every host `openmoat init` installs a hook for is checked.
#[test]
fn only_hosts_with_a_hook_of_their_own_are_checked() {
    let mut skipped = 0;
    for host in Host::EVERY {
        let own = host.spec().install.is_some();
        assert_eq!(pre_flighted(host), own, "{host:?}");
        if !own {
            assert!(host.spec().speaks.is_some(), "{host:?}");
            skipped += 1;
        }
    }
    assert!(skipped >= 1, "a host that runs another's hook exists");
    for host in Host::ALL {
        assert!(pre_flighted(host), "{host:?}");
    }
}

/// A host with a session override, and its override.
fn with_override() -> (Host, &'static SessionOverride) {
    Host::ALL
        .into_iter()
        .find_map(|h| Some((h, h.spec().hook_off.session_override.as_ref()?)))
        .unwrap()
}

/// A host with none.
fn without_override() -> Host {
    Host::ALL
        .into_iter()
        .find(|h| {
            h.spec().hook_off.session_override.is_none() && h.spec().project_settings.is_some()
        })
        .unwrap()
}

fn off(overridden: bool) -> Off {
    Off {
        path: PathBuf::from("/p/settings.json"),
        what: vec!["`x` turns every hook off".to_owned()],
        overridden,
    }
}

fn args(list: &[&str]) -> Vec<OsString> {
    list.iter().map(OsString::from).collect()
}

fn message(found: &Preflight) -> String {
    match found {
        Preflight::Refused { message, .. } => message.clone(),
        other => panic!("not refused: {other:?}"),
    }
}

#[test]
fn nothing_found_is_clear_for_a_host_without_an_override() {
    let host = without_override();
    let found = decide(host, &[], None, &[], ProjectHookOff::Refuse);
    assert_eq!(found, Preflight::Clear);
    assert_eq!(notice(Some(host), &found), None);
}

#[test]
fn a_file_that_turns_the_hook_off_is_refused_with_the_way_out() {
    let host = without_override();
    let shown = message(&decide(
        host,
        &[off(false)],
        None,
        &[],
        ProjectHookOff::Refuse,
    ));
    assert!(shown.contains("does not start"), "{shown}");
    assert!(
        shown.contains("/p/settings.json: `x` turns every hook off"),
        "{shown}"
    );
    assert!(shown.contains("sandbox.project_hook_off: start"), "{shown}");
}

/// Only the person's own policy starts the agent anyway, and says so.
#[test]
fn the_persons_policy_starts_it_anyway_and_says_so() {
    let host = without_override();
    let found = decide(host, &[off(false)], None, &[], ProjectHookOff::Start);
    assert!(matches!(found, Preflight::Started { .. }), "{found:?}");
    let said = notice(Some(host), &found).unwrap();
    assert!(said.contains("without OpenMoat's hook"), "{said}");
}

/// The host's own option goes on every run, whatever the project holds
/// now: the host may reload the project's files mid-session.
#[test]
fn the_hosts_own_option_goes_on_every_run() {
    let (host, over) = with_override();
    for files in [vec![], vec![off(true)]] {
        let found = decide(host, &files, Some(over), &[], ProjectHookOff::Refuse);
        let Preflight::Override { args, .. } = &found else {
            panic!("{found:?}");
        };
        assert_eq!(*args, over.args);
        assert!(notice(Some(host), &found).is_some());
    }
    // A file it does not cover is refused.
    let refused = decide(
        host,
        &[off(true), off(false)],
        Some(over),
        &[],
        ProjectHookOff::Refuse,
    );
    assert!(matches!(refused, Preflight::Refused { .. }), "{refused:?}");
}

/// The person's own option of the same name would replace the override's,
/// so it is not added; over a file that turns the hook off the run is
/// refused, and without one it starts as the person asked.
#[test]
fn a_persons_own_clashing_option_is_never_overridden() {
    let (host, over) = with_override();
    let clash = over.clashes[0];
    for given in [
        args(&[clash, "{}"]),
        args(&[&format!("{clash}=x.json"), "-p", "hi"]),
    ] {
        let shown = message(&decide(
            host,
            &[off(true)],
            Some(over),
            &given,
            ProjectHookOff::Refuse,
        ));
        assert!(shown.contains(&format!("your own `{clash}`")), "{shown}");
        let clear = decide(host, &[], Some(over), &given, ProjectHookOff::Refuse);
        assert_eq!(clear, Preflight::Clear);
    }
    let started = decide(
        host,
        &[off(true)],
        Some(over),
        &args(&[clash, "{}"]),
        ProjectHookOff::Start,
    );
    assert!(matches!(started, Preflight::Started { .. }), "{started:?}");
}

/// A prompt that mentions the option, or the option after `--`, is not
/// the person's option.
#[test]
fn only_option_words_before_a_double_dash_clash() {
    let (_, over) = with_override();
    let clash = over.clashes[0];
    assert_eq!(
        clashing(over, &args(&[&format!("explain {clash} please")])),
        None
    );
    assert_eq!(clashing(over, &args(&["-p", "--", clash, "{}"])), None);
    assert_eq!(
        clashing(over, &args(&["-p", clash, "{}"])),
        Some(clash.to_owned())
    );
}