# =============================================================================
# Android E2E - integration tests (pre-publish)
# =============================================================================
#
# Stage: RC-E2E in the RC release pipeline.
#
# Runs the .af-e2e/test-plan.json scenarios against the plugin source
# (example linked to the plugin). Drives the unified
# scripts/af-scenario-runner.sh on an Android emulator via
# reactivecircus/android-emulator-runner (KVM on ubuntu-latest).
#
# Triggers:
#   - workflow_call from release.yml (the gate before publish-to-npm)
#   - workflow_dispatch for manual reruns
#   - Weekly cron (Sunday 03:00 UTC) to catch SDK / RN drift
#
# Reports land in .af-e2e/reports/ and are uploaded as android-e2e-<run>
# artifacts.
# =============================================================================

name: Android E2E

on:
  workflow_call:
    inputs:
      ref:
        description: 'Branch, tag, or SHA to check out. Defaults to the calling workflow''s ref.'
        required: false
        type: string
        default: ''
  workflow_dispatch:
    inputs:
      ref:
        description: 'Branch, tag, or SHA to check out (default: workflow ref)'
        required: false
        type: string
        default: ''
  schedule:
    - cron: '0 3 * * 0'

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

jobs:
  e2e-android:
    name: E2E Tests (Android)
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          ref: ${{ inputs.ref != '' && inputs.ref || github.ref }}

      # Some ubuntu-latest runner images ship /dev/kvm owned by group kvm
      # without adding the runner user to that group. The udev rule below
      # grants 0666 perms so android-emulator-runner uses -accel kvm.
      - name: Enable KVM group perms
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' \
            | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm
          ls -l /dev/kvm

      - name: Setup Java
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: 'gradle'

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install example app dependencies
        working-directory: example
        run: npm install

      - name: Write example app .env
        env:
          ENV_FILE: ${{ secrets.ENV_FILE }}
        run: |
          echo "$ENV_FILE" | base64 -d > example/.env
          echo "::group::.env keys (values redacted)"
          sed 's/=.*/=***/' example/.env
          echo "::endgroup::"

      - name: Build and run E2E on Android emulator
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 33
          arch: x86_64
          profile: pixel_6
          # -dns-server bypasses the host's systemd-resolved stub at 127.0.0.53,
          # which the emulator's network namespace can't reach. Without this,
          # every AppsFlyer SDK HTTP call fails with Unable to resolve host.
          emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -dns-server 8.8.8.8,1.1.1.1
          disable-animations: true
          script: |
            set -e
            adb shell 'getprop net.dns1; getprop net.dns2' || true
            adb shell 'nslookup conversions.appsflyersdk.com 2>&1 | head -5' || { sleep 10; adb shell 'nslookup conversions.appsflyersdk.com 2>&1 | head -5'; } || { echo "::error::Emulator DNS cannot resolve AppsFlyer hosts"; exit 1; }
            cd example/android && ./gradlew assembleDebug && cd ../..
            bash ./scripts/af-scenario-runner.sh --platform android --plan .af-e2e/test-plan.json

      - name: Upload E2E reports
        if: always()
        uses: actions/upload-artifact@v5
        with:
          name: android-e2e-${{ github.run_number }}
          path: .af-e2e/reports/
          retention-days: 30
