Fix get_mgr() to use ctx.lifespan_context (works in both request and standalone Client mode). Add autouse fixture to reset event-loop-bound singleton state per test, preventing cross-test asyncio.Event contamination. 21 new tests exercise all 14 tools through FastMCP's in-memory transport. New shortcuts: network_status (NetworkManager), battery_status (UPower), bluetooth_devices (bluez), kwin_windows (KRunner WindowsRunner). 53 tests pass.
46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
"""Test fixtures for mcdbus."""
|
|
|
|
import asyncio
|
|
|
|
import pytest
|
|
from fastmcp import Client
|
|
|
|
from mcdbus._bus import BusManager
|
|
from mcdbus.server import mcp
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset_mcp_singleton():
|
|
"""Reset event-loop-bound state on the mcp singleton.
|
|
|
|
FastMCP creates an asyncio.Event() at construction time. pytest-asyncio
|
|
creates a fresh loop per test, so after test 1 the Event is bound to a
|
|
dead loop and test 2 would crash. Re-creating these objects keeps every
|
|
test isolated.
|
|
"""
|
|
mcp._started = asyncio.Event()
|
|
mcp._lifespan_result = None
|
|
mcp._lifespan_result_set = False
|
|
yield
|
|
|
|
|
|
@pytest.fixture
|
|
async def client():
|
|
"""MCP client connected via in-memory transport (no HTTP)."""
|
|
async with Client(mcp) as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture
|
|
async def bus_manager():
|
|
"""Provide a BusManager instance, cleaned up after test."""
|
|
mgr = BusManager()
|
|
yield mgr
|
|
await mgr.disconnect_all()
|
|
|
|
|
|
@pytest.fixture
|
|
async def session_bus(bus_manager: BusManager):
|
|
"""Provide a connected session bus."""
|
|
return await bus_manager.get_bus("session")
|