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.

42.1 KB
Raw Blame
"""Failure branches and response shapes of ``web/routers/metrics.py``.

Ported from ``tests/web/routes/test_metrics_routes.py`` (104 tests) and
``tests/web/routes/test_metrics_routes_coverage.py`` (103), both deleted by
the Flask->FastAPI migration. All 32 metrics routes survived the port 1:1
(only ``jsonify(x), N`` -> ``JSONResponse(x, status_code=N)``,
``request.args`` -> ``request.query_params``), so the assertions port
directly.

Roughly 118 of those 207 tests ARE superseded and are not re-ported:

* the 22 ``requires_authentication`` tests — by the runtime anonymous sweep
  in ``tests/security/test_auth_dependencies_fastapi.py`` and the static
  parity check in ``tests/web/test_route_table_parity.py``;
* ``POST /api/ratings/{id}`` validation and persistence, and the whole
  ``GET /api/star-reviews`` payload shape / TokenUsage fan-out —
  ``tests/web/routers/test_metrics_star_reviews.py``;
* the malformed / non-dict JSON bodies — ``test_metrics_benchmark_hostile_
  input.py`` and ``tests/web/test_request_response_boundary_contracts.py``;
* ``/api/journals`` non-integer paging, page-above-max, the ``per_page``
  value reaching the DB kwargs, and the ``score_source`` allowlist —
  ``tests/web/test_pagination_clamping_census.py`` and
  ``tests/security/test_metrics_hostile_input_fastapi.py``;
* the journal-download egress-scope refusals and the message-scrub canary —
  ``test_metrics_hostile_input_fastapi.py``;
* the four analytics helpers — ``tests/metrics/test_link_analytics_
  computation.py`` and this directory's ``test_metrics_analytics_
  aggregation.py``.

What is recovered here falls into three groups.

**1. Every ``except ...: return 500`` tail in the router.** This is the
branch-2 trap in its purest form: ``test_all_endpoints.py``'s
``test_get_endpoint_no_500`` sweeps every route asserting ``< 500``. That
proves the *happy* path does not blow up and says nothing whatever about
the failure path — deleting a whole ``except`` block (so the exception
escapes to the app handler) or flipping a status code leaves it green.

**2. The success envelopes and query-param echoes.** ``status ==
"success"``, the ``period`` / ``research_mode`` echo, and the
"no data" zero-shapes are what the dashboard actually reads; the sweeps
above only check that a response arrived.

**3. ``GET /api/journals``'s echoed ``pagination`` object.** The
post-query clamp ``page = min(page, total_pages)`` and the ceiling
``total_pages = -(-total // per_page) ...`` have never been executed by
any test: every existing stub returns ``total = 0`` and inspects only the
kwargs handed to the DB.

Deliberately NOT ported: ``TestApiCostAnalytics::test_exception``, which
asserted a 200 on the failure path. That was changed on purpose — the
Flask version returned ``200 {"status": "success", ...}`` on a DB error,
masking it as "no data". The branch returns 500, and
``test_route_table_parity.py``'s ``EXPECTED_STATUS_CODES_GAINED`` records
the change. The assertion is inverted below rather than dropped.

Route functions are called directly. ``/api/journals`` carries a slowapi
``shared_limit`` decorator that rejects a ``Mock`` request, so a real
``starlette.requests.Request`` is built for every call.
"""

import asyncio
import json
from contextlib import contextmanager
from datetime import UTC, datetime
from unittest.mock import MagicMock, Mock, patch

import pytest
from starlette.requests import Request

MODULE = "local_deep_research.web.routers.metrics"
PRICING_FETCHER = (
    "local_deep_research.metrics.pricing.pricing_fetcher.PricingFetcher"
)
COST_CALCULATOR = (
    "local_deep_research.metrics.pricing.cost_calculator.CostCalculator"
)
JOURNAL_REF_DB = (
    "local_deep_research.journal_quality.db.get_journal_reference_db"
)


def _request(query=""):
    """A real Starlette Request — slowapi's decorator rejects a Mock."""
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/metrics/api/probe",
            "query_string": query.encode(),
            "headers": [],
            "client": ("10.0.0.1", 1234),
        }
    )


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

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


def _broken_db_patch(exc=None):
    """``get_user_db_session`` that raises on entry."""
    return patch(
        f"{MODULE}.get_user_db_session",
        side_effect=exc or RuntimeError("db down"),
    )


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


# ===========================================================================
# GET /metrics/api/metrics
# ===========================================================================


