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

108 lines
3.1 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channels.adapters.mattermost.send import (
build_post_options,
build_patch_options,
chunk_text_for_outbound,
)
from yuxi.channels.models import (
ChannelIdentity,
ChannelResponse,
ChannelType,
)
def make_identity(channel_chat_id="ch1", channel_user_id="u1", channel_message_id="m1"):
return ChannelIdentity(
channel_id="mattermost",
channel_type=ChannelType.MATTERMOST,
channel_user_id=channel_user_id,
channel_chat_id=channel_chat_id,
channel_message_id=channel_message_id,
)
class TestBuildPostOptions:
def test_basic_post(self):
identity = make_identity()
response = ChannelResponse(identity=identity, content="hello")
opts = build_post_options(response)
assert opts["channel_id"] == "ch1"
assert opts["message"] == "hello"
assert opts["root_id"] == ""
assert opts["props"] == {}
assert opts["file_ids"] == []
def test_post_with_reply(self):
identity = make_identity()
response = ChannelResponse(identity=identity, content="reply", reply_to_message_id="root1")
opts = build_post_options(response)
assert opts["root_id"] == "root1"
def test_post_with_props(self):
identity = make_identity()
response = ChannelResponse(
identity=identity,
content="poll",
metadata={"props": {"key": "value"}},
)
opts = build_post_options(response)
assert opts["props"] == {"key": "value"}
def test_post_with_files(self):
identity = make_identity()
response = ChannelResponse(
identity=identity,
content="image",
metadata={"file_ids": ["f1", "f2"]},
)
opts = build_post_options(response)
assert opts["file_ids"] == ["f1", "f2"]
class TestBuildPatchOptions:
def test_basic_patch(self):
opts = build_patch_options("hello world")
assert opts == {"message": "hello world"}
def test_empty_patch(self):
opts = build_patch_options("")
assert opts == {"message": ""}
class TestChunkTextForOutbound:
def test_short_text_no_chunk(self):
result = chunk_text_for_outbound("hello", limit=16383)
assert result == ["hello"]
def test_long_text_chunks_by_paragraph(self):
text = "para1\n\npara2\n\npara3"
result = chunk_text_for_outbound(text, limit=8)
assert len(result) >= 2
def test_exact_limit(self):
text = "a" * 100
result = chunk_text_for_outbound(text, limit=200)
assert result == [text]
def test_single_paragraph_over_limit(self):
text = "a" * 200
result = chunk_text_for_outbound(text, limit=50)
assert len(result) >= 4
for chunk in result:
assert len(chunk) <= 50
def test_empty_text(self):
result = chunk_text_for_outbound("", limit=100)
assert result == [""]
def test_chunks_preserve_boundaries(self):
text = "first\n\nsecond\n\nthird"
result = chunk_text_for_outbound(text, limit=8)
assert len(result) >= 2