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.

19.6 KB
Raw Blame
"""Cover metrics aggregation, journal enrichment, and failure edge branches."""

import json
from collections.abc import Callable, Iterator
from contextlib import AbstractContextManager, contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock

from datetime import UTC, datetime

import pytest
from fastapi.responses import JSONResponse
from pytest_mock import MockerFixture
from starlette.requests import Request

from local_deep_research.web.routers import metrics

type JsonValue = (
    None | bool | int | float | str | list[JsonValue] | dict[str, JsonValue]
)


def _request(body: JsonValue = None, query: str = "") -> Request:
    payload = json.dumps(body).encode()

    async def receive() -> dict[str, str | bytes | bool]:
        return {"type": "http.request", "body": payload, "more_body": False}

    return Request(
        {
            "type": "http",
            "method": "POST",
            "path": "/metrics/test",
            "headers": [(b"content-type", b"application/json")],
            "query_string": query.encode(),
            "client": ("127.0.0.1", 1),
            "server": ("test", 80),
            "scheme": "http",
        },
        receive,
    )


def _db_factory(
    session: MagicMock,
) -> Callable[..., AbstractContextManager[MagicMock]]:
    @contextmanager
    def open_session(*_args: str) -> Iterator[MagicMock]:
        yield session

    return open_session


def _chain_query() -> MagicMock:
    query = MagicMock()
    for method in (
        "filter",
        "join",
        "group_by",
        "order_by",
        "limit",
        "distinct",
    ):
        getattr(query, method).return_value = query
    return query


def _broken_db(*_args: str) -> AbstractContextManager[MagicMock]:
    @contextmanager
    def fail() -> Iterator[MagicMock]:
        raise RuntimeError("db down")
        yield MagicMock()

    return fail()


async def _run_inline[T](callback: Callable[[], T]) -> T:
    return callback()


def test_link_analytics_skips_resource_parser_error(
    mocker: MockerFixture,
) -> None:
    """Mutation: delete the per-row ``except Exception`` at metrics.py:322-323
    and the ``RuntimeError`` reaches the function-level handler at
    metrics.py:445, whose fallback dict *also* carries
    ``"total_unique_domains": 0`` and leaves ``parser.call_count`` at 2. Besides
    ``total_links`` (1 from the real return, 0 from the fallback), the two
    dicts have different key sets: the fallback carries ``"error"`` and
    ``"academic_vs_general"``, neither of which the success dict has, while
    only the success dict carries ``"category_distribution"``,
    ``"domain_categories"``, ``"domain_metrics"``, ``"temporal_trend"``, and
    ``"total_researches"``.
    """
    resource = SimpleNamespace(
        url="https://broken.example/x",
        research_id="r1",
        created_at=None,
        source_type=None,
        title=None,
        has_preview=False,
    )
    resource_query = MagicMock()
    resource_query.all.return_value = [resource]
    classification_query = MagicMock()
    classification_query.filter.return_value.all.return_value = []
    session = MagicMock()
    session.query.side_effect = [resource_query, classification_query]
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )
    parser = mocker.patch.object(
        metrics,
        "_extract_domain",
        side_effect=["broken.example", RuntimeError("bad")],
    )

    result = metrics.get_link_analytics("all", username="alice")[
        "link_analytics"
    ]

    assert "error" not in result
    assert result["total_links"] == 1
    assert result["total_unique_domains"] == 0
    assert result["source_type_analysis"] == {}
    assert parser.call_count == 2


@pytest.mark.parametrize(
    ("period", "days"),
    [
        ("7d", 7),
        ("30d", 30),
        ("3m", 90),
        ("90d", 90),
        ("1y", 365),
        ("365d", 365),
        # Unknown periods fall back to 30 days, matching the token window
        # (get_time_filter_condition) in the same /api/metrics response.
        ("today", 30),
        ("", 30),
    ],
)
def test_rate_limit_period_uses_the_shared_period_window(
    mocker: MockerFixture, period: str, days: int
) -> None:
    """#6942: every bounded period, including the ``90d``/``365d`` aliases
    the old local ``7d/30d/3m/1y`` ladder did not know, filters the
    ``RateLimitEstimate`` rows on ``last_updated`` at the right bound.

    The real ``get_period_cutoff`` runs here (no stub), so this pins the
    whole path from the router's period argument to the bound it applies:
    a router that hard-codes a period, allowlists only the old
    vocabulary, routes a period to the ``cutoff_time = 0`` (all-time)
    arm, or filters on the wrong column fails. ``"all"`` (no filter) is
    pinned by ``test_period_all_skips_the_recency_filter`` in
    ``test_metrics_analytics_aggregation.py``.
    """
    query = _chain_query()
    query.filter.return_value = query
    query.all.return_value = []
    session = MagicMock()
    session.query.return_value = query
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )

    expected = datetime.now(UTC).timestamp() - days * 24 * 3600
    metrics.get_rate_limiting_analytics(period, username="alice")

    query.filter.assert_called_once()
    criterion = query.filter.call_args.args[0]
    assert "last_updated" in str(criterion)
    # 300 s tolerance absorbs the wall clock moving during the call; the
    # nearest wrong window (30 vs 90 days) is ~60 days away.
    assert criterion.right.value == pytest.approx(expected, abs=300)


