//! Integration tests for `mcptest init`.
//!
//! Each test runs the compiled binary inside a fresh tempdir so the
//! "refuses to overwrite" path is exercised against a real filesystem.

use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::tempdir;

fn mcptest() -> Command {
    Command::cargo_bin("mcptest").expect("binary builds")
}

#[test]
fn init_creates_example_and_config_in_empty_dir() {
    let dir = tempdir().unwrap();
    mcptest()
        .current_dir(dir.path())
        .arg("init")
        .assert()
        .success()
        .stdout(predicate::str::contains("Next: mcptest run"));
    assert!(dir.path().join("tests/example.yaml").exists());
    assert!(dir.path().join("tests/example-server.yml").exists());
    assert!(dir.path().join("mcptest.yml").exists());
}

#[test]
fn init_scaffolds_suites_that_pass_validate() {
    let dir = tempdir().unwrap();
    mcptest()
        .current_dir(dir.path())
        .arg("init")
        .assert()
        .success();
    // Bare validate resolves ./mcptest.yml exactly like a bare run, so this
    // pins the WOR-1352 contract: the scaffolded project suite is valid.
    mcptest()
        .current_dir(dir.path())
        .arg("validate")
        .assert()
        .success();
    mcptest()
        .current_dir(dir.path())
        .args(["validate", "--config", "tests/example.yaml"])
        .assert()
        .success();
}

#[test]
fn init_refuses_to_overwrite_existing_files_without_force() {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("tests")).unwrap();
    std::fs::write(dir.path().join("tests/example.yaml"), "old content").unwrap();
    let assertion = mcptest()
        .current_dir(dir.path())
        .arg("init")
        .assert()
        .code(2);
    let stderr = String::from_utf8(assertion.get_output().stderr.clone()).unwrap();
    assert!(stderr.contains("refusing to overwrite"));
    let body = std::fs::read_to_string(dir.path().join("tests/example.yaml")).unwrap();
    assert_eq!(body, "old content");
}

#[test]
fn init_with_jury_includes_a_jury_eval() {
    let dir = tempdir().unwrap();
    mcptest()
        .current_dir(dir.path())
        .args(["init", "--with-jury"])
        .assert()
        .success();
    let body = std::fs::read_to_string(dir.path().join("tests/example.yaml")).unwrap();
    assert!(body.contains("evals:"));
    assert!(body.contains("jury:"));
}

#[test]
fn init_with_jury_scaffolds_suites_that_pass_validate() {
    let dir = tempdir().unwrap();
    mcptest()
        .current_dir(dir.path())
        .args(["init", "--with-jury"])
        .assert()
        .success();
    // WOR-1353 contract: the jury appendix is a schema-valid `evals:` array,
    // so the scaffolded example suite passes validate as-is. Bare validate
    // covers the mirrored project suite exactly like a bare run would.
    mcptest()
        .current_dir(dir.path())
        .arg("validate")
        .assert()
        .success();
    mcptest()
        .current_dir(dir.path())
        .args(["validate", "--config", "tests/example.yaml"])
        .assert()
        .success();
}

#[test]
fn init_with_force_overwrites_existing_file() {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("tests")).unwrap();
    std::fs::write(dir.path().join("tests/example.yaml"), "old").unwrap();
    mcptest()
        .current_dir(dir.path())
        .args(["init", "--force"])
        .assert()
        .success();
    let body = std::fs::read_to_string(dir.path().join("tests/example.yaml")).unwrap();
    assert!(body.contains("say_hello"));
}
