name: Test Report

on:
  workflow_call:
    inputs:
      core_matrix:
        required: false
        type: string
        default: '[]'
        description: Core shard matrix JSON
      tensor_matrix:
        required: false
        type: string
        default: '[]'
        description: Tensor shard matrix JSON
      distributed_matrix:
        required: false
        type: string
        default: '[]'
        description: Distributed shard matrix JSON
      graph_matrix:
        required: false
        type: string
        default: '[]'
        description: Graph shard matrix JSON
      others_matrix:
        required: false
        type: string
        default: '[]'
        description: Others shard matrix JSON
      docker_image:
        required: false
        type: string
        default: 'quay.io/kerer/pytorch:torch-npu-test-aarch64-cann-a3-py3.10-torch-nightly'
        description: Docker image used for test execution
      pytorch_short:
        required: false
        type: string
        default: 'unknown'
        description: Short SHA of upstream PyTorch
      torch_npu_short:
        required: false
        type: string
        default: 'unknown'
        description: Short SHA of torch_npu
jobs:
  report:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Checkout this repo for report scripts
        uses: actions/checkout@v6
        with:
          ref: ci-test

      - name: Download core shard reports
        uses: actions/download-artifact@v4
        with:
          pattern: test-reports-core-*
          path: all-test-reports
          merge-multiple: true
        continue-on-error: true

      - name: Download tensor shard reports
        uses: actions/download-artifact@v4
        with:
          pattern: test-reports-tensor-*
          path: all-test-reports
          merge-multiple: true
        continue-on-error: true

      - name: Download distributed shard reports
        uses: actions/download-artifact@v4
        with:
          pattern: test-reports-dist-*
          path: all-test-reports
          merge-multiple: true
        continue-on-error: true

      - name: Download graph shard reports
        uses: actions/download-artifact@v4
        with:
          pattern: test-reports-graph-*
          path: all-test-reports
          merge-multiple: true
        continue-on-error: true

      - name: Download others shard reports
        uses: actions/download-artifact@v4
        with:
          pattern: test-reports-others-*
          path: all-test-reports
          merge-multiple: true
        continue-on-error: true

      - name: Download cases collection summary
        uses: actions/download-artifact@v4
        with:
          name: cases-shards
          path: cases-shards
        continue-on-error: true

      - name: Generate consolidated summary
        id: generate_report
        run: |
          REPORT_MD=npu-full-test-summary.md
          REPORT_JSON=npu-full-test-summary.json

          # Build combined matrix with category prefixes
          COMBINED_MATRIX=$(python3 -c "
          import json
          mats = {
              'core': '${{ inputs.core_matrix }}',
              'tensor': '${{ inputs.tensor_matrix }}',
              'distributed': '${{ inputs.distributed_matrix }}',
              'graph': '${{ inputs.graph_matrix }}',
              'others': '${{ inputs.others_matrix }}',
          }
          pfx = {
              'core': 'core', 'tensor': 'tensor',
              'distributed': 'dist', 'graph': 'graph', 'others': 'others',
          }
          combined = []
          for cat, ms in mats.items():
              for n in json.loads(ms):
                  combined.append(f'{pfx[cat]}-{n}')
          print(json.dumps(combined))
          ")

          echo "Combined matrix: ${COMBINED_MATRIX}"

          # Copy skipped cases record from collection artifact
          cp cases-shards/skipped_cases.json skipped_cases.json 2>/dev/null || \
            echo "No skipped_cases.json found in cases-shards"

          python3 .github/scripts/generate_npu_full_test_report.py \
            --reports-root all-test-reports \
            --output-markdown ${REPORT_MD} \
            --output-json ${REPORT_JSON} \
            --pytorch-version "${{ inputs.pytorch_short }}" \
            --torch-npu-short "${{ inputs.torch_npu_short }}" \
            --shard-matrix-json "${COMBINED_MATRIX}" \
            --docker-image "${{ inputs.docker_image }}" \
            --cases-summary cases-shards/cases_collection_summary.json \
            --cases-by-file-dir cases-shards

          cat ${REPORT_MD} >> $GITHUB_STEP_SUMMARY

      - name: Upload consolidated summary
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: npu-full-test-summary
          path: |
            npu-full-test-summary.md
            npu-full-test-summary.json
            core_cases_results_by_file.jsonl
            tensor_cases_results_by_file.jsonl
            distributed_cases_results_by_file.jsonl
            graph_cases_results_by_file.jsonl
            others_cases_results_by_file.jsonl
            skipped_cases.json
          if-no-files-found: warn
          retention-days: 60

      - name: Send Feishu notification
        if: always()
        continue-on-error: true
        env:
          FEISHU_WEBHOOK_URL: ${{ secrets.FEISHU_WEBHOOK_URL }}
        run: |
          python3 .github/scripts/send_feishu_notification.py \
            --report-json npu-full-test-summary.json \
            --skipped-cases skipped_cases.json