//! Integration tests for `mcptest model-compat`.
//!
//! Exercise each of the three subcommands through the compiled binary so
//! the clap wiring stays in sync with the library entry points in
//! `crates/mcptest/src/commands/model_compat.rs`.

use std::path::{Path, PathBuf};

use assert_cmd::Command;

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

fn fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("model-compat")
        .join(name)
}

#[test]
fn model_compat_capture_writes_baseline() {
    let dir = tempfile::tempdir().unwrap();
    let output = dir.path().join("out.json");
    mcptest()
        .args([
            "model-compat",
            "capture",
            "--model",
            "anthropic:claude-sonnet-5.0",
            "--input",
        ])
        .arg(fixture("baseline.json"))
        .arg("--output")
        .arg(&output)
        .assert()
        .success();
    let body = std::fs::read_to_string(&output).unwrap();
    assert!(
        body.contains("\"anthropic:claude-sonnet-5.0\""),
        "model_id should be rewritten"
    );
}

#[test]
fn model_compat_diff_identical_files_exits_zero() {
    mcptest()
        .args(["model-compat", "diff"])
        .arg(fixture("baseline.json"))
        .arg(fixture("baseline.json"))
        .args(["--format", "json"])
        .assert()
        .code(0);
}

#[test]
fn model_compat_diff_drift_exits_six() {
    // baseline.json vs candidate.json: same ids, slightly different
    // text. ADR 0026 classifies the case-or-whitespace edit as DRIFT.
    mcptest()
        .args(["model-compat", "diff"])
        .arg(fixture("baseline.json"))
        .arg(fixture("candidate.json"))
        .args(["--format", "json"])
        .assert()
        .code(6);
}

#[test]
fn model_compat_run_uses_rubric_exit_code() {
    mcptest()
        .args(["model-compat", "run", "--baseline"])
        .arg(fixture("baseline.json"))
        .args(["--model", "anthropic:claude-sonnet-5.0", "--candidate"])
        .arg(fixture("baseline.json"))
        .assert()
        .code(0);
}

#[test]
fn model_compat_run_against_drifted_candidate_exits_six() {
    mcptest()
        .args(["model-compat", "run", "--baseline"])
        .arg(fixture("baseline.json"))
        .args(["--model", "anthropic:claude-sonnet-5.0", "--candidate"])
        .arg(fixture("candidate.json"))
        .assert()
        .code(6);
}

#[test]
fn model_compat_help_lists_three_subcommands() {
    let output = mcptest()
        .args(["model-compat", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let body = String::from_utf8(output).expect("utf8 help");
    assert!(body.contains("capture"));
    assert!(body.contains("diff"));
    assert!(body.contains("run"));
}
