Files
tobiichiGPT/test.sh
ChenKaiLiuG 13c8f8aa3c Add test.sh
2025-12-23 22:47:40 +08:00

175 lines
6.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
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
# TobiichiGPT 系統測試腳本
# 用途: 驗證所有服務是否正常運行並測試端到端流程
set -e
# 顏色定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 測試結果計數
TESTS_PASSED=0
TESTS_FAILED=0
# 輔助函數
print_header() {
echo -e "\n${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}\n"
}
print_test() {
echo -e "${YELLOW}[測試] $1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
((TESTS_PASSED++))
}
print_error() {
echo -e "${RED}$1${NC}"
((TESTS_FAILED++))
}
# 主測試開始
print_header "TobiichiGPT 系統測試"
# ==================== 測試 1: 容器狀態 ====================
print_test "檢查容器運行狀態"
CONTAINERS=("tobiichiGPT-postgres" "tobiichiGPT-api" "tobiichiGPT-ui" "tobiichiGPT-chatwoot" "tobiichiGPT-redis")
for container in "${CONTAINERS[@]}"; do
if sudo docker ps --format '{{.Names}}' | grep -q "^${container}$"; then
print_success "容器 ${container} 運行中"
else
print_error "容器 ${container} 未運行"
fi
done
# ==================== 測試 2: API 健康檢查 ====================
print_test "測試 API 健康狀態"
API_RESPONSE=$(curl -s http://localhost:18000/)
if echo "$API_RESPONSE" | grep -q "TobiichiGPT API"; then
print_success "API 健康檢查通過"
else
print_error "API 健康檢查失敗"
fi
# ==================== 測試 3: API Models 端點 ====================
print_test "測試 API Models 端點"
MODELS_RESPONSE=$(curl -s http://localhost:18000/v1/models)
if echo "$MODELS_RESPONSE" | grep -q "tobiichiGPT"; then
print_success "Models 端點返回 tobiichiGPT 模型"
else
print_error "Models 端點測試失敗"
fi
# ==================== 測試 4: 資料庫連線 ====================
print_test "測試 PostgreSQL 連線"
if sudo docker exec tobiichiGPT-postgres psql -U tobiichi3227 -d tobiichiGPT -c "\dt" | grep -q "reply_queue"; then
print_success "資料庫連線正常reply_queue 表存在"
else
print_error "資料庫連線失敗或 reply_queue 表不存在"
fi
# ==================== 測試 5: Open WebUI ====================
print_test "測試 Open WebUI"
WEBUI_RESPONSE=$(curl -s http://localhost:10060/)
if echo "$WEBUI_RESPONSE" | grep -q "Open WebUI"; then
print_success "Open WebUI 正常運行"
else
print_error "Open WebUI 測試失敗"
fi
# ==================== 測試 6: Chatwoot ====================
print_test "測試 Chatwoot"
CHATWOOT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:13000/)
if [ "$CHATWOOT_STATUS" == "200" ]; then
print_success "Chatwoot HTTP 響應正常 (200)"
else
print_error "Chatwoot HTTP 響應異常 ($CHATWOOT_STATUS)"
fi
# ==================== 測試 7: 端到端流程 ====================
print_test "測試端到端訊息流程"
# 7.1 發送測試訊息(背景執行)
echo " → 發送測試訊息到 API..."
curl -X POST http://localhost:18000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "tobiichiGPT",
"messages": [
{"role": "user", "content": "自動化測試訊息"}
]
}' > /tmp/api_response.json 2>&1 &
CURL_PID=$!
sleep 2
# 7.2 檢查訊息是否進入佇列
echo " → 檢查訊息是否進入佇列..."
QUEUE_CHECK=$(sudo docker exec tobiichiGPT-postgres psql -U tobiichi3227 -d tobiichiGPT -t -c "SELECT conversation_id FROM reply_queue WHERE user_message = '自動化測試訊息' AND status = 'pending' ORDER BY created_at DESC LIMIT 1;")
if [ ! -z "$QUEUE_CHECK" ]; then
CONV_ID=$(echo "$QUEUE_CHECK" | xargs)
print_success "訊息已進入佇列 (ID: ${CONV_ID:0:8}...)"
# 7.3 模擬管理員回覆
echo " → 模擬管理員回覆..."
sudo docker exec tobiichiGPT-postgres psql -U tobiichi3227 -d tobiichiGPT -c "UPDATE reply_queue SET admin_reply = '測試回覆成功 ✅', status = 'replied', replied_at = NOW() WHERE conversation_id = '$CONV_ID';" > /dev/null
# 7.4 等待 API 響應
sleep 2
# 檢查是否收到回覆
if wait $CURL_PID 2>/dev/null; then
if grep -q "測試回覆成功" /tmp/api_response.json 2>/dev/null; then
print_success "端到端流程測試成功"
else
print_error "API 未返回正確回覆"
fi
else
# curl 可能已經完成,檢查輸出
if [ -f /tmp/api_response.json ] && grep -q "content" /tmp/api_response.json; then
print_success "端到端流程測試完成"
else
print_error "API 響應異常"
fi
fi
# 清理測試資料
sudo docker exec tobiichiGPT-postgres psql -U tobiichi3227 -d tobiichiGPT -c "DELETE FROM reply_queue WHERE conversation_id = '$CONV_ID';" > /dev/null
else
print_error "訊息未進入佇列"
kill $CURL_PID 2>/dev/null || true
fi
# 清理臨時檔案
rm -f /tmp/api_response.json
# ==================== 測試總結 ====================
print_header "測試結果總結"
echo -e "${GREEN}通過: $TESTS_PASSED${NC}"
echo -e "${RED}失敗: $TESTS_FAILED${NC}"
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}═══════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}所有測試通過!✅ 系統運行正常${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════════════${NC}"
exit 0
else
echo -e "${RED}═══════════════════════════════════════════════════════════${NC}"
echo -e "${RED}部分測試失敗 ⚠️ 請檢查日誌${NC}"
echo -e "${RED}═══════════════════════════════════════════════════════════${NC}"
exit 1
fi