name: detox-android

on:
  pull_request:
    types:
      - opened
      - reopened
      - labeled
      - unlabeled
      - synchronize

jobs:
  android:
    # only run this CI with github label match to the workflow name
    if: contains(github.event.pull_request.labels.*.name, github.workflow)
    concurrency:
      group: ci-android-${{ github.head_ref }}
      cancel-in-progress: true
    runs-on: ${{ matrix.runners }}
    strategy:
      matrix:
        runners: [[self-hosted, macOS, macos-selfhosted-14-sh, aws]]
    name: Detox Android E2E Testing
    timeout-minutes: 60

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Common detox android setup
        uses: mattrinternal/sre-reusable-action/actions/common-detox-android-setup@master

      - name: Build project
        run: |
          yarn install --frozen-lockfile
          yarn build

      - name: Install Example Dependencies
        working-directory: ./example
        run: yarn install --frozen-lockfile

      - name: Detox Build Android Configuration
        working-directory: ./example
        run: yarn detox:setup && yarn detox:build:android

      - name: Prepare test runner script
        id: script
        env:
          SCRIPT: |
            java --version
            cd ./example
            pwd
            yarn detox:test:android --maxWorkers 1 --retries 3 --cleanup --headless --loglevel warn
        run: |
          script_file="/tmp/react-native-cryptography-android-emulator-runner-prepared-script.sh"
          echo "${SCRIPT}" > "${script_file}"
          echo "file=${script_file}" >> "${GITHUB_OUTPUT}"
      - name: Common detox android test
        uses: mattrinternal/sre-reusable-action/actions/common-detox-android-test@master
        with:
          SCRIPT: ${{ steps.script.outputs.file }}

      - name: Archive detox failed test results
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: detox-artifacts-${{ github.sha }}
          include-hidden-files: true
          path: ./examples/.detox-artifacts
