//! Integration tests for `mcptest schema`.
//!
//! Pins the byte-equivalence between the binary's output and the
//! `schemas/v1.json` source of truth so a future ADR refactor cannot
//! silently drift the published schema from the embedded copy.

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

use assert_cmd::Command;

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

fn workspace_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .and_then(|p| p.parent())
        .expect("workspace root above crates/mcptest")
        .to_path_buf()
}

#[test]
fn schema_prints_byte_equivalent_v1_json() {
    let expected = std::fs::read(workspace_root().join("schemas/v1.json")).unwrap();
    let output = mcptest()
        .arg("schema")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    assert_eq!(
        output, expected,
        "stdout must match schemas/v1.json byte-for-byte"
    );
}

#[test]
fn schema_v1_explicit_matches_default() {
    let default_out = mcptest()
        .arg("schema")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let explicit_out = mcptest()
        .args(["schema", "--version", "v1"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    assert_eq!(default_out, explicit_out);
}

#[test]
fn schema_unknown_version_exits_two() {
    mcptest()
        .args(["schema", "--version", "v99"])
        .assert()
        .code(2);
}

#[test]
fn schema_version_v1_exits_zero_and_emits_v1_schema() {
    //  regression: the `SchemaArgs` struct carries
    // `disable_version_flag = true` so the `--version v1` selector below
    // does not collide with clap's auto-generated `--version`. If a future
    // refactor reintroduces the conflict, this test fails at parse time.
    let expected = std::fs::read(workspace_root().join("schemas/v1.json")).unwrap();
    let output = mcptest()
        .args(["schema", "--version", "v1"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    assert_eq!(
        output, expected,
        "`schema --version v1` must print schemas/v1.json byte-for-byte"
    );
}
