name: Test

on: [push, pull_request]

jobs:
  docs:
    name: Documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
          cache: 'pip'
          cache-dependency-path: |
            **/setup.cfg
            **/requirements*.txt
      - name: Install dependencies
        run: |
          sudo apt-get install -y pandoc
          python -m pip install --upgrade pip
          python -m pip install file://$PWD/python/ipywidgets#egg=ipywidgets
          python -m pip install -r ./docs/requirements.txt
      - name: Build docs
        run: |
          cd docs/source
          python -m sphinx -T -E -b html -d ../build/doctrees -D language=en . ../build/html
  js:
    name: JavaScript
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
          cache: 'pip'
          cache-dependency-path: |
            **/setup.cfg
            **/requirements*.txt
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install jupyterlab==4.0.0
      - name: yarn install, build, test
        run: |
          jlpm --immutable
          jlpm build
          jlpm build:examples

          pushd packages/base
          jlpm test:unit:firefox:headless
          popd

          pushd packages/base-manager
          jlpm test:unit:firefox:headless
          popd

          pushd packages/controls
          jlpm test:unit:firefox:headless
          popd

          pushd packages/html-manager
          jlpm test:unit:firefox:headless
          popd

          pushd examples/web1
          jlpm test:firefox:headless
          popd

        env:
          CI: true
  python:
    name: Python
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11']

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'
          cache-dependency-path: |
            **/setup.cfg
            **/requirements*.txt

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install file://$PWD/python/ipywidgets#egg=ipywidgets[test]
      - name: Test with pytest
        run: |
          pip install "pytest<8"
          cd python/ipywidgets
          pytest --cov=ipywidgets ipywidgets

  spec:
    name: Message Specification
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
          cache: 'pip'
          cache-dependency-path: |
            **/setup.cfg
            **/requirements*.txt
      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install jupyterlab file://$PWD/python/ipywidgets#egg=ipywidgets[test]
      - name: Install JS dependencies
        run: |
          jlpm
      - name: Compare spec with latest version
        run: |
          python ./packages/schema/generate-spec.py -f markdown spec.md
          jlpm prettier
          diff -u ./packages/schema/jupyterwidgetmodels.latest.md ./spec.md

  ui-test:
    name: Visual Regression
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          cache: 'pip'
          cache-dependency-path: |
            **/setup.cfg
            **/requirements*.txt

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install -U jupyterlab==4.0.0 jupyter-packaging~=0.10

      - name: Build and Install ipywidgets
        run: |
          jlpm --immutable
          jlpm build

      - name: Build the extension
        run: |
          cd python/ipywidgets
          pip install .
          cd ../..
          jlpm
          jlpm build
          cd python/jupyterlab_widgets
          pwd
          pip install -e .
          jupyter labextension develop . --overwrite
          jupyter labextension list

      - name: Install Test Dependencies
        run: |
          jlpm --immutable
          npx playwright install chromium
        working-directory: ui-tests

      - name: Execute integration tests
        shell: bash -l {0}
        working-directory: ui-tests
        run: |
          npx playwright test

      - name: Upload Playwright Test assets
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: ipywidgets-test-assets
          path: |
            ui-tests/test-results

      - name: Upload Playwright Test report
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: ipywidgets-test-report
          path: |
            ui-tests/playwright-report

      - name: Update snapshots
        if: failure()
        working-directory: ui-tests
        run: |
          jlpm test:update

      - name: Upload updated snapshots
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: ipywidgets-updated-snapshots
          path: ui-tests/tests
