name: Coverage
on:
  workflow_call:

jobs:
  BuildAndTestAndCoverage:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Fetch tags
        run: git fetch --tags -p
      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: gradle
      - uses: gradle/actions/setup-gradle@v3
      #- uses: android-actions/setup-android@v3
      - name: Configure gradle...
        run: mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=true" >> ~/.gradle/gradle.properties
      - uses: KengoTODA/actions-setup-docker-compose@v1
        with:
          version: "2.23.3"
      - name: run selfoss
        run: |
          docker compose -f .gitea/workflows/assets/docker-compose.yml up -d

      - name: AVD cache
        uses: actions/cache@v4
        id: avd-cache
        with:
          path: |
            ~/.android/avd/*
            ~/.android/adb*
          key: avd-35

      - name: create AVD and generate snapshot for caching
        if: steps.avd-cache.outputs.cache-hit != 'true'
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 35
          force-avd-creation: false
          emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
          disable-animations: false
          script: echo "Generated AVD snapshot for caching."

      - name: run tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 35
          force-avd-creation: false
          emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
          disable-animations: true
          script: ./gradlew androidApp:connectedAndroidTest
      - uses: actions/upload-artifact@v3
        with:
          name: coverage-espresso
          path: build/reports/coverage/androidTest/githubConfig/debug/connected
          retention-days: 1
          overwrite: true
          include-hidden-files: true
      - name: Clean
        if: always()
        run: |
          docker compose -f .gitea/workflows/assets/docker-compose.yml stop