name: 'Build & Test'

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  javascript_unit_tests:
    name: Unit tests - javascript
    runs-on: macos-latest
    timeout-minutes: 5

    steps:
      - name: Checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      - name: Node
        uses: actions/setup-node@v1

      - name: Install npm dependencies
        run: |
          yarn install --frozen-lockfile

      - name: Run unit tests
        run: |
          yarn test

  java_unit_tests:
    name: Unit tests - java
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Install npm dependencies
        working-directory: ./examples/detox
        run: |
          yarn install --frozen-lockfile

      - name: Run unit tests
        working-directory: ./examples/detox/android
        run: ./gradlew testDebugUnitTest

  end_to_end_tests:
    name: End to end tests
    runs-on: macos-latest
    timeout-minutes: 60

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 1

      - name: Node
        uses: actions/setup-node@v2

      - name: Use specific Java version for sdkmanager to work
        uses: joschi/setup-jdk@v2
        with:
          java-version: 'openjdk8'
          architecture: 'x64'

      - name: Install npm dependencies
        working-directory: ./examples/detox
        run: |
          yarn install --frozen-lockfile --network-timeout 60000

      - name: Download Android Emulator Image
        timeout-minutes: 15
        run: |
          echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install "system-images;android-29;google_apis;x86"
          echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd --force --name emu --device "Nexus 5X" -k 'system-images;android-29;google_apis;x86'
          $ANDROID_HOME/emulator/emulator -list-avds

      - name: Build
        working-directory: ./examples/detox
        run: |
          yarn build:android-ci

      - name: Start android emulator
        timeout-minutes: 5
        working-directory: ./examples/detox
        continue-on-error: true
        run: |
          echo "Starting emulator"
          nohup $ANDROID_HOME/emulator/emulator -avd emu -no-audio -no-snapshot -no-window &
          $ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'
          $ANDROID_HOME/platform-tools/adb devices
          echo "Emulator started"

      - name: Run e2e tests
        timeout-minutes: 30
        working-directory: ./examples/detox
        run: yarn start & yarn test:android-ci

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: Failing tests
          path: ./examples/detox/artifacts
