name: Test the package

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    name: Test on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v5

      - name: Install R
        id: setup-r
        uses: r-lib/actions/setup-r@v2

      - name: Install R shared library (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update -y
          sudo apt-get install -y r-base-dev
          echo "LIBRARY_PATH=/usr/lib/R/lib:$LIBRARY_PATH" >> $GITHUB_ENV
          echo "LD_LIBRARY_PATH=/usr/lib/R/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Set R_HOME (Windows)
        if: runner.os == 'Windows'
        run: echo "R_HOME=${{ steps.setup-r.outputs.r-home }}" >> $env:GITHUB_ENV

      - name: Set Rust target (Windows)
        if: runner.os == 'Windows'
        run: |
          rustup target add x86_64-pc-windows-gnu
          rustup default stable-x86_64-pc-windows-gnu

      - name: Run tests (non-Windows)
        if: runner.os != 'Windows'
        env:
          LD_LIBRARY_PATH: ${{ steps.setup-r.outputs.r-home }}/lib
          DYLD_LIBRARY_PATH: ${{ steps.setup-r.outputs.r-home }}/lib
        run: cargo test --no-default-features && cargo test --features single-cell,multi-modal

      - name: Run tests (Windows)
        if: runner.os == 'Windows'
        run: cargo test --no-default-features && cargo test --features single-cell,multi-modal

  test-gpu:
    name: Test GPU on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v5

      - name: Install R
        id: setup-r
        uses: r-lib/actions/setup-r@v2

      - name: Install R shared library (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update -y
          sudo apt-get install -y r-base-dev
          echo "LIBRARY_PATH=/usr/lib/R/lib:$LIBRARY_PATH" >> $GITHUB_ENV
          echo "LD_LIBRARY_PATH=/usr/lib/R/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV
      - uses: dtolnay/rust-toolchain@stable
      - name: Install Vulkan (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libvulkan1 mesa-vulkan-drivers libgl1-mesa-dri
          echo "WGPU_BACKEND=vulkan" >> $GITHUB_ENV

      - name: Run tests
        if: runner.os != 'Windows'
        env:
          LD_LIBRARY_PATH: ${{ steps.setup-r.outputs.r-home }}/lib
          DYLD_LIBRARY_PATH: ${{ steps.setup-r.outputs.r-home }}/lib
        run: cargo test --features gpu
