This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.sh
More file actions
executable file
·205 lines (175 loc) · 7.44 KB
/
Copy pathtest-api.sh
File metadata and controls
executable file
·205 lines (175 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# API Test Script for Autobiography Interview System
# Tests all endpoints and WebSocket functionality
set -e
# Configuration
API_BASE_URL="${1:-http://localhost:3000/api/v2}"
WS_URL="${2:-ws://localhost:3000/ws}"
echo "🧪 Testing Autobiography Interview API"
echo "📍 API Base URL: ${API_BASE_URL}"
echo "🔌 WebSocket URL: ${WS_URL}"
echo ""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Test counter
TESTS_PASSED=0
TESTS_FAILED=0
# Helper function to test API endpoints
test_endpoint() {
local method=$1
local endpoint=$2
local data=$3
local expected_status=$4
local description=$5
echo -e "${BLUE}Testing:${NC} ${description}"
echo -e "${YELLOW}${method} ${endpoint}${NC}"
if [ -n "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X ${method} \
-H "Content-Type: application/json" \
-d "${data}" \
"${API_BASE_URL}${endpoint}")
else
response=$(curl -s -w "\n%{http_code}" -X ${method} \
"${API_BASE_URL}${endpoint}")
fi
# Split response and status code
status_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')
if [ "$status_code" = "$expected_status" ]; then
echo -e "${GREEN}✅ PASS${NC} (Status: ${status_code})"
echo "Response: ${response_body}" | head -c 200
echo ""
TESTS_PASSED=$((TESTS_PASSED + 1))
return 0
else
echo -e "${RED}❌ FAIL${NC} (Expected: ${expected_status}, Got: ${status_code})"
echo "Response: ${response_body}"
echo ""
TESTS_FAILED=$((TESTS_FAILED + 1))
return 1
fi
}
echo "🏥 Health Check"
# Health check is at root level, not under /api/v2
health_response=$(curl -s -w "\n%{http_code}" "http://localhost:3000/health")
health_status=$(echo "$health_response" | tail -n1)
health_body=$(echo "$health_response" | sed '$d')
if [ "$health_status" = "200" ]; then
echo -e "${GREEN}✅ PASS${NC} (Status: ${health_status})"
echo "Response: ${health_body}" | head -c 200
echo ""
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}❌ FAIL${NC} (Expected: 200, Got: ${health_status})"
echo "Response: ${health_body}"
echo ""
TESTS_FAILED=$((TESTS_FAILED + 1))
fi
echo ""
echo "📚 API Documentation Tests"
test_endpoint "GET" "/info" "" "200" "API information"
# Swagger UI returns HTML, so we test for 200 status
swagger_response=$(curl -s -w "\n%{http_code}" "${API_BASE_URL}/docs")
swagger_status=$(echo "$swagger_response" | tail -n1)
if [ "$swagger_status" = "200" ]; then
echo -e "${GREEN}✅ PASS${NC} (Status: ${swagger_status}) - Swagger UI is available"
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}❌ FAIL${NC} (Expected: 200, Got: ${swagger_status})"
TESTS_FAILED=$((TESTS_FAILED + 1))
fi
test_endpoint "GET" "/docs.json" "" "200" "Swagger JSON specification"
echo ""
echo "🔐 Authentication Tests"
# Test device registration
DEVICE_ID="test-device-$(date +%s)"
USER_DATA=$(test_endpoint "POST" "/auth/device-id-register" "{\"device_id\":\"${DEVICE_ID}\"}" "200" "Device registration")
if [ $? -eq 0 ]; then
# Extract user_id from response
USER_ID=$(echo "$USER_DATA" | grep -o '"user_id":"[^"]*"' | cut -d'"' -f4)
echo "📝 Extracted User ID: ${USER_ID}"
else
echo "❌ Failed to register device, using fallback user ID"
USER_ID="test-user-fallback"
fi
echo ""
echo "👤 User Metadata Tests"
test_endpoint "POST" "/user/metadata" "{
\"user_id\":\"${USER_ID}\",
\"age_group\":\"ADULT\",
\"education_level\":\"UNIVERSITY_GRADUATE\",
\"gender\":\"NONE\",
\"marital_status\":\"SINGLE\"
}" "200" "User metadata registration"
echo ""
echo "📖 Chapter Tests"
test_endpoint "GET" "/chapters?userId=${USER_ID}" "" "200" "Get all chapters"
echo ""
# Start chapter interview
CHAPTER_NUMBER="1"
test_endpoint "POST" "/chapters/${CHAPTER_NUMBER}/start" "{\"user_id\":\"${USER_ID}\"}" "200" "Start chapter interview"
echo ""
echo "💬 Conversation Tests"
# Test conversation flow - note: we still use chapter_id for conversation, not chapter_number
# We'll need to get the actual chapter_id from the chapters list
CHAPTER_ID="chapter-1" # This will be a UUID in the new system, but we'll use fallback for testing
test_endpoint "POST" "/conversation/${CHAPTER_ID}" "{
\"user_id\":\"${USER_ID}\",
\"message\":\"저는 어린 시절에 강가에서 뛰어놀던 기억이 가장 인상 깊어요. 여름마다 할아버지와 함께 낚시를 하며 많은 이야기를 나누었습니다.\"
}" "200" "Send first message"
test_endpoint "POST" "/conversation/${CHAPTER_ID}" "{
\"user_id\":\"${USER_ID}\",
\"message\":\"그 경험은 저에게 자연의 소중함과 가족의 사랑을 깨닫게 해주었습니다. 지금도 스트레스를 받을 때면 그 강가의 평온함을 떠올리곤 합니다.\"
}" "200" "Send second message"
test_endpoint "POST" "/conversation/${CHAPTER_ID}" "{
\"user_id\":\"${USER_ID}\",
\"message\":\"할아버지께서는 항상 인내심의 중요성을 강조하셨어요. 낚시를 통해 기다림의 미학을 배웠고, 이는 제 성격 형성에 큰 영향을 주었습니다.\"
}" "200" "Send third message"
test_endpoint "POST" "/conversation/${CHAPTER_ID}" "{
\"user_id\":\"${USER_ID}\",
\"message\":\"그 시절의 순수했던 마음과 호기심이 지금의 저를 만들었다고 생각합니다. 어려운 일이 있을 때마다 그때의 순수함을 잃지 않으려 노력하고 있어요.\"
}" "200" "Send fourth message"
echo ""
echo "📝 Autobiography Tests"
# Wait a bit for potential autobiography generation
echo "⏳ Waiting for potential autobiography generation..."
sleep 3
test_endpoint "GET" "/autobiography/chapters/${CHAPTER_ID}?userId=${USER_ID}" "" "200" "Get chapter autobiography (might not exist yet)"
echo ""
echo "📚 Book Generation Tests"
test_endpoint "POST" "/autobiography/book/generate" "{\"userId\":\"${USER_ID}\"}" "200" "Generate book (might fail if no autobiographies exist)"
echo ""
echo "🔌 WebSocket Connection Test"
# Simple WebSocket test using curl (limited functionality)
echo "Testing WebSocket connection availability..."
if command -v wscat &> /dev/null; then
echo "📡 Testing WebSocket with wscat..."
timeout 5s wscat -c "${WS_URL}?userId=${USER_ID}" -x "ping" || echo "WebSocket test completed (timeout expected)"
else
echo "⚠️ wscat not available, skipping WebSocket test"
echo " Install with: npm install -g wscat"
fi
echo ""
echo "🧪 Error Handling Tests"
test_endpoint "POST" "/auth/device-id-register" "{}" "400" "Invalid device registration (missing device_id)"
test_endpoint "GET" "/chapters?userId=invalid-user" "404" "Invalid user ID"
test_endpoint "POST" "/conversation/invalid-chapter" "{\"user_id\":\"${USER_ID}\",\"message\":\"test\"}" "404" "Invalid chapter ID"
test_endpoint "GET" "/nonexistent-endpoint" "" "404" "Non-existent endpoint"
echo ""
echo "📊 Test Summary"
echo "==============="
echo -e "${GREEN}✅ Tests Passed: ${TESTS_PASSED}${NC}"
echo -e "${RED}❌ Tests Failed: ${TESTS_FAILED}${NC}"
echo -e "📈 Success Rate: $(( TESTS_PASSED * 100 / (TESTS_PASSED + TESTS_FAILED) ))%"
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}🎉 All tests passed! API is working correctly.${NC}"
exit 0
else
echo -e "${YELLOW}⚠️ Some tests failed. Check the API implementation.${NC}"
exit 1
fi