surfaces/tests/adapter/max/test_dispatcher_max.py

98 lines
2.9 KiB
Python

from __future__ import annotations
import pytest
from adapter.max.handlers.chat import ChatHandler as MaxChatHandler
from adapter.max.handlers.commands import register_max_handlers
from adapter.max.store import ChatStore, RoomMeta
from core.auth import AuthManager
from core.chat import ChatManager
from core.handler import EventDispatcher
from core.handlers import register_all
from core.protocol import IncomingCommand, OutgoingMessage
from core.settings import SettingsManager
from core.store import InMemoryStore
from sdk.mock import MockPlatformClient
from sdk.prototype_state import PrototypeStateStore
def _build_dispatcher() -> tuple[
EventDispatcher,
ChatManager,
AuthManager,
ChatStore,
str,
]:
store_mem = InMemoryStore()
chat_store = ChatStore()
chat_handler = MaxChatHandler(chat_store)
prototype_state = PrototypeStateStore()
platform = MockPlatformClient()
chat_mgr = ChatManager(platform, store_mem)
auth_mgr = AuthManager(platform, store_mem)
settings_mgr = SettingsManager(platform, store_mem)
dispatcher = EventDispatcher(
platform=platform,
chat_mgr=chat_mgr,
auth_mgr=auth_mgr,
settings_mgr=settings_mgr,
)
register_all(dispatcher)
register_max_handlers(
dispatcher,
chat_store=chat_store,
max_chat_handler=chat_handler,
prototype_state=prototype_state,
)
pid = "550e8400-e29b-41d4-a716-446655440000"
chat_store.add_room(
RoomMeta(
platform_chat_id=pid,
max_chat_id="777",
name="Чат",
user_id="u1",
agent_id="agent-0",
workspace_path="/agents/0",
)
)
return dispatcher, chat_mgr, auth_mgr, chat_store, pid
@pytest.mark.asyncio
async def test_dispatcher_new_is_single_dialog_hint():
dispatcher, _chat_mgr, auth_mgr, _chat_store, pid = _build_dispatcher()
await auth_mgr.confirm("u1")
out = await dispatcher.dispatch(
IncomingCommand(user_id="u1", platform="max", chat_id=pid, command="new"),
)
assert len(out) == 1
assert isinstance(out[0], OutgoingMessage)
assert "один диалог" in out[0].text.lower()
@pytest.mark.asyncio
async def test_dispatcher_clear_rotates_platform_chat():
dispatcher, chat_mgr, auth_mgr, chat_store, pid = _build_dispatcher()
await auth_mgr.confirm("u1")
await chat_mgr.get_or_create(
user_id="u1",
chat_id=pid,
platform="max",
surface_ref="777",
name="Чат",
)
out = await dispatcher.dispatch(
IncomingCommand(user_id="u1", platform="max", chat_id=pid, command="clear"),
)
assert len(out) == 1
msg = out[0]
assert isinstance(msg, OutgoingMessage)
assert msg.chat_id != pid
assert "сброшен" in msg.text.lower()
room = chat_store.get_room_by_max_chat_id("777")
assert room is not None
assert room.platform_chat_id == msg.chat_id