def _stub_metrics_deps(**overrides):
    """Patch the five collaborators api_metrics fans out to."""
    counter = MagicMock()
    counter.get_overall_metrics.return_value = {"total_tokens": 10}
    tracker = MagicMock()
    tracker.get_search_metrics.return_value = {"searches": 2}
    patches = {
        f"{MODULE}.TokenCounter": patch(
            f"{MODULE}.TokenCounter", return_value=counter
        ),
        f"{MODULE}.get_search_tracker": patch(
            f"{MODULE}.get_search_tracker", return_value=tracker
        ),
        f"{MODULE}.get_strategy_analytics": patch(
            f"{MODULE}.get_strategy_analytics",
            return_value={"strategy_analytics": {}},
        ),
        f"{MODULE}.get_rate_limiting_analytics": patch(
            f"{MODULE}.get_rate_limiting_analytics",
            return_value={"rate_limiting": {}},
        ),
        f"{MODULE}.get_time_filter_condition": patch(
            f"{MODULE}.get_time_filter_condition", return_value=None
        ),
        f"{MODULE}.get_context_overflow_truncation_summary": patch(
            f"{MODULE}.get_context_overflow_truncation_summary",
            return_value={
                "truncation_rate": 12.34,
                "avg_tokens_truncated": 7.9,
            },
        ),
    }
    patches.update(overrides)
    return patches


@contextmanager
def _apply(patches):
    started = [p.start() for p in patches.values()]
    try:
        yield started
    finally:
        for p in patches.values():
            p.stop()


def _call_api_metrics(query="", session=None, patches=None):
    from local_deep_research.web.routers.metrics import api_metrics

    patches = patches if patches is not None else _stub_metrics_deps()
    session = session if session is not None else MagicMock()
    with _apply(patches), _db_patch(session):
        return api_metrics(_request(query), username="alice")


class TestApiMetrics:
    def test_success_envelope_echoes_the_period_and_mode(self):
        result = _call_api_metrics("period=7d&mode=web")

        assert result["status"] == "success"
        assert "metrics" in result
        assert result["period"] == "7d"
        assert result["research_mode"] == "web"

    def test_defaults_are_thirty_days_and_all_modes(self):
        result = _call_api_metrics()

        assert result["period"] == "30d"
        assert result["research_mode"] == "all"

    def test_a_failing_satisfaction_query_falls_back_without_taking_the_page_down(
        self,
    ):
        """The inner try around the ratings query has its own fallback —
        the dashboard still renders, with the satisfaction tile blank
        rather than a 500 for the whole request."""
        from local_deep_research.web.routers.metrics import api_metrics

        with _apply(_stub_metrics_deps()), _broken_db_patch():
            result = api_metrics(_request(), username="alice")

        assert result["status"] == "success"
        assert result["metrics"]["user_satisfaction"] == {
            "avg_rating": None,
            "total_ratings": 0,
        }

    def test_a_failing_truncation_summary_sentinels_to_none_not_zero(self):
        """``None`` and ``0`` mean opposite things here: 0 is "nothing was
        truncated", a green signal. Falling back to 0 on error would flip
        a red signal green, which is why the sentinel is None."""
        patches = _stub_metrics_deps(
            **{
                f"{MODULE}.get_context_overflow_truncation_summary": patch(
                    f"{MODULE}.get_context_overflow_truncation_summary",
                    side_effect=RuntimeError("no such table"),
                )
            }
        )
        result = _call_api_metrics(patches=patches)

        assert result["status"] == "success"
        assert result["metrics"]["truncation_rate"] is None
        assert result["metrics"]["avg_tokens_truncated"] is None

    def test_the_truncation_summary_is_rounded_and_integerised(self):
        result = _call_api_metrics()

        assert result["metrics"]["truncation_rate"] == 12.3
        assert result["metrics"]["avg_tokens_truncated"] == 7

    def test_an_outer_failure_is_a_500_error_envelope(self):
        from local_deep_research.web.routers.metrics import api_metrics

        with patch(f"{MODULE}.TokenCounter", side_effect=RuntimeError("boom")):
            resp = api_metrics(_request(), username="alice")

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


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


