on: [push, pull_request]
name: Python Tests
jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
        platform: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.platform }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.21'

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

      - name: Build Go shared library
        # We use 'shared' now because ABI mode needs .so or .dylib
        run: |
          # Ensure Go architecture matches the Python runner
          export GOARCH=$(python -c "import platform; m = platform.machine().lower(); print('arm64' if 'arm' in m or 'aarch64' in m else 'amd64')")
          make shared

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r python/requirements.txt
          if [ -f python/requirements.dev.txt ]; then pip install -r python/requirements.dev.txt; fi

      - name: Build and install package
        run: |
          cp LICENSE python/
          cd python
          python build_enry.py
          pip install -e .

      - name: Run tests
        env:
          PYTHONMALLOC: "malloc"
          GODEBUG: "cgocheck=0,installgoroot=0"

          # Memory tests:
          ENRY_MEM_RUN_SMOKE: "1"
          ENRY_MEM_ITERS_SHORT: "2000"
          ENRY_MEM_ITERS_SMOKE: "200"

          # Run the long leak regression only on one matrix job (fast CI, strong coverage)
          ENRY_MEM_RUN_LONG: ${{ matrix.platform == 'ubuntu-latest' && matrix.python-version == '3.11' && '1' || '0' }}
          ENRY_MEM_ITERS_LONG: "50000"
        run: pytest python/tests/ -v
