#!/usr/bin/env python3 """ 测试前端集成功能 """ import requests import time BASE_URL = "http://localhost:5002" def test_optimization_history_page(): """测试优化历史页面""" print("🧪 测试优化历史页面...") try: response = requests.get(f"{BASE_URL}/optimization-history") if response.status_code == 200: print(" ✅ 页面访问正常") # 检查页面内容 content = response.text # 检查是否包含必要的JavaScript文件引用 if 'optimization_history_db.js' in content: print(" ✅ 包含数据库JavaScript文件") else: print(" ❌ 缺少数据库JavaScript文件") return False # 检查是否包含OptimizationHistoryDB类 if 'OptimizationHistoryDB' in content: print(" ✅ 包含OptimizationHistoryDB类") else: print(" ❌ 缺少OptimizationHistoryDB类") 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" ✅ 获取历史记录成功: {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'): print(f" ✅ 添加记录成功: ID {result.get('data', {}).get('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 = [ ("优化历史页面", 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. 在优化历史页面查看、搜索、筛选记录") else: print("⚠️ 部分测试失败,请检查相关功能") return passed == total if __name__ == '__main__': success = main() exit(0 if success else 1)