name: CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

jobs:
  tox:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - {name: '3.12', python: '3.12', tox: py312}
          - {name: '3.11', python: '3.11', tox: py311}
          - {name: '3.10', python: '3.10', tox: py310}
          - {name: '3.9', python: '3.9', tox: py39}
          - {name: '3.8', python: '3.8', tox: py38}
          - {name: 'format', python: '3.12', tox: format}
          - {name: 'mypy', python: '3.12', tox: mypy}
          - {name: 'pep8', python: '3.12', tox: pep8}
          - {name: 'package', python: '3.12', tox: package}

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python }}

      - name: update pip
        run: |
          pip install -U wheel
          pip install -U setuptools
          python -m pip install -U pip
      - run: pip install tox

      - run: tox -e ${{ matrix.tox }}


  h2spec:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - {name: 'asyncio', worker: 'asyncio'}
          - {name: 'trio', worker: 'trio'}

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: update pip
        run: |
          pip install -U wheel
          pip install -U setuptools
          python -m pip install -U pip
      - run: pip install trio .

      - name: Run server
        working-directory: compliance/h2spec
        run: nohup hypercorn --keyfile key.pem --certfile cert.pem -k ${{ matrix.worker }} server:app &

      - name: Download h2spec
        run: |
          wget https://github.com/summerwind/h2spec/releases/download/v2.6.0/h2spec_linux_amd64.tar.gz
          tar -xvf h2spec_linux_amd64.tar.gz

      - name: Run h2spec
        run: ./h2spec -tk -h 127.0.0.1 -p 8000 -o 10

  autobahn:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - {name: 'asyncio', worker: 'asyncio'}
          - {name: 'trio', worker: 'trio'}

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: update pip
        run: |
          pip install -U wheel
          pip install -U setuptools
          python -m pip install -U pip
      - run: python3 -m pip install trio .
      - name: Run server
        working-directory: compliance/autobahn
        run: nohup hypercorn -k ${{ matrix.worker }} server:app &

      - name: Run Unit Tests
        working-directory: compliance/autobahn
        run: docker run --rm --network=host -v "${PWD}/:/config" -v "${PWD}/reports:/reports" --name fuzzingclient crossbario/autobahn-testsuite wstest -m fuzzingclient -s /config/fuzzingclient.json && python3 summarise.py