class TestRateLimitingMetrics:
    def test_success_envelope_wraps_the_analytics_under_data(self):
        from local_deep_research.web.routers.metrics import (
            get_rate_limiting_metrics,
        )

        with patch(
            f"{MODULE}.get_rate_limiting_analytics",
            return_value={"rate_limiting": {"total_attempts": 3}},
        ):
            result = get_rate_limiting_metrics(
                _request("period=7d"), username="alice"
            )

        assert result["status"] == "success"
        assert result["data"] == {"rate_limiting": {"total_attempts": 3}}
        assert result["period"] == "7d"

    def test_a_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            get_rate_limiting_metrics,
        )

        with patch(
            f"{MODULE}.get_rate_limiting_analytics",
            side_effect=RuntimeError("boom"),
        ):
            resp = get_rate_limiting_metrics(_request(), username="alice")

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


# ===========================================================================
# GET /metrics/api/metrics/research/{id}  (+ /timeline, /search)
# ===========================================================================


class TestPerResearchMetrics:
    def test_research_metrics_success_and_failure(self):
        from local_deep_research.web.routers.metrics import api_research_metrics

        counter = MagicMock()
        counter.get_research_metrics.return_value = {"total_tokens": 500}
        with patch(f"{MODULE}.TokenCounter", return_value=counter):
            result = api_research_metrics(_request(), "res-1", username="alice")
        assert result == {
            "status": "success",
            "metrics": {"total_tokens": 500},
        }
        counter.get_research_metrics.assert_called_once_with(
            "res-1", username="alice"
        )

        counter.get_research_metrics.side_effect = RuntimeError("boom")
        with patch(f"{MODULE}.TokenCounter", return_value=counter):
            resp = api_research_metrics(_request(), "res-1", username="alice")
        assert resp.status_code == 500
        assert _body(resp)["status"] == "error"

    def test_timeline_metrics_success_and_failure(self):
        from local_deep_research.web.routers.metrics import (
            api_research_timeline_metrics,
        )

        counter = MagicMock()
        counter.get_research_timeline_metrics.return_value = {"timeline": []}
        with patch(f"{MODULE}.TokenCounter", return_value=counter):
            result = api_research_timeline_metrics(
                _request(), "res-1", username="alice"
            )
        assert result == {"status": "success", "metrics": {"timeline": []}}

        counter.get_research_timeline_metrics.side_effect = RuntimeError("boom")
        with patch(f"{MODULE}.TokenCounter", return_value=counter):
            resp = api_research_timeline_metrics(
                _request(), "res-1", username="alice"
            )
        assert resp.status_code == 500

    def test_search_metrics_success_and_failure(self):
        from local_deep_research.web.routers.metrics import (
            api_research_search_metrics,
        )

        tracker = MagicMock()
        tracker.get_research_search_metrics.return_value = {"queries": 3}
        with patch(f"{MODULE}.get_search_tracker", return_value=tracker):
            result = api_research_search_metrics(
                _request(), "res-1", username="alice"
            )
        assert result == {"status": "success", "metrics": {"queries": 3}}

        tracker.get_research_search_metrics.side_effect = RuntimeError("boom")
        with patch(f"{MODULE}.get_search_tracker", return_value=tracker):
            resp = api_research_search_metrics(
                _request(), "res-1", username="alice"
            )
        assert resp.status_code == 500


# ===========================================================================
# GET /metrics/api/metrics/research/{id}/links
# ===========================================================================


def _resource(url, title=None, preview=None):
    resource = Mock()
    resource.url = url
    resource.title = title
    resource.content_preview = preview
    return resource


def _links_session(resources, classifications=()):
    """The route runs exactly two queries: resources, then (only when at
    least one domain parsed) the classification batch."""
    session = MagicMock()
    resource_query = MagicMock()
    resource_query.filter.return_value.all.return_value = resources
    class_query = MagicMock()
    class_query.filter.return_value.all.return_value = list(classifications)
    session.query.side_effect = [resource_query, class_query]
    return session


