name: Run Maestro Tests

on:
  pull_request:

jobs:
  build-android:
    runs-on: macos-15
    outputs:
      version: ${{ steps.setver.outputs.version }}
    steps:
      - name: 🛒 Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.SIGNING_REPO_PAT }}
      
      - name: 🖥 Setup Node 20
        uses: actions/setup-node@v4
        with:
          node-version: 20
      
      - name: 💎 Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
            ruby-version: '3.0'  
            bundler-cache: true

      - name: 🍎 Run yarn init-android
        run: yarn install --network-concurrency 1

      - name: 💬 Disable OTA Updates and Enable Maestro Build
        run: node scripts/updateEnv.js OTA_UPDATE_ENABLED=false IS_MAESTRO_BUILD=true
      
      - name: 🤫 Output TelemetryDeck Secrets to TelemetryDeck.json          
        run: |
          echo "{" > telemetrydeck.json
          echo "\"appID\": \"${{ secrets.TELEMETRYDECK_APPID }}\"," >> telemetrydeck.json
          echo "\"clientUser\": \"anonymous\"," >> telemetrydeck.json
          echo "\"app\": \"Jellify\"" >> telemetrydeck.json
          echo "}" >> telemetrydeck.json

          
      - name: 🤫 Output Glitchtip Secrets to Glitchtip.json
        run: |
          echo "{" > glitchtip.json
          echo "\"dsn\": \"${{ secrets.GLITCHTIP_DSN }}\"" >> glitchtip.json
          echo "}" >> glitchtip.json
      
      - name: ✅ Validate Config Files
        run: |
          node -e "JSON.parse(require('fs').readFileSync('telemetrydeck.json'))"
          node -e "JSON.parse(require('fs').readFileSync('glitchtip.json'))"
    
      - name: 🚀 Run Android fastlane build
        run: yarn fastlane:android:build
      
      - name: 📤 Upload Android Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: android-artifacts
          path: ./android/app/build/outputs/apk/release/*.apk

  run-maestro-tests:
    runs-on: ubuntu-latest
    needs: build-android
    env:
      JELLYFIN_TEST_ADDRESS: ${{ secrets.JELLYFIN_TEST_URL }}
      JELLYFIN_TEST_USERNAME: ${{ secrets.JELLYFIN_TEST_USERNAME }}
      JELLYFIN_TEST_PASSWORD: ${{ secrets.JELLYFIN_TEST_PASSWORD }}

    steps:
      - name: 🛒 Checkout
        uses: actions/checkout@v4

      - name: 🖥 Setup Node 20
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Installing Maestro
        shell: bash
        run: export MAESTRO_VERSION=1.40.0; curl -Ls "https://get.maestro.mobile.dev" | bash

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

      - name: 💎 Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
            ruby-version: '3.0'  
            bundler-cache: true
            
      - name: ⬇️ Download Android Artifacts
        uses: actions/download-artifact@v4
        with:
          name: android-artifacts
          path: artifacts/

      - name: Enable KVM group perms
        shell: bash
        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

      - name: Run e2e tests
        id: run-tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: '24'
          arch: x86
          ram-size: '8192M'
          heap-size: '4096M'
          disk-size: '10G'
          cores: '4'
          disable-animations: false
          avd-name: e2e_emulator
          script: |
            node scripts/maestro-android.js ${{ env.JELLYFIN_TEST_ADDRESS }} ${{ env.JELLYFIN_TEST_USERNAME }} ${{ env.JELLYFIN_TEST_PASSWORD }}
      - name: Store tests result
        uses: actions/upload-artifact@v4.3.4
        if: always()
        with:
          name: TestResult
          path: |
            video.mp4
