test_shell_features.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. """
  3. 测试 shell.py 新增的对话选择功能
  4. """
  5. import asyncio
  6. import sys
  7. import os
  8. # 确保导入路径正确
  9. CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
  10. sys.path.insert(0, CURRENT_DIR)
  11. from shell import CustomAgentShell
  12. async def test_conversation_selection():
  13. """测试对话选择功能"""
  14. print("🧪 测试对话选择功能...")
  15. try:
  16. # 创建shell实例
  17. shell = await CustomAgentShell.create()
  18. print("✅ Shell创建成功!")
  19. # 设置测试数据
  20. shell.user_id = 'test_user'
  21. shell.recent_conversations = [
  22. {
  23. 'thread_id': 'test_user:20250101120000001',
  24. 'conversation_preview': 'Python编程问题',
  25. 'timestamp': '20250101120000001',
  26. 'formatted_time': '2025-01-01 12:00:00'
  27. },
  28. {
  29. 'thread_id': 'test_user:20250101130000001',
  30. 'conversation_preview': 'SQL查询帮助',
  31. 'timestamp': '20250101130000001',
  32. 'formatted_time': '2025-01-01 13:00:00'
  33. },
  34. ]
  35. print("\n📋 测试对话选择解析:")
  36. # 测试不同的选择类型
  37. test_cases = [
  38. ('1', '数字序号选择'),
  39. ('test_user:20250101120000001', 'Thread ID选择'),
  40. ('2025-01-01', '日期选择'),
  41. ('new', '新对话命令'),
  42. ('What is Python?', '新问题'),
  43. ('999', '无效序号'),
  44. ('wrong_user:20250101120000001', '无效Thread ID'),
  45. ('2025-12-31', '无效日期'),
  46. ]
  47. for user_input, description in test_cases:
  48. result = shell._parse_conversation_selection(user_input)
  49. print(f" 输入: '{user_input}' ({description})")
  50. print(f" 结果: {result['type']}")
  51. if 'message' in result:
  52. print(f" 消息: {result['message']}")
  53. elif 'thread_id' in result:
  54. print(f" Thread ID: {result['thread_id']}")
  55. print()
  56. print("📄 测试对话列表显示:")
  57. shell._display_conversation_list(shell.recent_conversations)
  58. # 测试获取对话功能(这个需要真实的Agent连接)
  59. print("\n🔍 测试获取对话功能:")
  60. print(" (需要Redis和Agent连接,此处跳过)")
  61. await shell.close()
  62. print("✅ 所有测试完成!")
  63. except Exception as e:
  64. print(f"❌ 测试失败: {e}")
  65. import traceback
  66. traceback.print_exc()
  67. if __name__ == "__main__":
  68. asyncio.run(test_conversation_selection())