#!/usr/bin/env python3 """ 测试api后端连接 @author huazm """ import os import sys import requests import json # 配置 API_BASE_URL = os.environ.get('API_BASE_URL', 'http://localhost:8080') API_PREFIX = '/api/ai-assistant' def test_health(): """测试健康检查""" print("=" * 60) print("测试1: 健康检查") print("=" * 60) try: url = f'{API_BASE_URL}/actuator/health' print(f"请求: GET {url}") response = requests.get(url, timeout=5) print(f"状态码: {response.status_code}") print(f"响应: {response.text}") return response.status_code == 200 except Exception as e: print(f"❌ 错误: {e}") return False def test_get_applications(): """测试获取应用列表""" print("\n" + "=" * 60) print("测试2: 获取应用列表") print("=" * 60) try: url = f'{API_BASE_URL}{API_PREFIX}/chatapp' print(f"请求: GET {url}") response = requests.get(url, timeout=10) print(f"状态码: {response.status_code}") if response.status_code == 200: data = response.json() print(f"响应数据结构: {json.dumps(data, ensure_ascii=False, indent=2)[:500]}...") if data.get('code') == 200 and data.get('data'): apps = data['data'] print(f"\n✅ 成功获取 {len(apps)} 个应用") if apps: print(f"\n第一个应用示例:") print(json.dumps(apps[0], ensure_ascii=False, indent=2)) return True else: print(f"❌ 响应格式不正确") return False else: print(f"❌ 请求失败: {response.text}") return False except Exception as e: print(f"❌ 错误: {e}") return False def test_get_recommend_questions(app_id): """测试获取推荐问题""" print("\n" + "=" * 60) print("测试3: 获取推荐问题") print("=" * 60) try: url = f'{API_BASE_URL}{API_PREFIX}/chatapp/{app_id}/getRecommendQuestion' params = {'pageSize': 3, 'current': 1} print(f"请求: GET {url}") print(f"参数: {params}") response = requests.get(url, params=params, timeout=10) print(f"状态码: {response.status_code}") if response.status_code == 200: data = response.json() print(f"响应: {json.dumps(data, ensure_ascii=False, indent=2)[:500]}...") if data.get('code') == 200: records = data.get('data', {}).get('records', []) print(f"\n✅ 成功获取 {len(records)} 个推荐问题") for i, q in enumerate(records, 1): print(f" {i}. {q.get('question', 'N/A')}") return True else: print(f"⚠️ 响应code不为200") return False else: print(f"❌ 请求失败: {response.text}") return False except Exception as e: print(f"❌ 错误: {e}") return False def main(): """主函数""" print("\n" + "🔍 " * 20) print("AI助手Web客户端 - API连接测试") print("🔍 " * 20) print(f"\n后端地址: {API_BASE_URL}") print(f"API前缀: {API_PREFIX}\n") results = [] # 测试1: 健康检查 results.append(("健康检查", test_health())) # 测试2: 获取应用列表 apps_ok = test_get_applications() results.append(("获取应用列表", apps_ok)) # 测试3: 获取推荐问题(使用第一个应用) if apps_ok: # 尝试获取第一个应用的ID try: url = f'{API_BASE_URL}{API_PREFIX}/chatapp' response = requests.get(url, timeout=10) data = response.json() if data.get('code') == 200 and data.get('data'): first_app_id = data['data'][0]['id'] results.append(("获取推荐问题", test_get_recommend_questions(first_app_id))) else: results.append(("获取推荐问题", False)) except: results.append(("获取推荐问题", False)) else: results.append(("获取推荐问题", False)) # 打印测试结果 print("\n" + "=" * 60) print("测试结果汇总") print("=" * 60) for name, success in results: status = "✅ 通过" if success else "❌ 失败" print(f"{name:20s} {status}") all_passed = all(result[1] for result in results) print("\n" + "=" * 60) if all_passed: print("🎉 所有测试通过!Web客户端可以正常使用。") else: print("⚠️ 部分测试失败,请检查后端服务配置。") print("=" * 60 + "\n") return 0 if all_passed else 1 if __name__ == '__main__': sys.exit(main())