001project_wildgrowth/backend/scripts/test-course-generation.sh

139 lines
4.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 完整的课程生成流程测试脚本
BASE_URL="https://api.muststudy.xin"
EMAIL="test$(date +%s)@example.com"
PASSWORD="test123456"
echo "🧪 测试课程生成完整流程"
echo "📍 API: $BASE_URL"
echo "👤 测试用户: $EMAIL"
echo ""
# 1. 注册并登录
echo "1⃣ 注册并登录..."
REGISTER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/register" \
-H "Content-Type: application/json" \
-d "{\"email\":\"$EMAIL\",\"password\":\"$PASSWORD\",\"username\":\"测试用户\"}")
echo "注册响应: $REGISTER_RESPONSE" | head -c 200
echo ""
TOKEN=$(curl -s -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/json" \
-d "{\"email\":\"$EMAIL\",\"password\":\"$PASSWORD\"}" | \
python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('data', {}).get('token', ''))" 2>/dev/null)
if [ -z "$TOKEN" ]; then
echo "❌ 登录失败,尝试直接解析..."
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/json" \
-d "{\"email\":\"$EMAIL\",\"password\":\"$PASSWORD\"}")
echo "登录响应: $LOGIN_RESPONSE"
exit 1
fi
echo "✅ 登录成功Token: ${TOKEN:0:20}..."
echo ""
# 2. 创建课程
echo "2⃣ 创建课程(精华版)..."
CREATE_RESPONSE=$(curl -s -X POST "$BASE_URL/api/ai/content/upload" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"content": "社交是每个人都需要掌握的重要技能。在现代社会,良好的人际关系不仅能帮助我们获得更多机会,还能提升我们的生活质量。",
"style": "essence"
}')
COURSE_ID=$(echo "$CREATE_RESPONSE" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('data', {}).get('courseId', ''))" 2>/dev/null)
TASK_ID=$(echo "$CREATE_RESPONSE" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('data', {}).get('taskId', ''))" 2>/dev/null)
if [ -z "$COURSE_ID" ]; then
echo "❌ 创建课程失败"
echo "响应: $CREATE_RESPONSE"
exit 1
fi
echo "✅ 课程创建成功"
echo " Course ID: $COURSE_ID"
echo " Task ID: $TASK_ID"
echo ""
# 3. 轮询查询状态
echo "3⃣ 轮询查询生成状态最多等待2分钟..."
MAX_ITERATIONS=40
for i in $(seq 1 $MAX_ITERATIONS); do
sleep 3
STATUS_RESPONSE=$(curl -s -X GET "$BASE_URL/api/my-courses" \
-H "Authorization: Bearer $TOKEN")
# 提取进度
PROGRESS=$(echo "$STATUS_RESPONSE" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
courses = data.get('data', {}).get('courses', [])
for course in courses:
if course.get('id') == '$COURSE_ID':
progress = course.get('generation_progress')
status = course.get('generation_status') or course.get('status')
title = course.get('title', 'N/A')
if progress is not None:
print(f\"{int(progress * 100)}|{status}|{title}\")
else:
print(f\"N/A|{status}|{title}\")
break
except:
pass
" 2>/dev/null)
if [ -n "$PROGRESS" ]; then
PROG_VAL=$(echo "$PROGRESS" | cut -d'|' -f1)
STATUS_VAL=$(echo "$PROGRESS" | cut -d'|' -f2)
TITLE_VAL=$(echo "$PROGRESS" | cut -d'|' -f3)
if [ "$PROG_VAL" != "N/A" ]; then
echo " [$i/$MAX_ITERATIONS] 进度: ${PROG_VAL}% | 状态: $STATUS_VAL | 标题: $TITLE_VAL"
else
echo " [$i/$MAX_ITERATIONS] 状态: $STATUS_VAL | 标题: $TITLE_VAL"
fi
if [ "$STATUS_VAL" = "completed" ] || [ "$STATUS_VAL" = "已完成" ]; then
echo ""
echo "✅ 课程生成完成!"
break
fi
if [ "$STATUS_VAL" = "failed" ] || [ "$STATUS_VAL" = "失败" ]; then
echo ""
echo "❌ 课程生成失败"
break
fi
else
echo " [$i/$MAX_ITERATIONS] 查询中..."
fi
done
echo ""
echo "4⃣ 查询任务日志..."
LOGS_RESPONSE=$(curl -s -X GET "$BASE_URL/api/ai/prompts/logs?taskId=$TASK_ID" \
-H "Authorization: Bearer $TOKEN")
LOG_COUNT=$(echo "$LOGS_RESPONSE" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
logs = data.get('data', {}).get('logs', [])
print(len(logs))
for log in logs[:5]:
print(f\" - {log.get('promptType')} ({log.get('status')}) - {log.get('duration')}ms\")
except:
print('0')
" 2>/dev/null)
echo "找到 $LOG_COUNT 条日志记录"
echo ""
echo "✅ 测试完成!"