name: Acceptance Tests
on: pull_request
jobs:
  acceptance-tests:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: lts/*
    - name: Run Docker Compose and Install Dependencies in Parallel
      run: |
        echo "Start docker-compose in the background"
        docker compose -f ./acceptance/docker-compose.yaml up -d &
        DOCKER_COMPOSE_PID=$!
        echo "Docker Compose PID: $DOCKER_COMPOSE_PID"
        
        echo "Install app dependencies in the foreground"
        npm install -g pnpm && pnpm install
        
        echo "Install Test Browsers in the background"
        pnpm exec playwright install --with-deps &
        INSTALL_BROWSERS_PID=$!
        echo "Install browsers PID: $INSTALL_BROWSERS_PID"

        echo "Generate gRPC stubs in the background"
        pnpm generate &
        GENERATE_PID=$!
        echo "Generate stubs PID: $GENERATE_PID"
        
        # Wait for docker-compose
        wait $DOCKER_COMPOSE_PID
        if [ $? -ne 0 ]; then
          echo "Docker Compose failed"
          exit 1
        fi

        # Wait for Playwright browser installation
        wait $INSTALL_BROWSERS_PID
        if [ $? -ne 0 ]; then
          echo "Playwright browser installation failed"
          exit 1
        fi

        # Wait for gRPC stubs generation
        wait $GENERATE_PID
        if [ $? -ne 0 ]; then
          echo "gRPC stubs generation failed"
          exit 1
        fi
        
        echo "Generate and create a production build in the foreground"
        pnpm build
    - name: Run Playwright Tests
      run: pnpm exec playwright test
    - uses: actions/upload-artifact@v4
      if: ${{ !cancelled() }}
      with:
        name: playwright-report
        path: playwright-report/
        retention-days: 30
