aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_prompt.sh
blob: b4a002bcea5241612f72162a40ac2829ff432cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/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