#!/bin/bash # HyperTerm Prompt Test Suite # Organized testing environment for prompt functionality # shellcheck disable=SC1090,SC2034,SC2155 set -e # Logging functions log_info() { echo "[INFO] $*"; } log_warn() { echo "[WARN] $*"; } log_error() { echo "[ERROR] $*"; } log_success() { echo "[SUCCESS] $*"; } # Configuration readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" readonly TEST_DIR="/tmp/hyperterm_test_$(date +%s)" # Cleanup function cleanup() { if [[ -n "${TEST_DIR:-}" && -d "$TEST_DIR" ]]; then rm -rf "$TEST_DIR" log_info "Test directory cleaned: $TEST_DIR" fi } # Setup test environment setup_test_env() { log_info "Setting up test environment" mkdir -p "$TEST_DIR" cd "$TEST_DIR" # Create test git repository git init --quiet git config user.name "Test User" git config user.email "test@example.com" echo "# Test Repository" > README.md echo "test content" > test.txt git add README.md git commit -m "Initial commit" --quiet # Create various git states for testing echo "modified content" >> test.txt echo "untracked file" > untracked.txt echo "staged content" > staged.txt git add staged.txt log_success "Test environment created at: $TEST_DIR" log_info "Git states created: modified, untracked, staged files" } # Load hyperterm functions load_hyperterm_functions() { log_info "Loading HyperTerm functions" # Load colors local colors_file="$PROJECT_ROOT/hyperterm/core/colors.sh" if [[ -f "$colors_file" ]]; then source "$colors_file" log_success "Colors loaded from: $colors_file" else log_warn "Colors file not found, using fallback" # Fallback colors RESET='\033[0m' BOLD='\033[1m' RED='\033[31m' GREEN='\033[32m' YELLOW='\033[33m' BLUE='\033[34m' CYAN='\033[36m' WHITE='\033[37m' GREY='\033[90m' fi # Load git functions local git_file="$PROJECT_ROOT/hyperterm/core/git.sh" if [[ -f "$git_file" ]]; then source "$git_file" log_success "Git functions loaded" return 0 else log_error "Git functions not found" return 1 fi } # Test individual functions test_individual_functions() { log_info "Testing individual functions" echo "Branch detection:" if command -v _get_git_branch >/dev/null 2>&1; then local branch branch="$(_get_git_branch)" echo " Result: $branch" log_success "Branch detection working" else log_error "Branch detection function not found" fi echo "Git progress detection:" if command -v _get_git_progress >/dev/null 2>&1; then local progress progress="$(_get_git_progress)" echo " Result: ${progress:-"(none)"}" log_success "Progress detection working" else log_error "Progress detection function not found" fi echo "Git status:" if command -v _get_git_status_fast >/dev/null 2>&1; then local status status="$(_get_git_status_fast)" echo " Result: $status" log_success "Fast git status working" elif command -v _prompt_get_git_status >/dev/null 2>&1; then local status status="$(_prompt_get_git_status)" echo " Result: $status" log_success "Original git status working" else log_error "No git status function found" fi } # Performance benchmark run_performance_test() { log_info "Running performance benchmark" if ! command -v __prompt_git >/dev/null 2>&1; then log_error "Prompt function not available" return 1 fi local iterations=50 local start_time end_time duration log_info "Running $iterations iterations" start_time=$(date +%s%N) for ((i=1; i<=iterations; i++)); do __prompt_git >/dev/null 2>&1 done end_time=$(date +%s%N) duration=$(( (end_time - start_time) / 1000000 )) local avg_duration=$(( duration / iterations )) echo "Performance Results:" echo " Total time: ${duration}ms" echo " Average per call: ${avg_duration}ms" echo " Iterations: $iterations" if [[ $avg_duration -lt 10 ]]; then log_success "Performance: Excellent (< 10ms per call)" elif [[ $avg_duration -lt 50 ]]; then log_success "Performance: Good (< 50ms per call)" else log_warn "Performance: Slow (>= 50ms per call)" fi } # Test different git states test_git_states() { log_info "Testing different git states" # Clean state git checkout -- . >/dev/null 2>&1 git clean -fd >/dev/null 2>&1 echo "Clean repository:" __prompt_git echo "" # Modified files echo "modified" >> test.txt echo "Modified files:" __prompt_git echo "" # Untracked files echo "untracked" > new_file.txt echo "With untracked files:" __prompt_git echo "" # Staged files git add new_file.txt echo "With staged files:" __prompt_git echo "" log_success "Git states testing completed" } # Interactive test mode interactive_mode() { log_info "Entering interactive test mode" echo "Available commands:" echo " status - Show current git status" echo " prompt - Show full prompt" echo " perf - Run performance test" echo " states - Test different git states" echo " modify - Modify files for testing" echo " clean - Clean working directory" echo " help - Show this help" echo " exit - Exit interactive mode" echo "" while true; do echo -n "test> " read -r command case "$command" in "status") if command -v _get_git_status_fast >/dev/null 2>&1; then _get_git_status_fast else _prompt_get_git_status fi echo "" ;; "prompt") __prompt_git echo "" ;; "perf") run_performance_test ;; "states") test_git_states ;; "modify") echo "Creating test modifications" echo "change $(date)" >> test.txt echo "new file $(date)" > "file_$(date +%s).txt" log_success "Files modified" ;; "clean") git checkout -- . >/dev/null 2>&1 git clean -fd >/dev/null 2>&1 log_success "Working directory cleaned" ;; "help") echo "Available commands: status, prompt, perf, states, modify, clean, help, exit" ;; "exit") log_info "Exiting interactive mode" break ;; "") # Empty command, continue ;; *) log_warn "Unknown command: $command (type 'help' for available commands)" ;; esac done } # Main execution main() { log_info "Starting HyperTerm Prompt Test Suite" # Setup setup_test_env if ! load_hyperterm_functions; then log_error "Failed to load HyperTerm functions" exit 1 fi # Run tests test_individual_functions echo "" run_performance_test echo "" test_git_states echo "" # Interactive mode (only if not in CI or non-interactive mode) if [[ "$1" != "--non-interactive" && "$1" != "-n" && -z "${CI:-}" ]]; then log_info "All automated tests completed" echo "Enter interactive mode? (y/N)" read -r response if [[ "$response" =~ ^[Yy]$ ]]; then interactive_mode fi else log_info "Running in non-interactive mode, skipping interactive prompt" fi log_success "Test suite completed successfully" } # Trap cleanup on exit trap cleanup EXIT # Execute main function if script is run directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi