//! Golden tests for the strict input-schema lint and autofix (WOR-1241).

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

use serde_json::Value;

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()
}

fn load_json(rel: &str) -> Value {
    let raw = std::fs::read_to_string(workspace_root().join(rel))
        .unwrap_or_else(|e| panic!("read {rel}: {e}"));
    serde_json::from_str(&raw).unwrap_or_else(|e| panic!("parse {rel}: {e}"))
}

#[test]
fn tighten_matches_the_golden_sidecar() {
    let loose = load_json("examples/tool-schema-lint/loose.json");
    let golden = load_json("examples/tool-schema-lint/tightened.json");
    assert_eq!(
        mcptest_core::lint::tighten(&loose),
        golden,
        "tighten output drifted from the committed golden sidecar"
    );
}

#[test]
fn tool_schema_lint_suite_validates() {
    let schema = load_json("schemas/v1.json");
    let validator = jsonschema::validator_for(&schema).expect("schema compiles");
    let raw = std::fs::read_to_string(workspace_root().join("examples/tool-schema-lint/suite.yml"))
        .expect("read suite.yml");
    let yaml: serde_yaml::Value = serde_yaml::from_str(&raw).expect("suite parses as YAML");
    let value: Value = serde_json::to_value(yaml).expect("YAML to JSON");
    let errors: Vec<String> = validator
        .iter_errors(&value)
        .map(|e| e.to_string())
        .collect();
    assert!(
        errors.is_empty(),
        "tool-schema-lint suite should validate: {errors:#?}"
    );
}
