- 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
35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
import pytest
|
|
from unittest.mock import AsyncMock
|
|
from types import SimpleNamespace
|
|
from httpx import ASGITransport, AsyncClient
|
|
from naliiabotapi.main import app
|
|
from naliiabotapi.api.dependencies import get_agent
|
|
|
|
|
|
class TestWebhookChatBot:
|
|
"""Tests for WebhookChatBot."""
|
|
|
|
@pytest.mark.anyio
|
|
async def tests_webhook_success(self, send_message_payload):
|
|
"""Test that the webhook endpoint returns a successful response.
|
|
|
|
Se crea un `mock_agent` con `ainvoke` mockeado y se inyecta
|
|
mediante `app.dependency_overrides` antes de realizar la petición.
|
|
"""
|
|
mock_agent = SimpleNamespace()
|
|
mock_agent.ainvoke = AsyncMock(
|
|
return_value={"messages": [SimpleNamespace(content="Respuesta mock")]}
|
|
)
|
|
|
|
app.dependency_overrides[get_agent] = lambda: mock_agent
|
|
|
|
async with AsyncClient(
|
|
transport=ASGITransport(app=app), base_url="http://localhost:8010"
|
|
) as client:
|
|
response = await client.post("/webhook", json=send_message_payload)
|
|
|
|
app.dependency_overrides.pop(get_agent, None)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json().get("reply") == "Respuesta mock"
|