name: Flutter Integration Tests

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  drive_android:
    name: Android
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: read
    strategy:
      matrix:
        api-level: [29]
      fail-fast: false
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5.2.0
        with:
          distribution: "temurin"
          java-version: "17"
      - name: Enable KVM
        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
      - uses: subosito/flutter-action@fd55f4c5af5b953cc57a2be44cb082c8f6635e8e # v2.21.0
        with:
          channel: stable
          cache: true
      - uses: reactivecircus/android-emulator-runner@b530d96654c385303d652368551fb075bc2f0b6b # v2.35.0
        with:
          api-level: ${{ matrix.api-level }}
          arch: x86_64
          script: flutter test integration_test -d android
          working-directory: example

  drive_ios:
    name: iOS
    runs-on: macos-latest
    timeout-minutes: 20
    permissions:
      contents: read
    strategy:
      matrix:
        simulator: [{os: "18.6", model: "iPhone SE (3rd generation)"}]
      fail-fast: false
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: subosito/flutter-action@fd55f4c5af5b953cc57a2be44cb082c8f6635e8e # v2.21.0
        with:
          channel: stable
          cache: true
      - uses: futureware-tech/simulator-action@dab10d813144ef59b48d401cd95da151222ef8cd # v4
        with:
          wait_for_boot: true
          os: iOS
          os_version: ${{ matrix.simulator.os }}
          model: ${{ matrix.simulator.model }}
      - name: Pre-build the app
        working-directory: example
        run: flutter build ios --simulator --target=integration_test/app_test.dart
      - name: Run flutter integration tests
        working-directory: example
        run: flutter test integration_test -d iphone

  drive_macos:
    name: macOS
    runs-on: macos-latest
    timeout-minutes: 15
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: subosito/flutter-action@fd55f4c5af5b953cc57a2be44cb082c8f6635e8e # v2.21.0
        with:
          channel: stable
          cache: true
      - name: Run Flutter Integration tests
        working-directory: example
        run: flutter test integration_test -d macos

  drive_web:
    name: Web
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: subosito/flutter-action@fd55f4c5af5b953cc57a2be44cb082c8f6635e8e # v2.21.0
        with:
          channel: stable
          cache: true
      - name: Setup chromedriver
        run: |
          npx @puppeteer/browsers install chromedriver@stable
          chromedriver --port=4444 &
      - name: Run Flutter Integration tests
        working-directory: example
        run: flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d web-server \
