"""Tests for Gemini-powered search tool."""
import json
import os
import pytest
from dotenv import load_dotenv
from src.epic_mcp.data import PatientDataLoader
from src.epic_mcp.tools import handle_search
load_dotenv()
@pytest.fixture
def loader():
"""Create a PatientDataLoader instance."""
return PatientDataLoader()
class TestSearchTool:
"""Test Gemini-powered search functionality."""
def test_search_error_handling(self, loader):
"""Test search error handling for non-existent patient."""
result = handle_search("patient_999", "test query", loader)
data = json.loads(result)
assert "error" in data
@pytest.mark.skipif(not os.getenv("GEMINI_API_KEY"), reason="GEMINI_API_KEY not set")
def test_search_natural_language(self, loader):
"""Test natural language search returns relevant results."""
result = handle_search(
"patient_001",
"What medications is the patient taking for diabetes?",
loader
)
data = json.loads(result)
assert "response" in data
assert "Metformin" in data["response"]
@pytest.mark.skipif(not os.getenv("GEMINI_API_KEY"), reason="GEMINI_API_KEY not set")
def test_search_across_data_types(self, loader):
"""Test search can synthesize info from multiple data types."""
result = handle_search(
"patient_001",
"Summarize the patient's most recent lab results and vital signs",
loader
)
data = json.loads(result)
response_lower = data["response"].lower()
# Should mention data from both labs and vitals
assert any(term in response_lower for term in ["glucose", "blood pressure", "cholesterol"])