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.

4.4 KB
Raw Blame
#!/usr/bin/env python3
"""
Test the browser's research creation endpoint specifically
"""

# allow: no-sut-import — this module exercises production code over HTTP
# rather than by importing it: every test drives real FastAPI routes
# (/api/start_research, /research/api/status/<id>) through the
# `authenticated_client` fixture, which builds the real app. There is no
# symbol to import and call directly; the routes ARE the subject. Verified
# not to be a shadow test in the sense the hook guards against: breaking
# /research/api/status/<id> in src/ makes test_research_status_endpoint
# fail, so production code really is under test here.

import json


class TestBrowserEndpoint:
    """Test browser-specific endpoints."""

    def test_start_research_endpoint(self, authenticated_client):
        """Test the browser endpoint /api/start_research."""
        research_data = {
            "query": "Test from browser endpoint",
            "mode": "quick",
            "model_provider": "OLLAMA",
            "model": "llama2",
            "search_engine": "searxng",
            "max_results": 10,
            "time_period": "y",
            "iterations": 1,
            "questions_per_iteration": 3,
            "strategy": "source-based",
            "local_context": 2000,
            "web_context": 2000,
            "temperature": 0.7,
        }

        response = authenticated_client.post(
            "/api/start_research",
            json=research_data,
            content_type="application/json",
        )

        assert response.status_code in [200, 202]
        data = json.loads(response.data)

        if response.status_code == 200:
            assert data.get("status") in ["success", "processing"]
            if "research_id" in data:
                assert isinstance(data["research_id"], (str, int))

    def test_research_status_endpoint(self, authenticated_client):
        """Test research status endpoint.

        Was previously all-conditional: a 200 (or a research_id-less
        200, or a 404 from the status lookup) skipped every assertion
        and still passed. This "audit: ... issue resolved by prior PR"
        marker was added by a comment-only bulk annotation (PR #4296,
        explicitly "zero behavioral effect") — the underlying gap was
        never actually fixed. Empirically /api/start_research returns
        200 with a research_id, and the freshly created research_id
        resolves via /research/api/status/<id> with 200, in this test
        environment — so those are now asserted unconditionally.
        """
        research_data = {
            "query": "Test research for status check",
            "mode": "quick",
            "model_provider": "OLLAMA",
            "model": "llama2",
            "search_engine": "wikipedia",
            "iterations": 1,
        }

        response = authenticated_client.post(
            "/api/start_research",
            json=research_data,
            content_type="application/json",
        )

        assert response.status_code == 200, (
            f"unexpected status starting research: {response.status_code} "
            f"{response.data!r}"
        )
        data = json.loads(response.data)
        research_id = data.get("research_id")
        assert research_id, (
            f"start_research response is missing research_id: {data!r}"
        )

        # Check status
        status_response = authenticated_client.get(
            f"/research/api/status/{research_id}"
        )
        assert status_response.status_code == 200, (
            "status endpoint returned "
            f"{status_response.status_code} for a research_id obtained "
            "from start_research"
        )
        status_data = json.loads(status_response.data)
        assert "status" in status_data

    def test_endpoint_requires_authentication(self, client):
        """Test that endpoint requires authentication."""
        research_data = {
            "query": "Test without auth",
            "mode": "quick",
        }

        response = client.post(
            "/api/start_research",
            json=research_data,
            content_type="application/json",
        )

        # Should either redirect to login or return 401
        assert response.status_code in [
            302,
            401,
            403,
        ]  # 403 = CSRF rejection (Wave 2 fail-closed)

        if response.status_code == 302:
            assert "/auth/login" in response.location