"""Tests for MCP tool handlers."""
import json
import pytest
from src.epic_mcp.data import PatientDataLoader
from src.epic_mcp.tools import (
handle_list_allergies,
handle_get_allergy_details,
handle_list_medications,
handle_get_medication_details,
handle_list_clinical_notes,
handle_get_clinical_note_details,
handle_get_attachment,
)
@pytest.fixture
def loader():
"""Create a PatientDataLoader instance."""
return PatientDataLoader()
class TestListTools:
"""Test list/summary tools return only metadata."""
def test_list_allergies(self, loader):
"""Test list_allergies returns summary only."""
result = handle_list_allergies("patient_001", loader)
data = json.loads(result)
assert isinstance(data, list)
assert len(data) > 0
# Should have summary fields
assert "id" in data[0]
assert "allergen" in data[0]
assert "severity" in data[0]
assert "recorded_date" in data[0]
# Should NOT have reaction field
assert "reaction" not in data[0]
def test_list_medications(self, loader):
"""Test list_medications returns summary only."""
result = handle_list_medications("patient_001", loader)
data = json.loads(result)
assert isinstance(data, list)
assert len(data) > 0
# Should have summary fields
assert "id" in data[0]
assert "name" in data[0]
assert "status" in data[0]
# Should NOT have instructions field
assert "instructions" not in data[0]
def test_list_clinical_notes(self, loader):
"""Test list_clinical_notes returns summary only."""
result = handle_list_clinical_notes("patient_001", loader)
data = json.loads(result)
assert isinstance(data, list)
assert len(data) > 0
# Should have summary fields
assert "id" in data[0]
assert "type" in data[0]
assert "summary" in data[0]
# Should NOT have full content field
assert "content" not in data[0]
class TestDetailTools:
"""Test detail tools return full information."""
def test_get_allergy_details(self, loader):
"""Test get_allergy_details returns full info."""
# First get the list to find an ID
list_result = handle_list_allergies("patient_001", loader)
list_data = json.loads(list_result)
allergy_id = list_data[0]["id"]
# Now get details
result = handle_get_allergy_details("patient_001", allergy_id, loader)
data = json.loads(result)
assert "id" in data
assert "allergen" in data
assert "reaction" in data # Full detail includes reaction
assert "severity" in data
def test_get_medication_details(self, loader):
"""Test get_medication_details returns full info."""
# First get the list to find an ID
list_result = handle_list_medications("patient_001", loader)
list_data = json.loads(list_result)
medication_id = list_data[0]["id"]
# Now get details
result = handle_get_medication_details("patient_001", medication_id, loader)
data = json.loads(result)
assert "id" in data
assert "name" in data
assert "instructions" in data # Full detail includes instructions
def test_get_clinical_note_details(self, loader):
"""Test get_clinical_note_details returns full info."""
# First get the list to find an ID
list_result = handle_list_clinical_notes("patient_001", loader)
list_data = json.loads(list_result)
note_id = list_data[0]["id"]
# Now get details
result = handle_get_clinical_note_details("patient_001", note_id, loader)
data = json.loads(result)
assert "id" in data
assert "content" in data # Full detail includes content
class TestFiltering:
"""Test filtering options."""
def test_medication_status_filtering(self, loader):
"""Test medication status filtering works."""
result = handle_list_medications("patient_001", loader, status="active")
data = json.loads(result)
assert all(m["status"] == "active" for m in data)
def test_notes_type_filtering(self, loader):
"""Test clinical note type filtering works."""
result = handle_list_clinical_notes("patient_001", loader, note_type="Progress Note")
data = json.loads(result)
assert len(data) == 1
assert data[0]["type"] == "Progress Note"
class TestErrorHandling:
"""Test error handling."""
def test_list_nonexistent_patient(self, loader):
"""Test error handling for non-existent patient."""
result = handle_list_allergies("patient_999", loader)
data = json.loads(result)
assert "error" in data
def test_detail_nonexistent_patient(self, loader):
"""Test detail tool error for non-existent patient."""
result = handle_get_allergy_details("patient_999", "allergy-1", loader)
data = json.loads(result)
assert "error" in data
def test_detail_nonexistent_item(self, loader):
"""Test detail tool error for non-existent item."""
result = handle_get_allergy_details("patient_001", "allergy-999", loader)
data = json.loads(result)
assert "error" in data
def test_attachment_not_found(self, loader):
"""Test error when attachment doesn't exist."""
result = handle_get_attachment("patient_001", "att-999", loader)
data = json.loads(result)
assert "error" in data
class TestAttachments:
"""Test attachment retrieval (unchanged)."""
def test_attachment_retrieval(self, loader):
"""Test attachment content retrieval."""
result = handle_get_attachment("patient_001", "att-1", loader)
data = json.loads(result)
assert "content" in data
assert "SPRINGFIELD MEDICAL LABORATORY" in data["content"]