use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;

#[test]
fn check_blocks_botignore_match() {
    let tmp = tempfile::tempdir().unwrap();
    fs::write(tmp.path().join(".botignore"), ".env\n").unwrap();
    let target = tmp.path().join(".env");
    fs::write(&target, "").unwrap();

    Command::cargo_bin("fermata")
        .unwrap()
        .args(["check", "--op", "read", target.to_str().unwrap()])
        .assert()
        .failure()
        .code(1)
        .stdout(predicate::str::contains(".env"));
}

#[test]
fn check_allows_unmatched() {
    let tmp = tempfile::tempdir().unwrap();
    fs::write(tmp.path().join(".botignore"), ".env\n").unwrap();
    let target = tmp.path().join("src.rs");
    fs::write(&target, "").unwrap();

    Command::cargo_bin("fermata")
        .unwrap()
        .args(["check", "--op", "read", target.to_str().unwrap()])
        .assert()
        .success();
}

#[test]
fn check_emits_json_with_flag() {
    let tmp = tempfile::tempdir().unwrap();
    fs::write(tmp.path().join(".botignore"), ".env\n").unwrap();
    let target = tmp.path().join(".env");
    fs::write(&target, "").unwrap();

    let out = Command::cargo_bin("fermata")
        .unwrap()
        .args(["check", "--op", "read", "--json", target.to_str().unwrap()])
        .assert()
        .failure()
        .get_output()
        .stdout
        .clone();
    let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
    assert_eq!(v["kind"], "deny");
}
