name: "asio: build/target-tests"

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

jobs:
  build_asio:
    if: contains(github.event.pull_request.labels.*.name, 'asio') || github.event_name == 'push'
    name: Build
    strategy:
      matrix:
        idf_ver: ["latest", "release-v5.0", "release-v5.1", "release-v5.2", "release-v5.3"]
        idf_target: ["esp32", "esp32s2"]
        example: ["asio_chat", "async_request", "socks4", "ssl_client_server", "tcp_echo_server", "udp_echo_server"]
    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    env:
      TEST_DIR: components/asio/examples
    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 }}
        working-directory: ${{ env.TEST_DIR }}/${{ matrix.example }}
        env:
          IDF_TARGET: ${{ matrix.idf_target }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          test -f sdkconfig.ci && cat sdkconfig.ci >> sdkconfig.defaults || echo "No sdkconfig.ci"
          idf.py set-target ${{ matrix.idf_target }}
          idf.py build
      - name: Merge binaries with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }} for ${{ matrix.example }}
        working-directory: ${{ env.TEST_DIR }}/${{ matrix.example }}/build
        env:
          IDF_TARGET: ${{ matrix.idf_target }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          esptool.py --chip ${{ matrix.idf_target }} merge_bin --fill-flash-size 4MB -o flash_image.bin @flash_args
      - uses: actions/upload-artifact@v4
        with:
          name: examples_app_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.example }}
          path: |
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build/bootloader/bootloader.bin
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build//partition_table/partition-table.bin
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build/*.bin
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build/*.elf
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build/flasher_args.json
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build/config/sdkconfig.h
            ${{ env.TEST_DIR }}/${{ matrix.example }}/build/config/sdkconfig.json
          if-no-files-found: error

  target_tests_asio:
    # 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, 'asio') || github.event_name == 'push' )
    name: Target tests
    strategy:
      matrix:
        idf_ver: ["latest", "release-v5.0", "release-v5.2", "release-v5.3"]
        idf_target: ["esp32"]
        example: ["asio_chat", "tcp_echo_server", "udp_echo_server", "ssl_client_server"]
    needs: build_asio
    runs-on:
      - self-hosted
      - ESP32-ETHERNET-KIT
    env:
      TEST_DIR: components/asio/examples
    steps:
      - name: Clear repository
        run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          name: examples_app_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.example }}
          path: ${{ env.TEST_DIR }}/${{ matrix.example }}/build
      - name: Install Python packages
        env:
          PIP_EXTRA_INDEX_URL: "https://www.piwheels.org/simple"
        run: |
            sudo apt-get install -y dnsutils
      - name: Download Example Test to target ${{ matrix.config }}
        run: |
            python -m esptool --chip ${{ matrix.idf_target }} write_flash 0x0 ${{ env.TEST_DIR }}/${{ matrix.example }}/build/flash_image.bin
      - name: Run Example Test ${{ matrix.example }} on target
        working-directory: ${{ env.TEST_DIR }}/${{ matrix.example }}
        run: |
            python -m pytest --log-cli-level DEBUG --junit-xml=./examples_results_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.config }}.xml --target=${{ matrix.idf_target }}
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: examples_results_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.example }}
          path: ${{ env.TEST_DIR }}/${{ matrix.example }}/*.xml
