name: Verify prefect-client build

on:
  pull_request:
    paths:
      - client/*
      - src/prefect/**/*.py
      - pyproject.toml
      - .github/workflows/prefect-client.yaml
  push:
    branches:
      - main
    paths:
      - client/*
      - src/prefect/**/*.py
      - pyproject.toml
      - .github/workflows/prefect-client.yaml
  workflow_call:
    inputs:
      upload-artifacts:
        description: "Whether or not to upload artifacts created in this workflow"
        default: false
        type: boolean
      artifact-name:
        description: "The name for the build prefect-client artifact"
        default: "prefect-client-pypi-dists"
        type: string

jobs:
  prefect-client-smoke-test:
    name: Build and run prefect-client
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Set up uv
        uses: astral-sh/setup-uv@v7
        with:
          enable-cache: true
          python-version: "3.10"
          cache-dependency-glob: "pyproject.toml"
          activate-environment: true

      - name: Start server
        env:
          PREFECT_API_URL: http://127.0.0.1:4200/api
          PREFECT_SERVER_LOGGING_LEVEL: DEBUG
        run: >
          uv run --isolated prefect server start --analytics-off --host 0.0.0.0 2>&1 > server.log &

          uv run --isolated ./scripts/wait-for-server.py
  
      - name: Create a temp dir to stage our build
        run: echo "TMPDIR=$(mktemp -d)" >> $GITHUB_ENV

      - name: Prepare files for prefect-client build (omit the local build)
        run: sh client/build_client.sh
        env:
          TMPDIR: ${{ env.TMPDIR }}

      - name: Build a binary wheel and a source tarball
        run: uv build
        working-directory: ${{ env.TMPDIR }}

      - name: Install the built client from the locally built package
        run: uv pip install dist/*.tar.gz
        working-directory: ${{ env.TMPDIR }}

      - name: Verify the CLI isn't available in the active environment
        run: |
          prefect --help && { echo "Prefect CLI should not be available in the active environment"; exit 1; } || echo "Prefect CLI is not available in the active environment"

      - name: Get the version of built `prefect-client`
        run: |
          prefect_client_version=$(python -c "import prefect; print(prefect.__version__)")
          echo "prefect_client_version=$prefect_client_version" >> $GITHUB_OUTPUT
        working-directory: ${{ env.TMPDIR }}
        id: prefect_client_version

      - name: Run the smoke test flow using the built client
        run: python client/client_flow.py
        working-directory: ${{ env.TMPDIR }}
        env:
          PREFECT_API_URL: "http://127.0.0.1:4200/api"

      - name: Run deploy and execute smoke test using the built client
        run: python client/client_deploy.py
        working-directory: ${{ env.TMPDIR }}
        env:
          PREFECT_API_URL: "http://127.0.0.1:4200/api"


      - name: Install prefect from source
        run: uv pip install .

      - name: Get the version of built `prefect`
        run: |
          prefect_version=$(prefect --version)
          echo "prefect_version=$prefect_version" >> $GITHUB_OUTPUT
        id: prefect_version

      - name: Verify that the built `prefect` and `prefect-client` versions are the same
        run: |
          if [ "${{ steps.prefect_version.outputs.prefect_version }}" != "${{ steps.prefect_client_version.outputs.prefect_client_version }}" ]; then
            echo "The built versions of prefect and prefect-client are not the same."
            exit 1
          fi

      - name: Run the smoke test flow again with prefect and prefect-client installed
        run: uv run python client/client_flow.py
        working-directory: ${{ env.TMPDIR }}
        env:
          PREFECT_API_URL: "http://127.0.0.1:4200/api"

      - name: Publish build artifacts
        if: ${{ inputs.upload-artifacts }}
        uses: actions/upload-artifact@v6
        with:
          name: ${{ inputs.artifact-name }}
          path: "${{ env.TMPDIR }}/dist"
