"""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 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
