Files
GX-gp-notify/test_menu_fix.py
T

116 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
测试修复后的菜单功能
"""
import sys
import os
# 添加项目路径
sys.path.insert(0, os.path.dirname(__file__))
try:
from gx_gp_monitor.wechat.message_handler import WeChatMessageHandler
from gx_gp_monitor.core.config_manager import load_config
def test_menu_keys():
"""测试菜单key处理"""
print("🧪 测试菜单key处理...")
handler = WeChatMessageHandler()
# 测试旧菜单key
test_cases = [
("latest_announcements", "最新公告(兼容旧key"),
("announcements_by_type", "按类型查看公告"),
("search_announcements", "搜索公告(兼容旧key"),
("latest_news", "最新公告"),
("keyword_search", "关键词搜索"),
("system_status", "系统状态"),
("help_guide", "使用说明"),
]
results = {}
for key, description in test_cases:
try:
# 模拟事件处理
result = handler.handle_event('click', key, 'test_user')
results[key] = result is not None and "未知菜单项" not in (result or "")
print(f"✅ {description}: {'通过' if results[key] else '失败'}")
except Exception as e:
print(f"❌ {description}: 异常 - {str(e)}")
results[key] = False
success_count = sum(1 for success in results.values() if success)
print(f"\n📊 菜单key测试结果: {success_count}/{len(test_cases)} 通过")
return success_count == len(test_cases)
def test_text_commands():
"""测试文本命令处理"""
print("\n🧪 测试文本命令处理...")
handler = WeChatMessageHandler()
# 测试公告类型查询
test_cases = [
("采购公告", "采购公告查询"),
("结果公告", "结果公告查询"),
("更正公告", "更正公告查询"),
]
results = {}
for command, description in test_cases:
try:
result = handler.handle_text_message(command, 'test_user')
results[command] = result is not None
print(f"✅ {description}: {'通过' if results[command] else '失败'}")
except Exception as e:
print(f"❌ {description}: 异常 - {str(e)}")
results[command] = False
success_count = sum(1 for success in results.values() if success)
print(f"\n📊 文本命令测试结果: {success_count}/{len(test_cases)} 通过")
return success_count == len(test_cases)
def main():
"""主测试函数"""
# 加载配置
try:
load_config()
print("✅ 配置加载成功")
except Exception as e:
print(f"❌ 配置加载失败: {e}")
return False
print("🚀 测试修复后的菜单功能...")
print("=" * 50)
# 测试菜单key
test1_passed = test_menu_keys()
# 测试文本命令
test2_passed = test_text_commands()
print("\n" + "=" * 50)
if test1_passed and test2_passed:
print("🎉 所有菜单功能修复测试通过!")
print("✅ 旧菜单key现在可以正常工作")
print("✅ 新增了公告类型查询功能")
else:
print("⚠️ 部分测试失败,请检查上述错误信息")
return test1_passed and test2_passed
if __name__ == "__main__":
main()
except ImportError as e:
print(f"❌ 导入失败: {e}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"❌ 测试失败: {e}", file=sys.stderr)
sys.exit(1)