Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

17.9 KB
Raw Blame
"""
Tests for _get_metrics_from_encrypted_db() with non-empty data.

Existing tests mock all DB queries to return empty results, leaving the
data-processing branches (provider groups, recent researches, rate limit stats,
engine stats, alternative time filters) completely uncovered.
"""

import time
from typing import Any
from unittest.mock import MagicMock, Mock, patch

import pytest

from local_deep_research.metrics.token_counter import TokenCounter


def _patch_get_user_db_session(mock_session: MagicMock) -> Any:
    ctx = MagicMock()
    ctx.__enter__ = Mock(return_value=mock_session)
    ctx.__exit__ = Mock(return_value=False)
    return patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=ctx,
    )


def _make_session(
    all_sequence: list[Any] | None = None,
    count_sequence: list[int] | None = None,
) -> MagicMock:
    """Build a mock session using a single shared query chain (like existing tests).

    Args:
        all_sequence: list of return values for successive .all() calls.
            With "30d" period and empty data, .all() is called 5 times:
              1. sample_researches
              2. model_stats
              3. recent_research_data
              4. rate_limit_attempts
              5. engine_types
            With non-empty recent_data, an extra call is inserted after #3 (query_results).
            With non-empty engine_types, an extra call is appended (estimates).
        count_sequence: list of return values for successive .count() calls.
            Default: [0, 0, 0] (total_attempts, successful, rate_limit_events)
    """
    mock_session = MagicMock()
    q = MagicMock()
    mock_session.query.return_value = q
    for attr in (
        "filter",
        "filter_by",
        "with_entities",
        "group_by",
        "order_by",
        "limit",
        "distinct",
    ):
        getattr(q, attr).return_value = q

    q.scalar.return_value = 0
    q.first.return_value = MagicMock(
        total_input_tokens=500,
        total_output_tokens=500,
        avg_input_tokens=50,
        avg_output_tokens=50,
        avg_total_tokens=100,
    )

    q.all.return_value = []
    if all_sequence is not None:
        all_iter = iter(all_sequence)
        q.all.side_effect = lambda: next(all_iter, [])

    if count_sequence is not None:
        count_iter = iter(count_sequence)
        q.count.side_effect = lambda: next(count_iter, 0)
    else:
        q.count.return_value = 0

    return mock_session


class TestMetricsWithModelData:
    """Tests for model data processing branches when data is present."""

    def test_provider_comes_from_grouped_token_usage(self):
        """The provider and totals come from the same grouped usage rows."""
        counter = TokenCounter()

        model_stat = MagicMock(
            model_name="gpt-4",
            model_provider="openai",
            tokens=500,
            calls=10,
            prompt_tokens=300,
            completion_tokens=200,
        )
        # .all() order: samples, model_stats, recent, attempts, engines
        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [model_stat],  # 2. model_stats
                [],  # 3. recent_research_data
                [],  # 4. rate_limit_attempts
                [],  # 5. engine_types
            ]
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        assert len(result["by_model"]) == 1
        assert result["by_model"][0]["model"] == "gpt-4"
        assert result["by_model"][0]["provider"] == "openai"

    def test_by_model_preserves_unknown_provider(self):
        """A stored 'unknown' provider remains unknown in the summary."""
        counter = TokenCounter()

        model_stat = MagicMock(
            model_name="llama-3",
            model_provider="unknown",
            tokens=200,
            calls=5,
            prompt_tokens=100,
            completion_tokens=100,
        )

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [model_stat],  # 2. model_stats
                [],  # 3. recent_research_data
                [],  # 4. rate_limit_attempts
                [],  # 5. engine_types
            ]
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        assert result["by_model"][0]["provider"] == "unknown"

    def test_recent_research_query_map_loaded(self):
        """Recent research queries are batch-loaded and mapped."""
        counter = TokenCounter()

        recent = MagicMock(
            research_id="res-123",
            token_count=750,
            latest_timestamp="2026-01-01T00:00:00",
        )
        # query_results rows are unpacked as tuples: (research_id, research_query)
        query_row = ("res-123", "climate change effects")

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [],  # 2. model_stats
                [recent],  # 3. recent_research_data → triggers query batch load
                [query_row],  # 3b. query_results (tuple rows)
                [],  # 4. rate_limit_attempts
                [],  # 5. engine_types
            ]
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        assert len(result["recent_researches"]) == 1
        assert (
            result["recent_researches"][0]["query"] == "climate change effects"
        )
        assert result["recent_researches"][0]["tokens"] == 750

    def test_recent_research_fallback_query_text(self):
        """When no query text found, falls back to 'Research {id}' format."""
        counter = TokenCounter()

        recent = MagicMock(
            research_id="res-456",
            token_count=300,
            latest_timestamp="2026-02-01",
        )

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [],  # 2. model_stats
                [recent],  # 3. recent_research_data
                [],  # 3b. query_results (empty)
                [],  # 4. rate_limit_attempts
                [],  # 5. engine_types
            ]
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        assert result["recent_researches"][0]["query"] == "Research res-456"


