286 lines
8.3 KiB
Bash
Executable File
286 lines
8.3 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# Magic Building Image Generation API Test Script
|
||
# Usage: ./test-api.sh [server_url]
|
||
|
||
SERVER_URL=${1:-"http://localhost:3000"}
|
||
TEST_DIR="./test-images"
|
||
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
||
LOG_FILE="./logs/test-api-${TIMESTAMP}.log"
|
||
|
||
# Colors for output
|
||
GREEN='\033[0;32m'
|
||
RED='\033[0;31m'
|
||
YELLOW='\033[1;33m'
|
||
BLUE='\033[0;34m'
|
||
NC='\033[0m' # No Color
|
||
|
||
# Ensure directories exist
|
||
mkdir -p logs test-images
|
||
|
||
# Logging function
|
||
log() {
|
||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||
}
|
||
|
||
# Success/error output functions
|
||
success() {
|
||
echo -e "${GREEN}✅ $1${NC}" | tee -a "$LOG_FILE"
|
||
}
|
||
|
||
error() {
|
||
echo -e "${RED}❌ $1${NC}" | tee -a "$LOG_FILE"
|
||
}
|
||
|
||
warning() {
|
||
echo -e "${YELLOW}⚠️ $1${NC}" | tee -a "$LOG_FILE"
|
||
}
|
||
|
||
info() {
|
||
echo -e "${BLUE}ℹ️ $1${NC}" | tee -a "$LOG_FILE"
|
||
}
|
||
|
||
# Test functions
|
||
test_health_check() {
|
||
info "Testing health check endpoint..."
|
||
response=$(curl -s -o /tmp/health.json -w "%{http_code}" "$SERVER_URL/health")
|
||
|
||
if [ "$response" -eq 200 ]; then
|
||
success "Health check passed (HTTP $response)"
|
||
log "Response: $(cat /tmp/health.json)"
|
||
else
|
||
error "Health check failed (HTTP $response)"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
test_api_info() {
|
||
info "Testing API info endpoint..."
|
||
response=$(curl -s -o /tmp/api-info.json -w "%{http_code}" "$SERVER_URL/api/info")
|
||
|
||
if [ "$response" -eq 200 ]; then
|
||
success "API info endpoint passed (HTTP $response)"
|
||
log "Response: $(cat /tmp/api-info.json)"
|
||
else
|
||
error "API info endpoint failed (HTTP $response)"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
test_text_to_image() {
|
||
info "Testing text-to-image generation..."
|
||
|
||
response=$(curl -s -o /tmp/text-to-image.json -w "%{http_code}" \
|
||
-X POST "$SERVER_URL/api/generate" \
|
||
-F "prompt=A majestic dragon soaring through clouds above a medieval castle" \
|
||
-F "filename=dragon_castle_${TIMESTAMP}")
|
||
|
||
if [ "$response" -eq 200 ]; then
|
||
success "Text-to-image generation passed (HTTP $response)"
|
||
log "Response: $(cat /tmp/text-to-image.json)"
|
||
|
||
# Check if image file was created
|
||
filename=$(jq -r '.data.filename' /tmp/text-to-image.json 2>/dev/null)
|
||
if [ "$filename" != "null" ] && [ -f "src/results/$filename" ]; then
|
||
success "Generated image file found: $filename"
|
||
log "Image size: $(ls -lh src/results/$filename | awk '{print $5}')"
|
||
else
|
||
warning "Generated image file not found or response malformed"
|
||
fi
|
||
else
|
||
error "Text-to-image generation failed (HTTP $response)"
|
||
log "Error response: $(cat /tmp/text-to-image.json)"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
test_validation_errors() {
|
||
info "Testing validation error handling..."
|
||
|
||
# Test missing prompt
|
||
response=$(curl -s -o /tmp/validation1.json -w "%{http_code}" \
|
||
-X POST "$SERVER_URL/api/generate" \
|
||
-F "filename=test")
|
||
|
||
if [ "$response" -eq 400 ]; then
|
||
success "Missing prompt validation works (HTTP $response)"
|
||
else
|
||
error "Missing prompt validation failed (HTTP $response)"
|
||
fi
|
||
|
||
# Test missing filename
|
||
response=$(curl -s -o /tmp/validation2.json -w "%{http_code}" \
|
||
-X POST "$SERVER_URL/api/generate" \
|
||
-F "prompt=Test prompt")
|
||
|
||
if [ "$response" -eq 400 ]; then
|
||
success "Missing filename validation works (HTTP $response)"
|
||
else
|
||
error "Missing filename validation failed (HTTP $response)"
|
||
fi
|
||
|
||
# Test invalid filename characters
|
||
response=$(curl -s -o /tmp/validation3.json -w "%{http_code}" \
|
||
-X POST "$SERVER_URL/api/generate" \
|
||
-F "prompt=Test prompt" \
|
||
-F "filename=invalid/filename*with?bad&chars")
|
||
|
||
if [ "$response" -eq 200 ]; then
|
||
success "Filename sanitization works (HTTP $response)"
|
||
log "Sanitized filename response: $(cat /tmp/validation3.json)"
|
||
else
|
||
warning "Filename sanitization test had unexpected result (HTTP $response)"
|
||
fi
|
||
}
|
||
|
||
test_404_endpoint() {
|
||
info "Testing 404 error handling..."
|
||
response=$(curl -s -o /tmp/404.json -w "%{http_code}" "$SERVER_URL/api/nonexistent")
|
||
|
||
if [ "$response" -eq 404 ]; then
|
||
success "404 error handling works (HTTP $response)"
|
||
else
|
||
error "404 error handling failed (HTTP $response)"
|
||
fi
|
||
}
|
||
|
||
create_test_image() {
|
||
info "Creating test reference image..."
|
||
|
||
# Create a simple test image using ImageMagick (if available) or skip
|
||
if command -v convert &> /dev/null; then
|
||
convert -size 300x200 xc:lightblue -pointsize 24 -fill black \
|
||
-gravity center -annotate 0 "Test Reference Image" \
|
||
"$TEST_DIR/test-reference.png"
|
||
success "Test reference image created"
|
||
else
|
||
warning "ImageMagick not available, skipping reference image test"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
test_image_with_reference() {
|
||
if [ ! -f "$TEST_DIR/test-reference.png" ]; then
|
||
warning "No reference image available, skipping reference image test"
|
||
return 0
|
||
fi
|
||
|
||
info "Testing image generation with reference image..."
|
||
|
||
response=$(curl -s -o /tmp/with-reference.json -w "%{http_code}" \
|
||
-X POST "$SERVER_URL/api/generate" \
|
||
-F "prompt=Add magical sparkles and glowing effects to this image" \
|
||
-F "filename=sparkled_reference_${TIMESTAMP}" \
|
||
-F "referenceFiles=@${TEST_DIR}/test-reference.png")
|
||
|
||
if [ "$response" -eq 200 ]; then
|
||
success "Reference image generation passed (HTTP $response)"
|
||
log "Response: $(cat /tmp/with-reference.json)"
|
||
|
||
# Check if image file was created
|
||
filename=$(jq -r '.data.filename' /tmp/with-reference.json 2>/dev/null)
|
||
if [ "$filename" != "null" ] && [ -f "src/results/$filename" ]; then
|
||
success "Generated image with reference found: $filename"
|
||
else
|
||
warning "Generated image with reference not found"
|
||
fi
|
||
else
|
||
error "Reference image generation failed (HTTP $response)"
|
||
log "Error response: $(cat /tmp/with-reference.json)"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
# Performance test
|
||
test_concurrent_requests() {
|
||
info "Testing concurrent requests (performance test)..."
|
||
|
||
# Start 3 concurrent requests
|
||
for i in {1..3}; do
|
||
(
|
||
response=$(curl -s -o "/tmp/concurrent-$i.json" -w "%{http_code}" \
|
||
-X POST "$SERVER_URL/api/generate" \
|
||
-F "prompt=Concurrent test image $i - fantasy landscape with mountains" \
|
||
-F "filename=concurrent_test_${i}_${TIMESTAMP}")
|
||
|
||
if [ "$response" -eq 200 ]; then
|
||
log "Concurrent request $i succeeded (HTTP $response)"
|
||
else
|
||
log "Concurrent request $i failed (HTTP $response)"
|
||
fi
|
||
) &
|
||
done
|
||
|
||
# Wait for all background jobs
|
||
wait
|
||
|
||
success "Concurrent requests test completed"
|
||
}
|
||
|
||
# Main test execution
|
||
main() {
|
||
info "🚀 Starting Magic Building API Test Suite"
|
||
info "Server URL: $SERVER_URL"
|
||
info "Test log: $LOG_FILE"
|
||
|
||
# Check if server is running
|
||
if ! curl -s "$SERVER_URL/health" > /dev/null; then
|
||
error "Server is not responding at $SERVER_URL"
|
||
error "Please start the server with: pnpm server"
|
||
exit 1
|
||
fi
|
||
|
||
# Run tests
|
||
tests_passed=0
|
||
tests_total=0
|
||
|
||
run_test() {
|
||
((tests_total++))
|
||
if $1; then
|
||
((tests_passed++))
|
||
fi
|
||
}
|
||
|
||
run_test test_health_check
|
||
run_test test_api_info
|
||
run_test test_text_to_image
|
||
run_test test_validation_errors
|
||
run_test test_404_endpoint
|
||
|
||
# Optional tests
|
||
if create_test_image; then
|
||
run_test test_image_with_reference
|
||
fi
|
||
|
||
run_test test_concurrent_requests
|
||
|
||
# Summary
|
||
echo
|
||
info "📊 Test Results Summary"
|
||
success "Passed: $tests_passed/$tests_total tests"
|
||
|
||
if [ "$tests_passed" -eq "$tests_total" ]; then
|
||
success "🎉 All tests passed!"
|
||
else
|
||
warning "⚠️ Some tests failed. Check the logs for details."
|
||
fi
|
||
|
||
info "Generated images are in: src/results/"
|
||
info "Test logs saved to: $LOG_FILE"
|
||
}
|
||
|
||
# Check dependencies
|
||
check_dependencies() {
|
||
if ! command -v curl &> /dev/null; then
|
||
error "curl is required for testing"
|
||
exit 1
|
||
fi
|
||
|
||
if ! command -v jq &> /dev/null; then
|
||
warning "jq not found - JSON response parsing will be limited"
|
||
fi
|
||
}
|
||
|
||
# Run the tests
|
||
check_dependencies
|
||
main "$@" |