#!/bin/bash
# ====================================================
# PR-A Baseline Test Suite Runner
# OpenClaw Agent OS v2 — Architecture Stabilization
# ====================================================
# Freezes current system behavior as safety net
# for subsequent architecture refactoring.
# ====================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
TEST_DIR="$SCRIPT_DIR/../test/baseline"
WORKSPACE="$(cd "$SCRIPT_DIR/../.." && pwd)"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${CYAN}  OpenClaw Agent OS v2 — PR-A Baseline Test${NC}"
echo -e "${CYAN}  $(date '+%Y-%m-%d %H:%M:%S %Z')${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""

PASS=0
FAIL=0
SKIP=0
RESULTS=""

run_test() {
  local file="$1"
  local name
  name="$(basename "$file" .test.js)"
  
  echo -e "${YELLOW}[RUN ]${NC} $name"
  
  if node --test "$file" 2>&1; then
    PASS=$((PASS + 1))
    RESULTS="$RESULTS  [PASS] $name\n"
    echo -e "${GREEN}[PASS]${NC} $name"
  else
    FAIL=$((FAIL + 1))
    RESULTS="$RESULTS  [FAIL] $name\n"
    echo -e "${RED}[FAIL]${NC} $name"
  fi
  echo ""
}

# Run all 12 tests
for i in $(seq -w 1 12); do
  test_file="$TEST_DIR/test-${i}-*.test.js"
  # Expand glob
  for f in $test_file; do
    if [ -f "$f" ]; then
      run_test "$f"
    fi
  done
done

echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${CYAN}  Results Summary${NC}"
echo -e "${CYAN}============================================${NC}"
echo -e "$RESULTS"
echo ""
echo -e "Total: $((PASS + FAIL)) | ${GREEN}Pass: $PASS${NC} | ${RED}Fail: $FAIL${NC}"

if [ $FAIL -eq 0 ]; then
  echo ""
  echo -e "${GREEN}✓ ALL BASELINE TESTS PASSED${NC}"
  echo -e "${GREEN}  System behavior frozen — safe to proceed to PR-B${NC}"
  exit 0
else
  echo ""
  echo -e "${RED}✗ SOME TESTS FAILED${NC}"
  echo -e "${RED}  Review failures before proceeding to PR-B${NC}"
  exit 1
fi
