File size: 6,812 Bytes
644f326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import json
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi.testclient import TestClient
from langchain_core.messages import HumanMessage, AIMessage, AIMessageChunk
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.outputs import ChatGenerationChunk, ChatResult

from src.agents.graph import create_agent_graph


class FakeStreamingChatModel(BaseChatModel):
    def _generate(self, messages, stop=None, run_manager=None, **kwargs):
        raise NotImplementedError()

    async def _agenerate(self, messages, stop=None, run_manager=None, **kwargs):
        chunk_reasoning = ChatGenerationChunk(
            message=AIMessageChunk(
                content="",
                additional_kwargs={"reasoning_content": "Analyzing user inquiry..."},
            )
        )
        if run_manager:
            await run_manager.on_llm_new_token("", chunk=chunk_reasoning)

        tokens = ["<think>", "Internal deliberations", "</think>", "This is the final response."]
        for t in tokens:
            chunk = ChatGenerationChunk(message=AIMessageChunk(content=t))
            if run_manager:
                await run_manager.on_llm_new_token(t, chunk=chunk)

        return ChatResult(
            generations=[
                ChatGenerationChunk(
                    message=AIMessageChunk(content="This is the final response.")
                )
            ]
        )

    @property
    def _llm_type(self):
        return "fake_streaming_chat_model"


def parse_sse_events(sse_text: str):
    """Parse SSE raw text into a list of (event_type, json_data) tuples."""
    events = []
    current_event = "message"
    for line in sse_text.splitlines():
        line = line.strip()
        if not line:
            continue
        if line.startswith("event:"):
            current_event = line.replace("event:", "").strip()
        elif line.startswith("data:"):
            data_str = line.replace("data:", "").strip()
            data = json.loads(data_str)
            events.append((current_event, data))
    return events


def test_chat_unauthenticated(client):
    """Chat endpoint should reject unauthenticated requests without user_id / thread_id."""
    response = client.post(
        "/api/v1/chat",
        json={"message": "Hello"},
    )
    assert response.status_code == 401


def test_chat_streaming_success(client):
    """Chat endpoint should stream reasoning, tool_start/tool_end, token, and final SSE events."""
    async def mock_planner(state):
        return {"route_to": "retriever", "queries": "kubernetes ingress"}

    async def mock_retriever(state):
        return {"retreived_content": ["Document snippet about ingress"]}

    fake_llm = FakeStreamingChatModel()

    with patch("src.agents.graph.planner_node", side_effect=mock_planner), \
            patch("src.agents.nodes.responder.get_llm", return_value=fake_llm) as mock_get_llm, \
         patch("src.agents.graph.retreived_node", side_effect=mock_retriever):

        test_graph = create_agent_graph()
        with patch("api.routes.chat_router.get_graph", return_value=test_graph):
            response = client.post(
                "/api/v1/chat",
                headers={"x-user-id": "usr_123", "x-thread-id": "thr_456"},
                json={
                    "message": "How does ingress work?",
                    "stream": True,
                    "metadata": {"model": "openai/gpt-oss-20b"},
                },
            )

            assert response.status_code == 200
            assert "text/event-stream" in response.headers.get("content-type", "")

            events = parse_sse_events(response.text)
            payloads = [ev[1] for ev in events if isinstance(ev[1], dict)]
            payload_types = [p.get("type") for p in payloads]

            assert "reasoning" in payload_types
            reasoning_contents = [p.get("content") for p in payloads if p.get("type") == "reasoning"]
            assert any("analyzing" in c.lower() for c in reasoning_contents)

            assert "tool_start" in payload_types
            assert any(p.get("tool") == "retriever" for p in payloads if p.get("type") == "tool_start")
            assert "tool_end" in payload_types
            assert any(p.get("tool") == "retriever" for p in payloads if p.get("type") == "tool_end")

            assert "token" in payload_types
            token_contents = "".join([p.get("content", "") for p in payloads if p.get("type") == "token"])
            assert "This is the final response." in token_contents

            assert "final" in payload_types
            final_payload = next(p for p in payloads if p.get("type") == "final")
            assert "This is the final response." in final_payload.get("ai_response", "")
            assert final_payload.get("state") == "completed"
            mock_get_llm.assert_called_once_with(model="openai/gpt-oss-20b")


def test_chat_non_streaming_success(client):
    """Chat endpoint should support non-streaming JSON responses when stream=False."""
    async def mock_planner(state):
        return {"route_to": "responder"}

    async def mock_responder(state):
        return {
            "messages": [
                HumanMessage(content=state.get("user_message")),
                AIMessage(content="Direct JSON answer"),
            ],
            "retreived_content": [
                type("FakeDocument", (), {"page_content": "Retrieved DevOps context"})()
            ],
        }

    with patch("src.agents.graph.planner_node", side_effect=mock_planner), \
         patch("src.agents.graph.responder_node", side_effect=mock_responder):

        test_graph = create_agent_graph()
        with patch("api.routes.chat_router.get_graph", return_value=test_graph):
            response = client.post(
                "/api/v1/chat",
                headers={"x-user-id": "usr_123", "x-thread-id": "thr_456"},
                json={"message": "What is Python?", "stream": False},
            )

            assert response.status_code == 200
            data = response.json()
            assert data.get("success") is True
            assert data.get("response") == "Direct JSON answer"
            assert data.get("data", {}).get("retreived_content") == ["Retrieved DevOps context"]


def test_chat_graph_unconfigured(client):
    """Chat endpoint should handle missing graph configuration gracefully."""
    with patch("api.routes.chat_router.get_graph", side_effect=Exception("Config error")):

        response = client.post(
            "/api/v1/chat",
            headers={"x-user-id": "usr_123", "x-thread-id": "thr_456"},
            json={"message": "Hello", "stream": False},
        )
        assert response.status_code == 500
        assert "error" in response.json()