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.

3.2 KB
Raw Blame
"""Route-level contracts for the FastAPI unified-search router.

Replaces the Flask tests/web/routes/test_unified_search_routes.py deleted in
the FastAPI migration. Uses a real register/login (the search endpoints open
the user's encrypted library DB). With an empty library, keyword/semantic
search return an empty result set (never a 5xx).
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def client():
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)
    user = f"usearch_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    def _csrf():
        c.get("/auth/login")
        r = c.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Login bootstrap failed: expected 302, got {resp.status_code}: "
            f"{resp.text[:500]}"
        )
    yield c


def test_endpoints_require_auth():
    from local_deep_research.web.fastapi_app import app

    anon = TestClient(app, raise_server_exceptions=False)
    for path in (
        "/library/search/api/keyword?q=hello",
        "/library/search/api/semantic?q=hello",
    ):
        r = anon.get(path, follow_redirects=False)
        assert r.status_code != 200, f"{path} served an anonymous 200"
        assert r.status_code < 500, f"{path} 5xx'd for anon: {r.text[:150]}"


def test_page_renders(client):
    r = client.get("/library/search/", follow_redirects=False)
    assert r.status_code == 200
    assert "text/html" in r.headers.get("content-type", "")


def test_keyword_query_too_short_is_400(client):
    r = client.get("/library/search/api/keyword?q=a")
    assert r.status_code == 400
    assert r.json()["success"] is False


def test_keyword_empty_library_returns_empty(client):
    r = client.get("/library/search/api/keyword?q=nonexistent-term")
    assert r.status_code == 200
    body = r.json()
    assert body["success"] is True
    assert body["query"] == "nonexistent-term"
    assert body["results"] == []
    assert body["count"] == 0


def test_keyword_invalid_limit_is_400(client):
    r = client.get("/library/search/api/keyword?q=hello&limit=notanint")
    assert r.status_code == 400
    assert r.json()["success"] is False


def test_semantic_query_too_short_is_400(client):
    r = client.get("/library/search/api/semantic?q=a")
    assert r.status_code == 400
    assert r.json()["success"] is False


def test_semantic_empty_library_no_5xx(client):
    # No collections configured for a fresh user: the route short-circuits to
    # an empty result set and must never 5xx.
    r = client.get("/library/search/api/semantic?q=nonexistent-term")
    assert r.status_code < 500, r.text[:200]
    if r.status_code == 200:
        assert r.json()["success"] is True