name: Run Tests
on:
  push:
  pull_request:
  workflow_dispatch:
    inputs:
      extended_tests:
        description: 'Extended tests'
        required: false
        default: 'false'
        type: choice
        options:
          - 'false'
          - 'true'

concurrency:
  group: build-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-24.04
    steps:
      - name: setup jdk
        uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: 'temurin'
      - uses: actions/checkout@v4
      - uses: gradle/actions/wrapper-validation@v4
      - run: ./gradlew build check -x test --stacktrace --warning-mode fail
      - uses: Juuxel/publish-checkstyle-report@v1
        if: ${{ failure() }}
        with:
          reports: |
            build/reports/checkstyle/*.xml

  build_windows:
    if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.extended_tests == 'true' }}

    runs-on: windows-2022
    steps:
      - uses: actions/checkout@v4
      - name: setup jdk
        uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: 'temurin'
      - uses: gradle/actions/wrapper-validation@v4
      - run: ./gradlew build check -x test --stacktrace --warning-mode fail

  # This job is used to feed the test matrix of next job to allow the tests to run in parallel
  prepare_test_matrix:
    # Lets wait to ensure it builds before going running tests
    needs: build

    runs-on: ubuntu-24.04

    steps:
      - name: setup jdk
        uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: 'temurin'
      - uses: actions/checkout@v4
      - run: ./gradlew writeActionsTestMatrix --stacktrace --warning-mode fail
      -
        id: set-matrix
        run: echo "matrix=$(cat build/test_matrix.json)" >> $GITHUB_OUTPUT

    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}

  run_tests:
    needs: prepare_test_matrix

    strategy:
      fail-fast: false
      matrix:
        test: ${{ fromJson(needs.prepare_test_matrix.outputs.matrix) }}

    runs-on: ubuntu-24.04

    steps:
      - uses: actions/checkout@v4
      - name: setup jdk
        uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: 'temurin'
      - run: ./gradlew printActionsTestName --name="${{ matrix.test }}" test --tests ${{ matrix.test }} --stacktrace --warning-mode fail
        env:
          TEST_WARNING_MODE: fail
          EXTENDED_TESTS: ${{ github.event.inputs.extended_tests }}
        id: test

      - uses: actions/upload-artifact@v4
        if: ${{ failure() }}
        with:
          name: ${{ steps.test.outputs.test }} Results
          path: build/reports/
      - uses: actions/upload-artifact@v4
        if: ${{ failure() }}
        with:
          name: ${{ steps.test.outputs.test }} Heap Dump
          path: "*.hprof"

  run_tests_windows:
    if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.extended_tests == 'true' }}

    needs: prepare_test_matrix

    strategy:
      fail-fast: false
      matrix:
        test: ${{ fromJson(needs.prepare_test_matrix.outputs.matrix) }}

    runs-on: windows-2025

    steps:
      - uses: actions/checkout@v4
      - name: setup jdk
        uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: 'temurin'
      - run: ./gradlew printActionsTestName --name="${{ matrix.test }}" test --tests ${{ matrix.test }} --stacktrace --warning-mode fail
        env:
          TEST_WARNING_MODE: fail
        id: test

      - uses: actions/upload-artifact@v4
        if: ${{ failure() }}
        with:
          name: ${{ steps.test.outputs.test }} (${{ matrix.java }}) Results (Windows)
          path: build/reports/
      - uses: actions/upload-artifact@v4
        if: ${{ failure() }}
        with:
          name: ${{ steps.test.outputs.test }} Heap Dump (Windows)
          path: "*.hprof"

  # Special case this test to run across all os's
  reproducible_build_test:
    needs: build

    strategy:
      fail-fast: false
      matrix:
        java: [ 21 ]
        os: [ windows-2025, ubuntu-24.04, macos-15 ]

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          java-version: ${{ matrix.java }}
          distribution: 'temurin'

      - run: ./gradlew test --tests *ReproducibleBuildTest --stacktrace --warning-mode fail

      - uses: actions/upload-artifact@v4
        if: ${{ failure() }}
        with:
          name: Reproducible Build ${{ matrix.os }} (${{ matrix.java }}) Results
          path: build/reports/
