name: coordinator-testing

permissions:
  contents: read
  actions: read

on:
  workflow_call:
    inputs:
      commit_tag:
        required: true
        type: string
    secrets:
      DOCKERHUB_USERNAME:
        required: false
      DOCKERHUB_TOKEN:
        required: false

concurrency:
  group: coordinator-testing-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  run-tests:
    env:
      COMMIT_TAG: ${{ inputs.commit_tag }}
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
      DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
    # ? Seems to fail more often on xl
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-large
    name: Coordinator tests
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b #v4.5.0
        with:
          distribution: temurin
          java-version: 21
      - name: Setup Gradle
        # Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
        # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
        uses: gradle/actions/setup-gradle@8379f6a1328ee0e06e2bb424dadb7b159856a326 # 4.4.0
      # Install pnpm to compile smart contracts
      - name: Setup nodejs environment
        uses: ./.github/actions/setup-nodejs
        with:
          pnpm-install-options: '--frozen-lockfile --prefer-offline --filter contracts --ignore-scripts'
      - name: Build coordinator and Unit tests
        run: |
          ./gradlew -V coordinator:app:buildNeeded
      - name: Login to Docker Hub
        if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
        uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 #v3.4.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Run integration tests
        timeout-minutes: 15
        run: |
          ./gradlew coordinator:app:integrationTestAllNeeded
      - name: Run Jacoco
        run: |
          ./gradlew jacocoRootReport
      - name: Upload Jacoco test coverage report
        uses: actions/upload-artifact@v4
        with:
          name: jacocoRootReport-${{ env.COMMIT_TAG }}.xml
          if-no-files-found: error
          path: |
            ${{ github.workspace }}/build/reports/jacoco/jacocoRootReport/jacocoRootReport.xml
      - name: Upload coverage to Codecov
        if: ${{ env.CODECOV_TOKEN != '' }}
        uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24 #v5.4.3
        with:
          fail_ci_if_error: true
          files: ${{ github.workspace }}/build/reports/jacoco/jacocoRootReport/jacocoRootReport.xml
          flags: kotlin
          os: linux
          name: codecov-coordinator
          verbose: true
          token: ${{ secrets.CODECOV_TOKEN }}
