#!/bin/bash

################################################################################
# Run Specific Tests Script
#
# Simple script to run specific test classes for fast iteration.
# Reads test classes from test-results/failed_tests.txt (auto-generated by run_all_tests.sh)
# or you can edit the file manually.
#
# Usage:
#   ./run_specific_tests.sh
#
# Configuration:
#   Option 1: Edit test-results/failed_tests.txt (recommended)
#   Option 2: Edit SPECIFIC_TESTS array in this script
#
# File format (test-results/failed_tests.txt):
#   One test class per line with full package path
#   Lines starting with # are comments
#   Example: code.api.v6_0_0.RateLimitsTest
#
# IMPORTANT: ScalaTest requires full package path!
#   - Must include: code.api.vX_X_X.TestClassName
#   - Do NOT use just "TestClassName"
#   - Do NOT include .scala extension
#
# How to find package path:
#   1. Find test file: obp-api/src/test/scala/code/api/v6_0_0/RateLimitsTest.scala
#   2. Package path: code.api.v6_0_0.RateLimitsTest
#
# Output:
#   - test-results/last_specific_run.log
#   - test-results/last_specific_run_summary.log
#
# Technical Note:
#   Uses Maven -Dsuites parameter (NOT -Dtest) because we use scalatest-maven-plugin
#   The -Dtest parameter is for surefire plugin and doesn't work with ScalaTest
################################################################################

set -e

################################################################################
# CONFIGURATION
################################################################################

FAILED_TESTS_FILE="test-results/failed_tests.txt"

# Test class names - MUST include full package path for ScalaTest!
# This will be overridden if test-results/failed_tests.txt exists
# Format: "code.api.vX_X_X.TestClassName"
# Example: "code.api.v6_0_0.RateLimitsTest"
SPECIFIC_TESTS=(
  "code.api.v6_0_0.RateLimitsTest"
)

################################################################################
# Script Logic
################################################################################

LOG_DIR="test-results"
DETAIL_LOG="${LOG_DIR}/last_specific_run.log"
SUMMARY_LOG="${LOG_DIR}/last_specific_run_summary.log"

mkdir -p "${LOG_DIR}"

# Read tests from file if it exists, otherwise use SPECIFIC_TESTS array
if [ -f "${FAILED_TESTS_FILE}" ]; then
  echo "Reading test classes from: ${FAILED_TESTS_FILE}"
  # Read non-empty, non-comment lines from file into array
  mapfile -t SPECIFIC_TESTS < <(grep -v '^\s*#' "${FAILED_TESTS_FILE}" | grep -v '^\s*$')
  echo "Loaded ${#SPECIFIC_TESTS[@]} test(s) from file"
  echo ""
fi

# Check if tests are configured
if [ ${#SPECIFIC_TESTS[@]} -eq 0 ]; then
  echo "ERROR: No tests configured!"
  echo "Either:"
  echo "  1. Run ./run_all_tests.sh first to generate ${FAILED_TESTS_FILE}"
  echo "  2. Create ${FAILED_TESTS_FILE} manually with test class names"
  echo "  3. Edit this script and add test names to SPECIFIC_TESTS array"
  exit 1
fi

echo "=========================================="
echo "Running Specific Tests"
echo "=========================================="
echo ""
echo "Tests to run:"
for test in "${SPECIFIC_TESTS[@]}"; do
  echo "  - $test"
done
echo ""
echo "Logs: ${DETAIL_LOG}"
echo ""

# Set Maven options
export MAVEN_OPTS="-Xss128m -Xms3G -Xmx6G -XX:MaxMetaspaceSize=2G --add-opens java.base/java.lang.invoke=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED"

# Build test list (space-separated for ScalaTest -Dsuites)
TEST_ARG="${SPECIFIC_TESTS[*]}"

# Start time
START_TIME=$(date +%s)

# Run tests
# NOTE: We use -Dsuites (NOT -Dtest) because obp-api uses scalatest-maven-plugin
# The -Dtest parameter only works with maven-surefire-plugin (JUnit tests)
# ScalaTest requires the -Dsuites parameter with full package paths
echo "Executing: mvn -pl obp-api test -Dsuites=\"$TEST_ARG\""
echo ""

if mvn -pl obp-api test -Dsuites="$TEST_ARG" 2>&1 | tee "${DETAIL_LOG}"; then
  TEST_RESULT="SUCCESS"
else
  TEST_RESULT="FAILURE"
fi

# End time
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
DURATION_MIN=$((DURATION / 60))
DURATION_SEC=$((DURATION % 60))

# Write summary
{
  echo "=========================================="
  echo "Test Run Summary"
  echo "=========================================="
  echo "Result:   ${TEST_RESULT}"
  echo "Duration: ${DURATION_MIN}m ${DURATION_SEC}s"
  echo ""
  echo "Tests Run:"
  for test in "${SPECIFIC_TESTS[@]}"; do
    echo "  - $test"
  done
  echo ""
  echo "Logs:"
  echo "  ${DETAIL_LOG}"
  echo "  ${SUMMARY_LOG}"
} | tee "${SUMMARY_LOG}"

echo ""
echo "=========================================="
echo "Done!"
echo "=========================================="

# Exit with test result
if [ "$TEST_RESULT" = "FAILURE" ]; then
  exit 1
fi
