"""测试预览API - 验证是否能正确渲染模板""" import requests API_BASE = "http://localhost:8000/api" # 获取一个模板 response = requests.get(f"{API_BASE}/templates") templates = response.json() if not templates: print("❌ 没有模板数据") exit(1) # 找一个有template_body的模板 template = None for tpl in templates: if tpl.get('template_body'): template = tpl break if not template: print("❌ 没有找到有template_body的模板") exit(1) print(f"✅ 找到模板: {template['machine_model']} / {template['template_type']}") print(f" ID: {template['id'][:12]}...") print(f" 模板长度: {len(template['template_body'])} 字符") print() # 预览模板 preview_payload = { "template_body": template['template_body'] } response = requests.post(f"{API_BASE}/templates/preview", json=preview_payload) result = response.json() print("📋 API返回结果:") print(f" success: {result.get('success')}") print(f" error: {result.get('error', '')}") print() if result.get('success'): print("✨ 渲染结果:") print("=" * 60) print(result.get('result', '')) print("=" * 60) print() context = result.get('context_used', {}) print(f"📝 使用的变量 ({len(context)} 个):") for key, val in list(context.items())[:10]: if isinstance(val, dict): print(f" {key}: {str(val)[:50]}...") else: print(f" {key}: {val}") if len(context) > 10: print(f" ... 还有 {len(context) - 10} 个变量") else: print("❌ 渲染失败") print(result.get('error', ''))