def test_research_link_metrics_classifies_skips_and_recovers(
    mocker: MockerFixture,
) -> None:
    resources = [
        SimpleNamespace(
            url="https://example.com/a", title="A", content_preview="preview"
        ),
        SimpleNamespace(url="/relative/path", title=None, content_preview=None),
    ]
    resource_query = MagicMock()
    resource_query.filter.return_value.all.return_value = resources
    classification = SimpleNamespace(domain="example.com", category="Science")
    classification_query = MagicMock()
    classification_query.filter.return_value.all.return_value = [classification]
    session = MagicMock()
    session.query.side_effect = [resource_query, classification_query]
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )

    result = metrics.api_research_link_metrics(
        _request(), "research-1", username="alice"
    )

    assert result["data"]["category_distribution"] == {"Science": 1}
    assert result["data"]["unique_domains"] == 1

    session.query.side_effect = [resource_query, classification_query]
    # The four side effects are consumed in call order and mirror the two
    # passes over the same two resources: pass one has no per-row guard, so
    # the failure is scheduled for the second visit to "/relative/path".
    parser = mocker.patch.object(
        metrics,
        "_extract_domain",
        side_effect=[
            "example.com",
            None,
            "example.com",
            AttributeError("bad row"),
        ],
    )
    result = metrics.api_research_link_metrics(
        _request(), "research-1", username="alice"
    )
    # Deleting the per-row ``except (AttributeError, KeyError)``
    # (metrics.py:1075-1076) sends the raised AttributeError to the
    # function-level handler, which returns a JSONResponse with status
    # "error" and no "data" key.
    assert not isinstance(result, JSONResponse)
    assert result["status"] == "success"
    assert result["data"]["unique_domains"] == 1
    assert result["data"]["category_distribution"] == {"Science": 1}
    # ``_extract_domain`` runs once per resource in each of the two passes
    # (metrics.py:1033-1039 and :1054-1060), so all four ``side_effect``
    # entries are consumed and the fourth (the ``AttributeError``) is the one
    # that exercises ``except (AttributeError, KeyError)`` at
    # metrics.py:1075-1076. Pin the exact count rather than a lower bound: a
    # single-pass refactor would only call ``_extract_domain`` twice, so the
    # AttributeError side effect (and the except branch under test) would
    # never be reached, and ``>= 2`` would hide that silently.
    assert parser.call_count == 4


@pytest.mark.asyncio
async def test_rating_update_feedback_and_save_failure(
    mocker: MockerFixture,
) -> None:
    existing = SimpleNamespace(
        rating=2,
        updated_at=None,
        accuracy=None,
        completeness=None,
        relevance=None,
        readability=None,
        feedback="old",
    )
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        existing
    )
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )
    mocker.patch.object(metrics, "run_db_sync", side_effect=_run_inline)

    result = await metrics.api_save_research_rating(
        _request({"rating": 5, "feedback": "new"}),
        "research-1",
        username="alice",
    )

    assert result["status"] == "success"
    assert existing.feedback == "new"
    session.commit.assert_called_once()

    mocker.patch.object(metrics, "get_user_db_session", side_effect=_broken_db)
    result = await metrics.api_save_research_rating(
        _request({"rating": 4}), "research-1", username="alice"
    )
    assert isinstance(result, JSONResponse)
    assert result.status_code == 500


