ForcePilot/backend/test/unit/channels/test_twitch_helix.py
Kris 69fe97a90d test: 批量修复并新增单元测试用例
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重连相关测试
2026-05-13 16:43:01 +08:00

164 lines
6.5 KiB
Python

from __future__ import annotations
from unittest.mock import AsyncMock, Mock
import aiohttp
import pytest
from yuxi.channels.adapters.twitch.helix import HelixClient
def _make_mock_resp(status: int, data: dict | None = None, body: str = "error"):
mock_resp = AsyncMock()
mock_resp.status = status
if data is not None:
mock_resp.json = AsyncMock(return_value=data)
mock_resp.text = AsyncMock(return_value=body)
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
mock_resp.__aexit__ = AsyncMock(return_value=None)
return mock_resp
def _build_session(request_mock=None):
session = AsyncMock()
session.closed = False
if request_mock is not None:
session.request = request_mock
else:
session.request = Mock(return_value=_make_mock_resp(200))
return session
class TestHelixGet:
@pytest.mark.asyncio
async def test_get_200_returns_data(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(return_value=_make_mock_resp(200, {"data": [{"id": "123"}]}))
)
result = await client._get("/helix/users", login="testuser")
assert result == {"data": [{"id": "123"}]}
@pytest.mark.asyncio
async def test_get_401_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(Mock(return_value=_make_mock_resp(401)))
result = await client._get("/helix/users")
assert result is None
@pytest.mark.asyncio
async def test_get_404_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(Mock(return_value=_make_mock_resp(404)))
result = await client._get("/helix/users")
assert result is None
@pytest.mark.asyncio
async def test_get_500_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(return_value=_make_mock_resp(500, body="Internal Server Error"))
)
result = await client._get("/helix/users")
assert result is None
@pytest.mark.asyncio
async def test_get_connection_error_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(side_effect=aiohttp.ClientError("Connection refused"))
)
result = await client._get("/helix/users")
assert result is None
class TestHelixPost:
@pytest.mark.asyncio
async def test_create_eventsub_subscription_200(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(return_value=_make_mock_resp(200, {"data": [{"id": "sub_123"}]}))
)
result = await client.create_eventsub_subscription({"type": "channel.follow"})
assert result == "sub_123"
@pytest.mark.asyncio
async def test_create_eventsub_409_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(Mock(return_value=_make_mock_resp(409)))
result = await client.create_eventsub_subscription({"type": "channel.follow"})
assert result is None
@pytest.mark.asyncio
async def test_create_eventsub_429_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(Mock(return_value=_make_mock_resp(429)))
result = await client.create_eventsub_subscription({"type": "channel.follow"})
assert result is None
@pytest.mark.asyncio
async def test_create_eventsub_connection_error_returns_none(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(side_effect=aiohttp.ClientError("Connection refused"))
)
result = await client.create_eventsub_subscription({"type": "channel.follow"})
assert result is None
@pytest.mark.asyncio
async def test_delete_eventsub_subscription_204(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(Mock(return_value=_make_mock_resp(204)))
result = await client.delete_eventsub_subscription("sub_123")
assert result is True
@pytest.mark.asyncio
async def test_delete_eventsub_connection_error(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(side_effect=aiohttp.ClientError("Connection refused"))
)
result = await client.delete_eventsub_subscription("sub_123")
assert result is False
class TestHelixToken:
@pytest.mark.asyncio
async def test_get_app_access_token_200(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(return_value=_make_mock_resp(200, {"access_token": "app_token_123"}))
)
result = await client.get_app_access_token("secret")
assert result is not None
@pytest.mark.asyncio
async def test_get_app_access_token_connection_error(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(side_effect=aiohttp.ClientError("Connection refused"))
)
result = await client.get_app_access_token("secret")
assert result is None
@pytest.mark.asyncio
async def test_refresh_user_token_200(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(
return_value=_make_mock_resp(
200, {"access_token": "new_token", "refresh_token": "new_refresh"}
)
)
)
result = await client.refresh_user_token("secret", "refresh_123")
assert result is not None
@pytest.mark.asyncio
async def test_refresh_user_token_connection_error(self):
client = HelixClient(client_id="test_id", access_token="test_token")
client._session = _build_session(
Mock(side_effect=aiohttp.ClientError("Connection refused"))
)
result = await client.refresh_user_token("secret", "refresh_123")
assert result is None