test_full_preview.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # 完整测试预览功能
  2. import requests
  3. import json
  4. API_BASE = "http://localhost:8000/api"
  5. print("=" * 80)
  6. print("测试1: Common模板 - PIN_CONSECUTIVE分支")
  7. print("=" * 80)
  8. # 获取common模板
  9. resp = requests.get(f"{API_BASE}/templates/x11_wash_common")
  10. common_tpl = resp.json()
  11. body = common_tpl['template_body']
  12. # 添加输出语句
  13. body_with_output = body + "\n\n{{ prefix_message }}"
  14. # 测试PIN_CONSECUTIVE分支
  15. resp = requests.post(f"{API_BASE}/templates/preview", json={
  16. "template_body": body_with_output,
  17. "parameters": {
  18. "rerank_reason": "PIN_CONSECUTIVE",
  19. "program_id": "1",
  20. "period": "早上",
  21. "changed_params": {
  22. "setTemp": 40,
  23. "speed": 12,
  24. "spinTime": 3,
  25. "washTime": 45
  26. }
  27. }
  28. })
  29. result = resp.json()
  30. print(f"Success: {result['success']}")
  31. print(f"Result: {result['result'][:200]}")
  32. print(f"Error: {result['error']}")
  33. print(f"Condition vars count: {len(result['condition_vars'])}")
  34. print(f"Data vars count: {len(result['data_vars'])}")
  35. print("\n" + "=" * 80)
  36. print("测试2: Common模板 - POPULAR分支")
  37. print("=" * 80)
  38. resp = requests.post(f"{API_BASE}/templates/preview", json={
  39. "template_body": body_with_output,
  40. "parameters": {
  41. "rerank_reason": "",
  42. "recommend_type": "POPULAR",
  43. "program_id": "15",
  44. "city": "青岛市",
  45. "period": "早上"
  46. }
  47. })
  48. result = resp.json()
  49. print(f"Success: {result['success']}")
  50. print(f"Result: {result['result'][:200]}")
  51. print(f"Error: {result['error']}")
  52. print("\n" + "=" * 80)
  53. print("测试3: 重新生成应该随机不同(连续5次)")
  54. print("=" * 80)
  55. results = []
  56. for i in range(5):
  57. resp = requests.post(f"{API_BASE}/templates/preview", json={
  58. "template_body": body_with_output,
  59. "parameters": {
  60. "rerank_reason": "PIN_CONSECUTIVE",
  61. "program_id": "1",
  62. "period": "早上"
  63. }
  64. })
  65. result = resp.json()
  66. results.append(result['result'])
  67. print(f"第{i+1}次: {result['result'][:100]}")
  68. # 检查是否有不同
  69. unique_results = set(results)
  70. print(f"\n5次生成中有 {len(unique_results)} 种不同结果")
  71. if len(unique_results) > 1:
  72. print("✅ 随机功能正常!")
  73. else:
  74. print("❌ 随机功能可能有问题")
  75. print("\n" + "=" * 80)
  76. print("测试4: 天气模板预览")
  77. print("=" * 80)
  78. # 获取天气模板
  79. resp = requests.get(f"{API_BASE}/templates/x11_wash_weather")
  80. weather_tpl = resp.json()
  81. weather_body = weather_tpl['template_body']
  82. print(f"Weather template length: {len(weather_body)}")
  83. if weather_body:
  84. resp = requests.post(f"{API_BASE}/templates/preview", json={
  85. "template_body": weather_body,
  86. "parameters": {
  87. "weather": "小雪",
  88. "city": "青岛市",
  89. "period": "早上"
  90. }
  91. })
  92. result = resp.json()
  93. print(f"Success: {result['success']}")
  94. print(f"Result: {result['result'][:200]}")
  95. print(f"Error: {result['error']}")
  96. else:
  97. print("天气模板为空")
  98. print("\n" + "=" * 80)
  99. print("测试完成")
  100. print("=" * 80)