name: Updates e2e

on:
  workflow_dispatch: {}
  pull_request:
    paths:
      - .github/workflows/updates-e2e.yml
      - packages/expo-json-utils/**
      - packages/expo-manifests/**
      - packages/expo-modules-core/**
      - packages/expo-structured-headers/**
      - packages/expo-updates-interface/**
      - packages/expo-updates/**
      - packages/expo/android/**
      - packages/expo/ios/**
      - templates/expo-template-bare-minimum/**
  push:
    branches: [main, 'sdk-*']
    paths:
      - .github/workflows/updates-e2e.yml
      - packages/expo-json-utils/**
      - packages/expo-manifests/**
      - packages/expo-modules-core/**
      - packages/expo-structured-headers/**
      - packages/expo-updates-interface/**
      - packages/expo-updates/**
      - packages/expo/android/**
      - packages/expo/ios/**
      - templates/expo-template-bare-minimum/**

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

jobs:
  # (2022-05): we run both iOS and Android tests in a single job in order to avoid taking up 2 macOS
  # runners each time this workflow runs
  build:
    runs-on: macos-11
    timeout-minutes: 120
    env:
      UPDATES_PORT: 4747
    strategy:
      matrix:
        api-level: [31]
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
      - name: ⬢ Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: '16'
      - name: 🔨 Switch to Xcode 13.2.1
        run: sudo xcode-select --switch /Applications/Xcode_13.2.1.app
      - name: 🔨 Use JDK 11
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '11'
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(yarn global bin)" >> $GITHUB_PATH
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          avd: 'true'
          avd-api: ${{ matrix.api-level }}
          react-native-gradle-downloads: 'true'
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: 🛠 Build local Expo CLI
        working-directory: packages/@expo/cli
        run: yarn prepare
      - name: Build expo-updates CLI
        working-directory: packages/expo-updates
        run: yarn build:cli
      - name: 🔧 Install Expo CLI
        run: yarn global add expo-cli
      - name: 🌳 Add EXPO_REPO_ROOT to environment
        run: echo "EXPO_REPO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
      - name: 🌐 Get local IP address
        run: echo "UPDATES_HOST=$(ifconfig -l | xargs -n1 ipconfig getifaddr)" >> $GITHUB_ENV
      - name: 📦 Get artifacts path
        run: mkdir -p artifact && echo "ARTIFACTS_DEST=$(pwd)/artifact" >> $GITHUB_ENV
      - name: 🚀 Setup test project and build
        run: node packages/expo-updates/e2e/__tests__/setup/index.js
      - name: Upload test project artifacts
        uses: actions/upload-artifact@v3
        with:
          name: updates-e2e-artifacts
          path: artifact
      - name: 🍏 Start iOS simulator
        run: |
          xcrun simctl list devices -j \
          | jq -rc '[ .[] | .[] | .[] | select( .name | contains( "iPhone" ) ) | select( .isAvailable == true ) ] | last.udid ' \
          | xargs open -a Simulator --args -CurrentDeviceUDID
      - name: 🍏 Run iOS tests
        timeout-minutes: 30
        working-directory: packages/expo-updates
        run: yarn test --config e2e/jest.config.ios.js --runInBand
      - name: 🤖 Start Android emulator and run tests
        timeout-minutes: 30
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: ${{ matrix.api-level }}
          avd-name: avd-${{ matrix.api-level }}
          arch: x86_64
          force-avd-creation: false
          script: yarn test --config e2e/jest.config.android.js --runInBand
          working-directory: packages/expo-updates
