name: package:cronet_http CI

on:
  push:
    branches:
      - main
      - master
    paths:
      - '.github/workflows/cronet.yml'
      - 'pkgs/cronet_http/**'
      - 'pkgs/http_client_conformance_tests/**'
  pull_request:
    paths:
      - '.github/workflows/cronet.yml'
      - 'pkgs/cronet_http/**'
      - 'pkgs/http_client_conformance_tests/**'
  schedule:
    - cron: "0 0 * * 0"

env:
  PUB_ENVIRONMENT: bot.github

jobs:
  generate:
    name: jnigen
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: pkgs/cronet_http
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
        with:
          distribution: 'zulu'
          java-version: '17'
      - uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2
        with:
          channel: 'stable'
      - id: install
        name: Install dependencies
        run: flutter pub get
      - name: Build APK
        run: cd example && flutter build apk && cd ..
      - name: Upgrade
        # Set this to `true` if you are using this workflow to regenerate the jni
        # bindings.
        if: false
        run: dart pub upgrade --major-versions jnigen jni
      - name: Regenerate
        run: dart run jnigen --config jnigen.yaml
      - name: Generated Diff
        id: diff
        run: git diff --exit-code lib/src/jni/jni_bindings.dart
      - name: Upload bindings
        if: failure()
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a
        with:
          name: JNI Bindings
          path: pkgs/cronet_http/lib/src/jni/jni_bindings.dart
          retention-days: 1
  verify:
    name: Format & Analyze & Test (${{ matrix.cronetHttpNoPlay == 'true' && 'without Google Play' || 'with Google Play'}})
    runs-on: ubuntu-cpu16-ram64
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        cronetHttpNoPlay: ['false', 'true']
    defaults:
      run:
        working-directory: pkgs/cronet_http
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
        with:
          distribution: 'zulu'
          java-version: '17'
      - uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2
        with:
          channel: 'stable'
      - id: install
        name: Install dependencies
        run: flutter pub get
      - name: Check formatting
        if: always() && steps.install.outcome == 'success'
        run: dart format --output=none --set-exit-if-changed .
      - name: Analyze code
        if: always() && steps.install.outcome == 'success'
        run: flutter analyze --fatal-infos
      - 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
      - name: Run tests (${{ matrix.cronetHttpNoPlay == 'true' && 'without Google Play' || 'with Google Play'}})
        uses: reactivecircus/android-emulator-runner@e89f39f1abbbd05b1113a29cf4db69e7540cae5a
        if: always() && steps.install.outcome == 'success'
        with:
          # api-level/minSdkVersion should be help in sync in:
          # - .github/workflows/cronet.yml
          # - pkgs/cronet_http/android/build.gradle
          # - pkgs/cronet_http/example/android/app/build.gradle

          # Google Play services does not support older Android API levels;
          # as of March 2025, they supported API level 23 and higher. Instead
          # of breaking when support for API level 23 is removed, just run
          # the tests that rely on Google Play services with the newest API
          # level (34 as of March 2025). The tests that don't rely on Google
          # Play serviecs can test the oldest supported API level.
          api-level: ${{ matrix.cronetHttpNoPlay == 'true' && '24' || '29' }} 
          disable-animations: true
          arch: x86_64
          target: ${{ matrix.cronetHttpNoPlay == 'true' && 'default' || 'google_apis' }}
          script: cd pkgs/cronet_http/example && flutter test --dart-define=cronetHttpNoPlay=${{ matrix.cronetHttpNoPlay }} --timeout=1200s integration_test/
