#!/usr/bin/env bash
# ==============================================================================
# XapBug Engine V2 Official Reproduction Script (2026-12-02)
# Independently verifies shipped Engine V2 Pure-AST (--engine v2 default):
#   1. Core Multi-File AST (101/101), 50-Lang Auto-Repair (72/72), Self-Heal (10/10)
#   2. Release Gate (#28: 1,000 Clean Gate A, Compile/ASan Gate B, 10K CRLF Gate C)
#   3. Real-World Open-Source Audit (Tier 1/2/3 across 37+ languages)
#   4. Unmodified Official NIST Juliet v1.3 (6,224-variant V2 raw baseline + H2H)
# ==============================================================================
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
JULIET_DIR="${ROOT_DIR}/tests/fixtures/official_juliet"

echo "=== [1/4] Verifying Core Engine V2 Pure-AST, Auto-Repair & Self-Healing ==="
node "${ROOT_DIR}/tests/test_engine_v2_multifile.js"
node "${ROOT_DIR}/tests/test_50lang_autorepair.js"
python3 "${ROOT_DIR}/tests/test_self_heal_suite.py"

echo "=== [2/4] Running Release Gate (#28 — False Repair 0%, Compile/ASan, 10K Stress) ==="
python3 "${ROOT_DIR}/scripts/run_release_gate_benchmark.py"

echo "=== [3/4] Running Global Open-Source Clean Rate Audit (Engine V2) ==="
python3 "${ROOT_DIR}/scripts/run_realworld_oss_audit.py"
python3 "${ROOT_DIR}/scripts/run_multilang_oss_audit.py"
python3 "${ROOT_DIR}/scripts/run_cpp_cs_oss_audit.py"
python3 "${ROOT_DIR}/scripts/run_tier2_oss_audit.py"

echo "=== [4/4] Checking Official NIST Juliet v1.3 Corpus (Raw V2 Measurement) ==="
if [ ! -d "${JULIET_DIR}/testcases/CWE401_Memory_Leak" ]; then
  echo "Cloning official arichardson/juliet-test-suite-c into ${JULIET_DIR}..."
  mkdir -p "$(dirname "${JULIET_DIR}")"
  git clone --depth 1 https://github.com/arichardson/juliet-test-suite-c.git "${JULIET_DIR}"
fi
python3 "${ROOT_DIR}/scripts/run_official_full_benchmark.py"

echo "✅ Engine V2 independent reproduction complete! Reports saved to ${ROOT_DIR}/tests/reports/"
