1. 移除Telegram格式化测试中未使用的导入项 2. 修复Teams测试用例,添加monkeypatch参数并配置通配符开关 3. 更新钉钉适配器测试,替换弃用的流属性检查 4. 修正Twitch规范化测试,更新ROOMSTATE测试逻辑 5. 重构会话映射测试,完善数据库执行结果模拟 6. 格式化Slack块构建测试的长参数调用 7. 修复LINE适配器测试,更新能力断言和异步锁使用 8. 修正Slack会话解析测试,修复聊天类型判断错误 9. 更新能力测试,补充缺失的字段检查 10. 修复Matrix适配器测试,修正位置参数和配置校验逻辑 11. 为飞书分析模块测试添加跳过标记 12. 新增微信能力、限流、链接格式、会话路由等模块的单元测试 13. 修复Twitch适配器导入路径和测试断言 14. 新增Discord Webhook、Nextcloud Talk、Signal多账户等模块的单元测试 15. 修复Manager阶段测试的导入路径 16. 新增iMessage异常和命令处理的单元测试 17. 新增Nostr健康检查和相关模块的单元测试 18. 新增Signal守护进程和SSE重连相关测试
357 lines
14 KiB
Python
357 lines
14 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from yuxi.channels.adapters.twitch.actions import (
|
|
_build_response,
|
|
describe_message_tool,
|
|
extract_target_from_args,
|
|
get_action_stats,
|
|
handle_action,
|
|
resolve_execution_mode,
|
|
supports_action,
|
|
)
|
|
from yuxi.channels.models import ChannelType, DeliveryResult
|
|
from yuxi.channels.protocols.actions import ActionContext
|
|
|
|
|
|
def make_adapter_mock(**kwargs):
|
|
adapter = MagicMock()
|
|
adapter.channel_type = ChannelType.TWITCH
|
|
adapter.config = kwargs.get("config", {})
|
|
adapter._helix = kwargs.get("_helix", None)
|
|
adapter._bot_user_id = kwargs.get("_bot_user_id", None)
|
|
adapter._outbound_cache = kwargs.get("_outbound_cache", MagicMock())
|
|
adapter._resolve_broadcaster_id = AsyncMock(return_value=kwargs.get("broadcaster_id", "123"))
|
|
adapter._send_via_helix = AsyncMock(return_value=DeliveryResult(success=True))
|
|
adapter.send = AsyncMock(return_value=DeliveryResult(success=True))
|
|
adapter.send_media = AsyncMock(return_value=DeliveryResult(success=True))
|
|
adapter.format_outbound = MagicMock(return_value={"target": "#test_channel"})
|
|
return adapter
|
|
|
|
|
|
def make_ctx(**kwargs):
|
|
action = kwargs.pop("action", "send")
|
|
chat_id = kwargs.pop("chat_id", "#test_channel")
|
|
msg_id = kwargs.pop("msg_id", "msg_test_123")
|
|
args = dict(kwargs)
|
|
if "chat_id" not in args:
|
|
args["chat_id"] = chat_id
|
|
if "msg_id" not in args:
|
|
args["msg_id"] = msg_id
|
|
return ActionContext(
|
|
action=action,
|
|
channel_id="twitch",
|
|
chat_id=chat_id,
|
|
msg_id=msg_id,
|
|
args=args,
|
|
)
|
|
|
|
|
|
class TestBuildResponse:
|
|
def test_builds_response(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send", content="Hello")
|
|
response = _build_response(ctx, adapter)
|
|
assert response is not None
|
|
assert response.content == "Hello"
|
|
assert response.identity.channel_chat_id == "#test_channel"
|
|
|
|
def test_returns_none_when_no_chat_id(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send", chat_id="", content="Hello")
|
|
response = _build_response(ctx, adapter)
|
|
assert response is None
|
|
|
|
def test_returns_none_when_no_content(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send", chat_id="#test", content="")
|
|
response = _build_response(ctx, adapter)
|
|
assert response is None
|
|
|
|
def test_uses_media_url_for_send_media(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send_media", content_key="media_url", chat_id="#test", media_url="http://img.png")
|
|
response = _build_response(ctx, adapter, "media_url")
|
|
assert response is not None
|
|
assert response.content == "http://img.png"
|
|
|
|
|
|
class TestHandleActionSend:
|
|
@pytest.mark.asyncio
|
|
async def test_send_action(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send", content="Hello world")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_missing_chat_id(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send", chat_id="", content="Hello")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "chat_id" in result.error
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_missing_content(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send", chat_id="#test", content="")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_via_helix_preferred(self):
|
|
adapter = make_adapter_mock(config={"prefer_helix_send": True})
|
|
adapter._send_via_helix = AsyncMock(return_value=DeliveryResult(success=True))
|
|
ctx = make_ctx(action="send", content="Hello")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
adapter._send_via_helix.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_via_helix_falls_back_to_irc(self):
|
|
adapter = make_adapter_mock(config={"prefer_helix_send": True})
|
|
adapter._send_via_helix = AsyncMock(return_value=DeliveryResult(success=False, error="fail"))
|
|
adapter.send = AsyncMock(return_value=DeliveryResult(success=True))
|
|
ctx = make_ctx(action="send", content="Hello")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
|
|
|
|
class TestHandleActionSendMedia:
|
|
@pytest.mark.asyncio
|
|
async def test_send_media_action(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send_media", media_url="http://img.png")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_media_missing_chat_id(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send_media", chat_id="", media_url="http://img.png")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_media_missing_url(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send_media", chat_id="#test", media_url="")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
|
|
class TestHandleActionReply:
|
|
@pytest.mark.asyncio
|
|
async def test_reply_action(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="reply", content="Reply text", reply_to_msg_id="msg123", reply_to_username="user1")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
adapter._send_via_helix.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reply_missing_chat_id(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="reply", chat_id="", content="Reply", reply_to_msg_id="msg123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reply_missing_content(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="reply", chat_id="#test", content="", reply_to_msg_id="msg123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reply_missing_reply_to_msg_id(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="reply", chat_id="#test", content="Reply", reply_to_msg_id="")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reply_falls_back_to_irc_with_prefix(self):
|
|
adapter = make_adapter_mock()
|
|
adapter._send_via_helix = AsyncMock(return_value=DeliveryResult(success=False, error="fail"))
|
|
adapter.send = AsyncMock(return_value=DeliveryResult(success=True))
|
|
ctx = make_ctx(action="reply", content="Reply text", reply_to_msg_id="msg123", reply_to_username="TestUser")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
assert "@TestUser" in adapter.send.call_args[0][0].content
|
|
|
|
|
|
class TestHandleActionUnsend:
|
|
@pytest.mark.asyncio
|
|
async def test_unsend_action(self):
|
|
adapter = make_adapter_mock(_helix=MagicMock(), _bot_user_id="bot123", broadcaster_id="12345")
|
|
adapter._helix.delete_chat_message = AsyncMock(return_value=True)
|
|
ctx = make_ctx(action="unsend", message_id="msg_delete_123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unsend_missing_helix(self):
|
|
adapter = make_adapter_mock(_helix=None)
|
|
ctx = make_ctx(action="unsend", message_id="msg123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_message_action(self):
|
|
adapter = make_adapter_mock(_helix=MagicMock(), _bot_user_id="bot123", broadcaster_id="12345")
|
|
adapter._helix.delete_chat_message = AsyncMock(return_value=True)
|
|
ctx = make_ctx(action="delete_message", message_id="msg123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unsend_no_broadcaster_id(self):
|
|
adapter = make_adapter_mock(_helix=MagicMock(), _bot_user_id="bot123", broadcaster_id=None)
|
|
adapter._resolve_broadcaster_id = AsyncMock(return_value=None)
|
|
adapter._helix.delete_chat_message = AsyncMock(return_value=True)
|
|
ctx = make_ctx(action="unsend", message_id="msg123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "broadcaster" in result.error
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unsend_no_bot_user_id(self):
|
|
adapter = make_adapter_mock(_helix=MagicMock(), _bot_user_id=None, broadcaster_id="12345")
|
|
adapter._helix.delete_chat_message = AsyncMock(return_value=True)
|
|
ctx = make_ctx(action="unsend", message_id="msg123")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "bot_user_id" in result.error
|
|
|
|
|
|
class TestHandleActionAnnouncement:
|
|
@pytest.mark.asyncio
|
|
async def test_announcement_action(self):
|
|
adapter = make_adapter_mock(_helix=MagicMock(), _bot_user_id="bot123", broadcaster_id="12345")
|
|
adapter._helix.send_chat_announcement = AsyncMock(return_value=True)
|
|
ctx = make_ctx(action="announcement", content="Big news!", color="blue")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_announcement_missing_helix(self):
|
|
adapter = make_adapter_mock(_helix=None)
|
|
ctx = make_ctx(action="announcement", content="News")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_announcement_no_broadcaster_id(self):
|
|
adapter = make_adapter_mock(_helix=MagicMock(), _bot_user_id="bot123", broadcaster_id=None)
|
|
adapter._resolve_broadcaster_id = AsyncMock(return_value=None)
|
|
ctx = make_ctx(action="announcement", content="News")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
|
|
|
|
class TestHandleActionUnsupported:
|
|
@pytest.mark.asyncio
|
|
async def test_edit_action_unsupported(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="edit", content="new content")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "editing" in result.error
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reactions_unsupported(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="send_reaction")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "reactions" in result.error
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_polls_unsupported(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="polls")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "polls" in result.error
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unknown_action(self):
|
|
adapter = make_adapter_mock()
|
|
ctx = make_ctx(action="some_random_action")
|
|
result = await handle_action(ctx, adapter)
|
|
assert result.success is False
|
|
assert "unknown action" in result.error
|
|
|
|
|
|
class TestSupportsAction:
|
|
def test_supported_actions(self):
|
|
for action in ("send", "send_media", "reply", "unsend", "delete_message", "announcement"):
|
|
assert supports_action(action) is True
|
|
|
|
def test_unsupported_actions(self):
|
|
for action in ("edit", "reactions", "polls", "pin"):
|
|
assert supports_action(action) is False
|
|
|
|
def test_unknown_action(self):
|
|
assert supports_action("random_action") is False
|
|
|
|
|
|
class TestDescribeMessageTool:
|
|
def test_returns_dict_with_all_actions(self):
|
|
result = describe_message_tool()
|
|
assert isinstance(result, dict)
|
|
assert "send" in result
|
|
assert "send_media" in result
|
|
assert "reply" in result
|
|
assert "unsend" in result
|
|
assert "delete_message" in result
|
|
assert "announcement" in result
|
|
|
|
def test_each_action_has_description(self):
|
|
result = describe_message_tool()
|
|
for action_info in result.values():
|
|
assert "description" in action_info
|
|
|
|
def test_each_action_has_parameters(self):
|
|
result = describe_message_tool()
|
|
for action_info in result.values():
|
|
assert "parameters" in action_info
|
|
|
|
|
|
class TestExtractTargetFromArgs:
|
|
def test_extracts_chat_id(self):
|
|
result = extract_target_from_args({"chat_id": "#test_channel"})
|
|
assert result == {"chat_id": "#test_channel"}
|
|
|
|
def test_returns_none_when_no_chat_id(self):
|
|
result = extract_target_from_args({"other_key": "value"})
|
|
assert result is None
|
|
|
|
def test_returns_none_for_empty_dict(self):
|
|
assert extract_target_from_args({}) is None
|
|
|
|
|
|
class TestResolveExecutionMode:
|
|
def test_always_returns_send(self):
|
|
assert resolve_execution_mode("send") == "send"
|
|
assert resolve_execution_mode("reply") == "send"
|
|
assert resolve_execution_mode("unknown") == "send"
|
|
|
|
|
|
class TestGetActionStats:
|
|
def test_returns_stats_dict(self):
|
|
result = get_action_stats()
|
|
assert "implemented" in result
|
|
assert "planned" in result
|
|
assert "unsupported" in result
|
|
|
|
def test_unimplemented_actions_in_unsupported(self):
|
|
result = get_action_stats()
|
|
assert "edit" in result["unsupported"]
|
|
assert "reactions" in result["unsupported"]
|
|
assert "polls" in result["unsupported"] |