class TestResearchLinkMetrics:
    def test_no_resources_returns_an_explicit_zero_shape(self):
        from local_deep_research.web.routers.metrics import (
            api_research_link_metrics,
        )

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = []
        with _db_patch(session):
            result = api_research_link_metrics(
                _request(), "res-1", username="alice"
            )

        assert result["status"] == "success"
        assert result["data"]["total_links"] == 0
        assert result["data"]["unique_domains"] == 0
        assert result["data"]["domains"] == []

    def test_domains_are_counted_deduplicated_and_percentaged(self):
        from local_deep_research.web.routers.metrics import (
            api_research_link_metrics,
        )

        session = _links_session(
            [
                _resource("https://example.com/a"),
                _resource("https://www.example.com/b"),
                _resource("https://other.com/c"),
                _resource(None),
            ]
        )
        with _db_patch(session):
            result = api_research_link_metrics(
                _request(), "res-1", username="alice"
            )

        data = result["data"]
        # total_links counts rows, including the one with no URL.
        assert data["total_links"] == 4
        # www. is stripped, so the first two collapse into one domain.
        assert data["unique_domains"] == 2
        assert data["domains"][0] == {
            "domain": "example.com",
            "count": 2,
            "percentage": 50.0,
        }
        # Nothing classified -> everything lands in the Unclassified bucket.
        assert data["category_distribution"] == {"Unclassified": 3}

    def test_a_database_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            api_research_link_metrics,
        )

        with _broken_db_patch():
            resp = api_research_link_metrics(
                _request(), "res-1", username="alice"
            )

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


# ===========================================================================
# GET /metrics/api/metrics/enhanced
# ===========================================================================


class TestEnhancedMetrics:
    def _call(self, query="", rating=None, counter_exc=None):
        from local_deep_research.web.routers.metrics import api_enhanced_metrics

        counter = MagicMock()
        counter.get_enhanced_metrics.return_value = {"enhanced": True}
        tracker = MagicMock()
        tracker.get_search_time_series.return_value = []
        counter_patch = (
            patch(f"{MODULE}.TokenCounter", side_effect=counter_exc)
            if counter_exc
            else patch(f"{MODULE}.TokenCounter", return_value=counter)
        )
        with (
            counter_patch,
            patch(f"{MODULE}.get_search_tracker", return_value=tracker),
            patch(
                f"{MODULE}.get_rating_analytics",
                return_value=rating
                or {"rating_analytics": {"avg_rating": 4.0}},
            ),
        ):
            return api_enhanced_metrics(_request(query), username="alice")

    def test_success_echoes_the_period_and_mode_and_merges_the_ratings(self):
        result = self._call("period=7d&mode=web")

        assert result["status"] == "success"
        assert result["period"] == "7d"
        assert result["research_mode"] == "web"
        assert result["metrics"]["enhanced"] is True
        assert result["metrics"]["search_time_series"] == []
        assert result["metrics"]["rating_analytics"]["avg_rating"] == 4.0

    def test_a_failure_is_a_500(self):
        resp = self._call(counter_exc=RuntimeError("boom"))

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


# ===========================================================================
# GET /metrics/api/ratings/{id}
# ===========================================================================


class TestGetResearchRating:
    def test_an_unrated_research_returns_a_null_rating_not_a_404(self):
        from local_deep_research.web.routers.metrics import (
            api_get_research_rating,
        )

        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            None
        )
        with _db_patch(session):
            result = api_get_research_rating(
                _request(), "res-1", username="alice"
            )

        assert result == {"status": "success", "rating": None}

    def test_an_existing_rating_carries_both_timestamps(self):
        from local_deep_research.web.routers.metrics import (
            api_get_research_rating,
        )

        now = datetime.now(UTC)
        rating = Mock()
        rating.rating = 4
        rating.created_at = now
        rating.updated_at = now
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            rating
        )
        with _db_patch(session):
            result = api_get_research_rating(
                _request(), "res-1", username="alice"
            )

        assert result["status"] == "success"
        assert result["rating"] == 4
        assert result["created_at"] == now.isoformat()
        assert result["updated_at"] == now.isoformat()

    def test_a_database_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            api_get_research_rating,
        )

        with _broken_db_patch():
            resp = api_get_research_rating(
                _request(), "res-1", username="alice"
            )

        assert resp.status_code == 500


# ===========================================================================
# GET /metrics/api/pricing  and  /api/pricing/{model}
# ===========================================================================


class TestPricing:
    def test_pricing_success_and_failure(self):
        from local_deep_research.web.routers.metrics import api_pricing

        fetcher = MagicMock()
        fetcher.static_pricing = {"gpt-4": {"input": 1}}
        with patch(PRICING_FETCHER, return_value=fetcher):
            result = api_pricing(_request(), username="alice")
        assert result["status"] == "success"
        assert result["pricing"] == {"gpt-4": {"input": 1}}

        with patch(PRICING_FETCHER, side_effect=RuntimeError("boom")):
            resp = api_pricing(_request(), username="alice")
        assert resp.status_code == 500

    def test_model_pricing_echoes_the_model_and_provider(self):
        from local_deep_research.web.routers.metrics import api_model_pricing

        calculator = MagicMock()
        calculator.calculate_cost_sync.return_value = {
            "pricing_used": {"input": 0.03}
        }
        with patch(COST_CALCULATOR, return_value=calculator):
            result = api_model_pricing(
                _request("provider=openai"), "gpt-4", username="alice"
            )

        assert result["status"] == "success"
        assert result["model"] == "gpt-4"
        assert result["provider"] == "openai"
        assert result["pricing"] == {"input": 0.03}

    def test_model_pricing_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import api_model_pricing

        with patch(COST_CALCULATOR, side_effect=RuntimeError("boom")):
            resp = api_model_pricing(_request(), "gpt-4", username="alice")

        assert resp.status_code == 500


