# note: to invalidate caches, adjust the pip-v? number below.
name: Python tests

on:
  push:
    branches: [latest]
  pull_request:
  schedule:
    - cron: "0 0 * * *" # daily

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        py: ["3.12", "3.11", "3.13"]
      fail-fast: false

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python ${{ matrix.py }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.py }}

      - name: Get pip cache dir
        id: pip-cache
        run: |
          echo "::set-output name=dir::$(pip cache dir)"

      - name: pip cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pip-cache.outputs.dir }}
          key: ${{ runner.os }}-pip-v4-${{ hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-pip-v4-

      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install tox tox-gh-actions

      - name: Set up Pandoc
        uses: r-lib/actions/setup-pandoc@v2

      - name: Set up IPFS
        if: startsWith(runner.os, 'Linux') && (matrix.py == '3.11')
        uses: ibnesayeed/setup-ipfs@master
        with:
          ipfs_version: 0.6
          run_daemon: true

      - name: Start Redis
        if: startsWith(runner.os, 'Linux') && (matrix.py == '3.11')
        uses: supercharge/redis-github-action@1.8.0
        with:
          redis-version: 6

      - name: tox cache
        uses: actions/cache@v4
        with:
          path: .tox/
          key: ${{ runner.os }}-tox-v4-${{ hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-tox-v4-

      - name: Test with tox
        run: tox
        env:
          PYTHONDEVMODE: 1

      - name: Upload Python coverage to codecov
        uses: codecov/codecov-action@v3
        with:
          flags: python
          fail_ci_if_error: true
          files: .tox/coverage.xml
