Refactor the cron job functionality to transition from a crawling to a searching model. Update related documentation and scripts to reflect the new terminology and functionality, including changes in the README and script comments. Enhance WeChat menu and message handling to support the new search features, ensuring clarity in user interactions and logging.
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试企业微信菜单功能
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目路径
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
try:
|
||||
from gx_gp_monitor.wechat.menu_manager import WeChatMenuManager
|
||||
from gx_gp_monitor.wechat.message_handler import WeChatMessageHandler
|
||||
from gx_gp_monitor.core.config_manager import load_config
|
||||
|
||||
def test_menu_manager():
|
||||
"""测试菜单管理器"""
|
||||
print("🧪 测试菜单管理器...")
|
||||
|
||||
try:
|
||||
# 加载配置
|
||||
load_config()
|
||||
|
||||
# 创建菜单管理器
|
||||
menu_manager = WeChatMenuManager()
|
||||
|
||||
# 测试菜单信息获取
|
||||
menu_info = menu_manager.get_menu_info()
|
||||
print("✅ 菜单信息获取成功")
|
||||
print(f" 菜单按钮数量: {menu_info['menu_structure']['total_buttons']}")
|
||||
|
||||
# 打印菜单结构
|
||||
for button in menu_info['menu_structure']['buttons']:
|
||||
sub_count = len(button.get('sub_buttons', [])) if isinstance(button.get('sub_buttons'), list) else button.get('sub_buttons', 0)
|
||||
print(f" • {button['name']} ({sub_count}个子菜单)")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 菜单管理器测试失败: {str(e)}")
|
||||
return False
|
||||
|
||||
def test_message_handler():
|
||||
"""测试消息处理器"""
|
||||
print("🧪 测试消息处理器...")
|
||||
|
||||
try:
|
||||
# 创建消息处理器
|
||||
handler = WeChatMessageHandler()
|
||||
|
||||
# 测试各种菜单功能
|
||||
test_results = {}
|
||||
|
||||
# 测试帮助功能
|
||||
result = handler._handle_help_guide("test_user")
|
||||
test_results["help_guide"] = result is not None and "广西政府采购网" in result
|
||||
|
||||
# 测试联系信息
|
||||
result = handler._handle_contact_info("test_user")
|
||||
test_results["contact_info"] = result is not None and "联系我们" in result
|
||||
|
||||
# 测试关于系统
|
||||
result = handler._handle_about_system("test_user")
|
||||
test_results["about_system"] = result is not None and "关于系统" in result
|
||||
|
||||
# 测试关键词搜索菜单
|
||||
result = handler._handle_keyword_search_menu("test_user")
|
||||
test_results["keyword_search"] = result is not None and "关键词搜索" in result
|
||||
|
||||
# 打印测试结果
|
||||
success_count = sum(1 for success in test_results.values() if success)
|
||||
print(f"✅ 消息处理器测试完成: {success_count}/{len(test_results)} 项通过")
|
||||
|
||||
for func_name, success in test_results.items():
|
||||
status = "✅" if success else "❌"
|
||||
print(f" {status} {func_name}")
|
||||
|
||||
return success_count == len(test_results)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 消息处理器测试失败: {str(e)}")
|
||||
return False
|
||||
|
||||
def test_menu_structure():
|
||||
"""测试菜单结构合理性"""
|
||||
print("🧪 测试菜单结构...")
|
||||
|
||||
try:
|
||||
menu_manager = WeChatMenuManager()
|
||||
menu_data = menu_manager.menu_data
|
||||
|
||||
# 检查菜单结构
|
||||
checks = {
|
||||
"has_buttons": len(menu_data.get("button", [])) > 0,
|
||||
"max_buttons": len(menu_data.get("button", [])) <= 3, # 企业微信最多3个一级菜单
|
||||
"has_sub_buttons": all("sub_button" in btn for btn in menu_data.get("button", [])),
|
||||
"sub_buttons_limit": all(len(btn.get("sub_button", [])) <= 5 for btn in menu_data.get("button", [])),
|
||||
"valid_keys": True
|
||||
}
|
||||
|
||||
# 检查所有按钮都有有效的key
|
||||
all_keys = []
|
||||
for button in menu_data.get("button", []):
|
||||
for sub_btn in button.get("sub_button", []):
|
||||
key = sub_btn.get("key")
|
||||
if key:
|
||||
all_keys.append(key)
|
||||
else:
|
||||
checks["valid_keys"] = False
|
||||
|
||||
# 检查key唯一性
|
||||
checks["unique_keys"] = len(all_keys) == len(set(all_keys))
|
||||
|
||||
# 打印检查结果
|
||||
success_count = sum(1 for success in checks.values() if success)
|
||||
print(f"✅ 菜单结构检查完成: {success_count}/{len(checks)} 项通过")
|
||||
|
||||
for check_name, success in checks.items():
|
||||
status = "✅" if success else "❌"
|
||||
print(f" {status} {check_name}")
|
||||
|
||||
return success_count == len(checks)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 菜单结构测试失败: {str(e)}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""主测试函数"""
|
||||
print("🚀 开始测试企业微信菜单功能...")
|
||||
print("=" * 60)
|
||||
|
||||
# 测试菜单管理器
|
||||
test1_passed = test_menu_manager()
|
||||
print()
|
||||
|
||||
# 测试消息处理器
|
||||
test2_passed = test_message_handler()
|
||||
print()
|
||||
|
||||
# 测试菜单结构
|
||||
test3_passed = test_menu_structure()
|
||||
print()
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
# 总体结果
|
||||
all_passed = test1_passed and test2_passed and test3_passed
|
||||
if all_passed:
|
||||
print("🎉 所有测试通过!企业微信菜单功能正常")
|
||||
print("💡 您可以运行以下命令创建菜单:")
|
||||
print(" python -c \"from gx_gp_monitor.wechat.menu_manager import WeChatMenuManager; m = WeChatMenuManager(); m.create_menu()\"")
|
||||
else:
|
||||
print("⚠️ 部分测试失败,请检查上述错误信息")
|
||||
|
||||
return all_passed
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
except ImportError as e:
|
||||
print(f"❌ 导入失败: {e}", file=sys.stderr)
|
||||
print("请确保已安装所有依赖: pip install -r gx_gp_monitor/requirements.txt", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"❌ 测试失败: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user