"""High-value tests for filters module: base_filter.py and followup_relevance_filter.py.

Covers a minimal BaseFilter subclass and FollowUpRelevanceFilter's
regex fallback for non-JSON LLM responses plus its ``max_results``
configuration.
"""

from unittest.mock import MagicMock

from local_deep_research.advanced_search_system.filters.base_filter import (
    BaseFilter,
)
from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (
    FollowUpRelevanceFilter,
)


class TestBaseFilterABC:
    """Test BaseFilter abstract base class."""

    def test_complete_subclass_works(self):
        class Complete(BaseFilter):
            def filter_results(self, results, query, **kwargs):
                return results

        f = Complete()
        assert isinstance(f, BaseFilter)


class TestFollowUpRelevanceFilterWithModel:
    """Test FollowUpRelevanceFilter with mocked LLM."""

    def _make_filter(self, llm_content="[0, 2, 4]"):
        model = MagicMock()
        model.invoke.return_value = MagicMock(content=llm_content)
        return FollowUpRelevanceFilter(model=model)

    def test_non_json_response_regex_fallback(self):
        """Non-JSON LLM response falls back to regex number extraction."""
        f = self._make_filter("The relevant sources are indices 0, 2, and 4.")
        results = [
            {"title": f"R{i}", "url": f"http://{i}.com"} for i in range(5)
        ]
        filtered = f.filter_results(results, "query")
        assert len(filtered) == 3
        assert filtered[0]["title"] == "R0"
        assert filtered[1]["title"] == "R2"
        assert filtered[2]["title"] == "R4"


class TestFollowUpRelevanceFilterMaxResults:
    """Test max_results parameter."""

    def test_default_max_results_is_ten(self):
        """Default max_results parameter is 10."""
        f = FollowUpRelevanceFilter(model=None)
        results = [
            {"title": f"R{i}", "url": f"http://{i}.com"} for i in range(20)
        ]
        filtered = f.filter_results(results, "query")
        assert len(filtered) == 10

    def test_custom_max_results(self):
        """Custom max_results limits output."""
        f = FollowUpRelevanceFilter(model=None)
        results = [
            {"title": f"R{i}", "url": f"http://{i}.com"} for i in range(20)
        ]
        filtered = f.filter_results(results, "query", max_results=3)
        assert len(filtered) == 3
