name: Code tests (plain pip)

on:
  push:
    branches: [ main ]
    paths:
      - '**/*.py'
      - '**/*.ipynb'
      - '**/*.yaml'
      - '**/*.yml'
      - '**/*.sh'
  pull_request:
    branches: [ main ]
    paths:
      - '**/*.py'
      - '**/*.ipynb'
      - '**/*.yaml'
      - '**/*.yml'
      - '**/*.sh'
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  pip-tests:
    name: Pip Tests (Ubuntu Only)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"  # tests for backwards compatibility

      - name: Create Virtual Environment and Install Dependencies
        run: |
          python -m venv .venv
          source .venv/bin/activate
          pip install --upgrade pip
          # Necessary because there is not much storage space on this runner:
          pip install torch==2.9.1+cpu --index-url https://download.pytorch.org/whl/cpu
          pip install -r requirements.txt --no-deps
          pip install jupyterlab pandas tensorflow matplotlib
          pip install pytest pytest-ruff nbval

      - name: Test Selected Python Scripts
        run: |
          source .venv/bin/activate
          pytest setup/02_installing-python-libraries/tests.py
          pytest ch04/01_main-chapter-code/tests.py
          pytest ch05/01_main-chapter-code/tests.py
          pytest ch06/01_main-chapter-code/tests.py

      - name: Validate Selected Jupyter Notebooks
        run: |
          source .venv/bin/activate
          pytest --nbval ch02/01_main-chapter-code/dataloader.ipynb
          pytest --nbval ch03/01_main-chapter-code/multihead-attention.ipynb
          pytest --nbval ch02/04_bonus_dataloader-intuition/dataloader-intuition.ipynb