test_api_modifications.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. """
  3. 测试 API 修改是否正确实现
  4. 测试新增的 conversation_id 和 user_id 字段
  5. """
  6. import requests
  7. import json
  8. def test_api_modifications():
  9. """测试API修改"""
  10. api_url = "http://localhost:8000/api/chat"
  11. # 测试用例1: 使用 thread_id (原有方式)
  12. test_case_1 = {
  13. "question": "测试使用thread_id参数",
  14. "user_id": "test_user_1",
  15. "thread_id": "test_user_1:20250714120000001"
  16. }
  17. # 测试用例2: 使用 conversation_id (新增方式)
  18. test_case_2 = {
  19. "question": "测试使用conversation_id参数",
  20. "user_id": "test_user_2",
  21. "conversation_id": "test_user_2:20250714120000002"
  22. }
  23. # 测试用例3: 同时提供两个参数 (应该优先使用thread_id)
  24. test_case_3 = {
  25. "question": "测试同时提供两个参数",
  26. "user_id": "test_user_3",
  27. "thread_id": "test_user_3:20250714120000003",
  28. "conversation_id": "test_user_3:20250714120000004" # 这个应该被忽略
  29. }
  30. # 测试用例4: 都不提供 (应该自动生成)
  31. test_case_4 = {
  32. "question": "测试自动生成会话ID",
  33. "user_id": "test_user_4"
  34. }
  35. test_cases = [
  36. ("使用thread_id", test_case_1),
  37. ("使用conversation_id", test_case_2),
  38. ("同时提供两个参数", test_case_3),
  39. ("自动生成", test_case_4)
  40. ]
  41. print("🧪 开始测试 API 修改...")
  42. print("=" * 60)
  43. for test_name, test_data in test_cases:
  44. print(f"\n📋 测试用例: {test_name}")
  45. print(f"📨 请求数据: {json.dumps(test_data, ensure_ascii=False, indent=2)}")
  46. try:
  47. response = requests.post(
  48. api_url,
  49. json=test_data,
  50. headers={"Content-Type": "application/json"},
  51. timeout=30
  52. )
  53. print(f"📊 响应状态码: {response.status_code}")
  54. if response.status_code == 200:
  55. result = response.json()
  56. data = result.get("data", {})
  57. # 检查必需的新字段
  58. conversation_id = data.get("conversation_id")
  59. user_id = data.get("user_id")
  60. thread_id = data.get("react_agent_meta", {}).get("thread_id")
  61. print(f"✅ 响应成功:")
  62. print(f" - conversation_id: {conversation_id}")
  63. print(f" - user_id: {user_id}")
  64. print(f" - thread_id: {thread_id}")
  65. print(f" - conversation_id == thread_id: {conversation_id == thread_id}")
  66. print(f" - user_id 正确: {user_id == test_data['user_id']}")
  67. # 验证逻辑正确性
  68. if test_name == "同时提供两个参数":
  69. expected_thread_id = test_data["thread_id"]
  70. if thread_id == expected_thread_id:
  71. print(f" ✅ 优先使用 thread_id 逻辑正确")
  72. else:
  73. print(f" ❌ 优先使用 thread_id 逻辑错误,期望: {expected_thread_id}, 实际: {thread_id}")
  74. elif test_name == "使用conversation_id":
  75. expected_thread_id = test_data["conversation_id"]
  76. if thread_id == expected_thread_id:
  77. print(f" ✅ conversation_id 转换为 thread_id 逻辑正确")
  78. else:
  79. print(f" ❌ conversation_id 转换逻辑错误,期望: {expected_thread_id}, 实际: {thread_id}")
  80. else:
  81. print(f"❌ 请求失败: {response.text}")
  82. except requests.exceptions.RequestException as e:
  83. print(f"❌ 网络错误: {e}")
  84. except Exception as e:
  85. print(f"❌ 其他错误: {e}")
  86. print("\n" + "=" * 60)
  87. print("🎯 测试完成!")
  88. print("\n💡 预期结果:")
  89. print("1. 所有响应都应该包含 conversation_id 和 user_id 字段")
  90. print("2. conversation_id 应该等于 react_agent_meta.thread_id")
  91. print("3. user_id 应该等于请求中的 user_id")
  92. print("4. 当同时提供 thread_id 和 conversation_id 时,应该优先使用 thread_id")
  93. print("5. 当只提供 conversation_id 时,应该将其作为 thread_id 使用")
  94. if __name__ == "__main__":
  95. test_api_modifications()