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.

6.5 KB
Raw Blame
"""FD/thread diagnostics on GET /api/v1/health (#4915, FastAPI port).

Ports the resources-block tests from main's Flask-era
``tests/web/test_api_coverage.py``: an authenticated request exposes a
``resources`` dict (fd_count, fd_soft_limit, fd_hard_limit,
fd_usage_percent, thread_count); an unauthenticated request gets only the
public ``status``/``message``/``timestamp`` fields so process internals
never leak to anonymous callers.

Authentication gating uses the optional ``get_session_username`` dependency
(returns None without a session), so the authenticated cases override that
dependency directly — no login flow needed.
"""

import sys
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

HEALTH_URL = "/api/v1/health"
API_V1 = "local_deep_research.web.routers.api_v1"


@pytest.fixture
def client():
    """Anonymous client — no session, ``get_session_username`` yields None."""
    from local_deep_research.web.fastapi_app import app

    yield TestClient(app)


@pytest.fixture
def auth_client():
    """Client whose optional-session dependency resolves to ``testuser``."""
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import get_session_username

    app.dependency_overrides[get_session_username] = lambda: "testuser"
    try:
        yield TestClient(app)
    finally:
        app.dependency_overrides.pop(get_session_username, None)


class TestHealthPublicShape:
    def test_returns_ok_without_auth(self, client):
        resp = client.get(HEALTH_URL)
        assert resp.status_code == 200
        data = resp.json()
        assert data["status"] == "ok"
        assert data["message"] == "API is running"
        assert "timestamp" in data

    def test_no_resources_when_unauthenticated(self, client):
        """Unauthenticated requests must NOT get resource diagnostics."""
        resp = client.get(HEALTH_URL)
        assert resp.status_code == 200
        assert "resources" not in resp.json()


class TestHealthResourceDiagnostics:
    def test_resources_structure(self, auth_client):
        """Authenticated response contains resources with the expected keys."""
        resp = auth_client.get(HEALTH_URL)
        assert resp.status_code == 200
        data = resp.json()
        assert "resources" in data
        assert set(data["resources"].keys()) == {
            "fd_count",
            "fd_soft_limit",
            "fd_hard_limit",
            "fd_usage_percent",
            "thread_count",
        }

    def test_thread_count_positive(self, auth_client):
        """thread_count is always >= 1 (main thread)."""
        tc = auth_client.get(HEALTH_URL).json()["resources"]["thread_count"]
        assert isinstance(tc, int)
        assert tc >= 1

    @pytest.mark.skipif(
        sys.platform != "linux", reason="/proc/self/fd only on Linux"
    )
    def test_fd_count_on_linux(self, auth_client):
        """On Linux, fd_count is a non-negative integer."""
        fd = auth_client.get(HEALTH_URL).json()["resources"]["fd_count"]
        assert isinstance(fd, int)
        assert fd >= 0

    def test_no_resource_module(self, auth_client):
        """When the POSIX resource module is unavailable (Windows), FD limits
        are None and the endpoint still returns 200."""
        with patch(f"{API_V1}._resource_mod", None):
            resp = auth_client.get(HEALTH_URL)
        assert resp.status_code == 200
        data = resp.json()
        assert data["status"] == "ok"
        assert data["resources"]["fd_soft_limit"] is None
        assert data["resources"]["fd_hard_limit"] is None

    def test_no_proc_fs(self, auth_client):
        """When /proc/self/fd is unavailable (macOS), fd_count and the usage
        percentage are None rather than a 500."""
        with patch(f"{API_V1}.os.listdir", side_effect=OSError("no /proc")):
            resp = auth_client.get(HEALTH_URL)
        assert resp.status_code == 200
        res = resp.json()["resources"]
        assert res["fd_count"] is None
        assert res["fd_usage_percent"] is None

    def test_warning_status_high_fd(self, auth_client):
        """Status becomes 'warning' when FD usage exceeds 70%."""
        fake_fds = [str(i) for i in range(80)]
        mock_res = MagicMock()
        mock_res.RLIM_INFINITY = -1
        mock_res.RLIMIT_NOFILE = 7
        mock_res.getrlimit.return_value = (100, 100)
        with (
            patch(f"{API_V1}.os.listdir", return_value=fake_fds),
            patch(f"{API_V1}._resource_mod", mock_res),
        ):
            resp = auth_client.get(HEALTH_URL)
        assert resp.status_code == 200
        data = resp.json()
        assert data["status"] == "warning"
        assert "High FD usage" in data["message"]
        assert data["resources"]["fd_usage_percent"] == 80.0

    def test_ok_status_normal_fd(self, auth_client):
        """Status stays 'ok' when FD usage is low."""
        fake_fds = [str(i) for i in range(10)]
        mock_res = MagicMock()
        mock_res.RLIM_INFINITY = -1
        mock_res.RLIMIT_NOFILE = 7
        mock_res.getrlimit.return_value = (1000, 1000)
        with (
            patch(f"{API_V1}.os.listdir", return_value=fake_fds),
            patch(f"{API_V1}._resource_mod", mock_res),
        ):
            resp = auth_client.get(HEALTH_URL)
        data = resp.json()
        assert data["status"] == "ok"
        assert data["resources"]["fd_usage_percent"] == 1.0

    def test_getrlimit_oserror_does_not_500(self, auth_client):
        """A getrlimit OSError must not break the health endpoint."""
        mock_res = MagicMock()
        mock_res.RLIM_INFINITY = -1
        mock_res.RLIMIT_NOFILE = 7
        mock_res.getrlimit.side_effect = OSError("getrlimit failed")
        with patch(f"{API_V1}._resource_mod", mock_res):
            resp = auth_client.get(HEALTH_URL)
        assert resp.status_code == 200
        res = resp.json()["resources"]
        assert res["fd_soft_limit"] is None
        assert res["fd_hard_limit"] is None
        assert res["fd_usage_percent"] is None

    def test_rlim_infinity_becomes_none(self, auth_client):
        """RLIM_INFINITY limits are reported as None (not a huge sentinel)."""
        mock_res = MagicMock()
        mock_res.RLIM_INFINITY = -1
        mock_res.RLIMIT_NOFILE = 7
        mock_res.getrlimit.return_value = (-1, -1)
        with patch(f"{API_V1}._resource_mod", mock_res):
            resp = auth_client.get(HEALTH_URL)
        res = resp.json()["resources"]
        assert res["fd_soft_limit"] is None
        assert res["fd_hard_limit"] is None
        assert res["fd_usage_percent"] is None