新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块 同时在测试配置中添加了测试用的OpenAI API密钥环境变量
101 lines
3.2 KiB
Python
101 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
class TestStickerCache:
|
|
def test_set_and_get(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_cache import StickerCache
|
|
|
|
cache = StickerCache(ttl=3600, max_entries=100)
|
|
cache.set("stk_001", {"name": "Test Sticker", "category": "fun"})
|
|
|
|
result = cache.get("stk_001")
|
|
assert result is not None
|
|
assert result["name"] == "Test Sticker"
|
|
|
|
def test_miss(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_cache import StickerCache
|
|
|
|
cache = StickerCache(ttl=3600, max_entries=100)
|
|
assert cache.get("nonexistent") is None
|
|
|
|
def test_lru_eviction(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_cache import StickerCache
|
|
|
|
cache = StickerCache(ttl=3600, max_entries=3)
|
|
cache.set("a", {"name": "A"})
|
|
cache.set("b", {"name": "B"})
|
|
cache.set("c", {"name": "C"})
|
|
cache.set("d", {"name": "D"})
|
|
|
|
assert cache.get("a") is None
|
|
assert cache.get("b") is not None
|
|
|
|
def test_clear(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_cache import StickerCache
|
|
|
|
cache = StickerCache(ttl=3600, max_entries=100)
|
|
cache.set("a", {"name": "A"})
|
|
cache.clear()
|
|
assert cache.size() == 0
|
|
|
|
|
|
class TestStickerHandler:
|
|
def test_extract_metadata(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_handler import extract_sticker_metadata
|
|
|
|
meta = extract_sticker_metadata({
|
|
"sticker_id": "stk_001",
|
|
"sticker_name": "Cool Sticker",
|
|
"sticker_category": "fun",
|
|
})
|
|
|
|
assert meta["sticker_id"] == "stk_001"
|
|
assert meta["sticker_name"] == "Cool Sticker"
|
|
|
|
def test_build_description(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_handler import build_sticker_description
|
|
|
|
desc = build_sticker_description({
|
|
"sticker_name": "Cool Sticker",
|
|
"sticker_category": "fun",
|
|
})
|
|
assert "Cool Sticker" in desc
|
|
assert "fun" in desc
|
|
|
|
def test_build_description_no_category(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_handler import build_sticker_description
|
|
|
|
desc = build_sticker_description({
|
|
"sticker_name": "UnknownSticker",
|
|
})
|
|
assert "UnknownSticker" in desc
|
|
|
|
|
|
class TestStickerMessageAugmentation:
|
|
def test_no_sticker_attachments(self):
|
|
from yuxi.channels.adapters.zalo_user.sticker_handler import augment_sticker_message
|
|
from yuxi.channels.models import (
|
|
ChannelIdentity,
|
|
ChannelMessage,
|
|
ChannelType,
|
|
ChatType,
|
|
EventType,
|
|
MessageType,
|
|
)
|
|
|
|
identity = ChannelIdentity(
|
|
channel_id="zalo_user",
|
|
channel_type=ChannelType.ZALO_USER,
|
|
channel_user_id="user_1",
|
|
channel_chat_id="chat_1",
|
|
channel_message_id="msg_1",
|
|
)
|
|
msg = ChannelMessage(
|
|
identity=identity,
|
|
event_type=EventType.MESSAGE_RECEIVED,
|
|
message_type=MessageType.TEXT,
|
|
chat_type=ChatType.DIRECT,
|
|
content="Hello",
|
|
)
|
|
|
|
result = augment_sticker_message(msg)
|
|
assert result.content == "Hello" |