Bridge Linux D-Bus IPC into MCP with tools for service discovery (list_services, introspect, list_objects), method calls and property access, plus convenience shortcuts for notifications, systemd units, and MPRIS media player control. All 25 tests passing.
74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
"""Tests for BusManager and serialization utilities."""
|
|
|
|
import pytest
|
|
|
|
from mcdbus._bus import BusManager, deserialize_args, serialize_variant
|
|
|
|
|
|
class TestBusManager:
|
|
async def test_connect_session(self, bus_manager: BusManager):
|
|
bus = await bus_manager.get_bus("session")
|
|
assert bus.connected
|
|
|
|
async def test_connect_system(self, bus_manager: BusManager):
|
|
bus = await bus_manager.get_bus("system")
|
|
assert bus.connected
|
|
|
|
async def test_cached_connection(self, bus_manager: BusManager):
|
|
bus1 = await bus_manager.get_bus("session")
|
|
bus2 = await bus_manager.get_bus("session")
|
|
assert bus1 is bus2
|
|
|
|
async def test_invalid_bus_type(self, bus_manager: BusManager):
|
|
with pytest.raises(ValueError, match="must be 'session' or 'system'"):
|
|
await bus_manager.get_bus("invalid")
|
|
|
|
async def test_disconnect_all(self, bus_manager: BusManager):
|
|
bus = await bus_manager.get_bus("session")
|
|
assert bus.connected
|
|
await bus_manager.disconnect_all()
|
|
assert not bus.connected
|
|
|
|
|
|
class TestSerializeVariant:
|
|
def test_plain_values(self):
|
|
assert serialize_variant("hello") == "hello"
|
|
assert serialize_variant(42) == 42
|
|
assert serialize_variant(True) is True
|
|
|
|
def test_list(self):
|
|
assert serialize_variant([1, 2, 3]) == [1, 2, 3]
|
|
|
|
def test_dict(self):
|
|
assert serialize_variant({"a": 1}) == {"a": 1}
|
|
|
|
def test_nested(self):
|
|
result = serialize_variant({"a": [1, {"b": "c"}]})
|
|
assert result == {"a": [1, {"b": "c"}]}
|
|
|
|
def test_bytes_utf8(self):
|
|
assert serialize_variant(b"hello") == "hello"
|
|
|
|
def test_bytes_binary(self):
|
|
result = serialize_variant(b"\xff\xfe")
|
|
assert result == [255, 254]
|
|
|
|
|
|
class TestDeserializeArgs:
|
|
def test_empty(self):
|
|
assert deserialize_args("", "") == []
|
|
assert deserialize_args("[]", "") == []
|
|
assert deserialize_args("null", "") == []
|
|
|
|
def test_simple_args(self):
|
|
result = deserialize_args('["hello", 42]', "su")
|
|
assert result == ["hello", 42]
|
|
|
|
def test_no_signature(self):
|
|
result = deserialize_args('["hello"]', "")
|
|
assert result == ["hello"]
|
|
|
|
def test_single_value_wraps_in_list(self):
|
|
result = deserialize_args('"hello"', "s")
|
|
assert result == ["hello"]
|