Files
2026-02-05 13:32:25 +00:00

144 lines
3.4 KiB
Python

"""Test configuration and fixtures for MCP Server CLI."""
import os
import sys
import tempfile
from pathlib import Path
from typing import Generator
import pytest
sys.path.insert(0, str(Path(__file__).parent.parent))
from mcp_server_cli.config import AppConfig, ConfigManager
from mcp_server_cli.models import (
LocalLLMConfig,
SecurityConfig,
ServerConfig,
)
from mcp_server_cli.server import MCPServer
from mcp_server_cli.tools import (
FileTools,
GitTools,
ShellTools,
ToolRegistry,
)
@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
"""Create a temporary directory for tests."""
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)
@pytest.fixture
def temp_file(temp_dir: Path) -> Generator[Path, None, None]:
"""Create a temporary file for tests."""
file_path = temp_dir / "test_file.txt"
file_path.write_text("Hello, World!")
yield file_path
@pytest.fixture
def temp_yaml_file(temp_dir: Path) -> Generator[Path, None, None]:
"""Create a temporary YAML file for tests."""
file_path = temp_dir / "test_config.yaml"
content = """
server:
host: "127.0.0.1"
port: 8080
log_level: "DEBUG"
llm:
enabled: false
base_url: "http://localhost:11434"
model: "llama2"
security:
allowed_commands:
- ls
- cat
- echo
blocked_paths:
- /etc
- /root
"""
file_path.write_text(content)
yield file_path
@pytest.fixture
def sample_tool_definition() -> dict:
"""Sample tool definition for testing."""
return {
"name": "test_tool",
"description": "A test tool",
"input_schema": {
"type": "object",
"properties": {
"param1": {
"type": "string",
"description": "First parameter",
"required": True
},
"param2": {
"type": "integer",
"description": "Second parameter",
"default": 10
}
},
"required": ["param1"]
}
}
@pytest.fixture
def default_config() -> AppConfig:
"""Create a default server configuration."""
return AppConfig(
server=ServerConfig(host="127.0.0.1", port=3000, log_level="INFO"),
llm=LocalLLMConfig(enabled=False, base_url="http://localhost:11434"),
security=SecurityConfig(
allowed_commands=["ls", "cat", "echo"],
blocked_paths=["/etc", "/root"],
),
)
@pytest.fixture
def mcp_server(default_config: AppConfig) -> MCPServer:
"""Create an MCP server instance with registered tools."""
server = MCPServer(config=default_config)
server.register_tool(FileTools())
server.register_tool(GitTools())
server.register_tool(ShellTools())
return server
@pytest.fixture
def tool_registry() -> ToolRegistry:
"""Create a tool registry instance."""
return ToolRegistry()
@pytest.fixture
def config_manager() -> ConfigManager:
"""Create a configuration manager instance."""
return ConfigManager()
@pytest.fixture
def mock_env():
"""Mock environment variables."""
env = {
"MCP_PORT": "4000",
"MCP_HOST": "0.0.0.0",
"MCP_LOG_LEVEL": "DEBUG",
}
original_env = os.environ.copy()
os.environ.update(env)
yield
os.environ.clear()
os.environ.update(original_env)