"""
Pure-logic tests for AdaptiveDecompositionStrategy._calculate_confidence.

Tests the confidence ratio calculation with candidate boost and cap,
requiring no LLM or search calls.
"""

from unittest.mock import Mock

from local_deep_research.advanced_search_system.strategies.adaptive_decomposition_strategy import (
    AdaptiveDecompositionStrategy,
)


def _make_strategy(constraints=None, verified_facts=None, candidates=None):
    """Build a minimal Mock with the working_knowledge dict."""
    s = Mock(spec=[])
    s.working_knowledge = {
        "constraints": constraints or [],
        "verified_facts": verified_facts or [],
        "candidates": candidates or [],
    }
    return s


class TestCalculateConfidence:
    """Verify confidence calculation: verified/total ratio + candidate boost, capped at 0.95."""

    def test_no_constraints(self):
        """No constraints -> 0.0."""
        s = _make_strategy(constraints=[], verified_facts=["a"])
        result = AdaptiveDecompositionStrategy._calculate_confidence(s)
        assert result == 0.0

    def test_half_verified(self):
        """2 verified out of 4 constraints -> 0.5."""
        s = _make_strategy(
            constraints=["c1", "c2", "c3", "c4"],
            verified_facts=["f1", "f2"],
        )
        result = AdaptiveDecompositionStrategy._calculate_confidence(s)
        assert result == 0.5

    def test_candidate_boost(self):
        """Having candidates adds 0.1 boost."""
        s = _make_strategy(
            constraints=["c1", "c2", "c3", "c4"],
            verified_facts=["f1", "f2"],
            candidates=["cand1"],
        )
        result = AdaptiveDecompositionStrategy._calculate_confidence(s)
        assert abs(result - 0.6) < 1e-9

    def test_cap_at_095(self):
        """Confidence should never exceed 0.95."""
        s = _make_strategy(
            constraints=["c1"],
            verified_facts=["f1"],
            candidates=["cand1"],
        )
        # 1/1 + 0.1 = 1.1, but capped at 0.95
        result = AdaptiveDecompositionStrategy._calculate_confidence(s)
        assert result == 0.95

    def test_all_verified_no_candidates(self):
        """All verified, no candidates -> base ratio (capped at 0.95)."""
        s = _make_strategy(
            constraints=["c1", "c2"],
            verified_facts=["f1", "f2"],
        )
        result = AdaptiveDecompositionStrategy._calculate_confidence(s)
        assert result == 0.95

    def test_none_verified(self):
        """No verified facts -> 0.0 (no candidate boost since 0+0.1 stays)."""
        s = _make_strategy(
            constraints=["c1", "c2"],
            verified_facts=[],
            candidates=["cand1"],
        )
        # 0/2 + 0.1 = 0.1
        result = AdaptiveDecompositionStrategy._calculate_confidence(s)
        assert abs(result - 0.1) < 1e-9
