name: Client Test
on:
  pull_request:
  push:

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            java: 17
            distribution: zulu
          - os: macos-latest
            java: 17
            distribution: temurin
          - os: windows-latest
            java: 17
            distribution: zulu
    runs-on: ${{ matrix.os }}
    env:
      JAVA_OPTS: -Xms800M -Xmx2G -Xss6M -XX:ReservedCodeCacheSize=128M -server -Dsbt.io.virtual=false -Dfile.encoding=UTF-8
      JVM_OPTS: -Xms800M -Xmx2G -Xss6M -XX:ReservedCodeCacheSize=128M -server -Dsbt.io.virtual=false -Dfile.encoding=UTF-8
      SBT_ETC_FILE: $HOME/etc/sbt/sbtopts
      TEST_SBT_VER: 1.11.4
    steps:
    - uses: actions/checkout@v6
    - name: Setup JDK
      uses: actions/setup-java@v5
      with:
        distribution: "zulu"
        java-version: |
          25
          17
        cache: sbt
    - uses: sbt/setup-sbt@v1
      with:
        sbt-runner-version: 1.11.5
    - name: Set up Python 3.12
      uses: actions/setup-python@v6
      with:
        python-version: 3.12
    - name: Setup Windows C++ toolchain
      uses: ilammy/msvc-dev-cmd@v1
      if: ${{ matrix.os == 'windows-latest' }}
    - name: Client test (Linux)
      if: ${{ matrix.os == 'ubuntu-latest' }}
      shell: bash
      run: |
        # test building sbtn on Linux (sbtw native-image dropped: scopt lazy vals break under Graal)
        sbt "-Dsbt.io.virtual=false" nativeImage
        # smoke test native Image
        ./client/target/bin/sbtn --sbt-script=$(pwd)/sbt about
        ./client/target/bin/sbtn --sbt-script=$(pwd)/sbt shutdown
        # test launcher script
        echo build using JDK 17 test using JDK 17 and JDK 25
        sbt -Dsbt.build.version=$TEST_SBT_VER launcherPackage/Rpm/packageBin launcherPackage/Debian/packageBin
        sbt -Dsbt.build.version=$TEST_SBT_VER launcherPackageIntegrationTest/test
        cd launcher-package/citest && ./test.sh
        JAVA_HOME="$JAVA_HOME_25_X64"
        PATH="$JAVA_HOME_25_X64/bin:$PATH"
        java -Xmx32m -version
        ./test.sh
    - name: Client test (macOS)
      if: ${{ matrix.os == 'macos-latest' }}
      shell: bash
      run: |
        # test building sbtn on macOS (sbtw native-image dropped: scopt lazy vals break under Graal)
        ./sbt "-Dsbt.io.virtual=false" nativeImage
        # test launcher script
        launcher-package/bin/coursier resolve
        sbt -Dsbt.build.version=$TEST_SBT_VER launcherPackageIntegrationTest/test
        # This fails due to the JLine issue
        # cd launcher-package/citest && ./test.sh
    - name: build image (Windows)
      if: ${{ matrix.os == 'windows-latest' }}
      uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
      with:
        timeout_minutes: 10
        max_attempts: 3
        shell: bash
        command: |
          # test building sbtn on Windows (sbtw native-image dropped: scopt lazy vals break under Graal)
          sbt "-Dsbt.io.virtual=false" nativeImage
    - name: Client test (Windows)
      if: ${{ matrix.os == 'windows-latest' }}
      shell: bash
      run: |
        # smoke test native Image (sbtn)
        ./client/target/bin/sbtn --sbt-script=$(pwd)/launcher-package/src/universal/bin/sbt.bat about
        ./client/target/bin/sbtn --sbt-script=$(pwd)/launcher-package/src/universal/bin/sbt.bat shutdown
        # test launcher script
        echo build using JDK 17 test using JDK 17 and JDK 25
        launcher-package/bin/coursier.bat resolve
        sbt -Dsbt.build.version=$TEST_SBT_VER launcherPackageIntegrationTest/test
        # integration tests with sbtw as drop-in for sbt.bat (JVM runner)
        sbt -Dsbt.build.version=$TEST_SBT_VER -Dsbt.test.useSbtw=true launcherPackageIntegrationTest/test
        cd launcher-package/citest
        ./test.bat
        test3/test3.bat
