ForcePilot/backend/test/unit/channels/test_mattermost_session.py

70 lines
2.3 KiB
Python
Raw Normal View History

from __future__ import annotations
import pytest
from yuxi.channels.adapters.mattermost.session import (
build_thread_id,
resolve_chat_id,
resolve_chat_type,
)
from yuxi.channels.models import ChatType
class TestResolveChatId:
def test_dm_chat(self):
post = {"channel_id": "ch1", "user_id": "u1"}
channel_data = {"type": "D"}
assert resolve_chat_id(post, channel_data) == "dm_u1"
def test_channel_chat(self):
post = {"channel_id": "ch_abc"}
channel_data = {"type": "O"}
assert resolve_chat_id(post, channel_data) == "channel_ch_abc"
def test_thread_chat(self):
post = {"channel_id": "ch1", "root_id": "root99"}
channel_data = {}
assert resolve_chat_id(post, channel_data) == "channel_ch1:thread_root99"
def test_channel_with_root_takes_priority(self):
post = {"channel_id": "ch1", "user_id": "u1", "root_id": "r1"}
channel_data = {"type": "D"}
assert resolve_chat_id(post, channel_data) == "channel_ch1:thread_r1"
class TestResolveChatType:
def test_direct(self):
post = {}
channel_data = {"type": "D"}
assert resolve_chat_type(post, channel_data) == ChatType.DIRECT
def test_group(self):
post = {}
channel_data = {"type": "O"}
assert resolve_chat_type(post, channel_data) == ChatType.GROUP
def test_thread(self):
post = {"root_id": "r1"}
channel_data = {"type": "O"}
assert resolve_chat_type(post, channel_data) == ChatType.THREAD
def test_default_is_group(self):
assert resolve_chat_type({}, {}) == ChatType.GROUP
class TestBuildThreadId:
def test_dm_thread(self):
result = build_thread_id(ChatType.DIRECT, user_id="u1")
assert result == "agent:main:mattermost:dm:u1"
def test_channel_thread(self):
result = build_thread_id(ChatType.GROUP, channel_id="ch1")
assert result == "agent:main:mattermost:channel:ch1"
def test_thread_chat(self):
result = build_thread_id(ChatType.THREAD, channel_id="ch1")
assert result == "agent:main:mattermost:channel:ch1"
def test_custom_agent_id(self):
result = build_thread_id(ChatType.DIRECT, user_id="u1", agent_id="r2")
assert result == "agent:r2:mattermost:dm:u1"