Spaces:
Running
Running
| """ | |
| Quick Manual Test Script for Smart PGC Chatbot | |
| Run this script to interactively test the chatbot with various inputs. | |
| Usage: | |
| python tests/quick_test.py | |
| """ | |
| import asyncio | |
| import sys | |
| from pathlib import Path | |
| # Add parent directory to path | |
| sys.path.insert(0, str(Path(__file__).parent.parent)) | |
| from app.local_plant_db import search_plant, get_plant_parameters, reload_database | |
| def test_database_searches(): | |
| """Quick database search tests.""" | |
| print("\n" + "="*50) | |
| print("🔍 DATABASE SEARCH TESTS") | |
| print("="*50) | |
| reload_database() | |
| test_inputs = [ | |
| # By ID | |
| "watermelon", | |
| "lettuce", | |
| "chili_pepper", | |
| # By Indonesian name | |
| "Semangka", | |
| "Selada", | |
| "Cabai", | |
| "Kangkung", | |
| "Bayam", | |
| # By variety name | |
| "Baginda F1", | |
| "Amara", | |
| "Bara F1", | |
| "Grand Rapids", | |
| "Nauli F1", | |
| # By scientific name | |
| "Citrullus lanatus", | |
| "Lactuca sativa", | |
| # Not in database | |
| "dragon fruit", | |
| "strawberry", | |
| ] | |
| for query in test_inputs: | |
| plant = search_plant(query) | |
| if plant: | |
| print(f"✅ '{query}' → {plant.get('id')} (varieties: {plant.get('varieties', [])})") | |
| else: | |
| print(f"❌ '{query}' → NOT FOUND") | |
| def test_parameter_retrieval(): | |
| """Quick parameter retrieval tests.""" | |
| print("\n" + "="*50) | |
| print("📊 PARAMETER RETRIEVAL TESTS") | |
| print("="*50) | |
| test_cases = [ | |
| ("watermelon", "germination"), | |
| ("watermelon", "vegetative"), | |
| ("Semangka", "seedling"), | |
| ("Baginda F1", "germination"), # Search by variety | |
| ("lettuce", "germination"), | |
| ("chili_pepper", "seedling"), | |
| ] | |
| for plant_name, stage in test_cases: | |
| params = get_plant_parameters(plant_name, stage) | |
| if params: | |
| print(f"\n✅ {plant_name} ({stage}):") | |
| print(f" Temp: {params.get('ideal_temp_min')}-{params.get('ideal_temp_max')}°C") | |
| print(f" RH: {params.get('ideal_rh_min')}-{params.get('ideal_rh_max')}%") | |
| print(f" Light: {params.get('ideal_light_min')}-{params.get('ideal_light_max')} lux") | |
| print(f" Varieties: {params.get('varieties', [])}") | |
| print(f" Common Names: {params.get('common_names', [])}") | |
| else: | |
| print(f"\n❌ {plant_name} ({stage}): NOT FOUND") | |
| async def test_ai_extraction(): | |
| """Test AI plant extraction (requires API key).""" | |
| print("\n" + "="*50) | |
| print("🤖 AI EXTRACTION TESTS (requires CEREBRAS_API_KEY)") | |
| print("="*50) | |
| try: | |
| from app.ai_engine import extract_plant_from_query, classify_query | |
| test_queries = [ | |
| "I want to grow watermelon", | |
| "What temperature for tomatoes?", | |
| "Saya mau tanam cabai", | |
| "Parameter untuk semangka", | |
| "How to grow Baginda F1?", | |
| "What is the current humidity?", | |
| "How do I calibrate the sensor?", | |
| ] | |
| for query in test_queries: | |
| print(f"\n📝 Query: '{query}'") | |
| # Extract plant | |
| plant = await extract_plant_from_query(query) | |
| print(f" Plant: {plant}") | |
| # Classify query | |
| query_type = await classify_query(query) | |
| print(f" Type: {query_type.value}") | |
| except Exception as e: | |
| print(f"\n⚠️ AI tests failed: {e}") | |
| print("Make sure CEREBRAS_API_KEY is set in your .env file") | |
| async def test_full_chat(): | |
| """Test full chat response.""" | |
| print("\n" + "="*50) | |
| print("💬 FULL CHAT RESPONSE TEST") | |
| print("="*50) | |
| try: | |
| from app.ai_engine import generate_context_aware_response | |
| mock_sensors = {"temp": 28.5, "rh": 70.0, "light": 15000} | |
| test_queries = [ | |
| "I want to grow watermelon seeds", | |
| "What varieties of chili do you have?", | |
| "Parameter Baginda F1 untuk germination", | |
| ] | |
| for query in test_queries: | |
| print(f"\n📝 Query: '{query}'") | |
| response = await generate_context_aware_response( | |
| query=query, | |
| sensors=mock_sensors, | |
| ) | |
| print(f" Plant: {response.get('plant_detected')}") | |
| print(f" Stage: {response.get('stage')}") | |
| print(f" Source: {response.get('data_source')}") | |
| params = response.get('parameters', {}) | |
| if params: | |
| print(f" Varieties: {params.get('varieties', [])}") | |
| print(f" Common Names: {params.get('common_names', [])}") | |
| # Show truncated response | |
| resp_text = response.get('response', '') | |
| if len(resp_text) > 200: | |
| resp_text = resp_text[:200] + "..." | |
| print(f" Response: {resp_text}") | |
| except Exception as e: | |
| print(f"\n⚠️ Chat test failed: {e}") | |
| async def main(): | |
| """Run all quick tests.""" | |
| print("\n" + "="*50) | |
| print("🧪 SMART PGC QUICK TEST SUITE") | |
| print("="*50) | |
| # Database tests (no API needed) | |
| test_database_searches() | |
| test_parameter_retrieval() | |
| # AI tests (need API key) | |
| await test_ai_extraction() | |
| await test_full_chat() | |
| print("\n" + "="*50) | |
| print("✅ Quick tests completed!") | |
| print("="*50) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |