name: Reusable - Test latest deps

on:
  workflow_call:
    inputs:
      cache-read-only:
        type: boolean
        required: false
      no-build-cache:
        type: boolean
        required: false
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY:
        required: false
      GE_CACHE_USERNAME:
        required: false
      GE_CACHE_PASSWORD:
        required: false

permissions:
  contents: read

jobs:
  test-indy:
    name: testIndy${{ matrix.test-partition }}
    if: ${{ contains(github.event.pull_request.labels.*.name, 'test indy') }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        test-partition:
          - 0
          - 1
          - 2
          - 3
      fail-fast: false
    steps:
      - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2

      - name: Free disk space
        run: .github/scripts/gha-free-disk-space.sh

      - name: Set up JDK for running Gradle
        uses: actions/setup-java@9704b39bf258b59bc04b50fa2dd55e9ed76b47a8 # v4.1.0
        with:
          distribution: temurin
          java-version: 17.0.6

      - name: Increase gradle daemon heap size
        run: |
          sed -i "s/org.gradle.jvmargs=/org.gradle.jvmargs=-Xmx3g /" gradle.properties

      # vaadin 14 tests fail with node 18
      - name: Set up Node
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: 16

      # vaadin tests use pnpm
      - name: Cache pnpm modules
        uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
        with:
          path: ~/.pnpm-store
          key: ${{ runner.os }}-test-latest-cache-pnpm-modules

      - name: List tests
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
          GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
          GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}
        uses: gradle/gradle-build-action@29c0906b64b8fc82467890bfb7a0a7ef34bda89e # v3.1.0
        with:
          arguments: >
            check -x spotlessCheck
            listTestsInPartition
            -PtestPartition=${{ matrix.test-partition }}
          cache-read-only: true

      - name: Set test tasks
        run: |
          echo "test-tasks=$(cat test-tasks.txt | xargs echo | sed 's/\n/ /g')" >> $GITHUB_ENV

      - name: Test
        uses: gradle/gradle-build-action@29c0906b64b8fc82467890bfb7a0a7ef34bda89e # v3.1.0
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
          GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
          GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}
        with:
          arguments: >
            ${{ env.test-tasks }}
            -PtestIndy=true
            ${{ inputs.no-build-cache && ' --no-build-cache' || '' }}
          cache-read-only: ${{ inputs.cache-read-only }}
          # gradle enterprise is used for the build cache
          gradle-home-cache-excludes: caches/build-cache-1
