name: Jupyter Server Tests
on:
  push:
    branches: ["main"]
  pull_request:
  schedule:
    - cron: "0 8 * * *"

defaults:
  run:
    shell: bash -eux {0}

jobs:
  build:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.9", "3.11", "3.12"]
        include:
          - os: windows-latest
            python-version: "3.9"
          - os: ubuntu-latest
            python-version: "pypy-3.9"
          - os: macos-latest
            python-version: "3.10"
          - os: ubuntu-latest
            python-version: "3.12"
    steps:
      - name: Checkout
        uses: actions/checkout@v5
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Install nbconvert dependencies on Linux
        if: startsWith(runner.os, 'Linux')
        run: |
          sudo apt-get update
          sudo apt-get install texlive-plain-generic inkscape texlive-xetex
          sudo apt-get install xvfb x11-utils libxkbcommon-x11-0
          # pandoc is not up to date in the ubuntu repos, so we install directly
          wget https://github.com/jgm/pandoc/releases/download/3.1.2/pandoc-3.1.2-1-amd64.deb && sudo dpkg -i pandoc-3.1.2-1-amd64.deb
      - name: Run the tests on posix
        if: ${{ !startsWith(matrix.python-version, 'pypy') && !startsWith(matrix.os, 'windows') }}
        run: hatch run cov:test --cov-fail-under 75 || hatch -v run test:test --lf
      - name: Run the tests on pypy
        if: ${{ startsWith(matrix.python-version, 'pypy') }}
        run: hatch run test:nowarn || hatch -v run test:nowarn --lf
      - name: Run the tests on windows
        if: ${{ startsWith(matrix.os, 'windows') }}
        run: hatch run cov:nowarn -s || hatch -v run cov:nowarn --lf
      - uses: jupyterlab/maintainer-tools/.github/actions/upload-coverage@v1

  test_docs:
    name: Test Docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Install Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install enchant-2  # for spell checking
      - name: Build API docs
        run: |
          hatch -v run docs:api
          # If this fails run `hatch run docs:api` locally
          # and commit.
          git status --porcelain
          git status -s | grep "A" && exit 1
          git status -s | grep "M" && exit 1
          echo "API docs done"
      - run: hatch -v run docs:build

  test_lint:
    name: Test Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Run Linters
        run: |
          hatch -v run typing:test
          hatch fmt
          pipx run interrogate -v .
          pipx run doc8 --max-line-length=200  --ignore-path=docs/source/other/full-config.rst
          npm install -g eslint
          eslint .

  test_examples:
    name: Test Examples
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Install the Python dependencies for the examples
        run: |
          pip install -e ".[test]"
          cd examples/simple && pip install -e .
      - name: Run the tests for the examples
        run: |
          python -m pytest examples/simple

  test_minimum_versions:
    name: Test Minimum Versions
    timeout-minutes: 20
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
        with:
          dependency_type: minimum
      - name: Run the unit tests
        run: |
          hatch -vv run test:nowarn || hatch -v run test:nowarn --lf

  test_prereleases:
    name: Test Prereleases
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
        with:
          dependency_type: pre
      - name: Run the tests
        run: |
          hatch run test:nowarn || hatch -v run test:nowarn --lf

  make_sdist:
    name: Make SDist
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/make-sdist@v1

  test_sdist:
    runs-on: ubuntu-latest
    needs: [make_sdist]
    name: Install from SDist and Test
    timeout-minutes: 20
    steps:
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/test-sdist@v1
        with:
          package_spec: -vv .
          test_command: hatch run test:test || hatch -v run test:test --lf

  check_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Install Dependencies
        run: |
          pip install -e .
      - name: Check Release
        uses: jupyter-server/jupyter_releaser/.github/actions/check-release@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  check_links:
    name: Check Links
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1

  integration_check:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]
        python-version: ["3.9", "3.10", "3.11", "3.12"]
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Run the tests
        run: hatch -v run cov:integration
      - uses: jupyterlab/maintainer-tools/.github/actions/upload-coverage@v1

  integration_check_pypy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
        with:
          python_version: "pypy-3.9"
      - name: Run the tests
        run: hatch -v run test:nowarn --integration_tests=true

  coverage:
    runs-on: ubuntu-latest
    needs:
      - integration_check
      - build
    steps:
      - uses: actions/checkout@v5
      - uses: jupyterlab/maintainer-tools/.github/actions/report-coverage@v1
        with:
          fail_under: 80

  tests_check: # This job does nothing and is only used for the branch protection
    if: always()
    needs:
      - coverage
      - integration_check_pypy
      - test_docs
      - test_lint
      - test_examples
      - test_minimum_versions
      - test_prereleases
      - check_links
      - check_release
      - test_sdist
    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) }}
