fix: add MCP result validation, improve agent error handling and state management
- Add `_extract_mcp_content_text()` helper in naliia_tools.py to safely extract text from MCP results, preventing IndexError on empty content - Replace all direct `result.content[0].text` accesses with safe helper - Improve customer identification with preservation of existing state - Add proper JSON parsing with fallback and error handling - Simplify webhook state management (use agent's checkpointer internally) - Update system prompt to remove check_mcp_connection tool reference - Fix tests to use async mocks and correct expected values
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock
|
||||
from types import SimpleNamespace
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from naliiabotapi.main import app
|
||||
@@ -12,27 +13,21 @@ class TestWebhookChatBot:
|
||||
async def tests_webhook_success(self, send_message_payload):
|
||||
"""Test that the webhook endpoint returns a successful response.
|
||||
|
||||
Se crea un `mock_agent` con `invoke` mockeado y se inyecta
|
||||
Se crea un `mock_agent` con `ainvoke` mockeado y se inyecta
|
||||
mediante `app.dependency_overrides` antes de realizar la petición.
|
||||
"""
|
||||
# Crear mock del agente y su invoke
|
||||
mock_agent = SimpleNamespace()
|
||||
mock_agent.ainvoke = AsyncMock(
|
||||
return_value={"messages": [SimpleNamespace(content="Respuesta mock")]}
|
||||
)
|
||||
|
||||
def mock_invoke(state, config=None):
|
||||
return {"messages": [SimpleNamespace(content="Respuesta mock")]}
|
||||
|
||||
mock_agent.invoke = mock_invoke
|
||||
|
||||
# Sobrescribir la dependencia del agente en la app
|
||||
app.dependency_overrides[get_agent] = lambda: mock_agent
|
||||
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=app),
|
||||
base_url="http://localhost:8010"
|
||||
transport=ASGITransport(app=app), base_url="http://localhost:8010"
|
||||
) as client:
|
||||
response = await client.post("/webhook", json=send_message_payload)
|
||||
|
||||
# Limpiar override para no afectar a otros tests
|
||||
app.dependency_overrides.pop(get_agent, None)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user