name: staterecovery-testing

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

concurrency:
  group: staterecovery-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 }}
    # ~2.5 mins saved vs large
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-large
    name: Staterecovery 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
      - name: Staterecovery - Build and Unit tests
        run: |
          ./gradlew besu-plugins:state-recovery:besu-plugin:buildNeeded
      # Install pnpm to deploy smart contracts
      # FIXME: use web3j to deploy contracts and remove this.
      - name: Setup nodejs environment
        uses: ./.github/actions/setup-nodejs
        with:
          pnpm-install-options: '--frozen-lockfile --prefer-offline --filter contracts --ignore-scripts'
      - name: Staterecovery - Build plugin shadowJar
        run: |
          ./gradlew besu-plugins:state-recovery:besu-plugin:shadowJar
      - name: Login to Docker Hub
        if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Run integration tests
        timeout-minutes: 15
        run: |
          ./gradlew besu-plugins:state-recovery:test-cases:integrationTest
#      - name: Setup upterm session
#        if: ${{ inputs.e2e-tests-with-ssh }}
#        uses: lhotari/action-upterm@v1
#      Stack fails to start in CI - Disable for now
#      - name: Run E2E tests
#        timeout-minutes: 15
#        run: |
#          mkdir -p tmp/local/
#          chmod -R a+w tmp/local/
#          ./gradlew besu-plugins:state-recovery:test-cases:e2eTest
# TMP disable as it conflicts with coordinator report
#      - name: Run Jacoco Staterecovery
#        run: |
#          ./gradlew jacocoRootReport
#      - name: Upload Jacoco report Staterecovery
#        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 Staterecovery
#        if: ${{ env.CODECOV_TOKEN != '' }}
#        uses: codecov/codecov-action@v5
#        with:
#          fail_ci_if_error: true
#          files: ${{ github.workspace }}/build/reports/jacoco/jacocoRootReport/jacocoRootReport.xml
#          flags: kotlin
#          os: linux
#          name: codecov-staterecovery
#          verbose: true
#          token: ${{ secrets.CODECOV_TOKEN }}
