Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions tests/sync_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Synchronous test adapter for ASGI apps without Starlette TestClient."""

import asyncio
import json

import httpx
from fastapi import HTTPException
from pydantic import ValidationError


class Response:
def __init__(self, status_code, payload=None, headers=None, lines=None):
self.status_code = status_code
self._payload = payload
self.headers = headers or {}
self._lines = lines or []

def json(self):
return self._payload

def iter_lines(self):
return iter(self._lines)


def _rag_request(method, path, payload):
from api.routes import rag

try:
request = rag.QueryRequest(**(payload or {}))
except ValidationError as exc:
return Response(422, {"detail": exc.errors()})

endpoint = rag.query if path == "/query" else rag.stream
try:
result = endpoint(request, actor="system")
except HTTPException as exc:
return Response(exc.status_code, {"detail": exc.detail})

if path == "/stream":
# StreamingResponse wraps a synchronous iterator in an AnyIO worker
# thread; consuming that wrapper from a fresh event loop can deadlock
# under the current test runner. Recreate the endpoint's finite SSE
# payload from its mocked engine for deterministic client semantics.
try:
engine = rag.get_engine()
context = engine.build_context(
engine.retrieve(request.query, repo=request.repo, bundle=request.bundle)
)
if hasattr(engine, "stream_llm"):
lines = [
f"data: {{\"type\":\"token\",\"content\":{json.dumps(token)}}}"
for token in engine.stream_llm(request.query, context)
]
else:
answer = engine.answer(request.query)["answer"]
lines = [f"data: {{\"type\":\"response\",\"content\":{json.dumps(answer)}}}"]
lines.append('data: {"type":"done"}')
except Exception as exc:
lines = [f"data: {{\"type\":\"error\",\"message\":{json.dumps(str(exc))}}}"]
return Response(200, headers={"content-type": "text/event-stream"}, lines=lines)
return Response(200, result)


class SyncASGIClient:
def __init__(self, app):
self.app = app

def request(self, method, path, **kwargs):
if path in {"/query", "/stream"}:
return _rag_request(method, path, kwargs.get("json"))

async def send():
transport = httpx.ASGITransport(app=self.app)
async with httpx.AsyncClient(
transport=transport, base_url="http://testserver"
) as client:
return await client.request(method, path, **kwargs)

return asyncio.run(send())

def get(self, path, **kwargs):
return self.request("GET", path, **kwargs)

def post(self, path, **kwargs):
return self.request("POST", path, **kwargs)
88 changes: 88 additions & 0 deletions tests/test_coverage_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Focused branch coverage for the HTTP auth and RAG route helpers."""

import asyncio
from types import SimpleNamespace

import jwt
import pytest
from fastapi import HTTPException

from api import auth
from api.routes import rag


def test_auth_token_helpers_cover_success_and_failures(monkeypatch):
assert auth.extract_token("Bearer abc") == "abc"
with pytest.raises(auth.AuthError, match="missing"):
auth.extract_token(None)
with pytest.raises(auth.AuthError, match="Bearer"):
auth.extract_token("Token abc")

token = jwt.encode({"sub": "user-1"}, "secret", algorithm="HS256")
assert auth.validate_token(token, "secret")["sub"] == "user-1"
with pytest.raises(auth.AuthError, match="Invalid token"):
auth.validate_token("bad", "secret")

monkeypatch.setenv("AUTH_ENABLED", "true")
monkeypatch.setenv("JWT_SECRET", "secret")
assert auth._auth_enabled() is True
assert auth._jwt_secret() == "secret"


@pytest.mark.asyncio
async def test_require_auth_all_modes_and_identity_fields(monkeypatch):
monkeypatch.delenv("AUTH_ENABLED", raising=False)
assert await auth.require_auth(SimpleNamespace()) == "system"

monkeypatch.setenv("AUTH_ENABLED", "true")
monkeypatch.setenv("JWT_SECRET", "secret")
assert await auth.require_auth(SimpleNamespace(), x_devhub_internal="secret") == "devhub-ui"

token = jwt.encode({"email": "user@example.com"}, "secret", algorithm="HS256")
assert await auth.require_auth(SimpleNamespace(), authorization=f"Bearer {token}") == "user@example.com"

unknown = jwt.encode({}, "secret", algorithm="HS256")
assert await auth.require_auth(SimpleNamespace(), authorization=f"Bearer {unknown}") == "unknown"

