#!/bin/bash # RAG API 完整测试脚本 # 基于 docs/curl测试手册.md set -e BASE_URL="http://localhost:5001" PASS=0 FAIL=0 RESULTS="" log_result() { local name="$1" local status="$2" if [ "$status" = "PASS" ]; then PASS=$((PASS + 1)) RESULTS="${RESULTS}✅ ${name}\n" else FAIL=$((FAIL + 1)) RESULTS="${RESULTS}❌ ${name}\n" fi } echo "==========================================" echo "RAG API 完整测试" echo "==========================================" # 1. 健康检查 echo -e "\n--- 1. 健康检查 ---" result=$(curl -s "$BASE_URL/health") if echo "$result" | grep -q '"status":"ok"'; then log_result "GET /health" "PASS" else log_result "GET /health" "FAIL" fi # 2. 问答接口 echo -e "\n--- 2. 问答接口 ---" # POST /rag (简化测试) echo '{"message":"三峡工程","chat_history":[]}' > /tmp/rag.json result=$(curl -s -X POST "$BASE_URL/rag" -H "Content-Type: application/json" -d @/tmp/rag.json 2>&1) if echo "$result" | grep -q '"type":"finish"'; then log_result "POST /rag" "PASS" else log_result "POST /rag" "FAIL" fi # POST /chat echo '{"message":"你好","chat_history":[]}' > /tmp/chat.json result=$(curl -s -X POST "$BASE_URL/chat" -H "Content-Type: application/json" -d @/tmp/chat.json) if echo "$result" | grep -q '"answer"'; then log_result "POST /chat" "PASS" else log_result "POST /chat" "FAIL" fi # 3. 检索接口 echo -e "\n--- 3. 检索接口 ---" echo '{"query":"三峡工程","top_k":3}' > /tmp/search.json result=$(curl -s -X POST "$BASE_URL/search" -H "Content-Type: application/json" -d @/tmp/search.json) if echo "$result" | grep -q '"contexts"'; then log_result "POST /search" "PASS" else log_result "POST /search" "FAIL" fi # 4. 向量库管理 echo -e "\n--- 4. 向量库管理 ---" # GET /collections result=$(curl -s "$BASE_URL/collections") if echo "$result" | grep -q '"collections"'; then log_result "GET /collections" "PASS" else log_result "GET /collections" "FAIL" fi # 创建测试向量库 echo '{"name":"test_kb_auto","display_name":"自动测试库"}' > /tmp/create_kb.json result=$(curl -s -X POST "$BASE_URL/collections" -H "Content-Type: application/json" -d @/tmp/create_kb.json) if echo "$result" | grep -q '"success":true'; then log_result "POST /collections (创建)" "PASS" else log_result "POST /collections (创建)" "FAIL" fi # 修改向量库 echo '{"display_name":"自动测试库-已修改"}' > /tmp/update_kb.json result=$(curl -s -X PUT "$BASE_URL/collections/test_kb_auto" -H "Content-Type: application/json" -d @/tmp/update_kb.json) if echo "$result" | grep -q '"success"'; then log_result "PUT /collections/test_kb_auto" "PASS" else log_result "PUT /collections/test_kb_auto" "FAIL" fi # 删除测试向量库 result=$(curl -s -X DELETE "$BASE_URL/collections/test_kb_auto") if echo "$result" | grep -q '"success"'; then log_result "DELETE /collections/test_kb_auto" "PASS" else log_result "DELETE /collections/test_kb_auto" "FAIL" fi # GET /collections//documents result=$(curl -s "$BASE_URL/collections/public_kb/documents") if echo "$result" | grep -q '"documents"'; then log_result "GET /collections/public_kb/documents" "PASS" else log_result "GET /collections/public_kb/documents" "FAIL" fi # 5. 文档管理 echo -e "\n--- 5. 文档管理 ---" result=$(curl -s "$BASE_URL/documents/list?collection=public_kb&page=1&page_size=5") if echo "$result" | grep -q '"documents"'; then log_result "GET /documents/list" "PASS" else log_result "GET /documents/list" "FAIL" fi # 6. 同步服务 echo -e "\n--- 6. 同步服务 ---" result=$(curl -s "$BASE_URL/sync/status") if echo "$result" | grep -q '"enabled"'; then log_result "GET /sync/status" "PASS" else log_result "GET /sync/status" "FAIL" fi # 7. 反馈系统 echo -e "\n--- 7. 反馈系统 ---" result=$(curl -s "$BASE_URL/feedback/stats") if echo "$result" | grep -q '"stats"'; then log_result "GET /feedback/stats" "PASS" else log_result "GET /feedback/stats" "FAIL" fi # 8. FAQ 管理 echo -e "\n--- 8. FAQ 管理 ---" result=$(curl -s "$BASE_URL/faq?page=1&page_size=5") if echo "$result" | grep -q '"faqs"'; then log_result "GET /faq" "PASS" else log_result "GET /faq" "FAIL" fi # 9. 出题系统 echo -e "\n--- 9. 出题系统 ---" result=$(curl -s "$BASE_URL/exam/health") if echo "$result" | grep -q '"status":"ok"'; then log_result "GET /exam/health" "PASS" else log_result "GET /exam/health" "FAIL" fi # 10. 图片服务 echo -e "\n--- 10. 图片服务 ---" result=$(curl -s "$BASE_URL/images/list?limit=5") if echo "$result" | grep -q '"images"'; then log_result "GET /images/list" "PASS" else log_result "GET /images/list" "FAIL" fi # 获取第一张图片ID并测试 image_id=$(curl -s "$BASE_URL/images/list?limit=1" | grep -o '"image_id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -n "$image_id" ]; then http_code=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/images/$image_id") if [ "$http_code" = "200" ]; then log_result "GET /images/" "PASS" else log_result "GET /images/" "FAIL" fi else log_result "GET /images/" "FAIL (无图片)" fi # 11. 报告服务 echo -e "\n--- 11. 报告服务 ---" result=$(curl -s "$BASE_URL/reports/weekly") if echo "$result" | grep -q '"report"'; then log_result "GET /reports/weekly" "PASS" else log_result "GET /reports/weekly" "FAIL" fi # 12. 知识库路由 echo -e "\n--- 12. 知识库路由 ---" echo '{"query":"三峡工程"}' > /tmp/route.json result=$(curl -s -X POST "$BASE_URL/kb/route" -H "Content-Type: application/json" -d @/tmp/route.json) if echo "$result" | grep -q '"target_collections"'; then log_result "POST /kb/route" "PASS" else log_result "POST /kb/route" "FAIL" fi # 输出结果 echo -e "\n==========================================" echo "测试结果汇总" echo "==========================================" echo -e "$RESULTS" echo "------------------------------------------" echo "总计: $PASS 通过, $FAIL 失败" echo "==========================================" # 清理临时文件 rm -f /tmp/rag.json /tmp/chat.json /tmp/search.json /tmp/create_kb.json /tmp/update_kb.json /tmp/route.json exit $FAIL