ThinkOV2 / test_agent.py
Ayush-003's picture
Upload 28 files
9be2851 verified
Raw History Blame Contribute Delete
6.23 kB
"""
Test suite for Grocify Chat Agent
"""
import pytest
import asyncio
from httpx import AsyncClient
from app import app
from schemas import ChatRequest, ProductSearchInput, OrderLookupInput
from tools import tool_registry
from memory import session_manager
@pytest.fixture
def anyio_backend():
return "asyncio"
@pytest.fixture
async def client():
"""Create test client"""
async with AsyncClient(app=app, base_url="http://test") as ac:
yield ac
class TestHealthEndpoints:
"""Test health and info endpoints"""
@pytest.mark.anyio
async def test_root(self, client):
response = await client.get("/")
assert response.status_code == 200
data = response.json()
assert data["service"] == "Grocify Chat Agent"
assert data["status"] == "healthy"
@pytest.mark.anyio
async def test_health(self, client):
response = await client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
class TestChatEndpoint:
"""Test main chat endpoint"""
@pytest.mark.anyio
async def test_simple_chat(self, client):
request = {
"user_message": "Hello, I need help with my order",
"locale": "en-IN"
}
response = await client.post("/chat", json=request)
assert response.status_code == 200
data = response.json()
assert "session_id" in data
assert "response" in data
assert "reply_text" in data["response"]
assert data["response"]["confidence"] >= 0
@pytest.mark.anyio
async def test_product_search_intent(self, client):
request = {
"user_message": "Show me Diwali gifts under 2000",
"locale": "en-IN"
}
response = await client.post("/chat", json=request)
assert response.status_code == 200
data = response.json()
# Should trigger product search
assert data["response"]["confidence"] > 0.5
@pytest.mark.anyio
async def test_escalation_trigger(self, client):
request = {
"user_message": "I want to speak to a human agent",
"locale": "en-IN"
}
response = await client.post("/chat", json=request)
assert response.status_code == 200
data = response.json()
# Should trigger escalation
assert data["response"]["escalation"] is not None
class TestTools:
"""Test individual tools"""
@pytest.mark.anyio
async def test_product_search_tool(self):
tool = tool_registry.get_tool("product_search")
assert tool is not None
input_data = ProductSearchInput(
query="skincare",
max_price=2000,
limit=3
)
results = await tool.execute(input_data)
assert isinstance(results, list)
assert len(results) <= 3
if results:
assert hasattr(results[0], 'product_id')
assert hasattr(results[0], 'price')
@pytest.mark.anyio
async def test_order_lookup_tool(self):
tool = tool_registry.get_tool("order_lookup")
assert tool is not None
input_data = OrderLookupInput(
order_id="12345",
email="test@example.com"
)
result = await tool.execute(input_data)
assert hasattr(result, 'order_id')
assert hasattr(result, 'status')
@pytest.mark.anyio
async def test_promotions_check_tool(self):
from schemas import PromotionsCheckInput
tool = tool_registry.get_tool("promotions_check")
assert tool is not None
input_data = PromotionsCheckInput(
coupon="DIWALI25"
)
result = await tool.execute(input_data)
assert hasattr(result, 'coupon')
assert hasattr(result, 'valid')
class TestSessionManagement:
"""Test session and memory management"""
@pytest.mark.anyio
async def test_create_session(self):
session_id = "test-session-123"
session = await session_manager.get_or_create_session(session_id)
assert session.session_id == session_id
assert len(session.conversation) == 0
@pytest.mark.anyio
async def test_add_message_to_session(self):
session_id = "test-session-456"
await session_manager.get_or_create_session(session_id)
await session_manager.add_message(
session_id,
"user",
"Hello"
)
history = await session_manager.get_conversation_history(session_id)
assert len(history) == 1
assert history[0]["role"] == "user"
assert history[0]["content"] == "Hello"
@pytest.mark.anyio
async def test_failed_attempts_counter(self):
session_id = "test-session-789"
await session_manager.get_or_create_session(session_id)
count1 = await session_manager.increment_failed_attempts(session_id)
assert count1 == 1
count2 = await session_manager.increment_failed_attempts(session_id)
assert count2 == 2
await session_manager.reset_failed_attempts(session_id)
session = await session_manager.store.get_session(session_id)
assert session.failed_attempts == 0
class TestMultilingual:
"""Test multilingual capabilities"""
@pytest.mark.anyio
async def test_hindi_locale(self, client):
request = {
"user_message": "मुझे अपने ऑर्डर की जानकारी चाहिए",
"locale": "hi-IN"
}
response = await client.post("/chat", json=request)
assert response.status_code == 200
data = response.json()
assert data["metadata"]["locale"] == "hi-IN"
if __name__ == "__main__":
pytest.main([__file__, "-v"])