Download test_agent.py from Ayush-003/ThinkOV2: direct link, hf CLI and curl.
- Browser
- Download file 6.23 kB
-
https://huggingface.co/spaces/Ayush-003/ThinkOV2/resolve/main/test_agent.py
- Command line
-
hf download hf://spaces/Ayush-003/ThinkOV2/test_agent.py
-
curl -L -o test_agent.py https://huggingface.co/spaces/Ayush-003/ThinkOV2/resolve/main/test_agent.py
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 | |
| def anyio_backend(): | |
| return "asyncio" | |
| 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""" | |
| 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" | |
| 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""" | |
| 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 | |
| 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 | |
| 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""" | |
| 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') | |
| 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') | |
| 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""" | |
| 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 | |
| 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" | |
| 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""" | |
| 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"]) | |