name: 'Kubevela K8s Upgrade Unit Test'
description: 'Runs Kubevela K8s upgrade unit tests, uploads coverage, and collects diagnostics on failure.'

inputs:
  codecov-token:
    description: 'Codecov token for uploading coverage reports'
    required: false
    default: ''
  codecov-enable:
    description: 'Enable codecov coverage upload'
    required: false
    default: 'false'

runs:
  using: "composite"
  steps:
    # ========================================================================
    # Environment Setup
    # ========================================================================
    - name: Configure environment setup
      uses: ./.github/actions/env-setup

    # ========================================================================
    # Unit Test Execution
    # ========================================================================
    - name: Run unit tests
      shell: bash
      run: |
        echo "Running unit tests..."
        make test

    - name: Upload coverage report
      if: ${{ inputs.codecov-enable == 'true' }}
      uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24
      with:
        token: ${{ inputs.codecov-token }}
        files: ./coverage.txt
        flags: core-unittests
        name: codecov-umbrella
        fail_ci_if_error: false

    # ========================================================================
    # Failure Diagnostics
    # ========================================================================
    - name: Collect failure diagnostics
      if: failure()
      shell: bash
      run: |
        echo "=== FAILURE DIAGNOSTICS ==="
        echo "Collecting diagnostic information for debugging..."

        echo "--- Go Environment ---"
        go version || true
        go env || true

        echo "--- Cluster Status ---"
        kubectl get nodes -o wide || true
        kubectl get pods -A || true

        echo "--- KubeVela System Logs ---"
        kubectl logs -n vela-system -l app.kubernetes.io/name=vela-core --tail=100 || true

        echo "--- Recent Events ---"
        kubectl get events -A --sort-by='.lastTimestamp' --field-selector type!=Normal || true

        echo "--- Test Artifacts ---"
        find . -name "*.log" -o -name "*test*.xml" -o -name "coverage.*" | head -20 || true