Files
lda-wf/tests/wf_mcp/service/test_connection_service.py
T
2026-06-03 03:15:08 +07:00

177 lines
6.4 KiB
Python

from __future__ import annotations
from wf_mcp.broker import WfMcpService
from wf_mcp.broker.service.connection_service import ConnectionService
from wf_mcp.broker.service.events import BrokerEventRecorder
from wf_mcp.broker.service.source_catalog import SourceCatalogService
from wf_mcp.events import EventBus
from wf_mcp.models import BrokerConfig, ConnectionConfig
from wf_mcp.runtime import ToolExecutor
from wf_mcp.storage import FileStore
from ..test_support import local_temp_root
def _source_catalog(service: ConnectionService) -> SourceCatalogService:
store = FileStore(local_temp_root() / "connection_service_catalog")
def _tool_executor_for(_connection: ConnectionConfig) -> ToolExecutor:
raise AssertionError("tool executor should not be needed in these tests")
catalog = SourceCatalogService(
store=store,
connection_lookup=service.get,
connection_list_enabled=service.list_enabled,
connection_list_all=service.list_all,
tool_executor_for=_tool_executor_for,
load_auth=lambda _connection_id: None,
emit_event=service.events.record_event,
)
service.bind_source_catalog(catalog)
return catalog
def test_connection_service_rejects_reserved_connection_ids() -> None:
service = ConnectionService(events=BrokerEventRecorder(EventBus()))
_source_catalog(service)
for connection_id in ("wf.admin", "wf.mcp"):
try:
service.register_connection(
ConnectionConfig(id=connection_id, server="wf", account="reserved")
)
except ValueError as exc:
assert connection_id in str(exc)
assert "reserved by wf-mcp" in str(exc)
else:
raise AssertionError(f"expected {connection_id!r} to be rejected")
def test_connection_service_registers_connection_and_empty_source() -> None:
service = ConnectionService(events=BrokerEventRecorder(EventBus()))
catalog = _source_catalog(service)
service.register_connection(
ConnectionConfig(id="demo.personal", server="demo", account="personal")
)
assert service.get("demo.personal").server == "demo"
assert [connection.id for connection in service.list_enabled()] == ["demo.personal"]
source = catalog.capability_sources["demo.personal"]
assert source.enabled is True
assert source.description == "No catalog loaded for demo.personal."
assert service.events.list_events()[0].kind == "connection_registered"
assert service.events.list_events()[0].connection_id == "demo.personal"
def test_connection_service_sync_removes_retired_connections_and_sources() -> None:
service = ConnectionService(events=BrokerEventRecorder(EventBus()))
catalog = _source_catalog(service)
service.register_connection(
ConnectionConfig(id="demo.personal", server="demo", account="personal")
)
service.sync_connections_from_config(
BrokerConfig(store_root=local_temp_root(), connections=[])
)
assert service.list_all() == []
assert "demo.personal" not in catalog.capability_sources
removed = service.events.list_events()[-1]
assert removed.kind == "connection_removed"
assert removed.connection_id == "demo.personal"
assert removed.payload["server"] == "demo"
assert removed.payload["account"] == "personal"
def test_connection_service_sync_updates_existing_source_enabled_flag() -> None:
service = ConnectionService(events=BrokerEventRecorder(EventBus()))
catalog = _source_catalog(service)
service.register_connection(
ConnectionConfig(id="demo.personal", server="demo", account="personal")
)
service.sync_connections_from_config(
BrokerConfig(
store_root=local_temp_root(),
connections=[
ConnectionConfig(
id="demo.personal",
server="demo",
account="personal",
enabled=False,
)
],
)
)
assert service.get("demo.personal").enabled is False
assert catalog.capability_sources["demo.personal"].enabled is False
updated = service.events.list_events()[-1]
assert updated.kind == "connection_updated"
assert updated.connection_id == "demo.personal"
assert updated.payload["enabled"] is False
def test_connection_service_sync_registers_new_connections_with_event() -> None:
service = ConnectionService(events=BrokerEventRecorder(EventBus()))
catalog = _source_catalog(service)
service.sync_connections_from_config(
BrokerConfig(
store_root=local_temp_root(),
connections=[
ConnectionConfig(
id="demo.personal",
server="demo",
account="personal",
)
],
)
)
assert service.get("demo.personal").account == "personal"
assert catalog.capability_sources["demo.personal"].enabled is True
registered = service.events.list_events()[-1]
assert registered.kind == "connection_registered"
assert registered.connection_id == "demo.personal"
def test_wfmcpservice_exposes_connection_registry_from_connection_service() -> None:
service = WfMcpService(store=FileStore(local_temp_root() / "connection_facade"))
service.register_connection(
ConnectionConfig(id="demo.personal", server="demo", account="personal")
)
assert service.connections is service.connection_service.connections
assert service.connections.get("demo.personal").account == "personal"
assert "demo.personal" in service.capability_sources
def test_wfmcpservice_sync_connections_delegates_to_connection_service() -> None:
service = WfMcpService(store=FileStore(local_temp_root() / "connection_sync"))
service.register_connection(
ConnectionConfig(id="demo.personal", server="demo", account="personal")
)
service.sync_connections_from_config(
BrokerConfig(
store_root=local_temp_root(),
connections=[
ConnectionConfig(
id="demo.work",
server="demo",
account="work",
enabled=True,
)
],
)
)
assert [connection.id for connection in service.connections.list_all()] == [
"demo.work"
]
assert "demo.personal" not in service.capability_sources
assert "demo.work" in service.capability_sources