from __future__ import annotations

from leadtech_bmad_mcp.gates import apply_text_rules, get_gate_labels, load_gates_config


def test_load_gates_config_reads_yaml():
    config = load_gates_config()
    assert config["labels"]
    assert config["validate_plan"]
    assert config["validate_patch"]


def test_get_gate_labels_returns_configured_labels():
    labels = get_gate_labels()
    assert "Contracts-First / Zod-Infer / No-DTO" in labels


def test_apply_text_rules_triggers_backend_contracts_blocking_in_strict_mode():
    findings = apply_text_rules("validate_plan", "backend", "Plan backend sans mention de schema. Tests inclus.", strict=True)
    assert any(item["target"] == "blocking_issues" for item in findings)


def test_apply_text_rules_skips_backend_contracts_blocking_in_non_strict_mode():
    findings = apply_text_rules("validate_plan", "backend", "Plan backend sans mention de schema. Tests inclus.", strict=False)
    assert not any(item["target"] == "blocking_issues" for item in findings)
    assert any(item["target"] == "must_do" for item in findings)


def test_apply_text_rules_respects_required_pattern():
    findings = apply_text_rules("validate_plan", "backend", "Approche contracts-first avec Zod et tests.", strict=True)
    assert not any(item["rule_id"] == "backend_contracts" for item in findings)


def test_apply_text_rules_skips_test_hint_when_tests_present():
    findings = apply_text_rules("validate_patch", "backend", "describe('x', () => { it('works', () => {}) })", strict=True)
    assert not any(item["rule_id"] == "tests_visible_in_diff" for item in findings)
