#! /bin/bash


# import and run all tests


##########
# Colors
##########


_GREY='\033[30m'
_RED='\033[0;31m'
_GREEN='\033[32m'
_YELLOW='\033[33m'
_BLUE='\033[34m'
_PURPLE='\033[35m'
_CYAN='\033[36m'
_WHITE='\033[37m'
_END='\033[0m'


#test_file_names=("test_template.sh" "test_header.sh" "test_path.sh")
test_file_names=("test_method.sh" "test_header.sh" "test_path.sh" "test_valid_uri.sh" "test_delete.sh")

# Run this separately cuz of the content length subtlty
#test_file_names=("test_body.sh")

# kinda fucking useless, easier to test IRL
#test_file_names=("test_port.sh")

test_files=()
expected_result_files=()

fill_test_files()
{
	for i in "${test_file_names[@]}"
	do
		test_files+=("./Tester/$i")
	done
}

# needs to be invoked for each test_file!
# acutally kinda useless...
fill_expected_files()
{
	for i in "${local_expected_test_files[@]}"
	do
		expected_result_files+=("./Tester/expected_results/$i")
	done
}



test_all()
{

	arg=${1}

	make

	rm -rf telnet.log
	rm -rf webserv.log
	rm -rf compare.txt
	rm -rf expected.txt

	fill_test_files

	for i in "${test_files[@]}"
	do
		source $i
		source ./Tester/telnet_test.sh

		./webserv $config_file &>> webserv.log &
		echo -e "${_GREEN}Running Telnet Test on '$test_name'${_END}"
		sleep 1
		run | telnet | tee compare.txt >> telnet.log
#		run | telnet > compare.txt >> telnet.log
#		run | telnet >> telnet.log
		pkill webserv
		echo -e "\n\n------\n" &>> webserv.log

		fill_expected_files
		expected_result_file="./Tester/expected_results/$file"

		if [ "$arg" = "diff" ];
		then
			DIFF=$(diff -q compare.txt $expected_result_file)
			if [ "$DIFF" == "" ];
			then
				echo "Good diff"
			else
				diff compare.txt $expected_result_file
			fi
		elif [ "$arg" = "create" ];
		then 
			if ! test -f $expected_result_file;
			then
				cat compare.txt > $expected_result_file
			fi
		elif [ "$arg" = "replace" ];
		then 
			cat compare.txt > $expected_result_file
		fi

	done

	if [ "$arg" = "" ];
	then
		cat telnet.log
	fi


}

test_all $1

