#!/bin/bash
################################################################################
# Test Suite for Reforming Equilibrium Calculator
################################################################################
#
# This script runs automated tests to verify the program works correctly
# 
# Usage: ./test_cases.sh
#
################################################################################

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Counters
PASSED=0
FAILED=0
TOTAL=0

# Program name
PROGRAM="./Reforming_delG_Min"

# Check if program exists
if [ ! -f "$PROGRAM" ]; then
    echo -e "${RED}ERROR: $PROGRAM not found!${NC}"
    echo "Please compile first: make"
    exit 1
fi

################################################################################
# Test Helper Functions
################################################################################

run_test() {
    local test_name="$1"
    local test_cmd="$2"
    local expected_pattern="$3"
    
    TOTAL=$((TOTAL + 1))
    
    echo -e "${YELLOW}Test $TOTAL: $test_name${NC}"
    
    # Run the command and capture output
    output=$(eval "$test_cmd" 2>&1)
    exit_code=$?
    
    # Check if pattern is found in output
    if echo "$output" | grep -q "$expected_pattern"; then
        echo -e "  ${GREEN}✓ PASSED${NC}"
        PASSED=$((PASSED + 1))
        return 0
    else
        echo -e "  ${RED}✗ FAILED${NC}"
        echo "  Expected pattern: $expected_pattern"
        echo "  Got output:"
        echo "$output" | head -5
        FAILED=$((FAILED + 1))
        return 1
    fi
}

run_test_exit_code() {
    local test_name="$1"
    local test_cmd="$2"
    local expected_exit="$3"
    
    TOTAL=$((TOTAL + 1))
    
    echo -e "${YELLOW}Test $TOTAL: $test_name${NC}"
    
    # Run the command
    eval "$test_cmd" > /dev/null 2>&1
    exit_code=$?
    
    # Check exit code
    if [ $exit_code -eq $expected_exit ]; then
        echo -e "  ${GREEN}✓ PASSED${NC}"
        PASSED=$((PASSED + 1))
        return 0
    else
        echo -e "  ${RED}✗ FAILED${NC}"
        echo "  Expected exit code: $expected_exit"
        echo "  Got exit code: $exit_code"
        FAILED=$((FAILED + 1))
        return 1
    fi
}

################################################################################
# Test Cases
################################################################################

echo ""
echo "Starting test suite..."
echo ""

# Test 1: Help flag
run_test \
    "Help flag (--help)" \
    "$PROGRAM --help" \
    "Usage:"

# Test 2: Version flag (looks for version number pattern like 2.0.0)
run_test \
    "Version flag (--version)" \
    "$PROGRAM --version" \
    "[0-9]\+\.[0-9]"

# Test 3: Default run (should work)
run_test_exit_code \
    "Default run (no arguments)" \
    "$PROGRAM" \
    0

# Test 4: Methane reforming with default temperature
run_test_exit_code \
    "Methane reforming (CH4 + H2O)" \
    "$PROGRAM --CH4 1.0 --H2O 3.0" \
    0

# Test 5: Custom temperature
run_test_exit_code \
    "Custom temperature (1123 K)" \
    "$PROGRAM --CH4 1.0 --H2O 3.0 --temp 1123" \
    0

# Test 6: Custom pressure
run_test_exit_code \
    "Custom pressure (2 atm)" \
    "$PROGRAM --CH4 1.0 --H2O 3.0 --pres 2.0" \
    0

# Test 7: Methanol reforming
run_test_exit_code \
    "Methanol reforming (CH3OH + H2O)" \
    "$PROGRAM --CH3OH 1.0 --H2O 1.0" \
    0

# Test 8: With CO2 addition
run_test_exit_code \
    "With CO2 addition" \
    "$PROGRAM --CH4 1.0 --H2O 2.0 --CO2 0.5" \
    0

# Test 9: Output to file
run_test_exit_code \
    "Output to CSV file" \
    "$PROGRAM --CH4 1.0 --H2O 3.0 --output test_output.csv" \
    0

# Clean up test output file
rm -f test_output.csv

# Test 10: Error handling - negative moles (should fail or warn)
run_test \
    "Error handling: negative moles" \
    "$PROGRAM --CH4 -1.0 --H2O 3.0" \
    "error\|Error\|ERROR\|invalid\|Invalid"

################################################################################
# Test Summary
################################################################################

echo ""
echo "============================================"
echo "  Test Summary"
echo "============================================"
echo ""
echo "Tests passed: $PASSED / $TOTAL"
echo "Tests failed: $FAILED / $TOTAL"
echo ""

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}All tests passed! ✓${NC}"
    echo ""
    exit 0
else
    echo -e "${RED}Some tests failed! ✗${NC}"
    echo ""
    exit 1
fi