# ===========================================================================
# GET /metrics/api/research-costs/{id}
# ===========================================================================


class TestResearchCosts:
    def test_no_usage_records_returns_a_zero_cost_not_an_error(self):
        from local_deep_research.web.routers.metrics import api_research_costs

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = []
        with _db_patch(session):
            result = api_research_costs(_request(), "res-1", username="alice")

        assert result["status"] == "success"
        assert result["research_id"] == "res-1"
        assert result["total_cost"] == 0.0
        assert "message" in result

    def test_usage_records_are_summed_across_every_row(self):
        from local_deep_research.web.routers.metrics import api_research_costs

        record = Mock()
        record.model_name = "gpt-4"
        record.model_provider = "openai"
        record.prompt_tokens = 100
        record.completion_tokens = 50
        record.timestamp = "2024-01-01T00:00:00"
        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [
            record,
            record,
        ]
        calculator = MagicMock()
        calculator.calculate_cost_sync.return_value = {
            "total_cost": 0.005,
            "prompt_cost": 0.003,
            "completion_cost": 0.002,
            "pricing_used": {"prompt": 0.03, "completion": 0.04},
        }

        with (
            _db_patch(session),
            patch(COST_CALCULATOR, return_value=calculator),
        ):
            result = api_research_costs(_request(), "res-1", username="alice")

        assert result["status"] == "success"
        assert result["total_cost"] == 0.01
        assert result["prompt_tokens"] == 200
        assert result["completion_tokens"] == 100
        assert result["total_tokens"] == 300

    def test_a_database_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import api_research_costs

        with _broken_db_patch():
            resp = api_research_costs(_request(), "res-1", username="alice")

        assert resp.status_code == 500


# ===========================================================================
# GET /metrics/api/cost-analytics
# ===========================================================================


class TestCostAnalytics:
    def test_no_records_returns_an_explicit_zero_overview(self):
        from local_deep_research.web.routers.metrics import api_cost_analytics

        session = MagicMock()
        session.query.return_value.count.return_value = 0
        with (
            _db_patch(session),
            patch(f"{MODULE}.get_time_filter_condition", return_value=None),
        ):
            result = api_cost_analytics(
                _request("period=30d"), username="alice"
            )

        assert result["status"] == "success"
        assert result["period"] == "30d"
        assert result["overview"]["total_cost"] == 0.0
        assert result["research_count"] == 0

    def test_an_oversized_result_set_is_capped_at_a_thousand_rows(self):
        """The row cap is the reason this endpoint does not time out on a
        heavy account. Asserted on the ``.limit()`` argument, not on the
        response length — the stub returns nothing either way."""
        from local_deep_research.web.routers.metrics import api_cost_analytics

        session = MagicMock()
        query = session.query.return_value
        query.count.return_value = 1500
        query.order_by.return_value.limit.return_value.all.return_value = []
        with (
            _db_patch(session),
            patch(f"{MODULE}.get_time_filter_condition", return_value=None),
        ):
            result = api_cost_analytics(_request(), username="alice")

        assert result["status"] == "success"
        query.order_by.return_value.limit.assert_called_once_with(1000)

    def test_a_failure_is_a_500_not_a_success_shaped_empty_result(self):
        """Inverted from the Flask original on purpose.

        The pre-migration route answered ``200 {"status": "success", ...}``
        with a zeroed overview on a DB error, so a client that only read
        the status code rendered a genuine failure as "no data". The
        branch answers 500 and ``test_route_table_parity.py``'s
        ``EXPECTED_STATUS_CODES_GAINED`` records the change.
        """
        from local_deep_research.web.routers.metrics import api_cost_analytics

        with _broken_db_patch():
            resp = api_cost_analytics(_request("period=7d"), username="alice")

        assert resp.status_code == 500
        body = _body(resp)
        assert body["status"] == "error"
        assert body["period"] == "7d"


