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

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

#echo "sourcing init"
source this
source test.suite

log.level $level

competionArray=(once config list file ite)
source oo

test.loop() 
{
((TEST_COUNTER++))
console.log "


Test 0: loop \"$*\"
===================================================================="
loop.start "$@"
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
===================================================================="
}

test.case - "loop list PATH" \
   loop list PATH
expect 0 "*" "list PATH"

test.case - "loop list PATH silent" \
   loop list PATH silent
expect 0 "*" 

source loop

test.case - "reverse test.wo-da.de" \
   loop.this "test.wo-da.de" . reverse silent
expect 0 "de.wo-da.test." 

test.case - "convert de.wo-da.test to path" \
   loop.this $RESULT . r / silent
expect 0 "de/wo-da/test"

test.case - "convert some host to scenario" \
   loop.this somehost . add docker and add local and reverse silent and r / silent
expect 0 "local/docker/somehost"
# fails....strange because
#  loop localhost . add local and add host and reverse silent and r /
# works correctly

test.case - "convert some host to scenario" \
   loop somehost . add docker and add local and reverse silent and r / pipe
expect 0 "local/docker/somehost"


test.case - "convert some host to scenario" \
   loop.this "somehost.docker.local" . reverse and . r / 
expect 0 "local/docker/somehost"

mkdir -p $OOSH_DIR/test/tmp/result
test.case - "convert some/certbot/directory/some.nice.domain to some.nice.domain" \
   loop.this "some/certbot/directory/some.nice.domain" / silent result $OOSH_DIR/test/tmp/result.txt
source $OOSH_DIR/test/tmp/result.txt
RESULT=$lastItem
expect 0 "some.nice.domain"

DOMAIN=$lastItem
test.case - "convert some.nice.domain to domain/nice/some" \
   loop.this $DOMAIN . reverse and . r / save $OOSH_DIR/test/tmp/domain.txt
RESULT=$(cat $OOSH_DIR/test/tmp/domain.txt)
expect 0 "domain/nice/some"

rm -r $OOSH_DIR/test/tmp

# expect 0 "test.case - 'complete: config list' \
#   \"RESULT=\"\$(once.completion.discover 'config list')\"\"" \
#   ". config.list config list" \
#   "message $RESULT"

# test.case - "use sourced mycmd and call feature3 directly" \
#   mycmd.feature3 hello world