science-storyteller / tests /test_scholar_direct.py
tuhulab's picture
chore: Organize repository structure - move docs to docs/ and tests to tests/
28b3cfa
"""
Test script for Semantic Scholar Direct API integration
"""
import asyncio
import os
import sys
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Add project root to path
sys.path.insert(0, os.path.dirname(__file__))
from mcp_tools.scholar_tool import SemanticScholarTool
from agents.research_agent import ResearchAgent
async def test_scholar_tool():
"""Test Semantic Scholar tool directly"""
print("=" * 70)
print("TEST 1: Semantic Scholar Tool Direct API")
print("=" * 70)
tool = SemanticScholarTool()
# Test search
print("\nπŸ“ Testing search for '3I/ATLAS'...")
papers = await tool.search_papers("3I/ATLAS", max_results=3)
if papers:
print(f"\nβœ… Success! Found {len(papers)} papers:")
for i, paper in enumerate(papers, 1):
print(f"\n{i}. {paper['title']}")
print(f" Authors: {', '.join([a['name'] for a in paper.get('authors', [])[:3]])}")
print(f" Year: {paper.get('year', 'N/A')}")
print(f" Citations: {paper.get('citationCount', 0)}")
if paper.get('abstract'):
print(f" Abstract: {paper['abstract'][:150]}...")
else:
print("\n❌ No papers found")
await tool.disconnect()
return len(papers) > 0
async def test_research_agent():
"""Test ResearchAgent with Semantic Scholar"""
print("\n" + "=" * 70)
print("TEST 2: Research Agent with Semantic Scholar")
print("=" * 70)
agent = ResearchAgent()
await agent.initialize()
# Test search
print("\nπŸ“ Testing search for 'transformer neural networks'...")
papers = await agent.search("transformer neural networks", max_results=3, source="scholar")
if papers:
print(f"\nβœ… Success! Found {len(papers)} papers:")
for i, paper in enumerate(papers, 1):
print(f"\n{i}. {paper['title']}")
print(f" Year: {paper.get('year', 'N/A')}")
print(f" Citations: {paper.get('citationCount', 0)}")
else:
print("\n❌ No papers found")
await agent.cleanup()
return len(papers) > 0
async def main():
"""Run all tests"""
print("\nπŸ§ͺ Starting Semantic Scholar API Tests\n")
# Check API key
api_key = os.getenv("SEMANTIC_SCHOLAR_API")
if api_key:
print(f"βœ“ API Key found: {api_key[:10]}...")
else:
print("⚠️ No API key found - using public rate-limited access")
print()
# Run tests
test1_passed = await test_scholar_tool()
test2_passed = await test_research_agent()
# Summary
print("\n" + "=" * 70)
print("TEST SUMMARY")
print("=" * 70)
print(f"Test 1 (Scholar Tool): {'βœ… PASSED' if test1_passed else '❌ FAILED'}")
print(f"Test 2 (Research Agent): {'βœ… PASSED' if test2_passed else '❌ FAILED'}")
print("=" * 70)
if test1_passed and test2_passed:
print("\nπŸŽ‰ All tests passed! Semantic Scholar API is working!")
else:
print("\n⚠️ Some tests failed - check the output above")
if __name__ == "__main__":
asyncio.run(main())