def test_cost_analytics_aggregates_records_by_research(
    mocker: MockerFixture,
) -> None:
    """Per-research grouping (metrics.py:2132-2163) on top of the overall
    summation: two rows sharing one research_id collapse to a single
    ``top_expensive_research`` entry carrying their combined cost.

    NOTE: the ``total_tokens`` assertion below documents the #5677 bug rather
    than correct behaviour — see the comment at that line.
    """
    records = [
        SimpleNamespace(
            model_name="model-a",
            model_provider="provider",
            prompt_tokens=100,
            completion_tokens=25,
            research_id="r1",
            timestamp="2026-01-01",
        ),
        SimpleNamespace(
            model_name="model-a",
            model_provider="provider",
            prompt_tokens=50,
            completion_tokens=25,
            research_id="r1",
            timestamp="2026-01-02",
        ),
    ]
    query = _chain_query()
    query.count.return_value = len(records)
    query.all.return_value = records
    session = MagicMock()
    session.query.return_value = query
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )
    mocker.patch.object(metrics, "get_time_filter_condition", return_value=None)
    calculator = MagicMock()
    # Keyed by prompt_tokens so the fake is independent of how many times the
    # endpoint recomputes each record.
    costs = {
        100: {
            "total_cost": 0.25,
            "prompt_cost": 0.20,
            "completion_cost": 0.05,
            "pricing_used": {"prompt": 2.0, "completion": 2.0},
        },
        50: {
            "total_cost": 0.10,
            "prompt_cost": 0.08,
            "completion_cost": 0.02,
            "pricing_used": {"prompt": 1.6, "completion": 0.8},
        },
    }
    calculator.calculate_cost_sync.side_effect = (
        lambda _model, prompt, _completion, *, provider: costs[prompt]
    )
    mocker.patch(
        "local_deep_research.metrics.pricing.cost_calculator.CostCalculator",
        return_value=calculator,
    )

    result = metrics.api_cost_analytics(_request(), username="alice")

    expected_totals = {
        "total_cost": 0.35,
        # Recomputed as prompt + completion rather than read from the stored
        # TokenUsage.total_tokens column. This pins CURRENT behaviour, not
        # desired behaviour: issue #5677 tracks the fix, and this assertion
        # must be updated (to the stored total) when that lands.
        "total_tokens": 200,
        "prompt_tokens": 150,
        "completion_tokens": 50,
    }
    assert {
        key: result["overview"][key] for key in expected_totals
    } == expected_totals
    assert result["overview"]["pricing_complete"] is True
    assert result["overview"]["unpriced_calls"] == 0
    assert result["top_expensive_research"] == [
        {
            "research_id": "r1",
            "total_cost": 0.35,
            "pricing_complete": True,
            "unpriced_calls": 0,
            "unpriced_tokens": 0,
        }
    ]
    assert result["research_count"] == 1
    # Reuse each calculated price for the overall and per-research totals.
    assert calculator.calculate_cost_sync.call_count == len(records)
    # record_count (2) stayed under the 1000-row cap at metrics.py:2076, so
    # the endpoint took the unlimited ``query.all()`` branch.
    query.limit.assert_not_called()


def test_journal_summary_is_included_only_on_request(
    mocker: MockerFixture,
) -> None:
    """Mutation: delete the ``include_summary`` guard (metrics.py:2738) so the
    three summary queries always run. Only the negative phase catches it — the
    positive phase alone stays green either way.
    """
    ref = MagicMock(available=True)
    ref.get_journals_page.return_value = ([{"name": "A"}], 1)
    ref.get_summary.return_value = {"avg_quality": 4}
    ref.get_quality_distribution.return_value = {"4": 1}
    ref.get_source_distribution.return_value = {"openalex": 1}
    mocker.patch(
        "local_deep_research.journal_quality.db.get_journal_reference_db",
        return_value=ref,
    )

    result = metrics.api_journal_quality(_request(), username="alice")

    assert "summary" not in result
    ref.get_summary.assert_not_called()
    ref.get_quality_distribution.assert_not_called()
    ref.get_source_distribution.assert_not_called()

    result = metrics.api_journal_quality(
        _request(query="include_summary=true"), username="alice"
    )

    assert result["summary"] == {
        "avg_quality": 4,
        "quality_distribution": {"4": 1},
        "source_distribution": {"openalex": 1},
    }
    ref.get_summary.assert_called_once_with()


def test_reference_helpers_cover_empty_failure_and_precedence(
    mocker: MockerFixture,
) -> None:
    ref = MagicMock()
    assert metrics._ref_db_lookup(None, "Journal") == {}
    assert metrics._ref_db_lookup(ref, "") == {}
    ref.lookup_source.side_effect = RuntimeError("db down")
    assert metrics._ref_db_lookup(ref, "Journal") == {}
    ref.lookup_source.side_effect = None
    ref.lookup_source.return_value = {
        "h_index": 8,
        "quartile": "Q1",
        "is_predatory": 1,
    }
    assert metrics._ref_db_lookup(ref, "Journal") == {
        "h_index": 8,
        "impact_factor": None,
        "sjr_quartile": "Q1",
        "is_predatory": True,
        "predatory_source": None,
        "is_in_doaj": False,
        "publisher": None,
    }

    mocker.patch(
        "local_deep_research.journal_quality.db.get_journal_reference_db",
        side_effect=RuntimeError("unavailable"),
    )
    assert metrics._get_ref_db_or_none() is None
    assert metrics._resolve_paper_quality(5, {}) == (5, "llm")
    assert metrics._resolve_paper_quality(None, {}) == (None, None)
    assert metrics._resolve_paper_quality(None, {"score_source": "doaj"}) == (
        None,
        None,
    )

    db = MagicMock()
    assert metrics._lookup_journal_llm_quality(db, []) == {}
    assert metrics._lookup_journal_llm_quality(db, ["", ""]) == {}
    db.query.assert_not_called()