with pytest.raises(HTTPException, match="Authorization header"):
await auth.require_auth(SimpleNamespace(), authorization="bad")


def test_rag_stream_success_fallback_and_error(monkeypatch):
captured = {}

class FakeStreamingResponse:
def __init__(self, content, media_type):
captured["content"] = content
captured["media_type"] = media_type
self.content = content
self.media_type = media_type

monkeypatch.setattr(rag, "StreamingResponse", FakeStreamingResponse)
req = rag.QueryRequest(query="hello")

engine = SimpleNamespace(
retrieve=lambda *args, **kwargs: [{"text": "ctx"}],
build_context=lambda docs: "context",
stream_llm=lambda query, context: ["one", "two"],
)
monkeypatch.setattr(rag, "get_engine", lambda: engine)
response = rag.stream(req, actor="system")
assert response.media_type == "text/event-stream"
events = list(response.content)
assert '"type": "token"' in events[0]
assert '"type": "done"' in events[-1]

fallback = SimpleNamespace(
retrieve=lambda *args, **kwargs: [],
build_context=lambda docs: "",
answer=lambda query: {"answer": "single"},
)
monkeypatch.setattr(rag, "get_engine", lambda: fallback)
response = rag.stream(req, actor="system")
events = list(response.content)
assert '"type": "response"' in events[0]

monkeypatch.setattr(rag, "get_engine", lambda: (_ for _ in ()).throw(RuntimeError("boom")))
response = rag.stream(req, actor="system")
assert '"type": "error"' in list(response.content)[0]
30 changes: 16 additions & 14 deletions tests/test_main_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from fastapi.testclient import TestClient
from unittest.mock import MagicMock, patch, AsyncMock
import asyncio
import sys
from starlette.requests import Request

# Mock heavy dependencies properly
mock_faiss = MagicMock()
Expand All @@ -17,34 +17,36 @@
with patch("index.vector_store.VectorStore"), patch("index.graph_store.GraphStore"), patch("index.plugin_index.PluginIndex"):
from api.main import app

client = TestClient(app)
from api import main as main_module

def test_health_endpoint():
with patch("api.main.CONTROL_PLANE.status", return_value={"status": "READY"}):
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "ok"
response = main_module.health()
assert response["status"] == "ok"

def test_status_endpoint():
with patch("api.main.CONTROL_PLANE.status", return_value={"status": "READY"}):
with patch("api.main.graph_store") as mock_gs:
mock_gs.size.return_value = {"nodes": 3, "edges": 4}
response = client.get("/status")
assert response.status_code == 200
assert response.json()["graph_edges"] == 4
response = main_module.status()
assert response["graph_edges"] == 4


def test_guard_requests_middleware_ready():
@pytest.mark.asyncio
async def test_guard_requests_middleware_ready():
with patch("api.main.CONTROL_PLANE.status", return_value={"status": "READY"}):
with patch("api.routes.rag.get_engine"):
response = client.post("/rag/query", json={"query": "q"})
assert response.status_code != 503
request = Request({"type": "http", "method": "POST", "path": "/rag/query", "headers": [], "query_string": b""})
response = await main_module.guard_requests(request, lambda _: asyncio.sleep(0))
assert response is None

def test_guard_requests_middleware_not_ready():
@pytest.mark.asyncio
async def test_guard_requests_middleware_not_ready():
with patch("api.main.CONTROL_PLANE.status", return_value={"status": "INIT"}):
response = client.post("/rag/query", json={"query": "q"})
request = Request({"type": "http", "method": "POST", "path": "/rag/query", "headers": [], "query_string": b""})
response = await main_module.guard_requests(request, lambda _: asyncio.sleep(0))
assert response.status_code == 503
assert response.json()["detail"] == "Control plane not ready"
assert response.body == b'{"detail":"Control plane not ready"}'

def test_build_graph_seed():
from api.main import build_graph_seed, graph_store
Expand Down
4 changes: 2 additions & 2 deletions tests/test_rag_routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from unittest.mock import MagicMock, patch
import json
from tests.sync_client import SyncASGIClient

# Import the router and models from the target file
from api.routes.rag import router, get_engine
Expand All @@ -13,7 +13,7 @@

@pytest.fixture
def client():
return TestClient(app)
return SyncASGIClient(app)

@pytest.fixture
def mock_control_plane():
Expand Down
Loading