"""The curated measures library (data/measures_curated.json) must load, cover all
40 CRA Annex I requirements, and surface full text (name + norm_refs) in the
assessment output."""
from compliance.api.cra_annex_i_data import (
    ANNEX_I_REQUIREMENTS,
    MEASURES,
    MEASURE_DETAILS,
)
from compliance.services.cra_finding_mapper import assess_findings_payload


def test_library_loaded():
    assert len(MEASURE_DETAILS) >= 24
    # MEASURES (id->name) is the superset incl. the original M540-M548.
    assert "M540" in MEASURES and "M600" in MEASURES


def test_all_40_requirements_have_a_measure():
    uncovered = [r["req_id"] for r in ANNEX_I_REQUIREMENTS if not r.get("mapped_measures")]
    assert uncovered == [], f"uncovered: {uncovered}"


def test_mapped_measures_resolve_to_known_ids():
    for r in ANNEX_I_REQUIREMENTS:
        for mid in r.get("mapped_measures", []):
            assert mid in MEASURES, f"{r['req_id']} -> unknown measure {mid}"


def test_assessment_open_measures_carry_full_text():
    res = assess_findings_payload({"findings": [{"id": "x", "cwe": "CWE-79", "severity": "high"}]})
    assert res["open_measures"], "expected at least one measure"
    om = res["open_measures"][0]
    assert om.get("name")
    assert "norm_refs" in om
