ForcePilot/backend/test/unit/channels/test_channels_imessage_commands.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

95 lines
3.2 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channels.adapters.imessage.commands import (
get_command_help,
is_authorized_for_commands,
parse_command,
)
class TestParseCommand:
def test_valid_command_without_args(self):
result = parse_command("/status")
assert result == {"action": "status", "args": []}
def test_valid_command_with_args(self):
result = parse_command("/poll create Question Option1 Option2")
assert result["action"] == "poll"
assert result["args"] == ["create", "Question", "Option1", "Option2"]
def test_command_extra_spaces(self):
result = parse_command("/help ")
assert result == {"action": "help", "args": []}
def test_non_command_returns_none(self):
assert parse_command("Hello world") is None
def test_empty_string_returns_none(self):
assert parse_command("") is None
def test_slash_only_returns_none(self):
assert parse_command("/") is None
def test_slash_with_spaces_returns_none(self):
assert parse_command("/ ") is None
def test_command_uppercase_to_lowercase(self):
result = parse_command("/STATUS")
assert result["action"] == "status"
def test_command_mixed_case(self):
result = parse_command("/HeLp")
assert result["action"] == "help"
def test_command_with_quoted_args(self):
result = parse_command('/poll create "Question text" "Option A"')
assert result["action"] == "poll"
assert len(result["args"]) == 5
class TestIsAuthorizedForCommands:
def test_wildcard_allows_all(self):
assert is_authorized_for_commands("+8613800138000", ["*"]) is True
def test_exact_match(self):
assert is_authorized_for_commands("+8613800138000", ["+8613800138000"]) is True
def test_different_format_match(self):
assert is_authorized_for_commands("+8613800138000", ["8613800138000"]) is True
def test_handle_with_spaces(self):
assert is_authorized_for_commands(" +8613800138000 ", ["8613800138000"]) is True
def test_no_match(self):
assert is_authorized_for_commands("+8613800138000", ["+8613900139000"]) is False
def test_empty_allow_list(self):
assert is_authorized_for_commands("+8613800138000", []) is False
def test_email_handle(self):
assert is_authorized_for_commands("test@example.com", ["test@example.com"]) is True
def test_wildcard_in_list(self):
assert is_authorized_for_commands("anyone", ["user1", "*", "user2"]) is True
class TestGetCommandHelp:
def test_basic_help(self):
help_text = get_command_help()
assert "Available commands" in help_text
assert "/status" in help_text
assert "/help" in help_text
def test_include_owner_commands(self):
help_text = get_command_help(include_owner=True)
assert "/approve" in help_text
assert "/reject" in help_text
assert "/allowlist" in help_text
def test_exclude_owner_commands(self):
help_text = get_command_help(include_owner=False)
assert "/approve" not in help_text
assert "/reject" not in help_text
assert "/allowlist" not in help_text