"""Tests for pagination bounds enforcement (PR #1956).

Verifies:
- History routes clamp limit/offset/page/per_page to safe ranges
- Context overflow time series capped at 1000 for short periods
"""

import inspect


class TestHistoryRoutesPagination:
    """Verify get_history enforces limit/offset bounds."""

    def _get_source(self):
        from local_deep_research.web.routes.history_routes import get_history

        return inspect.getsource(get_history)

    def test_limit_clamped_to_max_500(self):
        """Source should clamp limit to max 500."""
        source = self._get_source()
        assert "min(limit, 500)" in source or "min(limit,500)" in source

    def test_limit_clamped_to_min_1(self):
        """Source should clamp limit to min 1."""
        source = self._get_source()
        assert "max(1," in source

    def test_offset_clamped_to_min_0(self):
        """Source should clamp offset to min 0."""
        source = self._get_source()
        assert "max(0, offset)" in source or "max(0,offset)" in source


class TestResearchRoutesPagination:
    """Verify api_get_history enforces page/per_page bounds."""

    def test_source_clamps_per_page_to_500(self):
        """per_page should be clamped to max 500."""
        from local_deep_research.web.routes.research_routes_orm import (
            api_get_history,
        )

        source = inspect.getsource(api_get_history)
        assert "min(per_page, 500)" in source or "min(per_page,500)" in source

    def test_source_clamps_page_min_1(self):
        """page should be clamped to min 1."""
        from local_deep_research.web.routes.research_routes_orm import (
            api_get_history,
        )

        source = inspect.getsource(api_get_history)
        assert "max(1," in source

    def test_source_clamps_per_page_min_1(self):
        """per_page should be clamped to min 1."""
        from local_deep_research.web.routes.research_routes_orm import (
            api_get_history,
        )

        source = inspect.getsource(api_get_history)
        assert "max(1, min(per_page" in source


class TestContextOverflowCap:
    """Verify time series query has a limit cap."""

    def test_short_period_capped_at_1000(self):
        """Short period time series queries should be capped at 1000."""
        from local_deep_research.web.routes.context_overflow_api import (
            get_context_overflow_metrics,
        )

        source = inspect.getsource(get_context_overflow_metrics)
        assert "limit(1000)" in source
