name: Windows tests impl

on:
  workflow_call:
    inputs:
      os:
        required: true
        type: string
      build_type:
        required: true
        type: string

defaults:
  run:
    # micromamba activation
    shell: cmd /C call {0}

jobs:
  build_shared_win:
    name: Build binaries
    runs-on: ${{ inputs.os }}
    steps:
      - name: Checkout mamba repository
        uses: actions/checkout@v4
      - name: Create build environment
        uses: mamba-org/setup-micromamba@v2
        with:
          environment-file: ./dev/environment-dev.yml
          environment-name: build_env
          cache-environment: true
          init-shell: cmd.exe
      - uses: hendrikmuhs/ccache-action@main
        with:
          variant: sccache
          key: ${{ github.job }}-${{ inputs.os }}
          restore-keys: |
            ccache-libmamba-${{ inputs.os }}
      - name: Build mamba
        run: |
          cmake -B build/ -G Ninja ^
            --preset mamba-win-shared-${{ inputs.build_type }} ^
            -D CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreadedDLL" ^
            -D CMAKE_CXX_COMPILER_LAUNCHER=sccache ^
            -D CMAKE_C_COMPILER_LAUNCHER=sccache ^
            -D BUILD_LIBMAMBAPY=OFF ^
            -D ENABLE_MAMBA_ROOT_PREFIX_FALLBACK=OFF
          if %errorlevel% neq 0 exit /b %errorlevel%
          cmake --build build/ --parallel
          if %errorlevel% neq 0 exit /b %errorlevel%
      - name: Show build cache statistics
        run: sccache --show-stats
      - name: Lock environment
        run: micromamba env export --explicit > build/environment.lock
      - name: Remove extra files before saving workspace
        shell: bash -eo pipefail {0}
        run: find build/ -type f -name '*.obj' -exec rm {} +
      - name: Save workspace
        uses: ./.github/actions/workspace
        with:
          action: save
          path: build/
          key_suffix: ${{ inputs.os }}-${{ inputs.build_type }}

  libmamba_tests_win:
    name: libmamba tests
    needs: ["build_shared_win"]
    runs-on: ${{ inputs.os }}
    steps:
      - name: Checkout mamba repository
        uses: actions/checkout@v4
      - name: Restore workspace
        uses: ./.github/actions/workspace
        with:
          action: restore
          path: build/
          key_suffix: ${{ inputs.os }}-${{ inputs.build_type }}
      - name: Create build environment
        uses: mamba-org/setup-micromamba@v2
        with:
          environment-file: ./build/environment.lock
          environment-name: build_env
          init-shell: bash cmd.exe
      - name: Run solv-cpp tests
        shell: bash -elo pipefail {0}
        run: |
          ./build/libmamba/ext/solv-cpp/tests/test_solv_cpp
      - name: Run libmamba tests
        shell: bash -elo pipefail {0}
        run: |
          unset CONDARC  # Interferes with tests
          cd ./build/libmamba && ./tests/test_libmamba

  libmambapy_tests_win:
    name: libmambapy tests
    needs: ["build_shared_win"]
    runs-on: ${{ inputs.os }}
    steps:
      - name: Checkout mamba repository
        uses: actions/checkout@v4
      - name: Restore workspace
        uses: ./.github/actions/workspace
        with:
          action: restore
          path: build/
          key_suffix: ${{ inputs.os }}-${{ inputs.build_type }}
      - name: Create build environment
        uses: mamba-org/setup-micromamba@v2
        with:
          environment-file: ./build/environment.lock
          environment-name: build_env
          init-shell: bash cmd.exe
      - name: Install libmambapy
        run: |
          cmake --install build/ --prefix %CONDA_PREFIX%
          # TODO add some ccache and parallelism to builds
          python -m pip install --no-deps --no-build-isolation ./libmambapy
      - name: Run libmamba Python bindings tests
        run: |
          # Only rerun flaky tests on the `main` branch
          python -m pytest libmambapy/tests/ ^
            ${{ runner.debug == 'true' && '-v --capture=tee-sys' || '--exitfirst' }} ^
            ${{ github.ref == 'refs/heads/main' && '--reruns 3' || '' }}

  mamba_integration_tests_win:
    name: mamba integration tests
    needs: ["build_shared_win"]
    runs-on: ${{ inputs.os }}
    steps:
      - name: Checkout mamba repository
        uses: actions/checkout@v4
      - name: Restore workspace
        uses: ./.github/actions/workspace
        with:
          action: restore
          path: build/
          key_suffix: ${{ inputs.os }}-${{ inputs.build_type }}
      - name: Create build environment
        uses: mamba-org/setup-micromamba@v2
        with:
          environment-file: ./build/environment.lock
          environment-name: build_env
          init-shell: bash cmd.exe powershell
      - name: Install mamba
        shell: bash -elo pipefail {0}
        run: |
          cmake --install build/ --prefix local/
      - name: mamba python based tests with pwsh
        shell: pwsh
        run: |
          $env:PYTHONIOENCODING='UTF-8'
          $env:MAMBA_ROOT_PREFIX = Join-Path -Path $pwd -ChildPath 'mambaroot'
          $env:TEST_MAMBA_EXE = Join-Path -Path $pwd -ChildPath 'local\bin\mamba.exe'
          $env:MAMBA_TEST_SHELL_TYPE='powershell'
          Remove-Item -Path "env:CONDARC"
          # Only rerun flaky tests on the `main` branch
          python -m pytest micromamba/tests/ `
            ${{ runner.debug == 'true' && '-v --capture=tee-sys' || '--exitfirst' }} `
            ${{ github.ref == 'refs/heads/main' && '--reruns 3' || '' }}
