version: '3.8'

services:
  postgres-test:
    image: postgres:16-alpine
    container_name: admin-service-postgres-test
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: admin_service_test
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
    volumes:
      - postgres-test-data:/var/lib/postgresql/data

  admin-service-test:
    build:
      context: .
      dockerfile: Dockerfile.test
    container_name: admin-service-test
    depends_on:
      postgres-test:
        condition: service_healthy
    environment:
      NODE_ENV: test
      DATABASE_URL: postgresql://postgres:password@postgres-test:5432/admin_service_test?schema=public
      JWT_SECRET: test-jwt-secret
      JWT_EXPIRES_IN: 7d
    volumes:
      - ./coverage:/app/coverage
    command: >
      sh -c "
        echo 'Waiting for database...' &&
        sleep 5 &&
        echo 'Running migrations...' &&
        npx prisma migrate deploy &&
        echo 'Running tests...' &&
        npm test
      "

volumes:
  postgres-test-data:
