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.6 KB
Raw Blame
"""Unit tests for Chat route helper functions.

Tests verify the helper functions used in chat routes:
- _parse_int_param: Integer parameter parsing with validation

Password retrieval is delegated to web.auth.password_utils.get_user_password,
covered by tests/security/test_auth_credential_lifetime_fastapi.py and
tests/security/test_research_password_gate_fastapi.py. (The Flask-era
tests/web/auth/test_password_utils_gaps.py named here previously does not
survive the FastAPI port.)
"""

from starlette.requests import Request


class TestParseIntParam:
    """Tests for the _parse_int_param helper function."""

    def test_parse_int_param_valid_value(self):
        """Test parsing a valid integer string."""
        from local_deep_research.web.routers.chat import _parse_int_param

        result = _parse_int_param("42", default=10)
        assert result == 42

    def test_parse_int_param_default_on_invalid(self):
        """Test that invalid values return the default."""
        from local_deep_research.web.routers.chat import _parse_int_param

        # Non-numeric string
        result = _parse_int_param("abc", default=10)
        assert result == 10

        # Empty string
        result = _parse_int_param("", default=15)
        assert result == 15

    def test_parse_int_param_respects_min_val(self):
        """Test that values below min_val are clamped."""
        from local_deep_research.web.routers.chat import _parse_int_param

        # Value below minimum
        result = _parse_int_param("-5", default=10, min_val=0)
        assert result == 0

        # Value at minimum
        result = _parse_int_param("0", default=10, min_val=0)
        assert result == 0

    def test_parse_int_param_respects_max_val(self):
        """Test that values above max_val are clamped."""
        from local_deep_research.web.routers.chat import _parse_int_param

        # Value above maximum
        result = _parse_int_param("200", default=10, min_val=0, max_val=100)
        assert result == 100

        # Value at maximum
        result = _parse_int_param("100", default=10, min_val=0, max_val=100)
        assert result == 100

    def test_parse_int_param_handles_none(self):
        """Test that None values return the default."""
        from local_deep_research.web.routers.chat import _parse_int_param

        result = _parse_int_param(None, default=25)
        assert result == 25

    def test_parse_int_param_handles_float_string(self):
        """Test that float strings fall back to default."""
        from local_deep_research.web.routers.chat import _parse_int_param

        result = _parse_int_param("3.14", default=10)
        assert result == 10


class TestChatUserKey:
    """Tests for the _chat_user_key helper function."""

    def test_chat_user_key_falls_back_to_client_ip_without_session(self):
        """Test that requests without session scope use the client IP."""
        from local_deep_research.web.routers.chat import _chat_user_key

        request = Request(
            {
                "type": "http",
                "method": "GET",
                "path": "/",
                "headers": [],
                "client": ("192.168.1.10", 12345),
            }
        )

        assert _chat_user_key(request) == "192.168.1.10"

    def test_chat_user_key_uses_username_from_session(self):
        """Test that a session username is used when session scope exists."""
        from local_deep_research.web.routers.chat import _chat_user_key

        request = Request(
            {
                "type": "http",
                "method": "GET",
                "path": "/",
                "headers": [],
                "client": ("192.168.1.10", 12345),
                "session": {"username": "alice"},
            }
        )

        assert _chat_user_key(request) == "user:alice"

    def test_chat_user_key_keeps_usernames_out_of_the_ip_namespace(self):
        """A username shaped like an address must not share the bucket of
        an anonymous caller from that address."""
        from local_deep_research.web.routers.chat import _chat_user_key

        def request(**extra):
            return Request(
                {
                    "type": "http",
                    "method": "GET",
                    "path": "/",
                    "headers": [],
                    "client": ("192.168.1.10", 12345),
                    **extra,
                }
            )

        named = request(session={"username": "192.168.1.10"})
        anonymous = request()
        assert _chat_user_key(anonymous) == "192.168.1.10"
        assert _chat_user_key(named) != _chat_user_key(anonymous)