#!/bin/bash # Script to start a test environment with backend and optional test runner SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PARENT_DIR="$(dirname "$SCRIPT_DIR")" BACKEND_DIR="$(dirname "$(dirname "$PARENT_DIR")")/backend" # Colors for output GREEN='\033[0;32m' YELLOW='\033[0;33m' RED='\033[0;31m' NC='\033[0m' # No Color echo -e "${GREEN}Starting MYP test environment...${NC}" echo "Script directory: $SCRIPT_DIR" echo "Frontend directory: $PARENT_DIR" echo "Backend directory: $BACKEND_DIR" # Check if Docker is available if ! command -v docker &> /dev/null; then echo -e "${RED}Error: Docker is not installed or not in PATH${NC}" exit 1 fi # Check if docker-compose is available if ! command -v docker-compose &> /dev/null && ! command -v docker compose &> /dev/null; then echo -e "${RED}Error: Neither docker-compose nor docker compose is available${NC}" exit 1 fi # Check if running in container IN_CONTAINER=false if [ -f /.dockerenv ]; then IN_CONTAINER=true fi # Check if Docker is available and if we need sudo NEED_SUDO=false if [ "$IN_CONTAINER" = false ]; then if ! docker ps &> /dev/null; then if sudo docker ps &> /dev/null; then echo -e "${YELLOW}Warning: Docker daemon requires sudo access. Will use sudo for all Docker commands.${NC}" NEED_SUDO=true fi fi fi # Function to run docker compose (handles both docker-compose and docker compose syntax) run_docker_compose() { if [ "$NEED_SUDO" = true ]; then if command -v docker-compose &> /dev/null; then sudo docker-compose "$@" else sudo docker compose "$@" fi else if command -v docker-compose &> /dev/null; then docker-compose "$@" else docker compose "$@" fi fi } # Check if backend Docker image exists echo -e "${YELLOW}Checking for backend Docker image...${NC}" cd "$SCRIPT_DIR" # Start the backend container echo -e "${GREEN}Starting backend container...${NC}" run_docker_compose -f docker-compose.test.yml up -d backend # Wait for backend to be ready echo -e "${YELLOW}Waiting for backend to be ready...${NC}" max_attempts=30 attempt=1 backend_ready=false while [ $attempt -le $max_attempts ] && [ "$backend_ready" = "false" ]; do echo "Checking backend readiness (attempt $attempt/$max_attempts)..." if curl -s http://localhost:5000/api/health 2>&1 | grep -q "healthy"; then backend_ready=true echo -e "${GREEN}Backend is ready!${NC}" else echo "Backend not ready yet, waiting..." sleep 2 attempt=$((attempt+1)) fi done if [ "$backend_ready" = "false" ]; then echo -e "${RED}Backend failed to start properly after $max_attempts attempts${NC}" echo "Logs from backend container:" run_docker_compose -f docker-compose.test.yml logs backend exit 1 fi # Start frontend development server if it's not already running if ! curl -s http://localhost:3000 > /dev/null; then echo -e "${YELLOW}Starting frontend development server...${NC}" cd "$PARENT_DIR" # Run in background echo "Starting Next.js development server in the background..." nohup pnpm dev > "$SCRIPT_DIR/frontend.log" 2>&1 & # Store the PID for later cleanup FRONTEND_PID=$! echo $FRONTEND_PID > "$SCRIPT_DIR/frontend.pid" echo -e "${GREEN}Frontend development server started with PID $FRONTEND_PID${NC}" echo "Frontend logs available at: $SCRIPT_DIR/frontend.log" # Wait for frontend to be ready echo -e "${YELLOW}Waiting for frontend to be ready...${NC}" max_attempts=30 attempt=1 frontend_ready=false while [ $attempt -le $max_attempts ] && [ "$frontend_ready" = "false" ]; do echo "Checking frontend readiness (attempt $attempt/$max_attempts)..." if curl -s http://localhost:3000 > /dev/null; then frontend_ready=true echo -e "${GREEN}Frontend is ready!${NC}" else echo "Frontend not ready yet, waiting..." sleep 2 attempt=$((attempt+1)) fi done if [ "$frontend_ready" = "false" ]; then echo -e "${RED}Frontend failed to start properly${NC}" exit 1 fi else echo -e "${GREEN}Frontend already running at http://localhost:3000${NC}" fi echo -e "${GREEN}Test environment is ready!${NC}" echo "Backend is available at: http://localhost:5000" echo "Frontend is available at: http://localhost:3000" echo "" echo "To run Cypress tests:" echo " cd $PARENT_DIR && pnpm cypress" echo "" echo "To stop the test environment:" echo " $SCRIPT_DIR/stop-test-environment.sh"