185 lines
5.8 KiB
Python
185 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试最终修复结果
|
||
"""
|
||
|
||
import requests
|
||
import time
|
||
|
||
BASE_URL = "http://localhost:5002"
|
||
|
||
def test_javascript_file():
|
||
"""测试JavaScript文件访问"""
|
||
print("🧪 测试JavaScript文件访问...")
|
||
|
||
try:
|
||
response = requests.get(f"{BASE_URL}/static/js/optimization_history_db.js")
|
||
|
||
if response.status_code == 200:
|
||
content = response.text
|
||
if 'class OptimizationHistoryDB' in content:
|
||
print(" ✅ JavaScript文件访问正常,包含OptimizationHistoryDB类")
|
||
return True
|
||
else:
|
||
print(" ❌ JavaScript文件缺少OptimizationHistoryDB类")
|
||
return False
|
||
else:
|
||
print(f" ❌ JavaScript文件访问失败: {response.status_code}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f" ❌ JavaScript文件访问异常: {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:
|
||
content = response.text
|
||
|
||
# 检查是否包含JavaScript文件引用
|
||
if 'optimization_history_db.js' in content:
|
||
print(" ✅ 页面包含JavaScript文件引用")
|
||
else:
|
||
print(" ❌ 页面缺少JavaScript文件引用")
|
||
return False
|
||
|
||
# 检查是否包含初始化代码
|
||
if 'new OptimizationHistoryDB()' 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_api_endpoints():
|
||
"""测试API接口"""
|
||
print("\n🧪 测试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} 条记录")
|
||
return True
|
||
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
|
||
|
||
def test_add_new_record():
|
||
"""测试添加新记录"""
|
||
print("\n🧪 测试添加新记录...")
|
||
|
||
test_data = {
|
||
"original_text": "测试最终修复",
|
||
"optimized_text": "这是测试最终修复的优化结果",
|
||
"optimization_type": "最终测试",
|
||
"industry": "IT",
|
||
"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}")
|
||
return True
|
||
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
|
||
|
||
def main():
|
||
"""主测试函数"""
|
||
print("🚀 最终修复验证测试")
|
||
print("=" * 50)
|
||
|
||
# 等待服务器启动
|
||
print("⏳ 等待服务器启动...")
|
||
time.sleep(3)
|
||
|
||
tests = [
|
||
("JavaScript文件访问", test_javascript_file),
|
||
("优化历史页面", test_optimization_history_page),
|
||
("API接口", test_api_endpoints),
|
||
("添加新记录", test_add_new_record)
|
||
]
|
||
|
||
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. 使用搜索和筛选功能")
|
||
print(" 4. 查看统计信息和历史记录")
|
||
else:
|
||
print("⚠️ 部分测试失败,请检查相关功能")
|
||
|
||
return passed == total
|
||
|
||
if __name__ == '__main__':
|
||
success = main()
|
||
exit(0 if success else 1)
|