"""Unit-Tests für die DSE Shadow-Verdrahtung (compute_obligation_shadow, pure)."""
from compliance.services.specialist_agents.dse._obligation_shadow import (
    compute_obligation_shadow,
)

NON_LLM = "art20_right_exists_core"          # nicht in der LLM_REQUIRED-Registry
LLM_REQ = "third_country_transfer_disclosed"  # in der LLM_REQUIRED-Registry


def _markers(n, ob, cond=None):
    return {f"C{i}": {"obl": [ob], "cond": cond} for i in range(n)}


class TestComputeShadow:
    def test_collapse_and_delta(self):
        results = [{"control_id": f"C{i}", "passed": False} for i in range(5)]
        s = compute_obligation_shadow(results, "x", _markers(5, NON_LLM))
        assert s["legacy_control_findings"] == 5
        assert s["obligation_findings"] == 1          # 5 → 1
        assert s["failed_by_current_checker"] == 1
        assert s["recall_limited"] == 0
        assert s["collapse_factor"] == 5.0
        assert s["met_failed_delta"] == 4
        assert s["met_count"] == 0
        top = s["top_collapsed_obligations"][0]
        assert top["obligation"] == NON_LLM and top["fehlt"] == 5
        assert top["recall_limited"] is False

    def test_fp_correction_one_passed_collapses_to_met(self):
        results = [{"control_id": f"C{i}", "passed": i == 0} for i in range(5)]
        s = compute_obligation_shadow(results, "x", _markers(5, NON_LLM))
        assert s["legacy_control_findings"] == 4
        assert s["obligation_findings"] == 0          # MET (anderswo erfüllt)
        assert s["met_failed_delta"] == 4

    def test_na_when_predicate_false(self):
        results = [{"control_id": "C0", "passed": False}]
        m = {"C0": {"obl": [LLM_REQ], "cond": "has_third_country_transfer"}}
        s = compute_obligation_shadow(results, "nur innerhalb der eu", m)
        assert s["na_count"] == 1
        assert s["obligation_findings"] == 0          # NA statt FEHLT

    def test_no_markers_returns_status(self):
        s = compute_obligation_shadow([{"control_id": "C0", "passed": False}], "x", {})
        assert "no obligation" in s["status"]

    def test_does_not_mutate_results(self):
        results = [{"control_id": "C0", "passed": False}]
        compute_obligation_shadow(results, "x", _markers(1, NON_LLM))
        assert set(results[0].keys()) == {"control_id", "passed"}


class TestRecallSegregation:
    def test_llm_required_failed_is_recall_limited_not_real_gap(self):
        # 5 verfehlte third_country-Controls, Transfer-Text vorhanden → FAILED,
        # aber LLM_REQUIRED → RECALL_LIMITED, NICHT failed_by_current_checker.
        results = [{"control_id": f"C{i}", "passed": False} for i in range(5)]
        m = {f"C{i}": {"obl": [LLM_REQ], "cond": "has_third_country_transfer"}
             for i in range(5)}
        s = compute_obligation_shadow(results, "übermittlung in ein drittland", m)
        assert s["obligation_findings"] == 1
        assert s["recall_limited"] == 1
        assert s["failed_by_current_checker"] == 0
        assert s["recall_limited_obligations"] == [LLM_REQ]
        assert s["top_collapsed_obligations"][0]["recall_limited"] is True

    def test_mixed_real_gap_and_recall_limited(self):
        results = [{"control_id": "A", "passed": False}, {"control_id": "B", "passed": False}]
        m = {"A": {"obl": [NON_LLM], "cond": None},
             "B": {"obl": [LLM_REQ], "cond": "has_third_country_transfer"}}
        s = compute_obligation_shadow(results, "übermittlung in ein drittland", m)
        assert s["obligation_findings"] == 2
        assert s["failed_by_current_checker"] == 1
        assert s["recall_limited"] == 1
