name: Test

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

env:
    CARGO_TERM_COLOR: always

jobs:
    core_unit_test:
        name: Core Unit Tests
        runs-on: ubuntu-latest
        container:
            image: ghcr.io/luminal-ai/luminal-docker:cpu
        timeout-minutes: 20

        steps:
            - uses: actions/checkout@v6
            - name: Run tests
              run: cargo test --workspace --exclude luminal_cuda_lite --exclude luminal_metal --exclude luminal_bench --verbose
    metal_unit_test:
        name: Metal Unit Tests
        runs-on: macos-14
        timeout-minutes: 30

        steps:
            - uses: actions/checkout@v6
            - name: Run Metal crate tests
              run: rustup update; cargo test -p luminal_metal --verbose -- --test-threads=1
    python_native_tests:
        name: Python Native Tests
        runs-on: ubuntu-latest
        container:
            image: ghcr.io/luminal-ai/luminal-docker:cpu
        timeout-minutes: 45
        defaults:
            run:
                working-directory: crates/luminal_python

        steps:
            - uses: actions/checkout@v6
            - name: Install system dependencies
            - name: Update Rust toolchain
              run: rustup update
            - name: Build maturin extension
              run: uv run maturin develop --manifest-path rust/Cargo.toml
            - name: Run pytest
              run: uv run pytest tests/ -v

    python_cuda_tests:
        name: Python CUDA Tests
        runs-on: cuda_t4_runner
        container:
            image: ghcr.io/luminal-ai/luminal-docker:cuda
            options: --gpus all
        timeout-minutes: 45
        defaults:
            run:
                working-directory: crates/luminal_python

        steps:
            - uses: actions/checkout@v6
            - name: Detect GPU compute capability
              run: |
                  CAP=$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader | head -1 | tr -d '.')
                  echo "CUDA_COMPUTE_CAP=${CAP}" >> "$GITHUB_ENV"
            - name: Install uv
              run: |
                  curl -LsSf https://astral.sh/uv/install.sh | sh
                  echo "$HOME/.local/bin" >> "$GITHUB_PATH"
            - name: Build maturin extension
              run: uv run maturin develop --manifest-path rust/Cargo.toml --features cuda
            - name: Run pytest with CUDA backend
              env:
                  LUMINAL_BACKEND: cuda
              run: uv run pytest tests/ -v
