name: Tests

on:
  pull_request:
    paths:
      - "lerobot/**"
      - "tests/**"
      - "examples/**"
      - ".github/**"
      - "pyproject.toml"
      - ".pre-commit-config.yaml"
      - "Makefile"
      - ".cache/**"
  push:
    branches:
      - main
    paths:
      - "lerobot/**"
      - "tests/**"
      - "examples/**"
      - ".github/**"
      - "pyproject.toml"
      - ".pre-commit-config.yaml"
      - "Makefile"
      - ".cache/**"

permissions: {}

env:
  UV_VERSION: "0.6.0"

jobs:
  pytest:
    name: Pytest
    runs-on: ubuntu-latest
    env:
      MUJOCO_GL: egl
    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true  # Ensure LFS files are pulled
          persist-credentials: false

      - name: Install apt dependencies
      # portaudio19-dev is needed to install pyaudio
        run: |
          sudo apt-get update && \
          sudo apt-get install -y libegl1-mesa-dev ffmpeg portaudio19-dev

      - name: Install uv and python
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true
          version: ${{ env.UV_VERSION }}
          python-version: "3.10"

      - name: Install lerobot (all extras)
        run: uv sync --all-extras

      - name: Test with pytest
        run: |
          uv run pytest tests -v --cov=./lerobot --durations=0 \
            -W ignore::DeprecationWarning:imageio_ffmpeg._utils:7 \
            -W ignore::UserWarning:torch.utils.data.dataloader:558 \
            -W ignore::UserWarning:gymnasium.utils.env_checker:247 \
            && rm -rf tests/outputs outputs

  pytest-minimal:
    name: Pytest (minimal install)
    runs-on: ubuntu-latest
    env:
      MUJOCO_GL: egl
    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true  # Ensure LFS files are pulled
          persist-credentials: false

      - name: Install apt dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg

      - name: Install uv and python
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true
          version: ${{ env.UV_VERSION }}
          python-version: "3.10"

      - name: Install lerobot
        run: uv sync --extra "test"

      - name: Test with pytest
        run: |
          uv run pytest tests -v --cov=./lerobot --durations=0 \
            -W ignore::DeprecationWarning:imageio_ffmpeg._utils:7 \
            -W ignore::UserWarning:torch.utils.data.dataloader:558 \
            -W ignore::UserWarning:gymnasium.utils.env_checker:247 \
            && rm -rf tests/outputs outputs

  end-to-end:
    name: End-to-end
    runs-on: ubuntu-latest
    env:
      MUJOCO_GL: egl
    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true  # Ensure LFS files are pulled
          persist-credentials: false

      - name: Install apt dependencies
      # portaudio19-dev is needed to install pyaudio
        run: |
          sudo apt-get update && \
          sudo apt-get install -y libegl1-mesa-dev portaudio19-dev

      - name: Install uv and python
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true
          version: ${{ env.UV_VERSION }}
          python-version: "3.10"

      - name: Install lerobot (all extras)
        run: |
          uv venv
          uv sync --all-extras

      - name: venv
        run: |
          echo "PYTHON_PATH=${{ github.workspace }}/.venv/bin/python" >> $GITHUB_ENV

      - name: Test end-to-end
        run: |
          make test-end-to-end \
            && rm -rf outputs
