Option C Tools
Loading...
Searching...
No Matches
conftest.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_mcp/conftest.py
4
5"""
6Purpose
7-------
8Shared fixtures for the oct-mcp test suite.
9
10Responsibilities
11----------------
12- Provide ``tmp_project_root`` with minimal project skeleton.
13- Provide pre-configured instances of ``McpConfig``, ``McpPolicy``,
14 ``SafetyGate``, ``Redactor``, ``McpMetrics``, ``McpAuditLogger``,
15 ``SandboxExecutor``, ``ToolExecutor``, and ``ServerState``.
16
17Diagnostics
18-----------
19Domain: MCP-TESTS
20L2: fixture lifecycle
21L3: fixture details
22L4: deep tracing
23
24Contracts
25---------
26- All project roots use ``tmp_path``; no real project files are touched.
27"""
28
29from __future__ import annotations
30
31import json
32from pathlib import Path
33from unittest.mock import MagicMock, patch
34
35import pytest
36
37from oct.mcp.audit import McpAuditLogger
38from oct.mcp.config import McpConfig
39from oct.mcp.executor import ToolExecutor
40from oct.mcp.metrics import McpMetrics
41from oct.mcp.policy import McpPolicy
42from oct.mcp.redactor import Redactor
43from oct.mcp.safety import SafetyGate
44from oct.mcp.sandbox import SandboxExecutor, SandboxResult
45from oct.mcp.server import RateLimiter, ServerState
46
47
48@pytest.fixture
49def tmp_project_root(tmp_path: Path) -> Path:
50 """A minimal Option C project root in a temp directory."""
51 (tmp_path / "pyproject.toml").write_text(
52 '[project]\nname = "test"\nversion = "0.1.0"\n'
53 )
54 (tmp_path / ".octrc.json").write_text('{"profile": "default"}')
55 return tmp_path
56
57
58@pytest.fixture
59def default_config() -> McpConfig:
60 """A default McpConfig with safe defaults."""
61 return McpConfig()
62
63
64@pytest.fixture
65def mock_sandbox() -> MagicMock:
66 """A mock SandboxExecutor that returns a success result."""
67 sandbox = MagicMock(spec=SandboxExecutor)
68 sandbox.run.return_value = SandboxResult(
69 exit_code=0,
70 stdout='{"tool": "lint", "violations": [], "exit_code": 0}',
71 stderr="",
72 )
73 return sandbox
74
75
76@pytest.fixture
77def mock_executor(mock_sandbox: MagicMock) -> ToolExecutor:
78 """A ToolExecutor backed by the mock sandbox."""
79 return ToolExecutor(sandbox=mock_sandbox)
80
81
82@pytest.fixture
83def default_redactor() -> Redactor:
84 """A Redactor with no custom patterns."""
85 return Redactor()
86
87
88@pytest.fixture
89def mcp_audit_logger(tmp_path: Path) -> McpAuditLogger:
90 """An McpAuditLogger writing to a temp directory."""
91 return McpAuditLogger(audit_log_path=tmp_path / "audit.log")
92
93
94@pytest.fixture
95def default_policy() -> McpPolicy:
96 """A McpPolicy in 'default' profile with safe_mode=False."""
97 return McpPolicy(profile="default", safe_mode=False)
98
99
100@pytest.fixture
101def isolated_metrics() -> McpMetrics:
102 """An :class:`McpMetrics` backed by a private ``CollectorRegistry``.
103
104 Prevents ``Duplicated timeseries in CollectorRegistry`` collisions
105 when multiple tests instantiate ``McpMetrics`` in the same process
106 and ``prometheus_client`` is installed. When ``prometheus_client`` is
107 absent, returns the default no-op stub (the ``registry`` argument is
108 inert in that case).
109 """
110 from oct.mcp.metrics import PROMETHEUS_AVAILABLE
111 if not PROMETHEUS_AVAILABLE:
112 return McpMetrics()
113 from prometheus_client import CollectorRegistry
114 return McpMetrics(registry=CollectorRegistry())
115
116
117@pytest.fixture
119 tmp_project_root: Path,
120 default_config: McpConfig,
121 default_policy: McpPolicy,
122 mock_executor: ToolExecutor,
123 default_redactor: Redactor,
124 mcp_audit_logger: McpAuditLogger,
125 isolated_metrics: McpMetrics,
126) -> ServerState:
127 """A fully wired ServerState for integration tests."""
128 return ServerState(
129 project_root=tmp_project_root,
130 config=default_config,
131 policy=default_policy,
132 executor=mock_executor,
133 redactor=default_redactor,
134 audit_logger=mcp_audit_logger,
135 rate_limiter=RateLimiter(limit_per_minute=30),
136 safety=SafetyGate(default_config),
137 metrics=isolated_metrics,
138 session_id="test-session-id",
139 )
Redactor default_redactor()
Definition conftest.py:83
ServerState server_state(Path tmp_project_root, McpConfig default_config, McpPolicy default_policy, ToolExecutor mock_executor, Redactor default_redactor, McpAuditLogger mcp_audit_logger, McpMetrics isolated_metrics)
Definition conftest.py:126
McpConfig default_config()
Definition conftest.py:59
ToolExecutor mock_executor(MagicMock mock_sandbox)
Definition conftest.py:77
McpMetrics isolated_metrics()
Definition conftest.py:101
McpAuditLogger mcp_audit_logger(Path tmp_path)
Definition conftest.py:89
Path tmp_project_root(Path tmp_path)
Definition conftest.py:49
MagicMock mock_sandbox()
Definition conftest.py:65
McpPolicy default_policy()
Definition conftest.py:95