# GMD smoke tests on CI.
#
# GitHub-hosted `ubuntu-latest` runners expose `/dev/kvm` for CPU acceleration
# (the "Enable KVM" step installs a world-writable `0666` udev rule, which is
# a common approach when running GMD tests on ephemeral Ubuntu runners to
# ensure the emulator process has necessary permissions).
# However, they have no GPU/Vulkan driver — so the GPU mode is forced to
# `swiftshader_indirect`. Note: swiftshader is documented to crash on Linux
# kernel >= 6.17 (see TESTING_SETUP.md). GitHub-hosted runners are currently
# on kernel ~6.8, but if that changes this workflow will need a different
# harness.
#
# To keep Free-tier Actions minutes in budget, this workflow does not auto-trigger.
# It runs only when the `smoke` label is added to a PR (via smoke-label.yml) or
# when manually invoked from the Actions tab (workflow_dispatch).
name: Smoke
permissions:
  contents: read

on:
  workflow_dispatch:
  workflow_call:

jobs:
  smoke:
    name: GMD Smoke Tests
    runs-on: ubuntu-latest
    timeout-minutes: 45
    concurrency:
      group: smoke-${{ github.ref }}
      cancel-in-progress: true
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v6
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/master' }}
          cache-cleanup: on-success

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - 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

      - name: Run GMD Smoke Tests
        run: ./gradlew pixel2api33Check -Pandroid.testoptions.manageddevices.emulator.gpu=swiftshader_indirect

      - name: Upload Managed Device Reports
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: gmd-test-reports
          path: |
            app/build/reports/androidTests/managedDevice/
            app/build/outputs/androidTest-results/managedDevice/
            app/build/outputs/managed_device_android_test_additional_output/
          if-no-files-found: warn
          retention-days: 7
