"""Tests for MCP server basic functionality."""
import pytest
from unittest.mock import Mock, AsyncMock
import time
from src.server.main import NewsCollectorServer
class TestNewsCollectorServer:
"""Test cases for NewsCollectorServer."""
@pytest.fixture
def server(self):
"""Create a NewsCollectorServer instance for testing."""
return NewsCollectorServer()
def test_server_initialization(self, server):
"""Test that the server initializes correctly."""
assert isinstance(server, NewsCollectorServer)
assert hasattr(server, 'app')
assert server.app.name == "news-collector"
def test_server_has_required_tools(self, server):
"""Test that server has all required tools registered."""
expected_tools = [
"ping",
"get_recent_news",
"analyze_news_sentiment",
"monitor_news_stream",
"get_news_summary",
"detect_market_rumors",
"analyze_news_impact",
"get_news_analytics"
]
# Get registered tools (this will be implemented)
registered_tools = server.get_registered_tools()
for tool_name in expected_tools:
assert tool_name in registered_tools
@pytest.mark.asyncio
async def test_ping_tool(self, server):
"""Test the ping tool functionality."""
result = await server.ping()
assert "pong" in str(result)
assert "timestamp" in str(result)
def test_get_recent_news_tool_signature(self, server):
"""Test get_recent_news tool has correct signature."""
tool_info = server.get_tool_info("get_recent_news")
assert tool_info is not None
assert tool_info["name"] == "get_recent_news"
assert "description" in tool_info
# Check parameters
params = tool_info["parameters"]
assert "keyword" in params
assert "source" in params
assert "limit" in params
assert "hours" in params
@pytest.mark.asyncio
async def test_server_health_check(self, server):
"""Test server health check functionality."""
health_status = await server.health_check()
assert health_status["status"] == "healthy"
assert "timestamp" in health_status
assert "version" in health_status
assert "uptime" in health_status
@pytest.mark.asyncio
async def test_server_startup_and_shutdown(self, server):
"""Test server startup and shutdown procedures."""
# Test startup
await server.startup()
assert server.is_running is True
# Test shutdown
await server.shutdown()
assert server.is_running is False
def test_server_logging_configuration(self, server):
"""Test that logging is properly configured."""
assert hasattr(server, 'logger')
assert server.logger.name == "news-collector"
@pytest.mark.asyncio
async def test_error_handling(self, server):
"""Test error handling in server operations."""
# Test with invalid tool name
with pytest.raises(ValueError):
await server.call_tool("invalid_tool_name", {})
def test_server_configuration(self, server):
"""Test server configuration loading."""
config = server.get_config()
assert "database_url" in config
assert "log_level" in config
assert "max_requests_per_minute" in config