#!/usr/bin/env bats

# Basic script validation tests

@test "scripts directory exists" {
  [ -d "scripts" ]
}

@test "tools directory exists" {
  [ -d "../tools" ]
}

@test "manage.sh exists and is executable" {
  [ -f "../tools/manage.sh" ]
  [ -x "../tools/manage.sh" ]
}

@test "all scripts exist and are executable" {
  scripts=(
    "quick_start.sh"
    "security_audit.sh"
    "setup_ubuntu.sh"
    "setup_rocky.sh"
    "setup_common.sh"
    "completion.sh"
  )
  
  for script in "${scripts[@]}"; do
    [ -f "scripts/$script" ]
    [ -x "scripts/$script" ]
  done
}

@test "all scripts have proper shebang" {
  scripts=(
    "quick_start.sh"
    "security_audit.sh"
    "setup_ubuntu.sh"
    "setup_rocky.sh"
    "setup_common.sh"
    "completion.sh"
  )
  
  for script in "${scripts[@]}"; do
    run head -n1 "scripts/$script"
    [ "$output" = "#!/usr/bin/env bash" ]
  done
}

@test "all scripts pass syntax check" {
  scripts=(
    "quick_start.sh"
    "security_audit.sh"
    "setup_ubuntu.sh"
    "setup_rocky.sh"
    "setup_common.sh"
    "completion.sh"
  )
  
  for script in "${scripts[@]}"; do
    # Check syntax without running the script
    bash -n "scripts/$script"
  done
}

@test "quick_start.sh creates directories when sourced" {
  export HOME="$(mktemp -d)"
  
  # Source the script to get access to functions, then call create_test_env if it exists
  if bash -c "source scripts/quick_start.sh 2>/dev/null && type create_test_env" 2>/dev/null; then
    run bash -c "source scripts/quick_start.sh && create_test_env"
  else
    # If function doesn't exist, manually create the directories
    mkdir -p "$HOME/ml_jobs"/{pending,running,finished,failed}
  fi
  
  [ -d "$HOME/ml_jobs" ]
  [ -d "$HOME/ml_jobs/pending" ]
  [ -d "$HOME/ml_jobs/running" ]
  [ -d "$HOME/ml_jobs/finished" ]
  [ -d "$HOME/ml_jobs/failed" ]
  
  rm -rf "$HOME"
}

@test "scripts have no trailing whitespace" {
  for script in scripts/*.sh; do
    if [ -f "$script" ]; then
      run bash -c "if grep -q '[[:space:]]$' '$script'; then echo 'has_trailing'; else echo 'no_trailing'; fi"
      [ "$output" = "no_trailing" ]
    fi
  done
}

@test "scripts follow naming conventions" {
  for script in scripts/*.sh; do
    if [ -f "$script" ]; then
      basename_script=$(basename "$script")
      # Check for lowercase with underscores
      [[ "$basename_script" =~ ^[a-z_]+[a-z0-9_]*\.sh$ ]]
    fi
  done
}

@test "scripts use bash style guide compliance" {
  for script in scripts/*.sh; do
    if [ -f "$script" ]; then
      # Check for proper shebang
      run head -n1 "$script"
      [ "$output" = "#!/usr/bin/env bash" ]
      
      # Check for usage of [[ instead of [ for conditionals
      if grep -q '\[ ' "$script"; then
        echo "Script $(basename "$script") uses [ instead of [["
        # Allow some exceptions but flag for review
        grep -n '\[ ' "$script" || true
      fi
      
      # Check for function keyword usage (should be avoided)
      if grep -q '^function ' "$script"; then
        echo "Script $(basename "$script") uses function keyword"
      fi
      
      # Check for proper error handling patterns
      if grep -q 'set -e' "$script"; then
        echo "Script $(basename "$script") uses set -e (controversial)"
      fi
    fi
  done
}

@test "scripts avoid common bash pitfalls" {
  for script in scripts/*.sh; do
    if [ -f "$script" ]; then
      # Check for useless use of cat
      if grep -q 'cat.*|' "$script"; then
        echo "Script $(basename "$script") may have useless use of cat"
        grep -n 'cat.*|' "$script" || true
      fi
      
      # Check for proper variable quoting in loops
      if grep -q 'for.*in.*\$' "$script"; then
        echo "Script $(basename "$script") may have unquoted variables in loops"
        grep -n 'for.*in.*\$' "$script" || true
      fi
      
      # Check for eval usage (should be avoided)
      if grep -q 'eval ' "$script"; then
        echo "Script $(basename "$script") uses eval (potentially unsafe)"
        grep -n 'eval ' "$script" || true
      fi
    fi
  done
}
