Q58Bot/tests/test_guard.py
wood d295c817b7 添加缺失的测试命令到build.yml
在build.yml文件中,移除了注释并添加了测试命令以运行单元测试。此更改确保CI/CD流程中包含测试执行。
2024-09-04 19:07:43 +08:00

33 lines
1.2 KiB
Python

import unittest
from unittest.mock import AsyncMock, patch
from guard import process_message, command_handler
class TestGuard(unittest.TestCase):
@patch('guard.link_filter')
async def test_process_message(self, mock_link_filter):
mock_link_filter.should_filter.return_value = (True, [])
event = AsyncMock()
event.is_private = False
event.sender_id = 12345 # 非管理员ID
await process_message(event, AsyncMock())
event.delete.assert_called_once()
event.respond.assert_called_once_with("已撤回该消息。注:包含关键词或重复发送的非白名单链接会被自动撤回。")
@patch('guard.handle_command')
@patch('guard.link_filter')
async def test_command_handler(self, mock_link_filter, mock_handle_command):
event = AsyncMock()
event.is_private = True
event.sender_id = int(os.environ.get('ADMIN_ID'))
event.raw_text = '/add keyword'
await command_handler(event, mock_link_filter)
mock_handle_command.assert_called_once()
mock_link_filter.load_data_from_file.assert_called_once()
if __name__ == '__main__':
unittest.main()