# Add to .github/workflows/Run_LabWired.yaml in a fork of
# https://github.com/Arm-Examples/CMSIS-Zephyr.
# Put rtt-smoke.yaml and system.yaml in .github/labwired/.
name: Run CMSIS-Zephyr in LabWired

on:
  workflow_run:
    workflows: ["Build NUCLEO-H563ZI Debug HIL"]
    types: [completed]

permissions:
  contents: read
  actions: read

jobs:
  simulate:
    if: >-
      github.event.workflow_run.conclusion == 'success' &&
      github.event.workflow_run.event == 'push'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Check out the commit that produced the firmware
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.head_sha }}

      - name: Download the firmware from this build run
        uses: actions/download-artifact@v4
        with:
          name: NUCLEO-H563ZI@Debug_HIL
          path: out
          run-id: ${{ github.event.workflow_run.id }}
          github-token: ${{ github.token }}

      - name: Install LabWired v0.25.0
        shell: bash
        run: |
          mkdir -p "$RUNNER_TEMP/labwired-cli"
          curl --fail --location --retry 3 \
            https://github.com/w1ne/labwired-core/releases/download/v0.25.0/labwired-v0.25.0-linux-x86_64.tar.gz \
            --output "$RUNNER_TEMP/labwired-cli.tar.gz"
          tar -xzf "$RUNNER_TEMP/labwired-cli.tar.gz" -C "$RUNNER_TEMP/labwired-cli"
          echo "$RUNNER_TEMP/labwired-cli" >> "$GITHUB_PATH"

      - name: Assert the RTT output and capture PB0
        run: |
          labwired --version
          labwired test --script .github/labwired/rtt-smoke.yaml \
            --rtt --watch-gpio gpiob:0 \
            --output-dir out/labwired \
            --junit out/labwired/junit.xml

      - name: Show the RTT log
        if: always()
        shell: bash
        run: |
          if [ -f out/labwired/rtt.log ]; then
            cat out/labwired/rtt.log
          else
            echo "No RTT log was produced; check the simulator step."
          fi

      - name: Upload simulation evidence
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: cmsis-zephyr-labwired
          path: out/labwired/
          if-no-files-found: warn
          retention-days: 14
