test_preview_render.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """测试预览API - 验证是否能正确渲染模板"""
  2. import requests
  3. API_BASE = "http://localhost:8000/api"
  4. # 获取一个模板
  5. response = requests.get(f"{API_BASE}/templates")
  6. templates = response.json()
  7. if not templates:
  8. print("❌ 没有模板数据")
  9. exit(1)
  10. # 找一个有template_body的模板
  11. template = None
  12. for tpl in templates:
  13. if tpl.get('template_body'):
  14. template = tpl
  15. break
  16. if not template:
  17. print("❌ 没有找到有template_body的模板")
  18. exit(1)
  19. print(f"✅ 找到模板: {template['machine_model']} / {template['template_type']}")
  20. print(f" ID: {template['id'][:12]}...")
  21. print(f" 模板长度: {len(template['template_body'])} 字符")
  22. print()
  23. # 预览模板
  24. preview_payload = {
  25. "template_body": template['template_body']
  26. }
  27. response = requests.post(f"{API_BASE}/templates/preview", json=preview_payload)
  28. result = response.json()
  29. print("📋 API返回结果:")
  30. print(f" success: {result.get('success')}")
  31. print(f" error: {result.get('error', '')}")
  32. print()
  33. if result.get('success'):
  34. print("✨ 渲染结果:")
  35. print("=" * 60)
  36. print(result.get('result', ''))
  37. print("=" * 60)
  38. print()
  39. context = result.get('context_used', {})
  40. print(f"📝 使用的变量 ({len(context)} 个):")
  41. for key, val in list(context.items())[:10]:
  42. if isinstance(val, dict):
  43. print(f" {key}: {str(val)[:50]}...")
  44. else:
  45. print(f" {key}: {val}")
  46. if len(context) > 10:
  47. print(f" ... 还有 {len(context) - 10} 个变量")
  48. else:
  49. print("❌ 渲染失败")
  50. print(result.get('error', ''))