use std::fs;
use std::path::Path;

fn count_lines(path: &Path) -> usize {
    fs::read_to_string(path)
        .expect("must read source file")
        .lines()
        .count()
}

#[test]
fn tool_modules_stay_small_and_single_purpose() {
    let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/tools");

    for entry in fs::read_dir(&dir).expect("tools directory must exist") {
        let entry = entry.expect("valid directory entry");
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("rs") {
            continue;
        }

        let file = path
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or_default();
        // mod.rs is the registry; common.rs is the shared validator
        // surface (whitelisted by the second test below). Both grow
        // independently of any single tool's complexity, so they
        // shouldn't be capped by the per-tool size guardrail.
        if file == "mod.rs" || file == "common.rs" {
            continue;
        }

        let lines = count_lines(&path);
        assert!(
            lines <= 140,
            "MCP tool file {file} has {lines} lines; split logic into core::ops/helpers"
        );
    }
}

#[test]
fn tool_registry_explicitly_exports_common_validators() {
    let mod_rs = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/tools/mod.rs");
    let content = fs::read_to_string(mod_rs).expect("must read tools/mod.rs");
    assert!(
        content.contains("pub mod common;"),
        "tools/common.rs should remain the shared validation surface"
    );
}
