name: Test

on:
  workflow_call:
    inputs:
      dfhack_ref:
        type: string
      scripts_ref:
        type: string
      structures_ref:
        type: string

jobs:
  build-windows:
    name: Windows MSVC
    uses: ./.github/workflows/build-windows.yml
    with:
      dfhack_ref: ${{ inputs.dfhack_ref }}
      scripts_ref: ${{ inputs.scripts_ref }}
      structures_ref: ${{ inputs.structures_ref }}
      artifact-name: test-msvc
      cache-id: test
      docs: true
      html: false
      tests: true

  build-linux:
    name: Linux gcc-${{ matrix.gcc }}
    uses: ./.github/workflows/build-linux.yml
    with:
      dfhack_ref: ${{ inputs.dfhack_ref }}
      scripts_ref: ${{ inputs.scripts_ref }}
      structures_ref: ${{ inputs.structures_ref }}
      artifact-name: test-gcc-${{ matrix.gcc }}
      cache-id: test
      stonesense: ${{ matrix.plugins == 'all' }}
      extras: ${{ matrix.plugins == 'all' }}
      docs: true
      html: false
      tests: true
      gcc-ver: ${{ matrix.gcc }}
    secrets: inherit
    strategy:
      fail-fast: false
      matrix:
        include:
        - gcc: 10
          plugins: "default"
        - gcc: 12
          plugins: "all"

  run-tests:
    name: Test (${{ matrix.os }}, ${{ matrix.compiler }}, ${{ matrix.plugins }} plugins, ${{ matrix.config }} config)
    needs:
    - build-windows
    - build-linux
    runs-on: ${{ matrix.os }}-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows
            compiler: msvc
            plugins: "default"
            config: "default"
          - os: windows
            compiler: msvc
            plugins: "default"
            config: "empty"
          - os: ubuntu
            compiler: gcc-10
            plugins: "default"
            config: "default"
          - os: ubuntu
            compiler: gcc-12
            plugins: "all"
            config: "default"
    steps:
    - name: Set env
      shell: bash
      run: echo "DF_FOLDER=DF" >> $GITHUB_ENV
    - name: Install dependencies
      if: matrix.os == 'ubuntu'
      run: |
        sudo apt-get update
        sudo apt-get install \
          libsdl2-2.0-0 \
          libsdl2-image-2.0-0
    - name: Clone DFHack
      uses: actions/checkout@v3
      with:
        repository: ${{ inputs.dfhack_ref && github.repository || 'DFHack/dfhack' }}
        ref: ${{ inputs.dfhack_ref }}
    - name: Detect DF version
      shell: bash
      run: echo DF_VERSION="$(sh ci/get-df-version.sh)" >> $GITHUB_ENV
    - name: Fetch DF cache
      id: restore-df
      uses: actions/cache/restore@v3
      with:
        path: ${{ env.DF_FOLDER }}
        key: df-${{ matrix.os }}-${{ env.DF_VERSION }}-${{ hashFiles('ci/download-df.sh') }}
    - name: Download DF
      if: steps.restore-df.outputs.cache-hit != 'true'
      run: sh ci/download-df.sh ${{ env.DF_FOLDER }} ${{ matrix.os }} ${{ env.DF_VERSION }}
    - name: Save DF cache
      if: steps.restore-df.outputs.cache-hit != 'true'
      uses: actions/cache/save@v3
      with:
        path: ${{ env.DF_FOLDER }}
        key: df-${{ matrix.os }}-${{ env.DF_VERSION }}-${{ hashFiles('ci/download-df.sh') }}
    - name: Install blank DFHack init scripts
      if: matrix.config == 'empty'
      shell: bash
      run: |
        mkdir -p ${{ env.DF_FOLDER }}/dfhack-config/init
        cd data/dfhack-config/init
        for fname in *.init; do touch ../../../${{ env.DF_FOLDER }}/dfhack-config/init/$fname; done
    - name: Download DFHack
      uses: actions/download-artifact@v3
      with:
        name: test-${{ matrix.compiler }}
    - name: Install DFHack
      shell: bash
      run: tar xjf test-${{ matrix.compiler }}.tar.bz2 -C ${{ env.DF_FOLDER }}
    - name: Start X server
      if: matrix.os == 'ubuntu'
      run: Xvfb :0 -screen 0 1600x1200x24 &
    - name: Run lua tests
      timeout-minutes: 10
      env:
        DISPLAY: :0
        TERM: xterm-256color
      run: python ci/run-tests.py --keep-status "${{ env.DF_FOLDER }}"
    - name: Check RPC interface
      run: python ci/check-rpc.py "${{ env.DF_FOLDER }}/dfhack-rpc.txt"
    - name: Upload test artifacts
      uses: actions/upload-artifact@v3
      if: always()
      continue-on-error: true
      with:
        name: test-output-${{ matrix.compiler }}-${{ matrix.plugins }}_plugins-${{ matrix.config }}_config
        path: |
          ${{ env.DF_FOLDER }}/dfhack-rpc.txt
          ${{ env.DF_FOLDER }}/test*.json
          ${{ env.DF_FOLDER }}/*.log