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.

7.5 KB
Raw Blame
"""Response shapes of two metrics endpoints nothing else pins.

Ported from ``tests/web/routes/test_metrics_routes_rate_limits.py`` (11
tests), deleted by the Flask->FastAPI migration. Both endpoints survived
the port line-for-line (``jsonify(...), 500`` became
``JSONResponse(..., status_code=500)``; ``request.get_json()`` became an
``await request.json()`` hoisted out of the try block).

Superseded and not re-ported:

* authentication on both routes —
  ``tests/security/test_unauthenticated_reachability_census.py`` lists
  ``GET /metrics/api/rate-limiting/current`` and
  ``POST /metrics/api/cost-calculation``;
* ``model_name is required`` -> 400, malformed/non-dict body -> 400, and
  the ``provider`` echo — ``tests/web/test_request_response_boundary_
  contracts.py`` and ``tests/web/routers/test_metrics_benchmark_hostile_
  input.py``.

What is recovered here is the body of the success and failure envelopes.
``tests/api_tests/test_metrics_api.py`` touches both routes but asserts
only ``status_code == 200`` (rate limits) and
``"cost" in data or "total_cost" in data`` (cost calculation) against a
real, usually-empty database — neither would go red if the derivations
below were dropped.

The route functions are called directly with a mocked ``Request``,
matching ``test_history_report_unit.py`` / ``test_check_ollama_unit.py``.
"""

import asyncio
import json
import time
from contextlib import contextmanager
from unittest.mock import AsyncMock, MagicMock, Mock, patch

METRICS = "local_deep_research.web.routers.metrics"
COST_CALCULATOR = (
    "local_deep_research.metrics.pricing.cost_calculator.CostCalculator"
)


def _db_patch(session):
    @contextmanager
    def fake_db_session(*a, **kw):
        yield session

    return patch(f"{METRICS}.get_user_db_session", side_effect=fake_db_session)


def _body(resp):
    return json.loads(resp.body)


# ---------------------------------------------------------------------------
# GET /metrics/api/rate-limiting/current
# ---------------------------------------------------------------------------


def _make_estimate(
    engine_type, base_wait, min_wait, max_wait, ts, attempts, rate
):
    estimate = MagicMock()
    estimate.engine_type = engine_type
    estimate.base_wait_seconds = base_wait
    estimate.min_wait_seconds = min_wait
    estimate.max_wait_seconds = max_wait
    estimate.last_updated = ts
    estimate.total_attempts = attempts
    estimate.success_rate = rate
    return estimate


def _call_current_limits(estimates):
    from local_deep_research.web.routers.metrics import api_current_rate_limits

    session = MagicMock()
    session.query.return_value.order_by.return_value.all.return_value = (
        estimates
    )
    with _db_patch(session):
        return api_current_rate_limits(Mock(), username="alice")


class TestCurrentRateLimits:
    def test_each_estimate_becomes_one_entry_with_a_percentage_and_a_status(
        self,
    ):
        """``success_rate`` is stored as a 0..1 fraction and must reach the
        client as a percentage; the ``status`` band is derived from the
        same number. Both derivations are invisible to an
        ``isinstance(list)`` assertion.
        """
        now = time.time()
        result = _call_current_limits(
            [
                _make_estimate("google", 2.0, 1.0, 5.0, now, 100, 0.95),
                _make_estimate("bing", 1.5, 0.5, 3.0, now, 50, 0.6),
            ]
        )

        assert result["status"] == "success"
        assert len(result["current_limits"]) == 2

        google, bing = result["current_limits"]
        assert google["engine_type"] == "google"
        assert google["base_wait_seconds"] == 2.0
        assert google["success_rate"] == 95.0
        assert google["status"] == "healthy"
        assert google["total_attempts"] == 100
        assert bing["status"] == "degraded"

    def test_a_low_success_rate_is_reported_as_poor(self):
        result = _call_current_limits(
            [
                _make_estimate(
                    "failing_engine", 10.0, 5.0, 30.0, time.time(), 200, 0.3
                )
            ]
        )

        assert result["current_limits"][0]["status"] == "poor"

    def test_no_estimates_is_a_success_with_an_empty_list(self):
        """An empty panel is not an error — the client renders "no data
        yet" rather than a failure banner."""
        result = _call_current_limits([])

        assert result["status"] == "success"
        assert result["current_limits"] == []

    def test_a_database_failure_is_a_500_not_an_empty_success(self):
        """Returning a bare dict here would serialise as 200 and the
        dashboard would silently render zero engines as if none were
        tracked."""
        from local_deep_research.web.routers.metrics import (
            api_current_rate_limits,
        )

        with patch(
            f"{METRICS}.get_user_db_session",
            side_effect=RuntimeError("db error"),
        ):
            resp = api_current_rate_limits(Mock(), username="alice")

        assert resp.status_code == 500
        assert _body(resp)["status"] == "error"


# ---------------------------------------------------------------------------
# POST /metrics/api/cost-calculation
# ---------------------------------------------------------------------------


def _call_cost_calculation(payload):
    from local_deep_research.web.routers.metrics import api_cost_calculation

    request = Mock()
    request.json = AsyncMock(return_value=payload)
    return asyncio.run(api_cost_calculation(request, username="alice"))


class TestCostCalculation:
    def test_the_calculated_cost_is_spread_into_the_response(self):
        """The calculator's whole result dict reaches the wire — the
        endpoint is a pass-through, so a caller reads ``total_cost`` from
        the top level and not from a nested object."""
        calculator = MagicMock()
        calculator.calculate_cost_sync.return_value = {
            "prompt_cost": 0.003,
            "completion_cost": 0.006,
            "total_cost": 0.009,
        }

        with patch(COST_CALCULATOR, return_value=calculator):
            result = _call_cost_calculation(
                {
                    "model_name": "gpt-4",
                    "provider": "openai",
                    "prompt_tokens": 100,
                    "completion_tokens": 200,
                }
            )

        assert result["status"] == "success"
        assert result["model_name"] == "gpt-4"
        assert result["total_cost"] == 0.009
        assert result["prompt_cost"] == 0.003
        # total_tokens is computed here, not by the calculator.
        assert result["total_tokens"] == 300
        calculator.calculate_cost_sync.assert_called_once_with(
            "gpt-4", 100, 200, provider="openai"
        )

    def test_omitted_token_counts_default_to_zero_rather_than_none(self):
        """``None + None`` would raise into the 500 handler; the defaults
        are what let a caller price a model without a usage sample."""
        calculator = MagicMock()
        calculator.calculate_cost_sync.return_value = {"total_cost": 0.0}

        with patch(COST_CALCULATOR, return_value=calculator):
            result = _call_cost_calculation({"model_name": "gpt-4"})

        assert result["prompt_tokens"] == 0
        assert result["completion_tokens"] == 0
        assert result["total_tokens"] == 0

    def test_a_calculator_failure_is_a_500(self):
        with patch(COST_CALCULATOR, side_effect=RuntimeError("pricing error")):
            resp = _call_cost_calculation({"model_name": "gpt-4"})

        assert resp.status_code == 500