-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·228 lines (186 loc) · 5.88 KB
/
test.sh
File metadata and controls
executable file
·228 lines (186 loc) · 5.88 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
set -uo pipefail
echo "CV Builder Test Suite"
echo "========================"
echo ""
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
SERVER_HOST="${CV_SERVER_HOST:-127.0.0.1}"
export SERVER_HOST
SERVER_PORT="${CV_SERVER_PORT:-8366}"
SERVER_URL="${CV_SERVER_URL:-http://${SERVER_HOST}:${SERVER_PORT}}"
SERVER_PID=""
SERVER_LOG="$(mktemp -t cv-builder-test-log.XXXXXX)"
cleanup() {
if [[ -n "${SERVER_PID}" ]] && kill -0 "${SERVER_PID}" 2>/dev/null; then
kill "${SERVER_PID}" 2>/dev/null || true
wait "${SERVER_PID}" 2>/dev/null || true
fi
rm -f "${SERVER_LOG}"
}
trap cleanup EXIT
# Check prerequisites
echo "Checking prerequisites..."
if ! command -v zig &> /dev/null; then
echo -e "${RED}❌ Zig not found. Please install Zig 0.12.0+${NC}"
exit 1
fi
echo -e "${GREEN}✓ Zig found: $(zig version)${NC}"
if ! command -v typst &> /dev/null; then
echo -e "${RED}❌ Typst not found. Please install Typst${NC}"
exit 1
fi
echo -e "${GREEN}✓ Typst found: $(typst --version)${NC}"
if ! command -v node &> /dev/null; then
echo -e "${YELLOW}⚠️ Node not found. Skipping server smoke tests (including PDF smoke coverage).${NC}"
exit 0
fi
echo -e "${GREEN}✓ Node found: $(node --version)${NC}"
echo ""
# Skip the integration smoke test in environments that block local socket binds
if ! node -e 'const net = require("net"); const server = net.createServer(); server.once("error", (err) => { console.error(err.code || err.message); process.exit(1); }); server.listen(0, process.env.SERVER_HOST || "127.0.0.1", () => server.close(() => process.exit(0)));' >/dev/null 2>&1; then
echo -e "${YELLOW}⚠️ Local socket binds are unavailable in this environment. Skipping server smoke test.${NC}"
exit 0
fi
echo ""
# Build
echo "Building CV server..."
zig build -Doptimize=ReleaseFast
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Build successful${NC}"
else
echo -e "${RED}❌ Build failed${NC}"
exit 1
fi
echo ""
# Start server in background
echo "Starting server..."
./zig-out/bin/cv-server >"${SERVER_LOG}" 2>&1 &
SERVER_PID=$!
# Give server time to start and respond
HEALTH_RESPONSE=""
for _ in {1..10}; do
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
break
fi
HEALTH_RESPONSE="$(curl -fsS "${SERVER_URL}/health" 2>/dev/null || true)"
if echo "$HEALTH_RESPONSE" | grep -q "healthy"; then
break
fi
sleep 1
done
# Check if server is running
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
echo -e "${RED}❌ Server failed to start${NC}"
echo ""
echo "Server log:"
sed -n '1,160p' "${SERVER_LOG}"
exit 1
fi
echo -e "${GREEN}✓ Server running (PID: $SERVER_PID)${NC}"
echo ""
# Test health endpoint
echo "Testing health endpoint..."
# Re-check health endpoint to avoid relying on potentially stale startup response
HEALTH_RESPONSE="$(curl -fsS "${SERVER_URL}/health" 2>/dev/null || true)"
if echo "$HEALTH_RESPONSE" | grep -q "healthy"; then
echo -e "${GREEN}✓ Health check passed${NC}"
echo " Response: $HEALTH_RESPONSE"
else
echo -e "${RED}❌ Health check failed${NC}"
kill $SERVER_PID
exit 1
fi
echo ""
# Test CV generation with sample data
echo "Testing CV generation..."
START_TIME=$(date +%s%3N)
curl -fsS "${SERVER_URL}/test" --output test-cv.pdf
END_TIME=$(date +%s%3N)
GENERATION_TIME=$((END_TIME - START_TIME))
if [ -f "test-cv.pdf" ]; then
FILE_SIZE=$(stat -f%z "test-cv.pdf" 2>/dev/null || stat -c%s "test-cv.pdf" 2>/dev/null)
echo -e "${GREEN}✓ CV generated successfully${NC}"
echo " File: test-cv.pdf"
echo " Size: $FILE_SIZE bytes"
echo " Time: ${GENERATION_TIME}ms"
# Verify it's a valid PDF
if file test-cv.pdf | grep -q "PDF"; then
echo -e "${GREEN}✓ Valid PDF file${NC}"
else
echo -e "${RED}❌ Invalid PDF file${NC}"
kill $SERVER_PID
exit 1
fi
else
echo -e "${RED}❌ CV generation failed${NC}"
kill $SERVER_PID
exit 1
fi
echo ""
# Test API endpoint with custom data
echo "Testing API endpoint..."
curl -fsS -X POST "${SERVER_URL}/api/generate-cv" \
-H "Content-Type: application/json" \
-d @test-data/sample-cv.json \
--output api-test-cv.pdf
if [ -f "api-test-cv.pdf" ]; then
API_FILE_SIZE=$(stat -f%z "api-test-cv.pdf" 2>/dev/null || stat -c%s "api-test-cv.pdf" 2>/dev/null)
echo -e "${GREEN}✓ API test passed${NC}"
echo " File: api-test-cv.pdf"
echo " Size: $API_FILE_SIZE bytes"
else
echo -e "${RED}❌ API test failed${NC}"
kill $SERVER_PID
exit 1
fi
echo ""
# Performance test
echo "Performance test (10 requests)..."
TOTAL_TIME=0
for i in {1..10}; do
START=$(date +%s%3N)
curl -fsS "${SERVER_URL}/test" --output perf-test-$i.pdf
END=$(date +%s%3N)
TIME=$((END - START))
TOTAL_TIME=$((TOTAL_TIME + TIME))
echo " Request $i: ${TIME}ms"
done
AVG_TIME=$((TOTAL_TIME / 10))
echo -e "${GREEN}✓ Performance test complete${NC}"
echo " Average time: ${AVG_TIME}ms"
echo " Total time: ${TOTAL_TIME}ms"
echo " Throughput: $(echo "scale=2; 10000 / $TOTAL_TIME" | bc) req/sec"
# Cleanup perf test files
rm -f perf-test-*.pdf
echo ""
# Memory check (if available)
if command -v ps &> /dev/null; then
MEMORY=$(ps -o rss= -p $SERVER_PID)
MEMORY_MB=$(echo "scale=2; $MEMORY / 1024" | bc)
echo "Memory usage: ${MEMORY_MB}MB"
fi
echo ""
# Cleanup
echo "Cleaning up..."
kill "$SERVER_PID"
wait "$SERVER_PID" 2>/dev/null || true
SERVER_PID=""
echo -e "${GREEN}✓ Server stopped${NC}"
echo ""
echo "================================"
echo -e "${GREEN}✅ All tests passed!${NC}"
echo "================================"
echo ""
echo "Generated files:"
echo " - test-cv.pdf (GET /test)"
echo " - api-test-cv.pdf (POST /api/generate-cv)"
echo ""
echo "Next steps:"
echo " 1. Open test-cv.pdf to verify output"
echo " 2. Run: zig build run (start server)"
echo " 3. Visit: ${SERVER_URL}/test"
echo ""