#!/usr/bin/env bash
#clear
#export PS4='\e[90m+${LINENO} in ${#BASH_SOURCE[@]}>${FUNCNAME[0]}:${BASH_SOURCE[@]##*/} \e[0m'
#set -x
TEST_CATEGORY=core

level=$1
if [ -z "$level" ]; then
  level=1
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$level>"

source this
source test.suite

log.level $level

# ============================================================================
# META-TEST: This test validates that test.suite correctly counts pass/fail
# ============================================================================
#
# IMPORTANT: This test file contains ONE INTENTIONAL FAILURE to verify that
# the test.suite correctly counts and reports failures.
#
# Expected results:
#   - Total assertions: 7
#   - Passed: 6
#   - Failed: 1 (the intentional failure)
#
# If all 5 tests pass, the counter logic is BROKEN!
# If more than 1 test fails, there's a real bug.
#
# DO NOT "FIX" THE INTENTIONAL FAILURE - it's testing the test framework!
# ============================================================================

echo ""
echo -e "\e[1;35m╔════════════════════════════════════════════════════════════════════╗\e[0m"
echo -e "\e[1;35m║  META-TEST: Testing the test.suite framework itself               ║\e[0m"
echo -e "\e[1;35m║  Expected: 4 PASS, 1 FAIL (intentional)                           ║\e[0m"
echo -e "\e[1;35m╚════════════════════════════════════════════════════════════════════╝\e[0m"

# Store initial counter values
INITIAL_TEST_COUNT=$TEST_SUITE_TEST_COUNTER
INITIAL_EXPECT_COUNT=$TEST_SUITE_EXPECT_COUNTER
INITIAL_SUCCESS_COUNT=$TEST_SUITE_SUCCESS_COUNTER

# ============================================================================
# Test 1: expect.pass increments both counters
# ============================================================================

test.case $level "expect.pass - increments counters correctly" \
  true

BEFORE_EXPECT=$TEST_SUITE_EXPECT_COUNTER
BEFORE_SUCCESS=$TEST_SUITE_SUCCESS_COUNTER

expect.pass "this should pass and increment both counters"

if [ "$TEST_SUITE_EXPECT_COUNTER" -eq $((BEFORE_EXPECT + 1)) ] && \
   [ "$TEST_SUITE_SUCCESS_COUNTER" -eq $((BEFORE_SUCCESS + 1)) ]; then
  expect.pass "expect.pass incremented EXPECT and SUCCESS counters"
else
  expect.fail "expect.pass should increment both counters"
fi

# ============================================================================
# Test 2: expect.fail increments only EXPECT counter (not SUCCESS)
# ============================================================================

test.case $level "expect.fail - increments only EXPECT counter" \
  true

BEFORE_EXPECT=$TEST_SUITE_EXPECT_COUNTER
BEFORE_SUCCESS=$TEST_SUITE_SUCCESS_COUNTER

# We'll check the delta after calling expect.fail
# Note: We need to call it but it will show as a failure - that's expected

# ============================================================================
# INTENTIONAL FAILURE - DO NOT FIX
# ============================================================================
# This failure is BY DESIGN to verify that:
# 1. expect.fail properly increments EXPECT_COUNTER but not SUCCESS_COUNTER
# 2. The final summary correctly shows 1 failure
# 3. The test framework is working correctly
#
# If you see this test passing, the test framework is BROKEN!
# ============================================================================
echo -e "\e[1;35m  ┌─────────────────────────────────────────────────────────────────┐\e[0m"
echo -e "\e[1;35m  │ INTENTIONAL FAILURE BELOW - THIS VERIFIES COUNTER LOGIC        │\e[0m"
echo -e "\e[1;35m  └─────────────────────────────────────────────────────────────────┘\e[0m"

expect.fail "INTENTIONAL: This failure tests that failures are counted correctly"

# Verify expect.fail only incremented EXPECT, not SUCCESS
if [ "$TEST_SUITE_EXPECT_COUNTER" -eq $((BEFORE_EXPECT + 1)) ] && \
   [ "$TEST_SUITE_SUCCESS_COUNTER" -eq "$BEFORE_SUCCESS" ]; then
  expect.pass "expect.fail correctly incremented only EXPECT counter"
else
  expect.fail "expect.fail should only increment EXPECT counter"
fi

# ============================================================================
# Test 3: test.case increments TEST_COUNTER
# ============================================================================

BEFORE_TEST=$TEST_SUITE_TEST_COUNTER

test.case $level "test.case - increments TEST counter" \
  true

if [ "$TEST_SUITE_TEST_COUNTER" -eq $((BEFORE_TEST + 1)) ]; then
  expect.pass "test.case incremented TEST_COUNTER"
else
  expect.fail "test.case should increment TEST_COUNTER"
fi

# ============================================================================
# Test 4: private.test.suite.category reads core from a core test file
# ============================================================================

test.case $level "private.test.suite.category reads core marker" \
  private.test.suite.category "$OOSH_DIR/test/test.this"
expect 0 "core" "test.this is categorized as core"

# ============================================================================
# Test 5: private.test.suite.category defaults to extended for unmarked files
# ============================================================================

test.case $level "private.test.suite.category defaults to extended" \
  private.test.suite.category "$OOSH_DIR/test/test.claudeCode"
expect 0 "extended" "test.claudeCode defaults to extended"

# ============================================================================
# Final verification
# ============================================================================

echo ""
echo -e "\e[1;35m╔════════════════════════════════════════════════════════════════════╗\e[0m"
echo -e "\e[1;35m║  META-TEST VERIFICATION                                           ║\e[0m"
echo -e "\e[1;35m╚════════════════════════════════════════════════════════════════════╝\e[0m"

TOTAL_EXPECTS=$((TEST_SUITE_EXPECT_COUNTER - INITIAL_EXPECT_COUNT))
TOTAL_SUCCESS=$((TEST_SUITE_SUCCESS_COUNTER - INITIAL_SUCCESS_COUNT))
TOTAL_FAILED=$((TOTAL_EXPECTS - TOTAL_SUCCESS))

echo ""
echo -e "  This test run:"
echo -e "    Assertions: $TOTAL_EXPECTS"
echo -e "    Passed:     $TOTAL_SUCCESS"
echo -e "    Failed:     $TOTAL_FAILED (should be exactly 1)"
echo ""

if [ "$TOTAL_FAILED" -eq 1 ]; then
  echo -e "\e[1;32m  ✓ META-TEST PASSED: Counter logic is working correctly!\e[0m"
  echo -e "\e[1;32m    (The 1 failure above is intentional and expected)\e[0m"
else
  echo -e "\e[1;31m  ✗ META-TEST FAILED: Expected exactly 1 failure, got $TOTAL_FAILED\e[0m"
  echo -e "\e[1;31m    The test.suite counter logic may be broken!\e[0m"
fi

echo ""

# ============================================================================
# Test Summary
# ============================================================================

test.suite.save.results
