~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Star-reviews analytics + rating persistence tests (FastAPI port).
Ports the high-value regression tests from main's Flask-era
``tests/web/routes/test_metrics_routes.py`` to the FastAPI routers:
* TokenUsage one-to-many fan-out dedup (PR #3804): a research has many
``token_usage`` rows (one per LLM call); joining TokenUsage directly used
to duplicate ``recent_ratings`` entries and inflate the LLM/search-engine
breakdown counts.
* "Missing model" sentinel merge (#4790): '', 'unknown', 'UNKNOWN',
whitespace, and no-token (NULL via outerjoin) all collapse into ONE
'Unknown' LLM bucket, counted once per rating.
* NULL/empty search-engine exclusion (#4786): non-search LLM-call rows
(``search_engine_selected`` NULL/empty/whitespace) must not put a research
that used a real engine into the 'Unknown' bucket too; engine-less
researches land in 'Unknown' exactly once.
* Rating sub-dimension / feedback validation for
``POST /metrics/api/ratings/<id>`` (PR #3804 hardening).
* ``quality_dimensions`` / ``recent_feedback`` payload shapes.
Auth is satisfied by overriding the ``require_auth`` dependency; the
per-user DB is replaced by a seeded in-memory SQLite DB (StaticPool keeps a
single shared connection) so the routes run their REAL SQL — that is what
catches the join-cardinality bugs these tests fence off.
"""
from contextlib import contextmanager
from datetime import datetime, UTC
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from local_deep_research.database.models import Base
from local_deep_research.database.models.metrics import (
ResearchRating,
TokenUsage,
)
from local_deep_research.database.models.research import ResearchHistory
STAR_REVIEWS_URL = "/metrics/api/star-reviews?period=all"
RATINGS_URL = "/metrics/api/ratings"
@pytest.fixture
def client():
"""FastAPI test client authenticated as ``testuser``.
Overrides the ``require_auth`` dependency instead of doing a real
register/login because the per-user DB is patched to a seeded SQLite DB
(``_seeded_metrics_db``) — a real auth flow would create a real encrypted
DB we couldn't seed rows into.
"""
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.dependencies.auth import require_auth
app.dependency_overrides[require_auth] = lambda: "testuser"
try:
yield TestClient(app)
finally:
app.dependency_overrides.pop(require_auth, None)
@contextmanager
def _seeded_metrics_db(*rows):
"""Patch ``get_user_db_session`` with an in-memory SQLite DB seeded with
*rows* (model instances).
Runs the routes' actual SQL, so it can catch join-cardinality bugs
(the TokenUsage one-to-many fan-out). A ``StaticPool`` keeps a single
shared in-memory connection so the seed data and the route (which runs
in the TestClient threadpool) see the same DB.
Yields the session factory so tests can inspect persisted rows.
"""
engine = create_engine(
"sqlite://",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
seed_session = Session()
seed_session.add_all(rows)
seed_session.commit()
seed_session.close()
@contextmanager
def _ctx(username=None, password=None):
with Session() as session:
yield session
with patch(
"local_deep_research.web.routers.metrics.get_user_db_session",
_ctx,
):
yield Session
engine.dispose()
def _token_usage(research_id, model_name="gpt-4", ts=None, search_engine=None):
"""Build a minimally-valid TokenUsage row (one per simulated LLM call)."""
return TokenUsage(
research_id=research_id,
model_provider="openai",
model_name=model_name,
prompt_tokens=1,
completion_tokens=1,
total_tokens=2,
timestamp=ts or datetime.now(UTC),
search_engine_selected=search_engine,
)
def _csrf_headers(client):
"""CSRF header for state-changing requests (middleware runs pre-route)."""
token = client.get("/auth/csrf-token").json()["csrf_token"]
return {"X-CSRFToken": token}
# ---------------------------------------------------------------------------
# Payload shape
# ---------------------------------------------------------------------------
class TestStarReviewsPayloadShape:
"""GET /metrics/api/star-reviews top-level and nested shapes."""
def test_top_level_shape(self, client):
with _seeded_metrics_db():
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
data = response.json()
for key in (
"overall_stats",
"llm_ratings",
"search_engine_ratings",
"rating_trends",
"recent_ratings",
"mode_ratings",
"quality_dimensions",
"recent_feedback",
):
assert key in data, f"missing top-level key {key!r}"
stats = data["overall_stats"]
assert "avg_rating" in stats
assert "total_ratings" in stats
for i in range(1, 6):
assert str(i) in stats["rating_distribution"]
def test_quality_dimensions_structure(self, client):
with _seeded_metrics_db():
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
dims = response.json()["quality_dimensions"]
for key in (
"avg_accuracy",
"avg_completeness",
"avg_relevance",
"avg_readability",
"dimension_count",
"dimension_counts",
):
assert key in dims, f"missing quality_dimensions key {key!r}"
for key in ("accuracy", "completeness", "relevance", "readability"):
assert key in dims["dimension_counts"]
def test_radar_not_hidden_when_only_some_dimensions_set(self, client):
"""Quality radar must not be suppressed when only SOME dimensions are
set: accuracy is NULL but completeness is set; the radar previously
keyed its empty-state on count(accuracy) and hid real data."""
now = datetime.now(UTC)
rows = [
ResearchRating(
research_id="r1", rating=4, completeness=4, created_at=now
),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
dims = response.json()["quality_dimensions"]
assert dims["dimension_count"] == 1, "radar would be hidden"
assert dims["avg_accuracy"] is None
assert dims["avg_completeness"] == 4.0
assert dims["dimension_counts"]["accuracy"] == 0
assert dims["dimension_counts"]["completeness"] == 1
def test_recent_feedback_shape(self, client):
"""Non-empty feedback surfaces in recent_feedback with the full entry
shape; ratings without feedback are excluded."""
now = datetime.now(UTC)
rows = [
ResearchRating(
research_id="r1",
rating=5,
feedback="Great report",
created_at=now,
),
ResearchRating(research_id="r2", rating=3, created_at=now),
ResearchRating(
research_id="r3", rating=2, feedback="", created_at=now
),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
feedback = response.json()["recent_feedback"]
assert isinstance(feedback, list)
assert len(feedback) == 1, "empty/absent feedback must be excluded"
entry = feedback[0]
assert entry["feedback"] == "Great report"
assert entry["rating"] == 5
assert entry["research_id"] == "r1"
assert entry["created_at"] is not None
# ---------------------------------------------------------------------------
# TokenUsage fan-out dedup (PR #3804)
# ---------------------------------------------------------------------------
class TestStarReviewsFanOut:
"""Each rating is counted exactly once despite many token_usage rows."""
def test_recent_ratings_not_duplicated_by_token_usage(self, client):
now = datetime.now(UTC)
rows = [
ResearchHistory(
id="r1",
query="What is X?",
mode="standard",
status="completed",
created_at=now.isoformat(),
),
ResearchRating(research_id="r1", rating=5, created_at=now),
_token_usage("r1", ts=now),
_token_usage("r1", ts=now),
_token_usage("r1", ts=now),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
recent = response.json()["recent_ratings"]
assert len(recent) == 1, (
f"fan-out: expected 1 unique rating, got {len(recent)}"
)
assert recent[0]["research_id"] == "r1"
assert recent[0]["query"] == "What is X?"
assert recent[0]["mode"] == "standard"
assert recent[0]["llm_model"] == "gpt-4"
def test_llm_breakdown_counts_not_inflated_by_token_usage(self, client):
now = datetime.now(UTC)
rows = [
ResearchRating(research_id="r1", rating=5, created_at=now),
_token_usage("r1", ts=now),
_token_usage("r1", ts=now),
_token_usage("r1", ts=now),
_token_usage("r1", ts=now),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
gpt4 = [
r for r in response.json()["llm_ratings"] if r["model"] == "gpt-4"
]
assert len(gpt4) == 1
assert gpt4[0]["rating_count"] == 1, "fan-out inflated rating_count"
assert gpt4[0]["positive_ratings"] == 1, "fan-out inflated positives"
assert gpt4[0]["avg_rating"] == 5.0
def test_search_engine_counts_not_inflated_by_token_usage(self, client):
now = datetime.now(UTC)
rows = [
ResearchRating(research_id="r1", rating=4, created_at=now),
_token_usage("r1", ts=now, search_engine="searxng"),
_token_usage("r1", ts=now, search_engine="searxng"),
_token_usage("r1", ts=now, search_engine="searxng"),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
searxng = [
r
for r in response.json()["search_engine_ratings"]
if r["search_engine"] == "searxng"
]
assert len(searxng) == 1
assert searxng[0]["rating_count"] == 1, "fan-out inflated count"
assert searxng[0]["positive_ratings"] == 1
def test_llm_breakdown_counts_with_multiple_models(self, client):
"""A rating on a research that used two models counts once per model
(the semantic the distinct subquery exists for) — never duplicated
within a model, and the recent list shows the rating once."""
now = datetime.now(UTC)
rows = [
ResearchRating(research_id="r1", rating=5, created_at=now),
_token_usage("r1", model_name="gpt-4", ts=now),
_token_usage("r1", model_name="gpt-4", ts=now),
_token_usage("r1", model_name="claude", ts=now),
_token_usage("r1", model_name="claude", ts=now),
_token_usage("r1", model_name="claude", ts=now),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
data = response.json()
by_model = {r["model"]: r for r in data["llm_ratings"]}
assert by_model["gpt-4"]["rating_count"] == 1
assert by_model["claude"]["rating_count"] == 1
assert by_model["gpt-4"]["positive_ratings"] == 1
assert by_model["claude"]["positive_ratings"] == 1
assert len(data["recent_ratings"]) == 1
# ---------------------------------------------------------------------------
# Unknown-bucket merge (#4790) and NULL-engine exclusion (#4786)
# ---------------------------------------------------------------------------
class TestStarReviewsUnknownBuckets:
def test_llm_unknown_buckets_merge(self, client):
"""All "missing model" sentinels collapse into a single 'Unknown'
bucket — empty string, lowercase 'unknown', uppercase 'UNKNOWN',
whitespace, and no-token (NULL) — counted once each, while real model
names keep casing (#4790).
r1 carries BOTH '' and 'unknown' token rows: normalization must happen
inside the DISTINCT subquery so its single rating is counted ONCE, not
once per sentinel variant (the merged bucket must total 4, not 5).
"""
now = datetime.now(UTC)
rows = [
# r1: two distinct sentinels on ONE research -> must count once
ResearchRating(research_id="r1", rating=5, created_at=now),
_token_usage("r1", model_name="", ts=now),
_token_usage("r1", model_name="unknown", ts=now),
# r2: uppercase sentinel (exercises lower())
ResearchRating(research_id="r2", rating=4, created_at=now),
_token_usage("r2", model_name="UNKNOWN", ts=now),
# r3: whitespace-only (exercises trim())
ResearchRating(research_id="r3", rating=3, created_at=now),
_token_usage("r3", model_name=" ", ts=now),
# r4: no token rows -> NULL model via the outerjoin
ResearchRating(research_id="r4", rating=2, created_at=now),
# r5: a real model -> its own bucket, casing preserved
ResearchRating(research_id="r5", rating=5, created_at=now),
_token_usage("r5", model_name="GPT-4", ts=now),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
buckets = {r["model"]: r for r in response.json()["llm_ratings"]}
# no sentinel variant survives as its own bucket
for sentinel in ("", "unknown", "UNKNOWN", " "):
assert sentinel not in buckets
# r1..r4 merge into Unknown, each counted once (r1's two sentinels = 1)
assert buckets["Unknown"]["rating_count"] == 4
assert buckets["Unknown"]["avg_rating"] == 3.5 # (5+4+3+2)/4
assert buckets["Unknown"]["positive_ratings"] == 2 # ratings 5 and 4
# real model keeps its original casing and stays separate
assert "GPT-4" in buckets
assert buckets["GPT-4"]["rating_count"] == 1
def test_search_engine_unknown_bucket_excludes_real_engine_ratings(
self, client
):
"""A research that used a real engine must not ALSO appear in the
'Unknown' bucket because of its non-search (NULL-engine) LLM-call
rows. 'Unknown' should hold only ratings with no recorded engine
(#4786)."""
now = datetime.now(UTC)
rows = [
# r1 used searxng, plus non-search LLM calls (engine NULL)
ResearchRating(research_id="r1", rating=5, created_at=now),
_token_usage("r1", ts=now, search_engine="searxng"),
_token_usage("r1", ts=now, search_engine=None),
_token_usage("r1", ts=now, search_engine=None),
# r2 made only non-search LLM calls -> belongs in 'Unknown' once
ResearchRating(research_id="r2", rating=3, created_at=now),
_token_usage("r2", ts=now, search_engine=None),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
buckets = {
r["search_engine"]: r
for r in response.json()["search_engine_ratings"]
}
# r1 counts under searxng only
assert buckets["searxng"]["rating_count"] == 1
# 'Unknown' holds only r2 (the engine-less research), not r1
assert buckets["Unknown"]["rating_count"] == 1
assert buckets["Unknown"]["avg_rating"] == 3.0
def test_search_engine_multi_engine_research_counts_once_per_engine(
self, client
):
"""A research that used two real engines counts once under EACH engine
(the distinct (research_id, engine) semantic) — not duplicated within
an engine, and its NULL-engine rows do not spawn an 'Unknown'
bucket."""
now = datetime.now(UTC)
rows = [
ResearchRating(research_id="r1", rating=5, created_at=now),
_token_usage("r1", ts=now, search_engine="searxng"),
_token_usage("r1", ts=now, search_engine="searxng"),
_token_usage("r1", ts=now, search_engine="arxiv"),
_token_usage("r1", ts=now, search_engine=None),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
buckets = {
r["search_engine"]: r
for r in response.json()["search_engine_ratings"]
}
assert buckets["searxng"]["rating_count"] == 1
assert buckets["arxiv"]["rating_count"] == 1
# the NULL-engine row must not create an 'Unknown' bucket here
assert "Unknown" not in buckets
def test_search_engine_empty_string_merges_into_unknown(self, client):
"""An empty-string search_engine_selected is treated as 'no engine'
and merges into 'Unknown' rather than forming a separate '' bucket
(#4786)."""
now = datetime.now(UTC)
rows = [
ResearchRating(research_id="r1", rating=4, created_at=now),
_token_usage("r1", ts=now, search_engine=""),
ResearchRating(research_id="r2", rating=2, created_at=now),
_token_usage("r2", ts=now, search_engine=None),
# whitespace-only engine (exercises trim()) -> also Unknown
ResearchRating(research_id="r3", rating=3, created_at=now),
_token_usage("r3", ts=now, search_engine=" "),
]
with _seeded_metrics_db(*rows):
response = client.get(STAR_REVIEWS_URL)
assert response.status_code == 200, response.text
buckets = {
r["search_engine"]: r
for r in response.json()["search_engine_ratings"]
}
assert "" not in buckets
assert " " not in buckets
# r1 (''), r2 (NULL), r3 (whitespace) all land in 'Unknown'
assert buckets["Unknown"]["rating_count"] == 3
# ---------------------------------------------------------------------------
# POST /metrics/api/ratings/<id> validation + persistence (PR #3804)
# ---------------------------------------------------------------------------
class TestSaveResearchRatingValidation:
def test_rejects_boolean_rating(self, client):
"""``True`` is an int in Python but must not pass the 1-5 check."""
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": True},
headers=_csrf_headers(client),
)
assert response.status_code == 400, response.text
assert "between 1 and 5" in response.json()["message"]
@pytest.mark.parametrize("bad_rating", [0, 6, "5", None, 4.5])
def test_rejects_invalid_rating_values(self, client, bad_rating):
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": bad_rating},
headers=_csrf_headers(client),
)
assert response.status_code == 400, response.text
def test_rejects_boolean_sub_dimension(self, client):
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": 4, "accuracy": True},
headers=_csrf_headers(client),
)
assert response.status_code == 400, response.text
assert "accuracy must be an integer" in response.json()["message"]
@pytest.mark.parametrize(
"field", ["accuracy", "completeness", "relevance", "readability"]
)
@pytest.mark.parametrize("bad_value", [0, 6, "3"])
def test_rejects_out_of_range_sub_dimensions(
self, client, field, bad_value
):
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": 4, field: bad_value},
headers=_csrf_headers(client),
)
assert response.status_code == 400, response.text
assert field in response.json()["message"]
def test_rejects_non_string_feedback(self, client):
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": 4, "feedback": 123},
headers=_csrf_headers(client),
)
assert response.status_code == 400, response.text
def test_rejects_overlong_feedback(self, client):
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": 4, "feedback": "x" * 10001},
headers=_csrf_headers(client),
)
assert response.status_code == 400, response.text
def test_accepts_max_length_feedback(self, client):
with _seeded_metrics_db():
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": 4, "feedback": "x" * 10000},
headers=_csrf_headers(client),
)
assert response.status_code == 200, response.text
class TestSaveResearchRatingPersistence:
"""Persistence through the real DB (not mocks): insert and update paths."""
def test_saves_rating_with_sub_dimensions(self, client):
with _seeded_metrics_db() as Session:
response = client.post(
f"{RATINGS_URL}/test-id",
json={
"rating": 4,
"accuracy": 5,
"completeness": 4,
"relevance": 5,
"readability": 3,
"feedback": "Good results",
},
headers=_csrf_headers(client),
)
assert response.status_code == 200, response.text
data = response.json()
assert data["status"] == "success"
assert data["rating"] == 4
with Session() as session:
saved = (
session.query(ResearchRating)
.filter_by(research_id="test-id")
.one()
)
assert saved.rating == 4
assert saved.accuracy == 5
assert saved.completeness == 4
assert saved.relevance == 5
assert saved.readability == 3
assert saved.feedback == "Good results"
def test_updates_existing_rating_with_sub_dimensions(self, client):
now = datetime.now(UTC)
existing = ResearchRating(
research_id="test-id",
rating=2,
feedback="old feedback",
created_at=now,
)
with _seeded_metrics_db(existing) as Session:
response = client.post(
f"{RATINGS_URL}/test-id",
json={"rating": 4, "accuracy": 5, "completeness": 3},
headers=_csrf_headers(client),
)
assert response.status_code == 200, response.text
assert response.json()["status"] == "success"
with Session() as session:
rows = (
session.query(ResearchRating)
.filter_by(research_id="test-id")
.all()
)
assert len(rows) == 1, "update must not insert a second row"
assert rows[0].rating == 4
assert rows[0].accuracy == 5
assert rows[0].completeness == 3
# feedback not in the payload -> left untouched
assert rows[0].feedback == "old feedback"