diff --git a/scripts/run.sh b/scripts/run.sh index 0fa4a69..525bede 100644 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -29,13 +29,9 @@ echo -e "${YELLOW}๐Ÿ“ฆ Step 1: Starting Docker services (PostgreSQL, Redis, Seaw docker compose up -d # Get the actual network name from docker compose (it adds project prefix) -# Docker Compose uses directory name as project prefix, so network will be {project}_p1ctos4ve-network NETWORK_NAME=$(docker network ls --format "{{.Name}}" | grep "p1ctos4ve-network" | head -n 1) -# Fallback to default if not found if [ -z "$NETWORK_NAME" ]; then - # Try to get from docker compose config NETWORK_NAME=$(docker compose config 2>/dev/null | grep -A 2 "networks:" | grep "name:" | awk '{print $2}' | tr -d '"' | head -n 1) - # Final fallback if [ -z "$NETWORK_NAME" ]; then NETWORK_NAME="p1ctos4ve-network" fi @@ -100,9 +96,7 @@ sleep 2 # Create S3 bucket in SeaweedFS if it doesn't exist echo -e "${YELLOW}๐Ÿ“ฆ Creating S3 bucket in SeaweedFS...${NC}" BUCKET_NAME="p1ctos4ve" -# Try to create bucket via S3 API (using AWS CLI-like approach) if ! curl -s -X PUT "http://localhost:8333/${BUCKET_NAME}" > /dev/null 2>&1; then - # Alternative: create via filer API curl -X PUT "http://localhost:8888/buckets/${BUCKET_NAME}" > /dev/null 2>&1 || true fi echo -e "${GREEN}โœ“ Bucket ready${NC}" @@ -116,7 +110,7 @@ docker build -t p1ctos4ve-backend:latest -f apps/backend/Dockerfile . echo -e "${YELLOW}๐Ÿ—„๏ธ Step 3: Running database migrations...${NC}" docker run --rm \ --network "${NETWORK_NAME}" \ - -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@host.docker.internal:5432/p1ctos4ve" \ + -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@postgres:5432/p1ctos4ve" \ -e REDIS_URL="redis://redis:6379" \ -e S3_ENDPOINT="http://seaweedfs-s3:8333" \ -e S3_REGION="us-east-1" \ @@ -124,7 +118,7 @@ docker run --rm \ -e S3_ACCESS_KEY_ID="any" \ -e S3_SECRET_ACCESS_KEY="any" \ -e S3_FORCE_PATH_STYLE="true" \ - -e BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \ + -e BETTER_AUTH_SECRET="$(openssl rand-hex 32 2>/dev/null || openssl rand -hex 32)" \ -e BETTER_AUTH_URL="http://localhost:3000" \ -e BASE_URL="http://localhost:3000" \ -e NODE_ENV="production" \ @@ -136,15 +130,15 @@ docker run --rm \ echo -e "${YELLOW}๐Ÿงช Step 4: Running unit tests...${NC}" docker run --rm \ --network "${NETWORK_NAME}" \ - -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@host.docker.internal:5432/p1ctos4ve" \ - -e REDIS_URL="redis://host.docker.internal:6379" \ - -e S3_ENDPOINT="http://host.docker.internal:8333" \ + -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@postgres:5432/p1ctos4ve" \ + -e REDIS_URL="redis://redis:6379" \ + -e S3_ENDPOINT="http://seaweedfs-s3:8333" \ -e S3_REGION="us-east-1" \ -e S3_BUCKET="${BUCKET_NAME}" \ -e S3_ACCESS_KEY_ID="any" \ -e S3_SECRET_ACCESS_KEY="any" \ -e S3_FORCE_PATH_STYLE="true" \ - -e BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \ + -e BETTER_AUTH_SECRET="$(openssl rand-hex 32 2>/dev/null || openssl rand -hex 32)" \ -e BETTER_AUTH_URL="http://localhost:3000" \ -e BASE_URL="http://localhost:3000" \ -e NODE_ENV="test" \ @@ -152,7 +146,9 @@ docker run --rm \ p1ctos4ve-backend:latest \ bun test src/tests/unit/ -if [ $? -eq 0 ]; then +UNIT_EXIT_CODE=$? + +if [ $UNIT_EXIT_CODE -eq 0 ]; then echo -e "${GREEN}โœ“ Unit tests passed${NC}" else echo -e "${RED}โœ— Unit tests failed${NC}" @@ -162,21 +158,20 @@ fi # Step 5: Run integration tests echo -e "${YELLOW}๐Ÿ”— Step 5: Running integration tests...${NC}" -# Start backend server in background for e2e tests echo -e "${YELLOW}Starting backend server for e2e tests...${NC}" BACKEND_CONTAINER=$(docker run -d \ --name p1ctos4ve-backend-test \ --network "${NETWORK_NAME}" \ -p 3000:3000 \ - -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@host.docker.internal:5432/p1ctos4ve" \ - -e REDIS_URL="redis://host.docker.internal:6379" \ - -e S3_ENDPOINT="http://host.docker.internal:8333" \ + -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@postgres:5432/p1ctos4ve" \ + -e REDIS_URL="redis://redis:6379" \ + -e S3_ENDPOINT="http://seaweedfs-s3:8333" \ -e S3_REGION="us-east-1" \ -e S3_BUCKET="${BUCKET_NAME}" \ -e S3_ACCESS_KEY_ID="any" \ -e S3_SECRET_ACCESS_KEY="any" \ -e S3_FORCE_PATH_STYLE="true" \ - -e BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \ + -e BETTER_AUTH_SECRET="$(openssl rand-hex 32 2>/dev/null || openssl rand -hex 32)" \ -e BETTER_AUTH_URL="http://localhost:3000" \ -e BASE_URL="http://localhost:3000" \ -e NODE_ENV="test" \ @@ -194,20 +189,19 @@ for i in {1..30}; do done echo -e " ${GREEN}โœ“${NC}" -# Run e2e tests in the same container (server runs in background via exec) -# This way tests can access localhost:3000 +# Try e2e tests inside backend container docker exec p1ctos4ve-backend-test sh -c "bun test --preload ./src/tests/setup.ts src/tests/e2e/" || \ docker run --rm \ --network "${NETWORK_NAME}" \ - -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@host.docker.internal:5432/p1ctos4ve" \ - -e REDIS_URL="redis://host.docker.internal:6379" \ - -e S3_ENDPOINT="http://host.docker.internal:8333" \ + -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@postgres:5432/p1ctos4ve" \ + -e REDIS_URL="redis://redis:6379" \ + -e S3_ENDPOINT="http://seaweedfs-s3:8333" \ -e S3_REGION="us-east-1" \ -e S3_BUCKET="${BUCKET_NAME}" \ -e S3_ACCESS_KEY_ID="any" \ -e S3_SECRET_ACCESS_KEY="any" \ -e S3_FORCE_PATH_STYLE="true" \ - -e BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \ + -e BETTER_AUTH_SECRET="$(openssl rand-hex 32 2>/dev/null || openssl rand -hex 32)" \ -e BETTER_AUTH_URL="http://p1ctos4ve-backend-test:3000" \ -e BASE_URL="http://p1ctos4ve-backend-test:3000" \ -e NODE_ENV="test" \ @@ -217,7 +211,6 @@ docker run --rm \ TEST_EXIT_CODE=$? -# Stop and remove backend test container echo -e "${YELLOW}Stopping test backend server...${NC}" docker stop p1ctos4ve-backend-test > /dev/null 2>&1 || true docker rm p1ctos4ve-backend-test > /dev/null 2>&1 || true @@ -237,18 +230,17 @@ echo -e "${YELLOW}Press Ctrl+C to stop${NC}" docker run --rm \ --network "${NETWORK_NAME}" \ -p 3000:3000 \ - -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@host.docker.internal:5432/p1ctos4ve" \ - -e REDIS_URL="redis://host.docker.internal:6379" \ - -e S3_ENDPOINT="http://host.docker.internal:8333" \ + -e DATABASE_URL="postgresql://p1ctos4ve:p1ctos4ve_password@postgres:5432/p1ctos4ve" \ + -e REDIS_URL="redis://redis:6379" \ + -e S3_ENDPOINT="http://seaweedfs-s3:8333" \ -e S3_REGION="us-east-1" \ -e S3_BUCKET="${BUCKET_NAME}" \ -e S3_ACCESS_KEY_ID="any" \ -e S3_SECRET_ACCESS_KEY="any" \ -e S3_FORCE_PATH_STYLE="true" \ - -e BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \ + -e BETTER_AUTH_SECRET="$(openssl rand-hex 32 2>/dev/null || openssl rand -hex 32)" \ -e BETTER_AUTH_URL="http://localhost:3000" \ -e BASE_URL="http://localhost:3000" \ -e NODE_ENV="production" \ -e PORT="3000" \ p1ctos4ve-backend:latest -