name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  android:
    name: Android Unit Tests & Lint & Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Set up Android SDK
        uses: android-actions/setup-android@v3

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4

      - name: Run Unit Tests
        run: ./gradlew --no-daemon testDebugUnitTest

      - name: Run Android Lint
        run: ./gradlew --no-daemon lint

      - name: Assemble Debug APK
        run: ./gradlew --no-daemon assembleDebug

      - name: Upload Test Reports
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: unit-test-reports
          path: app/build/reports/tests/

      - name: Upload Lint Reports
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: lint-reports
          path: app/build/reports/lint-results*

  rust:
    name: Rust hermes-pair Tests & Clippy
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: hermes-pair
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: hermes-pair -> hermes-pair/target

      - name: Run Cargo Tests
        run: cargo test --all-targets

      - name: Run Cargo Clippy
        run: cargo clippy -- -D warnings

  instrumented:
    name: Android Instrumented Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4

      - name: Enable KVM group perms
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm-rules.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

      - name: Run Instrumented Tests via Android Emulator Runner
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 29
          arch: x86_64
          target: default
          disable-animations: true
          script: ./gradlew --no-daemon connectedDebugAndroidTest

      - name: Upload Instrumented Test Reports
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: android-test-reports
          path: app/build/reports/androidTests/
