#!/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
source path

log.level $level

# Save original PATH for restoration
ORIGINAL_PATH="$PATH"

# Create temp directories for testing (use /tmp for multi-user compatibility)
TEST_PATH_TMP="/tmp/oosh-test-path-$$"
TEST_DIR_1="$TEST_PATH_TMP/test_path_dir_1"
TEST_DIR_2="$TEST_PATH_TMP/test_path_dir_2"
TEST_DIR_3="$TEST_PATH_TMP/test_path_dir_3"
mkdir -p "$TEST_DIR_1" "$TEST_DIR_2" "$TEST_DIR_3"

# Helper to reset PATH
reset_path() {
  export PATH="$ORIGINAL_PATH"
}

# ============================================================================
# Test path.list
# ============================================================================

test.case $level "path.list - lists PATH entries line by line" \
  path.list

# Verify output contains at least /usr/bin
RESULT=$(path.list | grep -c "/usr/bin" || echo "0")
if [ "$RESULT" -ge 1 ]; then
  expect.pass "path.list contains /usr/bin"
else
  expect.fail "path.list should contain /usr/bin"
fi

# ============================================================================
# Test path.env
# ============================================================================

test.case $level "path.env - returns PATH as export statement" \
  path.env

# Verify RESULT contains export PATH=
if [[ "$RESULT" == *"export PATH="* ]]; then
  expect.pass "path.env returns proper export statement"
else
  expect.fail "path.env should return 'export PATH=...'"
fi

# ============================================================================
# Test path.append (also tests path.add and path.push which are aliases)
# ============================================================================

reset_path
test.case $level "path.append - appends directory to PATH" \
  path.append "$TEST_DIR_1"

# Verify TEST_DIR_1 is at the end of PATH
if [[ "$PATH" == *":$TEST_DIR_1" ]]; then
  expect.pass "path.append added directory at end of PATH"
else
  expect.fail "path.append should add directory at end of PATH"
fi

# Test that duplicate append doesn't create duplicates
reset_path
path.append "$TEST_DIR_1" 2>/dev/null
path.append "$TEST_DIR_1" 2>/dev/null
DUPLICATE_COUNT=$(echo "$PATH" | tr ':' '\n' | grep -c "^${TEST_DIR_1}$" || echo "0")
if [ "$DUPLICATE_COUNT" -eq 1 ]; then
  expect.pass "path.append does not create duplicates"
else
  expect.fail "path.append should not create duplicates (found $DUPLICATE_COUNT)"
fi

# ============================================================================
# Test path.prepend
# ============================================================================

reset_path
test.case $level "path.prepend - prepends directory to PATH" \
  path.prepend "$TEST_DIR_1"

# Verify TEST_DIR_1 is at the beginning of PATH
if [[ "$PATH" == "$TEST_DIR_1:"* ]]; then
  expect.pass "path.prepend added directory at beginning of PATH"
else
  expect.fail "path.prepend should add directory at beginning of PATH"
fi

# Test that duplicate prepend doesn't create duplicates
reset_path
path.prepend "$TEST_DIR_1" 2>/dev/null
path.prepend "$TEST_DIR_1" 2>/dev/null
DUPLICATE_COUNT=$(echo "$PATH" | tr ':' '\n' | grep -c "^${TEST_DIR_1}$" || echo "0")
if [ "$DUPLICATE_COUNT" -eq 1 ]; then
  expect.pass "path.prepend does not create duplicates"
else
  expect.fail "path.prepend should not create duplicates (found $DUPLICATE_COUNT)"
fi

# ============================================================================
# Test path.remove
# ============================================================================

reset_path
path.append "$TEST_DIR_1" 2>/dev/null
test.case $level "path.remove - removes directory from PATH" \
  path.remove "$TEST_DIR_1"

# Verify TEST_DIR_1 is no longer in PATH
if [[ "$PATH" != *"$TEST_DIR_1"* ]]; then
  expect.pass "path.remove removed directory from PATH"
else
  expect.fail "path.remove should remove directory from PATH"
fi


# ============================================================================
# Test path.parameter.completion.dir - completion function
# ============================================================================

test.case $level "path.parameter.completion.dir - function exists" \
  this.functionExists path.parameter.completion.dir

if [ $RETURN_VALUE -eq 0 ]; then
  expect.pass "path.parameter.completion.dir function exists"
else
  expect.fail "path.parameter.completion.dir function should exist"
fi

# Test that completion returns directories
test.case $level "path.parameter.completion.dir - returns matching dirs" \
  path.parameter.completion.dir "$OOSH_DIR/te"

COMPLETION_RESULT=$(path.parameter.completion.dir "$OOSH_DIR/te")
if [[ "$COMPLETION_RESULT" == *"$OOSH_DIR/test"* ]]; then
  expect.pass "returns matching directories"
else
  expect.fail "should return matching directories, got: $COMPLETION_RESULT"
fi

# Test completion with /usr prefix
test.case $level "path.parameter.completion.dir - handles /usr prefix" \
  path.parameter.completion.dir "/usr"

COMPLETION_RESULT=$(path.parameter.completion.dir "/usr")
if [[ "$COMPLETION_RESULT" == *"/usr"* ]]; then
  expect.pass "handles /usr prefix"
else
  expect.fail "should handle /usr prefix"
fi

# Test completion for test tmp directories we created
test.case $level "path.parameter.completion.dir - returns test directories" \
  path.parameter.completion.dir "$TEST_PATH_TMP/test_path"

COMPLETION_RESULT=$(path.parameter.completion.dir "$TEST_PATH_TMP/test_path")
if [[ "$COMPLETION_RESULT" == *"test_path_dir"* ]]; then
  expect.pass "returns our test directories"
else
  expect.fail "should return test directories, got: $COMPLETION_RESULT"
fi

# ============================================================================
# Test path.usage
# ============================================================================

test.case $level "path.usage - displays usage information" \
  path.usage

if [[ "$RESULT" == *"Usage"* ]] || [ $RETURN_VALUE -eq 0 ]; then
  expect.pass "path.usage displays help"
else
  expect.fail "path.usage should display help information"
fi

# ============================================================================
# Cleanup
# ============================================================================

echo ""
echo -e "\e[90mCleanup: Removing test directories and restoring PATH\e[0m"

rm -rf "$TEST_PATH_TMP" 2>/dev/null
export PATH="$ORIGINAL_PATH"

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

test.suite.save.results
