PolyHedra / test_tools_interactive.py
yf176
Deploy Polyhedra to HF Spaces
891fe52
Raw History Blame Contribute Delete
6.55 kB
"""Interactive Test for Polyhedra MCP Tools"""
import asyncio
from pathlib import Path
from polyhedra.services.semantic_scholar import SemanticScholarService
from polyhedra.services.citation_manager import CitationManager
from polyhedra.services.context_manager import ContextManager
from polyhedra.services.rag_service import RAGService
from polyhedra.services.project_initializer import ProjectInitializer
async def test_search_papers():
"""Test 1: Search for papers"""
print("\n" + "="*80)
print("πŸ§ͺ TEST 1: Search Papers")
print("="*80)
service = SemanticScholarService()
query = "attention mechanism transformers"
print(f"πŸ” Searching for: '{query}'")
papers = await service.search(query=query, limit=3)
print(f"βœ… 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):,}")
if paper.get('paperId'):
print(f" ID: {paper['paperId']}")
return papers[0] if papers else None
async def test_get_paper(paper_id: str):
"""Test 2: Get detailed paper information"""
print("\n" + "="*80)
print("πŸ§ͺ TEST 2: Get Paper Details")
print("="*80)
service = SemanticScholarService()
print(f"πŸ” Fetching details for paper ID: {paper_id}")
paper = await service.get_paper(paper_id)
print(f"βœ… Paper retrieved:")
print(f" Title: {paper['title']}")
print(f" Year: {paper.get('year', 'N/A')}")
print(f" Authors: {', '.join(paper.get('authors', [])[:3])}")
print(f" Citations: {paper.get('citationCount', 0):,}")
if paper.get('abstract'):
abstract = paper['abstract'][:200] + "..." if len(paper.get('abstract', '')) > 200 else paper.get('abstract', '')
print(f" Abstract: {abstract}")
return paper
async def test_citation_manager(paper: dict):
"""Test 3: Add citation to bibliography"""
print("\n" + "="*80)
print("πŸ§ͺ TEST 3: Citation Management")
print("="*80)
project_root = Path.cwd()
citation_manager = CitationManager(project_root)
# Create a sample BibTeX entry
bibtex_entry = f"""@article{{{paper.get('paperId', 'test2024')},
title = {{{paper['title']}}},
author = {{{' and '.join(paper.get('authors', ['Unknown'])[:3])}}},
year = {{{paper.get('year', '2024')}}},
journal = {{ArXiv}},
note = {{Citations: {paper.get('citationCount', 0)}}}
}}"""
print("πŸ“ Adding citation to references.bib:")
print(bibtex_entry[:150] + "...")
citation_manager.add_citation(bibtex_entry)
print("βœ… Citation added successfully")
# List citations
print("\nπŸ“š Current citations in bibliography:")
citations = citation_manager.list_citations()
print(f" Total citations: {len(citations)}")
for key in list(citations.keys())[:3]:
print(f" - {key}")
async def test_context_manager():
"""Test 4: File context management"""
print("\n" + "="*80)
print("πŸ§ͺ TEST 4: Context Management")
print("="*80)
project_root = Path.cwd()
context_manager = ContextManager(project_root)
# Get project status
print("πŸ“Š Getting project status...")
status = context_manager.get_project_status()
print(f"βœ… Project root: {status.get('root', 'N/A')}")
print(f" Files found: {len(status.get('files', []))}")
# Read README
print("\nπŸ“– Reading README.md...")
readme_path = project_root / "README.md"
if readme_path.exists():
content = context_manager.get_context(str(readme_path))
print(f"βœ… README loaded ({len(content)} characters)")
print(f" Preview: {content[:100]}...")
else:
print("⚠️ README.md not found")
async def test_rag_service():
"""Test 5: RAG Service (semantic search)"""
print("\n" + "="*80)
print("πŸ§ͺ TEST 5: RAG Service")
print("="*80)
project_root = Path.cwd()
rag_service = RAGService(project_root)
print("πŸ” Testing semantic search capabilities...")
print("βœ… RAG service initialized")
print(" Note: RAG indexing requires papers to be saved in literature/")
async def test_project_initializer():
"""Test 6: Project Initialization"""
print("\n" + "="*80)
print("πŸ§ͺ TEST 6: Project Structure")
print("="*80)
project_root = Path.cwd()
initializer = ProjectInitializer(project_root)
print("πŸ“ Checking project structure...")
# Check if standard directories exist
dirs = ["literature", "ideas", "method", "paper"]
existing = [d for d in dirs if (project_root / d).exists()]
print(f"βœ… Standard directories found: {len(existing)}/{len(dirs)}")
for dir_name in existing:
print(f" - {dir_name}/")
async def main():
"""Run all tests"""
print("\n" + "="*80)
print("πŸš€ Polyhedra MCP Tools - Interactive Test Suite")
print("="*80)
try:
# Test 1: Search papers
paper = await test_search_papers()
if paper and paper.get('paperId'):
# Test 2: Get paper details
detailed_paper = await test_get_paper(paper['paperId'])
# Test 3: Citation management
await test_citation_manager(detailed_paper)
# Test 4: Context management
await test_context_manager()
# Test 5: RAG service
await test_rag_service()
# Test 6: Project structure
await test_project_initializer()
# Summary
print("\n" + "="*80)
print("βœ… All Tests Completed Successfully!")
print("="*80)
print("\nπŸ“š Polyhedra tools are working correctly:")
print(" βœ“ Paper search and retrieval")
print(" βœ“ Citation management")
print(" βœ“ Context/file management")
print(" βœ“ RAG/semantic search")
print(" βœ“ Project structure")
print("\nπŸ’‘ Try these commands in your AI assistant:")
print(" - 'Search for papers about neural architecture search'")
print(" - 'Add this paper to my bibliography'")
print(" - 'Find similar papers to my research topic'")
except Exception as e:
print(f"\n❌ Test failed with error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())