name: TheRock Test Packages

on:
  workflow_call:
    inputs:
      project_to_test:
        type: string
      amdgpu_families:
        type: string
      test_runs_on:
        type: string
      platform:
        type: string

permissions:
  contents: read

jobs:
  configure_test_matrix:
    name: "Configure test matrix"
    runs-on: ubuntu-24.04
    if: ${{ inputs.test_runs_on != '' }}
    outputs:
      components: ${{ steps.configure.outputs.components }}
    steps:
      - name: "Checking out repository"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          repository: "ROCm/TheRock"

      - name: "Configuring CI options"
        env:
          PLATFORM: ${{ inputs.platform }}
          project_to_test: ${{ inputs.project_to_test }}
        id: configure
        run: python ./build_tools/github_actions/fetch_test_configurations.py

  test_components:
    name: 'Test ${{ matrix.components.job_name }}'
    runs-on: ${{ inputs.test_runs_on }}
    needs: configure_test_matrix
    # skip tests if no test matrix to run
    if: ${{ needs.configure_test_matrix.outputs.components != '[]' }}
    strategy:
      fail-fast: false
      matrix:
        components: ${{ fromJSON(needs.configure_test_matrix.outputs.components) }}
    defaults:
      run:
        shell: bash
    env:
      VENV_DIR: ${{ github.workspace }}/.venv
      ARTIFACT_RUN_ID: "${{ github.run_id }}"
      OUTPUT_ARTIFACTS_DIR: ${{ github.workspace }}/build
      THEROCK_BIN_DIR: "./build/bin"
    steps:
      - name: Checkout Repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          repository: "ROCm/TheRock"

      - name: Run setup test environment workflow
        uses: './.github/actions/setup_test_environment'
        with:
          ARTIFACT_RUN_ID: ${{ env.ARTIFACT_RUN_ID }}
          AMDGPU_FAMILIES: ${{ inputs.amdgpu_families }}
          OUTPUT_ARTIFACTS_DIR: ${{ env.OUTPUT_ARTIFACTS_DIR }}
          VENV_DIR: ${{ env.VENV_DIR }}
          FETCH_ARTIFACT_ARGS: ${{ matrix.components.fetch_artifact_args }}
          PLATFORM: ${{ inputs.platform }}

      - name: Test
        timeout-minutes: ${{ matrix.components.timeout_minutes }}
        run: |
          if [ "${{ inputs.PLATFORM }}" == "linux" ]; then source ${VENV_DIR}/bin/activate ; else . ${VENV_DIR}/Scripts/activate ; fi
          ${{ matrix.components.test_script }}
