import requests import json def test_complete_flow(): """ 测试完整的目标创建和题目生成流程 模拟Android应用的完整流程 """ print("=== 测试完整的目标创建和题目生成流程 ===") # 步骤1: 创建目标 print("步骤1: 创建学习目标") goal_data = { "userId": 1, "subject": "物理", "goalType": "选择题", "goalContent": "万有引力定律的应用", "difficulty": "中等", "totalQuantity": 3, "estimatedTime": 45 } print(f"创建目标请求: {json.dumps(goal_data, indent=2, ensure_ascii=False)}") try: # 调用创建目标API create_response = requests.post( "http://localhost:8080/api/goals", json=goal_data, timeout=10 ) if create_response.status_code != 200: print(f"创建目标失败: {create_response.status_code} - {create_response.text}") return create_result = create_response.json() print(f"创建目标响应: {json.dumps(create_result, ensure_ascii=False, indent=2)}") # 步骤2: 解析goalId(模拟修复后的Android逻辑) print("\n步骤2: 解析goalId") goal_id = None if 'data' in create_result and isinstance(create_result['data'], dict): goal_id = create_result['data'].get('goalId') print(f"✅ 成功解析goalId: {goal_id}") else: print("❌ 无法解析goalId") return # 步骤3: 调用Coze工作流API生成题目 print(f"\n步骤3: 使用goalId({goal_id})调用Coze API生成题目") coze_data = { "userId": 1, "goalId": goal_id, "subject": goal_data["subject"], "knowledgePoint": goal_data["goalContent"], "difficulty": goal_data["difficulty"], "type": goal_data["goalType"], "totalQuantity": goal_data["totalQuantity"] } print(f"Coze API请求: {json.dumps(coze_data, indent=2, ensure_ascii=False)}") coze_response = requests.post( "http://localhost:8080/api/coze/generate-questions", json=coze_data, timeout=60 ) if coze_response.status_code != 200: print(f"Coze API调用失败: {coze_response.status_code} - {coze_response.text}") return coze_result = coze_response.json() print(f"\nCoze API响应: {json.dumps(coze_result, ensure_ascii=False, indent=2)}") # 步骤4: 验证生成的题目 print("\n步骤4: 验证生成的题目") if coze_result.get('success'): questions = coze_result.get('questions', []) print(f"✅ 成功生成 {len(questions)} 道题目") # 验证每道题目的goalId all_correct = True for i, question in enumerate(questions, 1): question_goal_id = question.get('goalId') if question_goal_id == goal_id: print(f"✅ 题目{i}: goalId正确 ({question_goal_id})") print(f" 题目内容: {question.get('content', '无内容')[:50]}...") else: print(f"❌ 题目{i}: goalId错误!期望: {goal_id}, 实际: {question_goal_id}") all_correct = False if all_correct: print("\n🎉 所有题目的goalId都正确!") else: print("\n❌ 存在goalId不正确的题目") else: print(f"❌ 题目生成失败: {coze_result.get('message', '未知错误')}") # 步骤5: 验证数据库中的题目 print(f"\n步骤5: 验证数据库中goalId={goal_id}的题目") db_response = requests.get( f"http://localhost:8080/api/ai/questions/{goal_id}", timeout=10 ) if db_response.status_code == 200: db_result = db_response.json() if db_result.get('data'): db_questions = db_result['data'] print(f"✅ 数据库中找到 {len(db_questions)} 道题目") # 验证数据库中题目的goalId for i, question in enumerate(db_questions, 1): question_goal_id = question.get('goalId') if question_goal_id == goal_id: print(f"✅ 数据库题目{i}: goalId正确 ({question_goal_id})") else: print(f"❌ 数据库题目{i}: goalId错误!期望: {goal_id}, 实际: {question_goal_id}") else: print("❌ 数据库中未找到题目") else: print(f"❌ 查询数据库失败: {db_response.status_code}") print("\n=== 测试完成 ===") except Exception as e: print(f"测试过程中发生异常: {e}") import traceback traceback.print_exc() if __name__ == "__main__": test_complete_flow()