name: "websocket: build/target-tests"

on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened, labeled]

jobs:
  build_websocket:
    if: contains(github.event.pull_request.labels.*.name, 'websocket') || github.event_name == 'push'
    name: Build
    strategy:
      matrix:
        idf_ver: ["release-v5.0", "release-v5.1", "release-v5.2", "release-v5.3", "latest"]
        test: [ { app: example, path: "examples/target" }, { app: unit_test, path: "test" } ]
    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    env:
      TEST_DIR: components/esp_websocket_client/${{ matrix.test.path }}
    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Build ${{ matrix.example }} with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          python -m pip install idf-build-apps
          python ./ci/build_apps.py ${TEST_DIR}
          cd ${TEST_DIR}
          for dir in `ls -d build_esp32_*`; do
          $GITHUB_WORKSPACE/ci/clean_build_artifacts.sh `pwd`/$dir
          zip -qur artifacts.zip $dir
          done
      - uses: actions/upload-artifact@v4
        with:
          name: websocket_bin_esp32_${{ matrix.idf_ver }}_${{ matrix.test.app }}
          path: ${{ env.TEST_DIR }}/artifacts.zip
          if-no-files-found: error

  run-target-websocket:
    # Skip running on forks since it won't have access to secrets
    if: |
      github.repository == 'espressif/esp-protocols' &&
      ( contains(github.event.pull_request.labels.*.name, 'websocket') || github.event_name == 'push' )
    name: Target test
    needs: build_websocket
    strategy:
      fail-fast: false
      matrix:
        idf_ver: ["release-v5.0", "release-v5.1", "release-v5.2", "release-v5.3", "latest"]
        idf_target: ["esp32"]
        test: [ { app: example, path: "examples/target" }, { app: unit_test, path: "test" } ]
    runs-on:
      - self-hosted
      - ESP32-ETHERNET-KIT
    env:
      TEST_DIR: components/esp_websocket_client/${{ matrix.test.path }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          name: websocket_bin_esp32_${{ matrix.idf_ver }}_${{ matrix.test.app }}
          path: ${{ env.TEST_DIR }}/ci/
      - name: Install Python packages
        env:
          PIP_EXTRA_INDEX_URL: "https://www.piwheels.org/simple"
        run: |
          pip install --only-binary cryptography --extra-index-url https://dl.espressif.com/pypi/ -r $GITHUB_WORKSPACE/ci/requirements.txt
      - name: Run Example Test on target
        working-directory: ${{ env.TEST_DIR }}
        run: |
          unzip ci/artifacts.zip -d ci
          for dir in `ls -d ci/build_*`; do
          rm -rf build sdkconfig.defaults
          mv $dir build
          python -m pytest --log-cli-level DEBUG --junit-xml=./results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${dir#"ci/build_"}.xml --target=${{ matrix.idf_target }}
          done
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}.xml
          path: components/esp_websocket_client/${{ matrix.test.path }}/*.xml
