#!/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
else 
  # remove the level parameter
  shift
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$1>"

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

log.level $level

# Self-managed fixture so the test isn't fragile to whatever happens to
# exist in $HOME. Previously the file-existence tests below assumed
# `~/.bashrc` exists and `~/config` is a directory — true on a typical
# oosh-installed user home, but not on a freshly-installed root@macOS
# in CI (GitHub Actions macos runner: root's /var/root has no .bashrc),
# which produced 5 spurious test failures in `os platform.test macos`'s
# Phase B.2 (run #6 — see commit message). The check framework itself
# is what's under test, so a self-managed fixture is the principled
# fix. mktemp -d gives us an isolated dir; we put a regular file
# (existsFile) and a subdirectory (existsDir) inside.
TEST_CHECK_FIXTURE_DIR="$(mktemp -d)"
touch "$TEST_CHECK_FIXTURE_DIR/existsFile"
mkdir -p "$TEST_CHECK_FIXTURE_DIR/existsDir"
cd "$TEST_CHECK_FIXTURE_DIR"

check message "existsFile exists" file existsFile exists
RETURN_VALUE=$?
expect 0 "*" "checking existsFile exists"

check file existsFile exists
RETURN_VALUE=$?
expect 0 "*" "checking existsFile exists without message"

expect.error 1
check message "existsFile not exists" file existsFile not exists
RETURN_VALUE=$?
expect 1 "*" "existsFile exists therefore test check failed with warning"

expect.error 1
check file existsFile not exists
RETURN_VALUE=$?
expect 1 "*" "existsFile exists therefore test check failed without warning"

expect.error 1
check file existsDir not exists
RETURN_VALUE=$?
expect 1 "*" "fails because file existsDir is a directory"

expect.error 1
check file existsDir not exists fix error.log "you will not see this otherwise check tried to fix this error"
RETURN_VALUE=$?
expect 1 "*" "fails because file existsDir is a directory and does not try to fix it"

expect.error 0
check dir existsDir exists fix error.log "you will not see this"
RETURN_VALUE=$?
expect 0 "*" "dir existsDir exists"


expect.error 1
check dir existsFile exists fix error.log "you will not see this"
RETURN_VALUE=$?
expect 1 "*" "dir existsFile exists fails because existsFile is a file and does not fix it"

# expect.error 1
check dir cool exists fix important.log "dir cool does not exist as expected"
RETURN_VALUE=$?
expect 0 "*" "dir cool not exists and therefore it fixes by important log"


expect.error 1
check message "This is a warning because dir cool does not exist" dir cool exists 
RETURN_VALUE=$?
expect 1 "*" "dir cool not exists without fixing it"




check message "this breaks completly because in check.message: this.fuctionExists checks the first word this..." dir cool exists fix important.log "fixed, does not fail anymore"
RETURN_VALUE=$?
expect 0 "*" "dir cool not exists but fixing it"


# Cleanup the fixture dir from the top of this file.
rm -rf "$TEST_CHECK_FIXTURE_DIR" 2>/dev/null

test.suite.save.results
# source $OOSH_DIR/config 
# config.save testresult TEST_SUITE
# check file $CONFIG_PATH/testresult.env exists;
# expect 0 "*" "$CONFIG_PATH/testresult.env exists"

#echo -e "\e[1;32m$TEST_SUITE_SUCCESS_COUNTER\e[0m / $TEST_SUITE_EXPECT_COUNTER expects on $TEST_SUITE_TEST_COUNTER Test Cases"


# 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 / pipe
# expect 0 "local/docker/somehost"



# 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
