test_preview_v2.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """测试修复后的预览API"""
  2. import requests
  3. import json
  4. API_BASE = "http://localhost:8000/api"
  5. # 获取x11 wash common模板
  6. response = requests.get(f"{API_BASE}/templates")
  7. templates = response.json()
  8. tpl = None
  9. for t in templates:
  10. if t['machine_model'] == 'x11' and t['template_type'] == 'common' and t['device_type'] == 'wash':
  11. tpl = t
  12. break
  13. if not tpl:
  14. print("❌ 未找到模板")
  15. exit(1)
  16. print(f"✅ 模板: {tpl['machine_model']}/{tpl['template_type']}")
  17. print(f" Body长度: {len(tpl['template_body'])}")
  18. print()
  19. # 测试1: 默认参数预览
  20. print("=" * 60)
  21. print("📝 测试1: 默认参数(rerank_reason=PIN_CONSECUTIVE)")
  22. print("=" * 60)
  23. resp = requests.post(f"{API_BASE}/templates/preview", json={
  24. "template_body": tpl['template_body'],
  25. "parameters": {"rerank_reason": "PIN_CONSECUTIVE", "program_id": "1"}
  26. })
  27. result = resp.json()
  28. print(f"Success: {result['success']}")
  29. print(f"Condition vars: {[v['name'] for v in result.get('condition_vars', [])]}")
  30. print(f"Data vars: {[v['name'] for v in result.get('data_vars', [])]}")
  31. print(f"Result:\n{result['result'][:300]}...")
  32. print()
  33. # 测试2: POPULAR分支
  34. print("=" * 60)
  35. print("📝 测试2: POPULAR分支")
  36. print("=" * 60)
  37. resp = requests.post(f"{API_BASE}/templates/preview", json={
  38. "template_body": tpl['template_body'],
  39. "parameters": {"rerank_reason": "", "recommend_type": "POPULAR", "program_id": "15"}
  40. })
  41. result = resp.json()
  42. print(f"Success: {result['success']}")
  43. if result['success']:
  44. print(f"Result:\n{result['result'][:300]}...")
  45. else:
  46. print(f"Error: {result['error']}")
  47. print()
  48. # 测试3: 重新生成(应该随机不同)
  49. print("=" * 60)
  50. print("📝 测试3: 两次重新生成(检查随机性)")
  51. print("=" * 60)
  52. resp1 = requests.post(f"{API_BASE}/templates/preview", json={
  53. "template_body": tpl['template_body'],
  54. "parameters": {"rerank_reason": "PIN_CONSECUTIVE", "program_id": "1"}
  55. })
  56. resp2 = requests.post(f"{API_BASE}/templates/preview", json={
  57. "template_body": tpl['template_body'],
  58. "parameters": {"rerank_reason": "PIN_CONSECUTIVE", "program_id": "1"}
  59. })
  60. r1 = resp1.json()
  61. r2 = resp2.json()
  62. print(f"第一次: {r1['result'][:100]}...")
  63. print(f"第二次: {r2['result'][:100]}...")
  64. print(f"是否不同: {r1['result'] != r2['result']}")