name: tests

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  pytest:
    # ubuntu-latest is 26.04 now and setup-python has no 3.9 build for it; 3.9 stays supported, so it runs
    # on 24.04 – the job keeps its name (pytest (ubuntu-latest, 3.9))
    runs-on: ${{ matrix.os == 'ubuntu-latest' && matrix.python == '3.9' && 'ubuntu-24.04' || matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python: ["3.9", "3.11", "3.13", "3.14"]
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7
        with:
          python-version: ${{ matrix.python }}
      # hash-locked; the MCP server's SDK comes only on 3.10+ – on 3.9 its tests skip
      - run: pip install --require-hashes -r .github/requirements/test.txt
      - run: python -m pytest -q
      # starts the program once for real – catches import errors the tests don't see
      - run: python proxy_scraper.py --help
      - run: python proxy_scraper.py --list-sources 5

  action:
    # the GitHub Action the way others use it (uses: maximilianfeix/proxy-scraper@v1), on every OS – with a fixture
    # list instead of the live one, so a pull request never fails because GitHub Pages or the list had a bad hour.
    # sitecustomize.py in live_fixture/ swaps only the download of proxies.json/stats.json; the install, the
    # filters and the outputs are the real ones. live-smoke.yml runs the same step against the real list.
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    env:
      PROXY_SCRAPER_LIVE_FIXTURE: "1"
      PYTHONPATH: ${{ github.workspace }}/.github/scripts/live_fixture
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - id: proxies
        uses: ./
        with:
          types: socks5,http
          https: "true"
          limit: "5"
      - name: The outputs are there
        shell: bash
        run: |
          echo "count=${{ steps.proxies.outputs.count }} fastest=${{ steps.proxies.outputs.proxy }}"
          test "${{ steps.proxies.outputs.count }}" -ge 1
          test -s "${{ steps.proxies.outputs.file }}"
          head -3 "${{ steps.proxies.outputs.file }}"

  parquet:
    # the Hugging Face mirror's Parquet round-trip (tests/test_hf_parquet.py) needs PyArrow, which the matrix above
    # doesn't install – there those tests skip. Here PyArrow comes from the hf lock and a missing one fails.
    runs-on: ubuntu-latest
    env:
      PROXY_SCRAPER_REQUIRE_PYARROW: "1"
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7
        with:
          python-version: "3.13"
      - run: pip install --require-hashes -r .github/requirements/test.txt
      - run: pip install --require-hashes -r .github/requirements/hf.txt
      - run: python -m pytest -q -rs tests/test_hf_parquet.py

  package:
    # builds wheel + sdist and installs the wheel fresh – the way users get it with pipx
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7
        with:
          python-version: "3.13"
      - run: pip install --require-hashes -r .github/requirements/test.txt
      - run: python -m build
      - name: Install the wheel into a clean environment
        run: |
          python -m venv /tmp/clean
          /tmp/clean/bin/pip install dist/*.whl
      - name: Run the installed command outside the repo
        working-directory: /tmp
        run: |
          /tmp/clean/bin/proxy-scraper --version
          /tmp/clean/bin/proxy-scraper --help > /dev/null
          /tmp/clean/bin/proxy-scraper --list-sources 3
      - name: Install the MCP extra and talk to the installed server over stdio
        working-directory: /tmp
        run: |
          /tmp/clean/bin/pip install "$(ls $GITHUB_WORKSPACE/dist/*.whl)[mcp]"
          /tmp/clean/bin/python - <<'PY'
          import anyio
          from mcp.client.client import Client
          from mcp.client.stdio import StdioServerParameters

          async def main():
              async with Client(StdioServerParameters(command="/tmp/clean/bin/proxy-scraper-mcp")) as client:
                  names = sorted(t.name for t in (await client.list_tools()).tools)
                  assert names == ["check_proxies", "fetch_url", "get_proxies"], names
                  print("MCP tools:", names)

          anyio.run(main)
          PY

  sdist:
    # what a source install gets (installing from the sdist, distro packagers): unpack the sdist, build the wheel
    # from it – not from the checkout – install that into a fresh venv and check, from outside the checkout, that
    # every file pyproject's package-data names arrived and loads (sources.json, the site and dashboard pages)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7
        with:
          python-version: "3.13"
      - run: pip install --require-hashes -r .github/requirements/test.txt
      - run: python -m build --sdist --outdir "$RUNNER_TEMP/sdist"
      - name: Build the wheel from the unpacked sdist
        run: |
          mkdir "$RUNNER_TEMP/from-sdist"
          tar -xzf "$RUNNER_TEMP"/sdist/*.tar.gz -C "$RUNNER_TEMP/from-sdist"
          python -m build --wheel --outdir "$RUNNER_TEMP/dist" "$RUNNER_TEMP"/from-sdist/*/
      - name: Install it into a clean environment and check the package assets
        working-directory: ${{ runner.temp }}
        run: |
          python -m venv "$RUNNER_TEMP/clean"
          "$RUNNER_TEMP/clean/bin/pip" install dist/*.whl
          "$RUNNER_TEMP/clean/bin/python" -I "$GITHUB_WORKSPACE/.github/scripts/package_assets.py" "$GITHUB_WORKSPACE"
          "$RUNNER_TEMP/clean/bin/proxy-scraper" --list-sources 3
      - name: The command line against the localhost lab, from the installed package
        working-directory: ${{ runner.temp }}
        env:
          PROXY_SCRAPER_SMOKE_INSTALLED: "1"
        run: |
          "$RUNNER_TEMP/clean/bin/python" "$GITHUB_WORKSPACE/.github/scripts/lab_smoke.py"
