78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from yuxi.channels.models import (
|
||
|
|
ChannelIdentity,
|
||
|
|
ChannelMessage,
|
||
|
|
ChannelType,
|
||
|
|
ChatType,
|
||
|
|
EventType,
|
||
|
|
MessageType,
|
||
|
|
SessionScope,
|
||
|
|
)
|
||
|
|
from yuxi.channels.protocols.session_router import BaseSessionRouter, SessionRouter
|
||
|
|
|
||
|
|
|
||
|
|
def _make_message(chat_type: ChatType = ChatType.DIRECT, chat_id: str = "chat_1") -> ChannelMessage:
|
||
|
|
return ChannelMessage(
|
||
|
|
identity=ChannelIdentity(
|
||
|
|
channel_id="test",
|
||
|
|
channel_type=ChannelType.WEBCHAT,
|
||
|
|
channel_user_id="u1",
|
||
|
|
channel_chat_id=chat_id,
|
||
|
|
),
|
||
|
|
content="hello",
|
||
|
|
chat_type=chat_type,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class TestBaseSessionRouter:
|
||
|
|
def test_resolve_chat_id(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
msg = _make_message(chat_id="chat_123")
|
||
|
|
assert router.resolve_chat_id(msg) == "chat_123"
|
||
|
|
|
||
|
|
def test_resolve_thread_key(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
msg = _make_message(chat_type=ChatType.DIRECT, chat_id="dm_123")
|
||
|
|
key = router.resolve_thread_key(msg)
|
||
|
|
assert "test:default" in key
|
||
|
|
assert "dm_123" in key
|
||
|
|
|
||
|
|
def test_resolve_session_scope_direct(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
msg = _make_message(chat_type=ChatType.DIRECT)
|
||
|
|
assert router.resolve_session_scope(msg) == SessionScope.DIRECT
|
||
|
|
|
||
|
|
def test_resolve_session_scope_group(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
msg = _make_message(chat_type=ChatType.GROUP)
|
||
|
|
assert router.resolve_session_scope(msg) == SessionScope.GROUP
|
||
|
|
|
||
|
|
def test_resolve_session_scope_thread(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
msg = _make_message(chat_type=ChatType.THREAD)
|
||
|
|
assert router.resolve_session_scope(msg) == SessionScope.THREAD
|
||
|
|
|
||
|
|
def test_normalize_target_without_prefix(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
assert router.normalize_target("target_1") == "test:target_1"
|
||
|
|
|
||
|
|
def test_normalize_target_with_prefix(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
assert router.normalize_target("test:target_1") == "test:target_1"
|
||
|
|
|
||
|
|
def test_resolve_agent_route(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test")
|
||
|
|
msg = _make_message(chat_type=ChatType.DIRECT, chat_id="dm_123")
|
||
|
|
route = router.resolve_agent_route(msg)
|
||
|
|
assert route.startswith("agent:main:")
|
||
|
|
assert "dm_123" in route
|
||
|
|
|
||
|
|
def test_build_key(self):
|
||
|
|
router = BaseSessionRouter(channel_type="test", account_id="acc1")
|
||
|
|
key = router._build_key("chat", "direct", "id_123")
|
||
|
|
assert key == "test:acc1:chat:direct:id_123"
|