class TestMetricsTimeFilters:
    """Tests for alternative time filter periods.

    Each test asserts ``"rate_limiting" in result``. That key is produced only
    on the full success path of ``_get_metrics_from_encrypted_db``; it is absent
    from the ``_get_empty_metrics()`` structure the method returns from its broad
    ``except`` fallback. Asserting it — rather than ``"total_tokens"``, which is
    present in both — guarantees the period branch ran to completion instead of
    silently crashing into the fallback.
    """

    def _run_with_period(self, period):
        counter = TokenCounter()
        # Provide explicit empty all_sequence so .all() returns [] not MagicMock
        mock_session = _make_session(
            all_sequence=[[], [], [], [], [], [], [], [], [], []],
        )
        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                return counter._get_metrics_from_encrypted_db(period, "all")

    def test_period_today(self):
        result = self._run_with_period("today")
        assert "rate_limiting" in result

    def test_period_week(self):
        result = self._run_with_period("week")
        assert "rate_limiting" in result

    def test_period_month(self):
        result = self._run_with_period("month")
        assert "rate_limiting" in result

    def test_period_3m(self):
        result = self._run_with_period("3m")
        assert "rate_limiting" in result

    def test_period_1y(self):
        result = self._run_with_period("1y")
        assert "rate_limiting" in result


class TestRateLimitPeriodWindow:
    """#6942: the rate-limit cutoff must honor the full period vocabulary.

    ``PERIOD_DAYS_MAP`` accepts the ``90d``/``365d`` aliases, and an
    unknown period falls back to 30 days for the token window. The old
    local ``7d/30d/3m/1y`` ladder knew none of these, so the rate-limit
    rows in the same result were computed all-time under a bounded
    label. Here the session mock keys one query chain per queried model,
    so the ``RateLimitAttempt`` chain's filter calls can be inspected
    directly.
    """

    @pytest.mark.parametrize(
        "period,days", [("90d", 90), ("365d", 365), ("today", 30)]
    )
    def test_rate_limit_rows_respect_the_bounded_window(self, period, days):
        from local_deep_research.database.models import RateLimitAttempt

        counter = TokenCounter()
        chains: dict[Any, MagicMock] = {}

        def _query_for(*args, **kwargs):
            chain = chains.setdefault(args[0], MagicMock())
            for attr in (
                "filter",
                "filter_by",
                "with_entities",
                "group_by",
                "order_by",
                "limit",
                "distinct",
            ):
                getattr(chain, attr).return_value = chain
            chain.scalar.return_value = 0
            chain.count.return_value = 0
            chain.all.return_value = []
            chain.first.return_value = MagicMock(
                total_input_tokens=0,
                total_output_tokens=0,
                avg_input_tokens=0,
                avg_output_tokens=0,
                avg_total_tokens=0,
            )
            return chain

        mock_session = MagicMock()
        mock_session.query.side_effect = _query_for

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db(period, "all")

        assert "rate_limiting" in result, "the full success path did not run"
        attempts_chain = chains[RateLimitAttempt]
        cutoffs = [
            condition.right.value
            for call in attempts_chain.filter.call_args_list
            for condition in call.args
            if "timestamp" in str(getattr(condition, "left", ""))
        ]
        assert cutoffs, (
            "no timestamp window was applied to the rate-limit rows: the "
            "period was silently widened to all-time"
        )
        expected = time.time() - days * 24 * 3600
        for cutoff in cutoffs:
            assert abs(cutoff - expected) < 300, (
                f"cutoff {cutoff} does not match the {period} window"
            )


