name: Test

on:
  push:
    branches:
      - "main"
      - "rc"
      - "hotfix-rc"
  pull_request:
    types: [opened, synchronize]
  merge_group:
    type: [checks_requested]
  workflow_dispatch:

env:
  _JAVA_VERSION: 17
  _GITHUB_ACTION_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/attempts/${{ github.run_attempt }}


jobs:
  test:
    name: Test
    runs-on: ubuntu-24.04
    permissions:
      packages: read

    steps:
      - name: Check out repo
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Validate Gradle wrapper
        uses: gradle/actions/wrapper-validation@0bdd871935719febd78681f197cd39af5b6e16a6 # v4.2.2

      - name: Cache Gradle files
        uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-v2-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties', '**/libs.versions.toml') }}
          restore-keys: |
            ${{ runner.os }}-gradle-v2-

      - name: Cache build output
        uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
        with:
          path: |
            ${{ github.workspace }}/build-cache
          key: ${{ runner.os }}-build-cache-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-build-

      - name: Configure Ruby
        uses: ruby/setup-ruby@28c4deda893d5a96a6b2d958c5b47fc18d65c9d3 # v1.213.0
        with:
          bundler-cache: true

      - name: Configure JDK
        uses: actions/setup-java@7a6d8a8234af8eb26422e24e3006232cccaa061b # v4.6.0
        with:
          distribution: "temurin"
          java-version: ${{ env._JAVA_VERSION }}

      - name: Install Fastlane
        run: |
          gem install bundler:2.2.27
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3

      - name: Build and test
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Used in settings.gradle.kts to download the SDK from GitHub Maven Packages
        run: |
          bundle exec fastlane check

      - name: Upload test reports
        uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
        if: always()
        with:
          name: test-reports
          path: |
            app/build/reports/tests/
            app/build/reports/kover/reportStandardDebug.xml

  report:
    name: Process Test Reports
    needs: test
    runs-on: ubuntu-24.04
    permissions:
      contents: read
      issues: write
      pull-requests: write
    if: success()

    steps:
      - name: Download test artifacts
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
        with:
          name: test-reports

      - name: Upload to codecov.io
        id: upload-to-codecov
        uses: codecov/codecov-action@1e68e06f1dbfde0e4cefc87efeba9e4643565303 # v5.1.2
        continue-on-error: true
        with:
          os: linux
          files: kover/reportStandardDebug.xml
          fail_ci_if_error: true

      - name: Comment PR if tests failed
        if: steps.upload-to-codecov.outcome == 'failure'
        env:
            PR_NUMBER: ${{ github.event.number }}
            GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            RUN_ACTOR: ${{ github.triggering_actor }}
        run: |
          echo "> [!WARNING]" >> $GITHUB_STEP_SUMMARY
          echo "> Uploading code coverage report failed. Please check the \"Upload to codecov.io\" step of \"Process Test Reports\" job for more details." >> $GITHUB_STEP_SUMMARY

          if [ ! -z "$PR_NUMBER" ]; then
            message=$'> [!WARNING]\n> @'$RUN_ACTOR' Uploading code coverage report failed. Please check the "Upload to codecov.io" step of [Process Test Reports job]('$_GITHUB_ACTION_RUN_URL') for more details.'
            gh pr comment --repo $GITHUB_REPOSITORY $PR_NUMBER --body "$message"
          fi
