name: Test downstream projects

on:
  push:
    branches: ["main"]
  pull_request:

concurrency:
  group: downstream-${{ github.ref }}
  cancel-in-progress: true

jobs:
  ipykernel:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
        with:
          package_name: ipykernel

  nbclient:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
        with:
          package_name: nbclient
          env_values: IPYKERNEL_CELL_NAME=\<IPY-INPUT\>
          # Skip test with execution_count mismatch - nbclient test issue, not jupyter_client
          test_command: "pytest -vv -raXxs -W default --durations 10 --color=yes --deselect tests/test_client.py::test_run_all_notebooks[update-display-id.ipynb-opts14]"

  nbconvert:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
        with:
          package_name: nbconvert
          package_spec: -e ".[test]"

  jupyter_server:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
        with:
          package_name: jupyter_server

  jupyter_kernel_test:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

      - name: Setup conda
        uses: conda-incubator/setup-miniconda@v3
        with:
          auto-update-conda: true
          activate-environment: jupyter_kernel_test
          miniforge-version: latest
          python-version: "3.10"

      - name: Test jupyter_kernel_test
        shell: bash -l {0}
        run: |
          git clone https://github.com/jupyter/jupyter_kernel_test.git
          cd jupyter_kernel_test
          conda env update --name jupyter_kernel_test --file environment.yml
          pip install -e ".[test]"
          python -m unittest -v

  qtconsole:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"
          architecture: "x64"

      - name: Install System Packages
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev

      - name: Install qtconsole dependencies
        shell: bash -l {0}
        run: |
          cd ${GITHUB_WORKSPACE}/..
          git clone https://github.com/jupyter/qtconsole.git
          cd qtconsole
          ${pythonLocation}/bin/python -m pip install -e ".[test]"
          ${pythonLocation}/bin/python -m pip install pyqt5

      - name: Install Jupyter-Client changes
        shell: bash -l {0}
        run: ${pythonLocation}/bin/python -m pip install -e .

      - name: Test qtconsole
        shell: bash -l {0}
        run: |
          cd ${GITHUB_WORKSPACE}/../qtconsole
          xvfb-run --auto-servernum ${pythonLocation}/bin/python -m pytest -x -vv -s --full-trace --color=yes qtconsole

  downstreams_check: # This job does nothing and is only used for the branch protection
    if: always()
    needs:
      - ipykernel
      - nbclient
      - nbconvert
      - jupyter_server
      - jupyter_kernel_test
      - qtconsole
    runs-on: ubuntu-latest
    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@release/v1
        with:
          jobs: ${{ toJSON(needs) }}
