//! Integration tests for `mcptest interop` (WOR-1431).
//!
//! Offline: import competitor artifacts into mcptest suites, export a suite
//! into a portable dataset, and build a starter suite from a tools/list
//! snapshot. Converted suites are validated with `mcptest validate`.

use std::path::PathBuf;

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

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

fn fixture(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../examples/interop")
        .join(name)
}

/// Import a fixture, assert the warning fires, and validate the output suite.
fn import_and_validate(from: &str, file: &str, expect_warning: &str) {
    let dir = tempfile::tempdir().unwrap();
    let out = dir.path().join("suite.yml");
    mcptest()
        .args(["interop", "import", "--from", from])
        .arg(fixture(file))
        .arg("--out")
        .arg(&out)
        .assert()
        .success()
        .stderr(predicate::str::contains(expect_warning));
    mcptest()
        .args(["validate", "--config"])
        .arg(&out)
        .assert()
        .success();
}

#[test]
fn promptfoo_imports_and_validates_with_warnings() {
    import_and_validate("promptfoo", "promptfoo.yaml", "providers");
}

#[test]
fn deepeval_imports_and_validates_with_warnings() {
    import_and_validate("deep-eval", "deepeval.json", "metrics");
}

#[test]
fn mcp_eval_imports_and_validates_with_warnings() {
    import_and_validate("mcp-eval", "mcp-eval.json", "setup");
}

#[test]
fn dataset_from_tools_list_validates() {
    let dir = tempfile::tempdir().unwrap();
    let out = dir.path().join("starter.yml");
    mcptest()
        .args(["interop", "dataset", "--tools-list"])
        .arg(fixture("tools-list.json"))
        .arg("--out")
        .arg(&out)
        .assert()
        .success();
    mcptest()
        .args(["validate", "--config"])
        .arg(&out)
        .assert()
        .success();
}

#[test]
fn export_carries_mcptest_provenance() {
    let dir = tempfile::tempdir().unwrap();
    let suite = dir.path().join("suite.yml");
    mcptest()
        .args(["interop", "import", "--from", "deep-eval"])
        .arg(fixture("deepeval.json"))
        .arg("--out")
        .arg(&suite)
        .assert()
        .success();

    mcptest()
        .args(["interop", "export"])
        .arg(&suite)
        .assert()
        .success()
        .stdout(
            predicate::str::contains("\"generated_by\": \"mcptest\"")
                .and(predicate::str::contains("mcptest.sh/interop/v1")),
        );
}

#[test]
fn dataset_with_no_source_is_a_usage_error() {
    mcptest().args(["interop", "dataset"]).assert().code(2);
}
