"""Pytest configuration and fixtures for Standards MCP Server tests."""
import pytest
from pathlib import Path
import tempfile
import shutil
from typing import Callable, Any
def get_tool_fn(tool: Any) -> Callable:
"""Extract the underlying function from a FastMCP FunctionTool.
FastMCP 2.x wraps decorated functions in FunctionTool objects.
This helper extracts the original function for testing.
Args:
tool: A FunctionTool instance or callable
Returns:
The underlying callable function
"""
if hasattr(tool, "fn"):
return tool.fn
return tool
@pytest.fixture
def temp_standards_dir():
"""Create a temporary standards directory with sample files."""
temp_dir = tempfile.mkdtemp()
standards_path = Path(temp_dir) / "standards"
standards_path.mkdir()
# Create agents category
agents_dir = standards_path / "agents"
agents_dir.mkdir()
(agents_dir / "agent-design.md").write_text("""# Agent Design Standards
Standards for designing AI agents.
## Overview
Agents follow a 5-layer architecture.
## Architecture
- Layer 1: Instructions
- Layer 2: Knowledge
- Layer 3: Tools
- Layer 4: Skills
- Layer 5: Memory
""")
(agents_dir / "agent-security.md").write_text("""# Agent Security Standards
Security requirements for agents.
## Authentication
Agents must authenticate with Azure Workload Identity.
## Authorization
Use role-based access control (RBAC).
""")
# Create mcp category
mcp_dir = standards_path / "mcp"
mcp_dir.mkdir()
(mcp_dir / "mcp-server-design.md").write_text("""# MCP Server Design Standards
Standards for MCP servers.
## Transport
Support both stdio and SSE transport.
""")
(mcp_dir / "README.md").write_text("""# MCP Standards
Standards for MCP implementation.
""")
yield standards_path
# Cleanup
shutil.rmtree(temp_dir)
@pytest.fixture
def mock_config(temp_standards_dir, monkeypatch):
"""Configure the server to use the temporary standards directory."""
monkeypatch.setenv("STANDARDS_PATH", str(temp_standards_dir))
from standards_mcp_server.config import ServerConfig
return ServerConfig(standards_path=temp_standards_dir)
@pytest.fixture
def mock_context():
"""Create a mock MCP context for testing."""
class MockContext:
async def info(self, message: str):
pass
async def warning(self, message: str):
pass
async def error(self, message: str):
pass
async def debug(self, message: str):
pass
def get_caller_identity(self):
return MockCallerIdentity()
class MockCallerIdentity:
subject = "test-user"
roles = ["developer"]
return MockContext()