# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'Test Report'
on:
  workflow_run:
    workflows: ['NVIDIA Test Github Validation']
    types:
      - completed
jobs:
  test-results:
    name: Test Results
    runs-on: ubuntu-latest
    if: github.event.workflow_run.conclusion != 'skipped'

    permissions:
      checks: write

      # required by download step to access artifacts API
      actions: read
      # needed unless run with comment_mode: off
      pull-requests: write

      # only needed for private repository
      contents: read
      issues: read

    steps:
      - name: Download and Extract Artifacts
        uses: dawidd6/action-download-artifact@20319c5641d495c8a52e688b7dc5fada6c3a9fbc # v8
        with:
           run_id: ${{ github.event.workflow_run.id }}
           path: artifacts

      - name: Summarize Test Results as Markdown Table
        run: |
          sudo apt-get update -y
          sudo apt-get install -y libxml2-utils
          echo "## Unit Test Results Summary" >> $GITHUB_STEP_SUMMARY
          echo "# This test report is a summary of the test run in workflow run: [${{ steps.set_test_run_url.outputs.test_run_url }}](${{ steps.set_test_run_url.outputs.test_run_url }})" >> $GITHUB_STEP_SUMMARY
          echo "| Test Suite | Tests | Passed | Failed | Skipped | Duration |" >> $GITHUB_STEP_SUMMARY
          echo "|------------|-------|--------|--------|---------|----------|" >> $GITHUB_STEP_SUMMARY
          total_tests=0
          total_passed=0
          total_failed=0
          total_skipped=0
          total_duration=0
          for file in artifacts/**/*.xml; do
            if [ -f "$file" ]; then
              suite_name=$(basename "$(dirname "$file")")
              tests=$(xmllint --xpath "count(//testcase)" "$file")
              passed=$(xmllint --xpath "count(//testcase[not(failure) and not(error) and not(skipped)])" "$file")
              failed=$(xmllint --xpath "count(//testcase[failure or error])" "$file")
              skipped=$(xmllint --xpath "count(//testcase[skipped])" "$file")
              duration=$(xmllint --xpath "sum(//testcase/@time)" "$file")
              # Debugging: Output values for each file
              echo "Processing file: $file"
              echo "Suite: $suite_name, Tests: $tests, Passed: $passed, Failed: $failed, Skipped: $skipped, Duration: $duration"
              echo "| $suite_name | $tests | $passed | $failed | $skipped | ${duration}s |" >> $GITHUB_STEP_SUMMARY
              total_tests=$((total_tests + tests))
              total_passed=$((total_passed + passed))
              total_failed=$((total_failed + failed))
              total_skipped=$((total_skipped + skipped))
              total_duration=$(echo "$total_duration + $duration" | bc)
            fi
          done
          echo "Total tests: $total_tests"
          echo "Total passed: $total_passed"
          echo "Total failed: $total_failed"
          echo "Total skipped: $total_skipped"
          echo "Total duration: $total_duration"
          echo "|------------|-------|--------|--------|---------|----------|" >> $GITHUB_STEP_SUMMARY
          echo "| **Total** | $total_tests | $total_passed | $total_failed | $total_skipped | ${total_duration}s |" >> $GITHUB_STEP_SUMMARY
