136 lines
4.5 KiB
Python
136 lines
4.5 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
测试优化历史记录完整流程
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import requests
|
|||
|
|
import json
|
|||
|
|
import time
|
|||
|
|
|
|||
|
|
BASE_URL = "http://localhost:5002"
|
|||
|
|
|
|||
|
|
def test_server_status():
|
|||
|
|
"""测试服务器状态"""
|
|||
|
|
print("🔍 检查服务器状态...")
|
|||
|
|
try:
|
|||
|
|
response = requests.get(f"{BASE_URL}/", timeout=5)
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print("✅ 服务器正常运行")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 服务器响应异常: {response.status_code}")
|
|||
|
|
return False
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 服务器连接失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_optimization_history_api():
|
|||
|
|
"""测试优化历史API接口"""
|
|||
|
|
print("\n🔍 测试优化历史API接口...")
|
|||
|
|
try:
|
|||
|
|
# 测试获取历史记录
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/optimization-history", timeout=10)
|
|||
|
|
print(f"📊 GET /api/optimization-history 状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
data = response.json()
|
|||
|
|
print(f"✅ API响应成功,包含 {len(data.get('data', []))} 条记录")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f"❌ API响应失败: {response.text}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ API测试失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_add_optimization_history():
|
|||
|
|
"""测试添加优化历史记录"""
|
|||
|
|
print("\n🔍 测试添加优化历史记录...")
|
|||
|
|
try:
|
|||
|
|
test_data = {
|
|||
|
|
"original_text": "前端页面设计",
|
|||
|
|
"optimized_text": "请帮我设计一个现代化的前端页面,要求:1. 响应式布局,适配移动端和桌面端 2. 使用现代UI框架(如React/Vue)3. 包含用户交互功能 4. 遵循Material Design或Ant Design设计规范 5. 考虑用户体验和可访问性",
|
|||
|
|
"optimization_type": "提示词优化",
|
|||
|
|
"industry": "互联网",
|
|||
|
|
"profession": "前端开发",
|
|||
|
|
"tags": ["前端开发", "UI设计", "响应式布局"]
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response = requests.post(
|
|||
|
|
f"{BASE_URL}/api/optimization-history",
|
|||
|
|
headers={'Content-Type': 'application/json'},
|
|||
|
|
json=test_data,
|
|||
|
|
timeout=10
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print(f"📊 POST /api/optimization-history 状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
result = response.json()
|
|||
|
|
if result.get('success'):
|
|||
|
|
print("✅ 添加优化历史记录成功")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 添加失败: {result.get('message', '未知错误')}")
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 请求失败: {response.text}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 添加测试失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_optimization_history_page():
|
|||
|
|
"""测试优化历史页面"""
|
|||
|
|
print("\n🔍 测试优化历史页面...")
|
|||
|
|
try:
|
|||
|
|
response = requests.get(f"{BASE_URL}/optimization-history", timeout=10)
|
|||
|
|
print(f"📊 GET /optimization-history 状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print("✅ 优化历史页面访问正常")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 页面访问失败: {response.text[:200]}...")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 页面测试失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主测试函数"""
|
|||
|
|
print("🚀 开始测试优化历史记录功能...")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
# 测试服务器状态
|
|||
|
|
if not test_server_status():
|
|||
|
|
print("\n❌ 服务器未运行,请先启动服务器")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 测试API接口
|
|||
|
|
api_ok = test_optimization_history_api()
|
|||
|
|
|
|||
|
|
# 测试添加记录
|
|||
|
|
add_ok = test_add_optimization_history()
|
|||
|
|
|
|||
|
|
# 测试页面访问
|
|||
|
|
page_ok = test_optimization_history_page()
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 50)
|
|||
|
|
print("📊 测试结果汇总:")
|
|||
|
|
print(f" API接口: {'✅ 正常' if api_ok else '❌ 异常'}")
|
|||
|
|
print(f" 添加记录: {'✅ 正常' if add_ok else '❌ 异常'}")
|
|||
|
|
print(f" 页面访问: {'✅ 正常' if page_ok else '❌ 异常'}")
|
|||
|
|
|
|||
|
|
if api_ok and add_ok and page_ok:
|
|||
|
|
print("\n🎉 优化历史记录功能测试通过!")
|
|||
|
|
else:
|
|||
|
|
print("\n⚠️ 部分功能异常,需要进一步检查")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|