199 lines
6.6 KiB
Python
199 lines
6.6 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
测试优化历史与生成页面的集成功能
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import requests
|
|||
|
|
import json
|
|||
|
|
import time
|
|||
|
|
|
|||
|
|
BASE_URL = "http://localhost:5002"
|
|||
|
|
|
|||
|
|
def test_add_optimization_history():
|
|||
|
|
"""测试添加优化历史记录"""
|
|||
|
|
print("🧪 测试添加优化历史记录...")
|
|||
|
|
|
|||
|
|
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'}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print(f" 状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
result = response.json()
|
|||
|
|
if result.get('success'):
|
|||
|
|
print(f" ✅ 成功: {result.get('message')}")
|
|||
|
|
print(f" 📝 记录ID: {result.get('data', {}).get('id')}")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f" ❌ 失败: {result.get('message')}")
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
print(f" ❌ HTTP错误: {response.status_code}")
|
|||
|
|
print(f" 响应内容: {response.text[:200]}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 请求失败: {str(e)}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_get_optimization_history():
|
|||
|
|
"""测试获取优化历史记录"""
|
|||
|
|
print("\n🧪 测试获取优化历史记录...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/optimization-history")
|
|||
|
|
|
|||
|
|
print(f" 状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
result = response.json()
|
|||
|
|
if result.get('success'):
|
|||
|
|
history = result.get('data', {}).get('history', [])
|
|||
|
|
print(f" ✅ 成功: 获取到 {len(history)} 条记录")
|
|||
|
|
|
|||
|
|
# 显示最新的几条记录
|
|||
|
|
for i, record in enumerate(history[:3]):
|
|||
|
|
print(f" 📝 记录 {i+1}: {record.get('original_text', '')[:30]}...")
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f" ❌ 失败: {result.get('message')}")
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
print(f" ❌ HTTP错误: {response.status_code}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 请求失败: {str(e)}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_generate_page_integration():
|
|||
|
|
"""测试生成页面集成"""
|
|||
|
|
print("\n🧪 测试生成页面集成...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 测试生成页面访问
|
|||
|
|
response = requests.get(f"{BASE_URL}/")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print(" ✅ 生成页面访问正常")
|
|||
|
|
|
|||
|
|
# 检查页面是否包含优化历史相关的JavaScript代码
|
|||
|
|
content = response.text
|
|||
|
|
if 'addToOptimizationHistory' in content:
|
|||
|
|
print(" ✅ 包含优化历史集成代码")
|
|||
|
|
else:
|
|||
|
|
print(" ❌ 缺少优化历史集成代码")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
if 'checkPendingHistory' in content:
|
|||
|
|
print(" ✅ 包含待保存历史检查代码")
|
|||
|
|
else:
|
|||
|
|
print(" ❌ 缺少待保存历史检查代码")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f" ❌ 生成页面访问失败: {response.status_code}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 请求失败: {str(e)}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_optimization_history_page():
|
|||
|
|
"""测试优化历史页面"""
|
|||
|
|
print("\n🧪 测试优化历史页面...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
response = requests.get(f"{BASE_URL}/optimization-history")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print(" ✅ 优化历史页面访问正常")
|
|||
|
|
|
|||
|
|
# 检查页面内容
|
|||
|
|
content = response.text
|
|||
|
|
if 'optimization_history.html' in content or '优化历史' in content:
|
|||
|
|
print(" ✅ 页面内容正常")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(" ❌ 页面内容异常")
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
print(f" ❌ 优化历史页面访问失败: {response.status_code}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 请求失败: {str(e)}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主测试函数"""
|
|||
|
|
print("🚀 优化历史集成功能测试")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
# 等待服务器启动
|
|||
|
|
print("⏳ 等待服务器启动...")
|
|||
|
|
time.sleep(3)
|
|||
|
|
|
|||
|
|
tests = [
|
|||
|
|
("生成页面集成", test_generate_page_integration),
|
|||
|
|
("优化历史页面", test_optimization_history_page),
|
|||
|
|
("添加历史记录", test_add_optimization_history),
|
|||
|
|
("获取历史记录", test_get_optimization_history)
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
results = []
|
|||
|
|
|
|||
|
|
for test_name, test_func in tests:
|
|||
|
|
try:
|
|||
|
|
result = test_func()
|
|||
|
|
results.append((test_name, result))
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 测试 {test_name} 异常: {str(e)}")
|
|||
|
|
results.append((test_name, False))
|
|||
|
|
|
|||
|
|
# 输出测试结果
|
|||
|
|
print("\n" + "=" * 50)
|
|||
|
|
print("📊 测试结果汇总")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
passed = 0
|
|||
|
|
total = len(results)
|
|||
|
|
|
|||
|
|
for test_name, result in results:
|
|||
|
|
status = "✅ 通过" if result else "❌ 失败"
|
|||
|
|
print(f"{test_name}: {status}")
|
|||
|
|
if result:
|
|||
|
|
passed += 1
|
|||
|
|
|
|||
|
|
print(f"\n📈 总体结果: {passed}/{total} 测试通过")
|
|||
|
|
|
|||
|
|
if passed == total:
|
|||
|
|
print("🎉 所有测试通过!优化历史集成功能正常!")
|
|||
|
|
print("\n💡 现在您可以:")
|
|||
|
|
print(" 1. 在生成页面生成提示词")
|
|||
|
|
print(" 2. 系统会自动保存到优化历史")
|
|||
|
|
print(" 3. 在优化历史页面查看所有记录")
|
|||
|
|
else:
|
|||
|
|
print("⚠️ 部分测试失败,请检查相关功能")
|
|||
|
|
|
|||
|
|
return passed == total
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
success = main()
|
|||
|
|
exit(0 if success else 1)
|