name: Update Test Durations

on:
  push:
    branches:
      - main
    paths:
      - 'tests/**/*.py'
  workflow_dispatch:

permissions:
  contents: read

jobs:
  update-durations:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.10', '3.11', '3.12', '3.13']
    env:
      OPENAI_API_KEY: fake-api-key
      PYTHONUNBUFFERED: 1
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1

      - name: Restore global uv cache
        id: cache-restore
        uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ~/.cache/uv
            ~/.local/share/uv
            .venv
          key: uv-main-py${{ matrix.python-version }}-${{ hashFiles('uv.lock') }}
          restore-keys: |
            uv-main-py${{ matrix.python-version }}-

      - name: Install uv
        uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
        with:
          version: "0.11.3"
          python-version: ${{ matrix.python-version }}
          enable-cache: false

      - name: Install the project
        run: uv sync --all-groups --all-extras

      - name: Run all tests and store durations
        run: |
          PYTHON_VERSION_SAFE=$(echo "${{ matrix.python-version }}" | tr '.' '_')
          uv run pytest --store-durations --durations-path=.test_durations_py${PYTHON_VERSION_SAFE} -n auto
        continue-on-error: true

      - name: Save durations to cache
        if: always()
        uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: .test_durations_py*
          key: test-durations-py${{ matrix.python-version }}

      - name: Save uv caches
        if: steps.cache-restore.outputs.cache-hit != 'true'
        uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ~/.cache/uv
            ~/.local/share/uv
            .venv
          key: uv-main-py${{ matrix.python-version }}-${{ hashFiles('uv.lock') }}