class TestMetricsRateLimiting:
    """Tests for rate limiting aggregation when data is present."""

    def test_avg_wait_time_with_attempts(self):
        """Average wait times calculated correctly when attempts exist."""
        counter = TokenCounter()

        attempt1 = MagicMock(wait_time=2.0, success=True, engine_type="searxng")
        attempt2 = MagicMock(
            wait_time=4.0, success=False, engine_type="searxng"
        )

        engine_row = MagicMock(engine_type="searxng")

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [],  # 2. model_stats
                [],  # 3. recent_research_data
                [attempt1, attempt2],  # 4. rate_limit_attempts
                [engine_row],  # 5. engine_types → triggers estimates
                [],  # 5b. estimates (empty)
            ],
            count_sequence=[2, 1, 0],  # total, successful, rate_limit_events
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        assert result["rate_limiting"]["avg_wait_time"] == 3.0
        assert result["rate_limiting"]["avg_successful_wait"] == 2.0

    def test_engine_stats_with_estimates(self):
        """Engine stats include RateLimitEstimate data when available."""
        counter = TokenCounter()

        attempt = MagicMock(wait_time=1.5, success=True, engine_type="brave")
        engine_row = MagicMock(engine_type="brave")

        estimate = MagicMock()
        estimate.engine_type = "brave"
        estimate.base_wait_seconds = 1.0
        estimate.min_wait_seconds = 0.5
        estimate.max_wait_seconds = 3.0
        estimate.success_rate = 0.95
        estimate.total_attempts = 100
        estimate.last_updated = time.time()

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [],  # 2. model_stats
                [],  # 3. recent_research_data
                [attempt],  # 4. rate_limit_attempts
                [engine_row],  # 5. engine_types
                [estimate],  # 5b. estimates
            ],
            count_sequence=[1, 1, 0],
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        stats = result["rate_limiting"]["engine_stats"]
        assert len(stats) >= 1
        brave_stats = [s for s in stats if s["engine"] == "brave"]
        assert len(brave_stats) == 1
        assert brave_stats[0]["base_wait_seconds"] == 1.0
        assert brave_stats[0]["status"] == "healthy"
        assert brave_stats[0]["last_updated"] != "Never"

    def test_engine_stats_without_estimates(self):
        """Engine stats fall back to recent success rate when no estimates."""
        counter = TokenCounter()

        attempt = MagicMock(wait_time=1.0, success=True, engine_type="wiki")
        engine_row = MagicMock(engine_type="wiki")

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [],  # 2. model_stats
                [],  # 3. recent_research_data
                [attempt],  # 4. rate_limit_attempts
                [engine_row],  # 5. engine_types
                [],  # 5b. estimates (empty)
            ],
            count_sequence=[1, 1, 0],
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        stats = result["rate_limiting"]["engine_stats"]
        wiki_stats = [s for s in stats if s["engine"] == "wiki"]
        assert len(wiki_stats) == 1
        assert wiki_stats[0]["last_updated"] == "Never"
        assert wiki_stats[0]["status"] == "healthy"

    def test_engine_status_degraded(self):
        """Engine with moderate success rate gets 'degraded' status."""
        counter = TokenCounter()

        attempts = []
        for i in range(3):
            a = MagicMock(wait_time=1.0, success=(i < 2), engine_type="ddg")
            attempts.append(a)

        engine_row = MagicMock(engine_type="ddg")

        mock_session = _make_session(
            all_sequence=[
                [],  # 1. sample_researches
                [],  # 2. model_stats
                [],  # 3. recent_research_data
                attempts,  # 4. rate_limit_attempts
                [engine_row],  # 5. engine_types
                [],  # 5b. estimates
            ],
            count_sequence=[3, 2, 0],
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        stats = result["rate_limiting"]["engine_stats"]
        ddg_stats = [s for s in stats if s["engine"] == "ddg"]
        assert len(ddg_stats) == 1
        assert ddg_stats[0]["status"] == "degraded"


class TestSampleResearchDebugLogging:
    """Tests for sample research debug logging branches."""

    def test_sample_researches_present(self):
        """When sample researches exist, debug logging path is exercised."""
        counter = TokenCounter()

        sample = ("id-1", "2026-01-01T00:00:00", "quick")

        mock_session = _make_session(
            all_sequence=[
                [sample],  # 1. sample_researches (non-empty)
                [],  # 2. model_stats
                [],  # 3. recent_research_data
                [],  # 4. rate_limit_attempts
                [],  # 5. engine_types
            ]
        )

        with patch(
            "local_deep_research.metrics.token_counter.get_current_username",
            return_value="testuser",
        ):
            with _patch_get_user_db_session(mock_session):
                result = counter._get_metrics_from_encrypted_db("30d", "all")

        assert "total_tokens" in result