//! Integration tests for `mcptest bakeoff` (WOR-1428).
//!
//! Offline: the command renders the embedded, pinned competitor matrix into a
//! dated scorecard. No network, no live competitors.

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

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

#[test]
fn markdown_scorecard_lists_the_named_competitors() {
    mcptest().arg("bakeoff").assert().success().stdout(
        predicate::str::contains("Official MCP Inspector")
            .and(predicate::str::contains("MCPJam"))
            .and(predicate::str::contains("ToolHive"))
            .and(predicate::str::contains("Snyk Agent Scan"))
            .and(predicate::str::contains("MCPCorpus"))
            .and(predicate::str::contains("Official MCP registry"))
            .and(predicate::str::contains("## mcptest gaps")),
    );
}

#[test]
fn json_output_carries_the_matrix() {
    let out = mcptest()
        .args(["bakeoff", "--format", "json"])
        .output()
        .unwrap();
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(value["schema_version"], "mcptest.sh/bakeoff/v1");
    assert!(value["competitors"].as_array().unwrap().len() >= 9);
    assert!(value["as_of"].is_string());
}

#[test]
fn fail_on_gap_exits_six_while_gaps_remain() {
    // The snapshot has at least one tracked gap, so this gate fails by design.
    mcptest()
        .args(["bakeoff", "--fail-on-gap"])
        .assert()
        .code(6);
}

#[test]
fn committed_scorecard_matches_a_fresh_render() {
    // The committed docs artifact must stay in sync with the generator.
    let committed = std::fs::read_to_string(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../../docs/competitive-bakeoff.md"
    ))
    .expect("committed scorecard exists");
    let fresh = mcptest().arg("bakeoff").output().unwrap();
    let fresh = String::from_utf8(fresh.stdout).unwrap();
    assert_eq!(
        committed.trim_end(),
        fresh.trim_end(),
        "docs/competitive-bakeoff.md is stale; regenerate with `mcptest bakeoff --out`"
    );
}
