|
30 | 30 | @pytest.fixture(scope="module") |
31 | 31 | def code_mode_enabled(ha_container_with_fresh_config): |
32 | 32 | """Enable code mode feature flag for the test module.""" |
| 33 | + old_val = os.environ.get(FEATURE_FLAG, "") |
33 | 34 | os.environ[FEATURE_FLAG] = "true" |
| 35 | + # Reset cached settings so the new server reads the fresh env var |
| 36 | + import ha_mcp.config |
| 37 | + ha_mcp.config._settings = None |
34 | 38 | logger.info("Code mode feature flag enabled") |
35 | 39 | yield |
36 | | - os.environ.pop(FEATURE_FLAG, None) |
| 40 | + os.environ["ENABLE_CODE_MODE"] = old_val |
| 41 | + ha_mcp.config._settings = None |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope="module") |
| 45 | +async def _code_mode_server(code_mode_enabled, ha_container_with_fresh_config): |
| 46 | + """Create a single MCP server with code mode enabled for the module.""" |
| 47 | + from ha_mcp.client.rest_client import HomeAssistantClient |
| 48 | + from ha_mcp.server import HomeAssistantSmartMCPServer |
| 49 | + from tests.test_constants import TEST_TOKEN |
| 50 | + |
| 51 | + container_info = ha_container_with_fresh_config |
| 52 | + base_url = container_info["base_url"] |
| 53 | + client = HomeAssistantClient(base_url=base_url, token=TEST_TOKEN) |
| 54 | + server = HomeAssistantSmartMCPServer(client=client) |
| 55 | + yield server |
37 | 56 |
|
38 | 57 |
|
39 | 58 | @pytest.fixture |
40 | | -async def mcp_client_with_code_mode(code_mode_enabled, mcp_server): |
41 | | - """Create MCP client with code mode enabled.""" |
| 59 | +async def mcp_client_with_code_mode(_code_mode_server): |
| 60 | + """Create MCP client connected to the code-mode-enabled server.""" |
42 | 61 | from fastmcp import Client |
43 | 62 |
|
44 | | - client = Client(mcp_server.mcp) |
45 | | - async with client: |
| 63 | + mcp_client = Client(_code_mode_server.mcp) |
| 64 | + async with mcp_client: |
46 | 65 | logger.debug("FastMCP client with code mode connected") |
47 | | - yield client |
| 66 | + yield mcp_client |
48 | 67 |
|
49 | 68 |
|
50 | 69 | async def _check_tool_available(mcp_client) -> tuple[bool, str | None]: |
|
0 commit comments