//! Integration coverage for the structured-output conformance check
//! (rule SCHEMA-006), driven by JSON fixtures so the conformant and
//! drifted cases stay readable and reviewable on disk.

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

use serde_json::Value;

use mcptest_core::conformance::{check_output_schema_conformance, OUTPUT_SCHEMA_RULE_ID};

/// Load one `{ "tool": ..., "result": ... }` pair from `tests/fixtures/output-schema`.
fn load_pair(name: &str) -> Value {
    let path: PathBuf = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("output-schema")
        .join(name);
    let body = std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("fixture {} must exist: {e}", path.display()));
    serde_json::from_str(&body)
        .unwrap_or_else(|e| panic!("fixture {} must be valid JSON: {e}", path.display()))
}

#[test]
fn conformant_fixture_passes() {
    let pair = load_pair("conformant.json");
    let finding = check_output_schema_conformance(&pair["tool"], &pair["result"])
        .expect("the fixture tool declares an outputSchema");
    assert!(finding.pass, "expected a pass, diffs: {:?}", finding.diffs);
    assert_eq!(finding.rule_id, OUTPUT_SCHEMA_RULE_ID);
}

#[test]
fn drifted_fixture_fails_with_diffs() {
    let pair = load_pair("drifted.json");
    let finding = check_output_schema_conformance(&pair["tool"], &pair["result"])
        .expect("the fixture tool declares an outputSchema");
    assert!(!finding.pass, "expected a conformance failure");
    assert!(
        !finding.diffs.is_empty(),
        "a type mismatch should produce schema diffs"
    );
    assert_eq!(finding.rule_id, OUTPUT_SCHEMA_RULE_ID);
}