# ===========================================================================
# GET /metrics/api/link-analytics
# ===========================================================================


class TestLinkAnalyticsEndpoint:
    def test_success_unwraps_the_helper_result_under_data(self):
        from local_deep_research.web.routers.metrics import api_link_analytics

        with patch(
            f"{MODULE}.get_link_analytics",
            return_value={"link_analytics": {"total_links": 5}},
        ):
            result = api_link_analytics(_request("period=7d"), username="alice")

        assert result["status"] == "success"
        assert result["data"] == {"total_links": 5}
        assert result["period"] == "7d"

    def test_a_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import api_link_analytics

        with patch(
            f"{MODULE}.get_link_analytics", side_effect=RuntimeError("boom")
        ):
            resp = api_link_analytics(_request(), username="alice")

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


# ===========================================================================
# /metrics/api/domain-classifications*
# ===========================================================================


class TestDomainClassifications:
    def test_listing_returns_the_dicts_with_a_total_and_closes_the_classifier(
        self,
    ):
        from local_deep_research.web.routers.metrics import (
            api_get_domain_classifications,
        )

        classification = Mock()
        classification.to_dict.return_value = {"domain": "example.com"}
        classifier = MagicMock()
        classifier.get_all_classifications.return_value = [classification]

        with patch(f"{MODULE}.DomainClassifier", return_value=classifier):
            result = api_get_domain_classifications(
                _request(), username="alice"
            )

        assert result["status"] == "success"
        assert result["classifications"] == [{"domain": "example.com"}]
        assert result["total"] == 1
        # The classifier holds a DB handle; the finally: must release it.
        classifier.close.assert_called_once()

    def test_listing_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            api_get_domain_classifications,
        )

        with patch(
            f"{MODULE}.DomainClassifier", side_effect=RuntimeError("boom")
        ):
            resp = api_get_domain_classifications(_request(), username="alice")

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

    def test_summary_returns_the_category_counts_and_closes_the_classifier(
        self,
    ):
        from local_deep_research.web.routers.metrics import (
            api_get_classifications_summary,
        )

        classifier = MagicMock()
        classifier.get_categories_summary.return_value = {"Technology": 5}

        with patch(f"{MODULE}.DomainClassifier", return_value=classifier):
            result = api_get_classifications_summary(
                _request(), username="alice"
            )

        assert result == {
            "status": "success",
            "summary": {"Technology": 5},
        }
        classifier.close.assert_called_once()

    def test_summary_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            api_get_classifications_summary,
        )

        with patch(
            f"{MODULE}.DomainClassifier", side_effect=RuntimeError("boom")
        ):
            resp = api_get_classifications_summary(_request(), username="alice")

        assert resp.status_code == 500

    def test_progress_counts_distinct_domains_and_skips_null_urls(self):
        from local_deep_research.web.routers.metrics import (
            api_classification_progress,
        )

        session = MagicMock()
        resources_query = MagicMock()
        resources_query.distinct.return_value.all.return_value = [
            ("https://example.com/1",),
            ("https://example.com/2",),
            ("https://other.com/1",),
            (None,),
        ]
        count_query = MagicMock()
        count_query.count.return_value = 1
        session.query.side_effect = [resources_query, count_query]

        with _db_patch(session):
            result = api_classification_progress(_request(), username="alice")

        progress = result["progress"]
        # Two distinct domains: the None row is skipped and the two
        # example.com URLs collapse.
        assert progress["total_domains"] == 2
        assert progress["classified"] == 1
        assert progress["unclassified"] == 1
        assert progress["percentage"] == 50.0
        assert progress["all_domains"] == ["example.com", "other.com"]

    def test_progress_with_no_domains_does_not_divide_by_zero(self):
        from local_deep_research.web.routers.metrics import (
            api_classification_progress,
        )

        session = MagicMock()
        resources_query = MagicMock()
        resources_query.distinct.return_value.all.return_value = []
        count_query = MagicMock()
        count_query.count.return_value = 0
        session.query.side_effect = [resources_query, count_query]

        with _db_patch(session):
            result = api_classification_progress(_request(), username="alice")

        assert result["progress"]["percentage"] == 0

    def test_progress_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            api_classification_progress,
        )

        with _broken_db_patch():
            resp = api_classification_progress(_request(), username="alice")

        assert resp.status_code == 500


