from __future__ import annotations

from grantflow.eval.harness import evaluate_expectations


def test_eval_harness_expectations_detect_regression():
    metrics = {
        "toc_schema_valid": True,
        "needs_revision": False,
        "quality_score": 8.5,
        "critic_score": 8.0,
        "citations_total": 3,
        "architect_citation_count": 2,
        "architect_claim_citation_count": 2,
        "mel_citation_count": 1,
        "high_confidence_citation_count": 1,
        "architect_threshold_hit_rate": 0.5,
        "architect_key_claim_coverage_ratio": 0.6,
        "architect_fallback_claim_ratio": 0.5,
        "citation_confidence_avg": 0.4,
        "low_confidence_citation_count": 1,
        "rag_low_confidence_citation_count": 0,
        "fallback_namespace_citation_count": 0,
        "retrieval_grounded_citation_rate": 0.5,
        "non_retrieval_citation_rate": 0.5,
        "traceability_gap_citation_rate": 0.5,
        "doc_id_present_citation_rate": 0.6,
        "retrieval_rank_present_citation_rate": 0.6,
        "retrieval_confidence_present_citation_rate": 0.6,
        "retrieval_metadata_complete_citation_rate": 0.6,
        "draft_version_count": 2,
        "fatal_flaw_count": 0,
        "high_severity_fatal_flaw_count": 0,
        "error_count": 0,
        "has_toc_draft": True,
        "has_logframe_draft": True,
    }
    passed, checks = evaluate_expectations(
        metrics,
        {
            "toc_schema_valid": True,
            "min_quality_score": 9.0,
            "min_architect_threshold_hit_rate": 0.8,
            "min_architect_key_claim_coverage_ratio": 0.8,
            "max_fatal_flaws": 0,
            "max_low_confidence_citations": 0,
            "max_rag_low_confidence_citations": 0,
            "max_architect_fallback_claim_ratio": 0.4,
            "max_fallback_namespace_citations": 0,
            "min_retrieval_grounded_citation_rate": 0.7,
            "max_non_retrieval_citation_rate": 0.4,
            "max_traceability_gap_citation_rate": 0.4,
            "min_doc_id_present_citation_rate": 0.7,
            "min_retrieval_rank_present_citation_rate": 0.7,
            "min_retrieval_confidence_present_citation_rate": 0.7,
            "min_retrieval_metadata_complete_citation_rate": 0.7,
            "require_toc_draft": True,
        },
    )

    assert passed is False
    failing = [c for c in checks if not c["passed"]]
    assert any(c["name"] == "min_quality_score" for c in failing)
    assert any(c["name"] == "min_retrieval_grounded_citation_rate" for c in failing)
