# test_search_tools.py import sys import os from dotenv import load_dotenv # Load environment variables load_dotenv() # Add tools to path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) def test_search_tools(): """Test search tool functions""" try: from tools.search_tools import SearchTools, search_web, search_news print(" 🔍 Initializing search tools...") search_tools = SearchTools() # Test DuckDuckGo search (free) print(" 🦆 Testing DuckDuckGo search...") ddg_results = search_tools.search_duckduckgo("Python programming", max_results=3) if ddg_results: print(f" ✅ DuckDuckGo returned {len(ddg_results)} results") for i, result in enumerate(ddg_results[:2]): print(f" {i+1}. {result.get('title', 'No title')[:50]}...") else: print(" ⚠️ DuckDuckGo returned no results") # Test Tavily search (if API key available) tavily_key = os.getenv("TAVILY_API_KEY") if tavily_key: print(" 🔎 Testing Tavily search...") tavily_results = search_tools.search_tavily("AI news 2025", max_results=3) if tavily_results: print(f" ✅ Tavily returned {len(tavily_results)} results") else: print(" ⚠️ Tavily returned no results") else: print(" ⚠️ Tavily API key not found, skipping Tavily tests") # Test SerpAPI search (if API key available) serpapi_key = os.getenv("SERPAPI_KEY") if serpapi_key: print(" 🐍 Testing SerpAPI search...") serp_results = search_tools.search_serpapi("machine learning", max_results=2) if serp_results: print(f" ✅ SerpAPI returned {len(serp_results)} results") else: print(" ⚠️ SerpAPI returned no results") else: print(" ⚠️ SerpAPI key not found, skipping SerpAPI tests") # Test main search function (with fallback) print(" 🎯 Testing main search function...") main_results = search_tools.search("CrewAI framework", max_results=3) if main_results: print(f" ✅ Main search returned {len(main_results)} results") print(f" Provider used: {main_results[0].get('source', 'Unknown')}") else: print(" ❌ Main search failed") # Test convenience functions print(" 🚀 Testing convenience functions...") web_results = search_web("OpenRouter API", max_results=2) news_results = search_news("artificial intelligence", max_results=2) if web_results: print(f" ✅ Web search returned {len(web_results)} results") if news_results: print(f" ✅ News search returned {len(news_results)} results") # Test specialized searches print(" 🎓 Testing specialized searches...") academic_results = search_tools.search_academic("machine learning algorithms", max_results=2) if academic_results: print(f" ✅ Academic search returned {len(academic_results)} results") print(" ✅ ALL SEARCH TESTS COMPLETED!") # Demo output if main_results: print("\n 📋 Search Results Demo:") for i, result in enumerate(main_results[:2]): title = result.get('title', 'No title')[:60] source = result.get('source', 'Unknown') print(f" {i+1}. [{source}] {title}...") except ImportError as e: print(f" ❌ Import error: {e}") except Exception as e: print(f" ❌ Test failed: {e}") print(f" Error details: {str(e)}") if __name__ == "__main__": test_search_tools()