ForcePilot/backend/test/unit/channels/test_mattermost_session.py
Kris 3264900bc9 test: 新增多渠道单元测试用例并配置测试环境变量
新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块
同时在测试配置中添加了测试用的OpenAI API密钥环境变量
2026-05-12 00:56:47 +08:00

70 lines
2.3 KiB
Python

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"