#!/usr/bin/env python3 """ 测试完整的优化历史工作流程 """ import requests import time import json BASE_URL = "http://localhost:5002" def test_complete_workflow(): """测试完整的工作流程""" print("🚀 完整工作流程测试") print("=" * 50) # 等待服务器启动 print("⏳ 等待服务器启动...") time.sleep(3) # 步骤1: 测试生成页面 print("\n📝 步骤1: 测试生成页面") try: response = requests.get(f"{BASE_URL}/") if response.status_code == 200: print(" ✅ 生成页面访问正常") else: print(f" ❌ 生成页面访问失败: {response.status_code}") return False except Exception as e: print(f" ❌ 生成页面访问异常: {str(e)}") return False # 步骤2: 测试优化历史页面 print("\n📚 步骤2: 测试优化历史页面") try: response = requests.get(f"{BASE_URL}/optimization-history") if response.status_code == 200: print(" ✅ 优化历史页面访问正常") else: print(f" ❌ 优化历史页面访问失败: {response.status_code}") return False except Exception as e: print(f" ❌ 优化历史页面访问异常: {str(e)}") return False # 步骤3: 测试API接口 print("\n🔌 步骤3: 测试API接口") try: response = requests.get(f"{BASE_URL}/api/optimization-history") if response.status_code == 200: result = response.json() if result.get('success'): history_count = len(result.get('data', {}).get('history', [])) print(f" ✅ API接口正常,当前有 {history_count} 条记录") else: print(f" ❌ API返回失败: {result.get('message')}") return False else: print(f" ❌ API接口失败: {response.status_code}") return False except Exception as e: print(f" ❌ API接口异常: {str(e)}") return False # 步骤4: 模拟生成提示词并保存 print("\n🎯 步骤4: 模拟生成提示词并保存") test_data = { "original_text": "请帮我写一个产品介绍", "optimized_text": "以下是专业的产品介绍模板:\n\n【产品概述】\n产品名称:AI智能助手\n核心功能:智能对话、任务管理、数据分析\n目标用户:企业用户、个人用户\n\n【产品特色】\n1. 智能对话:自然语言处理,理解用户意图\n2. 任务管理:自动规划,提高工作效率\n3. 数据分析:深度洞察,支持决策制定", "optimization_type": "产品文案", "industry": "科技", "profession": "产品经理", "tags": ["自动生成", "产品文案"] } try: response = requests.post( f"{BASE_URL}/api/optimization-history", json=test_data, headers={'Content-Type': 'application/json'} ) if response.status_code == 200: result = response.json() if result.get('success'): new_id = result.get('data', {}).get('id') print(f" ✅ 提示词保存成功,ID: {new_id}") else: print(f" ❌ 提示词保存失败: {result.get('message')}") return False else: print(f" ❌ 提示词保存请求失败: {response.status_code}") return False except Exception as e: print(f" ❌ 提示词保存异常: {str(e)}") return False # 步骤5: 验证新记录是否出现在历史中 print("\n🔍 步骤5: 验证新记录是否出现在历史中") try: response = requests.get(f"{BASE_URL}/api/optimization-history") if response.status_code == 200: result = response.json() if result.get('success'): history = result.get('data', {}).get('history', []) # 查找刚才添加的记录 found = False for record in history: if record.get('original_text') == test_data['original_text']: found = True print(f" ✅ 新记录已出现在历史中,ID: {record.get('id')}") break if not found: print(" ❌ 新记录未出现在历史中") return False else: print(f" ❌ 获取历史记录失败: {result.get('message')}") return False else: print(f" ❌ 获取历史记录请求失败: {response.status_code}") return False except Exception as e: print(f" ❌ 获取历史记录异常: {str(e)}") return False print("\n🎉 完整工作流程测试通过!") return True def main(): """主函数""" success = test_complete_workflow() if success: print("\n" + "=" * 50) print("🎊 测试结果: 全部通过!") print("=" * 50) print("✅ 生成页面正常") print("✅ 优化历史页面正常") print("✅ API接口正常") print("✅ 数据保存正常") print("✅ 数据查询正常") print("\n💡 现在您可以:") print(" 1. 在生成页面生成提示词") print(" 2. 系统会自动保存到优化历史") print(" 3. 在优化历史页面查看所有记录") print(" 4. 使用搜索和筛选功能") else: print("\n" + "=" * 50) print("❌ 测试结果: 部分失败") print("=" * 50) print("请检查相关功能") return success if __name__ == '__main__': success = main() exit(0 if success else 1)