env:
  PATH: "$HOME/.local/bin:$PATH"

steps:
  - label: "Unit Tests"
    key: "unit_tests"
    timeout_in_minutes: 60
    command: |
      echo $PWD # for debugging

      uv venv --python 3.12 .venv-$BUILDKITE_BUILD_ID
      source .venv-$BUILDKITE_BUILD_ID/bin/activate
      uv pip install --upgrade pip setuptools wheel

      if command -v nvidia-smi >/dev/null 2>&1; then
        source .buildkite/scripts/pick-free-gpu.sh 18000
        export CUDA_LAUNCH_BLOCKING=1
      else
        source .buildkite/scripts/pick-free-gpu-amd.sh 18000
        export AMD_SERIALIZE_KERNEL=1
        export PYTORCH_ROCM_ARCH="gfx942"
        export TORCH_DONT_CHECK_COMPILER_ABI=1
        export CXX=hipcc
        export BUILD_WITH_HIP=1
        uv pip install torch torchvision --index-url https://download.pytorch.org/whl/rocm7.0
      fi

      uv pip install -r requirements/common.txt
      uv pip install -r requirements/test.txt
      uv pip install -e . --no-build-isolation
      uv pip freeze

      if command -v nvidia-smi >/dev/null 2>&1; then
        LMCACHE_TRACK_USAGE="false" \
        pytest --maxfail=1 --cov=lmcache \
          --cov-report term --cov-report=html:coverage-test \
          --cov-report=xml:coverage-test.xml --html=durations/test.html \
          --ignore=tests/disagg --ignore=tests/v1/test_pos_kernels.py \
          --ignore=tests/v1/test_nixl_storage.py \
          --ignore=tests/skipped \
          --ignore=tests/v1/storage_backend/test_eic.py
      else
        LMCACHE_TRACK_USAGE="false" \
        pytest --maxfail=1 --cov=lmcache \
          --cov-report term --cov-report=html:coverage-test \
          --cov-report=xml:coverage-test.xml --html=durations/test.html \
          --ignore=tests/disagg --ignore=tests/v1/test_pos_kernels.py \
          --ignore=tests/v1/test_nixl_storage.py \
          --ignore=tests/skipped \
          --ignore=tests/v1/multiprocess \
          --ignore=tests/v1/storage_backend/test_eic.py
      fi


      cat << EOF | buildkite-agent annotate --style "info"
        Read the <a href="artifact://coverage-test/index.html">uploaded coverage report</a>
      EOF

      export TARGET="$PWD"
      echo "Deleting current workspace $TARGET"
      cd /
      sudo rm -rf "$TARGET"

    artifact_paths:
      - "durations/test.html"
      - "coverage-test.xml"
      - "coverage-test/**/*"      