# ---------------------------------------------------------------------------
# POST /metrics/api/domain-classifications/classify
# ---------------------------------------------------------------------------


def _call_classify(
    payload, classifier=None, classifier_exc=None, dependency_spies=None
):
    from local_deep_research.web.routers.metrics import api_classify_domains

    request = Mock()

    async def _json():
        return payload

    request.json = _json

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

    classifier_patch = (
        patch(f"{MODULE}.DomainClassifier", side_effect=classifier_exc)
        if classifier_exc
        else patch(f"{MODULE}.DomainClassifier", return_value=classifier)
    )
    with (
        classifier_patch as classifier_constructor,
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=fake_db_session,
        ) as db_session_factory,
        patch(
            "local_deep_research.settings.manager.SettingsManager"
        ) as settings_constructor,
    ):
        result = asyncio.run(api_classify_domains(request, username="alice"))

    if dependency_spies is not None:
        dependency_spies.update(
            classifier=classifier_constructor,
            db_session=db_session_factory,
            settings=settings_constructor,
        )
    return result


class TestClassifyDomains:
    def test_a_single_domain_is_classified_and_the_classifier_is_closed(self):
        classification = Mock()
        classification.to_dict.return_value = {"domain": "example.com"}
        classifier = MagicMock()
        classifier.classify_domain.return_value = classification

        result = _call_classify({"domain": "example.com"}, classifier)

        assert result["status"] == "success"
        assert result["classification"] == {"domain": "example.com"}
        classifier.classify_domain.assert_called_once_with("example.com", False)
        classifier.close.assert_called_once()

    def test_force_update_true_is_forwarded_as_a_boolean(self):
        classification = Mock()
        classification.to_dict.return_value = {"domain": "example.com"}
        classifier = MagicMock()
        classifier.classify_domain.return_value = classification

        result = _call_classify(
            {"domain": "example.com", "force_update": True}, classifier
        )

        assert result["status"] == "success"
        classifier.classify_domain.assert_called_once_with("example.com", True)

    def test_a_domain_the_classifier_cannot_place_is_a_400(self):
        classifier = MagicMock()
        classifier.classify_domain.return_value = None

        resp = _call_classify({"domain": "bad.com"}, classifier)

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

    def test_batch_mode_classifies_everything(self):
        classifier = MagicMock()
        classifier.classify_all_domains.return_value = {"classified": 5}

        result = _call_classify({"batch": True}, classifier)

        assert result == {
            "status": "success",
            "results": {"classified": 5},
        }
        classifier.classify_all_domains.assert_called_once_with(False)

    @pytest.mark.parametrize(
        "field,value,message",
        [
            ("force_update", "false", "force_update must be a boolean"),
            ("force_update", 0, "force_update must be a boolean"),
            ("force_update", 1, "force_update must be a boolean"),
            ("force_update", None, "force_update must be a boolean"),
            ("force_update", [], "force_update must be a boolean"),
            ("force_update", {}, "force_update must be a boolean"),
            ("batch", "false", "batch must be a boolean"),
            ("batch", 0, "batch must be a boolean"),
            ("batch", 1, "batch must be a boolean"),
            ("batch", None, "batch must be a boolean"),
            ("batch", [], "batch must be a boolean"),
            ("batch", {}, "batch must be a boolean"),
            ("domain", False, "domain must be a string"),
            ("domain", 1, "domain must be a string"),
            ("domain", 1.5, "domain must be a string"),
            ("domain", [], "domain must be a string"),
            ("domain", ["example.com"], "domain must be a string"),
            ("domain", {}, "domain must be a string"),
        ],
    )
    def test_typed_controls_reject_all_other_json_types_before_classification(
        self, field, value, message
    ):
        classifier = MagicMock()
        dependencies = {}
        payload = {"domain": "example.com", field: value}

        resp = _call_classify(
            payload, classifier, dependency_spies=dependencies
        )

        assert resp.status_code == 400
        assert _body(resp) == {"status": "error", "message": message}
        dependencies["db_session"].assert_not_called()
        dependencies["settings"].assert_not_called()
        dependencies["classifier"].assert_not_called()
        classifier.classify_domain.assert_not_called()
        classifier.classify_all_domains.assert_not_called()

    def test_neither_a_domain_nor_batch_is_a_400(self):
        resp = _call_classify({}, MagicMock())

        assert resp.status_code == 400
        assert "batch" in _body(resp)["message"]

    def test_a_classifier_failure_is_a_500(self):
        resp = _call_classify(
            {"domain": "example.com"}, classifier_exc=RuntimeError("boom")
        )

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