def test_user_research_journals_enriches_and_counts(
    mocker: MockerFixture,
) -> None:
    rows = [
        SimpleNamespace(
            container_title="Journal A",
            paper_count=2,
            year_min=2020,
            year_max=2024,
        ),
        SimpleNamespace(
            container_title="Journal B",
            paper_count=1,
            year_min=2022,
            year_max=2022,
        ),
    ]
    query = _chain_query()
    query.all.side_effect = [rows, [("Journal A",), ("Journal B",)]]
    session = MagicMock(bind=MagicMock())
    session.query.return_value = query
    inspector = MagicMock()
    inspector.has_table.return_value = True
    mocker.patch("sqlalchemy.inspect", return_value=inspector)
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )
    ref = MagicMock()
    ref.lookup_sources_batch.return_value = {
        "journal a": {
            "quality": 4,
            "score_source": "openalex",
            "is_predatory": True,
        },
        "journal b": {},
    }
    ref.count_predatory_by_names.return_value = 1
    mocker.patch.object(metrics, "_get_ref_db_or_none", return_value=ref)
    mocker.patch.object(
        metrics, "_lookup_journal_llm_quality", return_value={"journal b": 5}
    )
    mocker.patch(
        "local_deep_research.journal_quality.scoring.normalize_name",
        side_effect=str.lower,
    )

    result = metrics.api_user_research_journals(_request(), username="alice")

    assert result["summary"] == {
        "total_journals": len(rows),
        "avg_quality": 4.5,
        "total_papers": sum(row.paper_count for row in rows),
        "predatory_blocked": 1,
    }
    assert result["quality_distribution"] == {"4": 1, "5": 1}
    ref.count_predatory_by_names.assert_called_once_with(
        ["Journal A", "Journal B"]
    )


@pytest.mark.parametrize("route", ["user", "research"])
def test_journal_routes_return_empty_when_tables_or_rows_missing(
    mocker: MockerFixture, route: str
) -> None:
    """Both phases end at the same ``_empty_response`` (metrics.py:2874 /
    :3043), so ``result["journals"] == []`` alone cannot tell the missing-table
    guard (metrics.py:2891-2892, :3060-3063) from the ``if not rows`` guard
    below it. Deleting the ``has_table`` check is caught by
    ``session.query.assert_not_called()``: without the early return the route
    goes on to query ``Paper`` (and, on the research route,
    ``ResearchHistory``).
    """
    session = MagicMock(bind=MagicMock())
    query = _chain_query()
    query.first.return_value = SimpleNamespace(id="research-1")
    query.all.return_value = []
    session.query.return_value = query
    mocker.patch.object(
        metrics, "get_user_db_session", side_effect=_db_factory(session)
    )
    inspector = MagicMock()
    mocker.patch("sqlalchemy.inspect", return_value=inspector)

    inspector.has_table.return_value = False
    if route == "user":
        result = metrics.api_user_research_journals(
            _request(), username="alice"
        )
    else:
        result = metrics.api_research_journals(
            _request(), "research-1", username="alice"
        )
    assert result["journals"] == []
    session.query.assert_not_called()

    inspector.has_table.return_value = True
    if route == "user":
        result = metrics.api_user_research_journals(
            _request(), username="alice"
        )
    else:
        result = metrics.api_research_journals(
            _request(), "research-1", username="alice"
        )
    assert result["journals"] == []
    # The second phase got past the guard and fell out at ``if not rows``.
    assert session.query.called


@pytest.mark.parametrize("route", ["user", "research"])
def test_journal_routes_convert_database_failure_to_500(
    mocker: MockerFixture, route: str
) -> None:
    mocker.patch.object(metrics, "get_user_db_session", side_effect=_broken_db)
    if route == "user":
        result = metrics.api_user_research_journals(
            _request(), username="alice"
        )
    else:
        result = metrics.api_research_journals(
            _request(), "research-1", username="alice"
        )
    assert isinstance(result, JSONResponse)
    assert result.status_code == 500