From a8f57ec60b895ce101766ec613ba5347d220a641 Mon Sep 17 00:00:00 2001 From: ChenKaiLiuG Date: Sun, 21 Dec 2025 17:15:44 +0800 Subject: [PATCH] Add test.sh --- docker-compose.yml | 4 +- test.sh | 174 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 test.sh diff --git a/docker-compose.yml b/docker-compose.yml index c3be265..922966a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,9 @@ version: '3.8' services: - # PostgreSQL - 共用資料庫 + # PostgreSQL - 共用資料庫(支援 pgvector) postgres: - image: postgres:15-alpine + image: pgvector/pgvector:pg15 container_name: tobiichiGPT-postgres restart: unless-stopped environment: diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..0a650a2 --- /dev/null +++ b/test.sh @@ -0,0 +1,174 @@ +#!/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 "human-admin"; then + print_success "Models 端點返回 human-admin 模型" +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": "human-admin", + "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