# ===========================================================================
# GET /metrics/api/journals — the echoed pagination object
# ===========================================================================


def _ref_db(total=0, journals=()):
    ref = MagicMock()
    ref.available = True
    ref.get_journals_page.return_value = (list(journals), total)
    return ref


def _call_journals(query="", ref=None):
    from local_deep_research.web.routers.metrics import api_journal_quality

    ref = ref if ref is not None else _ref_db()
    with patch(JOURNAL_REF_DB, return_value=ref):
        return api_journal_quality(_request(query), username="alice"), ref


class TestJournalsPagination:
    """The clamp arithmetic no test has ever executed.

    Every existing stub on the branch returns ``total = 0`` and inspects
    only the kwargs handed to ``get_journals_page``, so
    ``total_pages = -(-total // per_page)`` and the post-query
    ``page = min(page, total_pages)`` are unreached.
    """

    def test_per_page_is_clamped_to_two_hundred_in_the_echo_and_the_query(self):
        result, ref = _call_journals("per_page=500")

        assert result["pagination"]["per_page"] == 200
        assert ref.get_journals_page.call_args.kwargs["per_page"] == 200

    def test_per_page_is_floored_at_one(self):
        result, ref = _call_journals("per_page=0")

        assert result["pagination"]["per_page"] == 1
        assert ref.get_journals_page.call_args.kwargs["per_page"] == 1

    def test_a_page_past_the_end_is_clamped_down_to_the_last_page(self):
        # 5 rows at 50 per page is a single page.
        result, _ = _call_journals("page=50", ref=_ref_db(total=5))

        pagination = result["pagination"]
        assert pagination["total_count"] == 5
        assert pagination["total_pages"] == 1
        assert pagination["page"] == 1

    def test_an_in_range_page_is_echoed_unchanged(self):
        """Positive control: without this a handler that always echoed
        page 1 would pass the clamp test above."""
        # 120 rows at 50 per page is three pages.
        result, _ = _call_journals("page=2", ref=_ref_db(total=120))

        pagination = result["pagination"]
        assert pagination["total_pages"] == 3
        assert pagination["page"] == 2

    def test_the_total_pages_ceiling_rounds_a_partial_page_up(self):
        result, _ = _call_journals("per_page=50", ref=_ref_db(total=101))

        assert result["pagination"]["total_pages"] == 3

    def test_an_empty_result_set_still_reports_one_page(self):
        """``-(-0 // 50)`` is 0, which would make ``min(page, total_pages)``
        echo page 0 — hence the ``if ... and total > 0 else 1`` guard."""
        result, _ = _call_journals("page=42", ref=_ref_db(total=0))

        pagination = result["pagination"]
        assert pagination["total_count"] == 0
        assert pagination["total_pages"] == 1
        assert pagination["page"] == 1

    def test_an_unavailable_reference_database_is_a_503(self):
        ref = MagicMock()
        ref.available = False

        resp, _ = _call_journals(ref=ref)

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

    @pytest.mark.parametrize("query", ["page=abc", "per_page=xyz"])
    def test_non_integer_paging_is_a_400_before_any_query_runs(self, query):
        resp, ref = _call_journals(query)

        assert resp.status_code == 400
        body = _body(resp)
        assert body["status"] == "error"
        assert "pagination" in body["message"].lower()
        assert ref.get_journals_page.call_count == 0

    def test_an_out_of_allowlist_score_source_never_reaches_the_query(self):
        """The allowlist itself is pinned elsewhere; what is not is that
        the rejection happens *before* the reference DB is touched."""
        resp, ref = _call_journals("score_source=' OR 1=1--")

        assert resp.status_code == 400
        assert ref.get_journals_page.call_count == 0

    def test_the_sort_column_is_passed_through_for_the_db_to_validate(self):
        """The route deliberately does not allowlist ``sort`` — the
        reference DB layer does. Pinned so a future edit that starts
        interpolating it here is a visible change."""
        _, ref = _call_journals("sort=name&order=asc")

        kwargs = ref.get_journals_page.call_args.kwargs
        assert kwargs["sort"] == "name"
        assert kwargs["order"] == "asc"

    def test_an_unexpected_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import api_journal_quality

        with patch(JOURNAL_REF_DB, side_effect=RuntimeError("boom")):
            resp = api_journal_quality(_request(), username="alice")

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