#!/usr/bin/env bash
# Tests for generate_branch_name function
# Run: bash .specify/scripts/bash/tests/test-branch-name.sh

set -uo pipefail
# Note: not using -e so we can continue after failed assertions

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PASSED=0
FAILED=0

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Extract the generate_branch_name function from create-new-feature.sh
# This avoids executing the rest of the script
eval "$(sed -n '/^generate_branch_name()/,/^}/p' "$SCRIPT_DIR/../create-new-feature.sh")"

# Test helper
assert_eq() {
    local description="$1"
    local expected="$2"
    local actual="$3"

    if [ "$expected" = "$actual" ]; then
        echo -e "${GREEN}PASS${NC}: $description"
        ((PASSED++))
    else
        echo -e "${RED}FAIL${NC}: $description"
        echo "  Expected: '$expected'"
        echo "  Actual:   '$actual'"
        ((FAILED++))
    fi
}

echo "=== Branch Name Generation Tests ==="
echo ""

# --- Stop Word Filtering ---
echo "## Stop Word Filtering"

result=$(generate_branch_name "Add a new feature to the system")
assert_eq "Filters common stop words (a, to, the)" "new-feature-system" "$result"

result=$(generate_branch_name "I want to add support for widgets")
assert_eq "Filters 'I want to add'" "support-widgets" "$result"

result=$(generate_branch_name "the quick brown fox")
assert_eq "Filters 'the'" "quick-brown-fox" "$result"

# --- Short Word Handling (Acronyms) ---
echo ""
echo "## Short Word Handling (Acronyms)"

result=$(generate_branch_name "Add API support")
assert_eq "Keeps 'API' as acronym (uppercase in original)" "api-support" "$result"

result=$(generate_branch_name "the UI is broken")
assert_eq "Keeps 'UI' as acronym, filters 'the' and 'is'" "ui-broken" "$result"

result=$(generate_branch_name "Fix DB connection issues")
assert_eq "Keeps 'DB' as acronym (4 words total)" "fix-db-connection-issues" "$result"

result=$(generate_branch_name "Add CLI tool support")
assert_eq "Keeps 'CLI' as acronym" "cli-tool-support" "$result"

# --- Word Count Limits ---
echo ""
echo "## Word Count Limits"

result=$(generate_branch_name "implement user authentication system module")
assert_eq "Limits to 3 words when more than 4" "implement-user-authentication" "$result"

result=$(generate_branch_name "user auth system handler")
assert_eq "Takes 4 words when exactly 4 meaningful" "user-auth-system-handler" "$result"

result=$(generate_branch_name "add user auth system")
assert_eq "Filters 'add', leaves 3 words" "user-auth-system" "$result"

result=$(generate_branch_name "one")
assert_eq "Single word input" "one" "$result"

result=$(generate_branch_name "two words")
assert_eq "Two word input" "two-words" "$result"

# --- Special Characters ---
echo ""
echo "## Special Character Handling"

result=$(generate_branch_name "Add feature (with special) chars!")
assert_eq "Removes parentheses and special chars" "feature-special-chars" "$result"

result=$(generate_branch_name "Fix bug #123 in module")
assert_eq "Keeps numbers (4 meaningful words)" "fix-bug-123-module" "$result"

result=$(generate_branch_name "Update config.yaml handling")
assert_eq "Handles dots as separators (4 words)" "update-config-yaml-handling" "$result"

result=$(generate_branch_name "feature_with_underscores")
assert_eq "Handles underscores, 'with' filtered as stop word" "feature-underscores" "$result"

# --- Unicode/International ---
echo ""
echo "## Unicode Handling"

result=$(generate_branch_name "Añadir función nueva")
assert_eq "Preserves unicode in output" "añadir-función-nueva" "$result"

result=$(generate_branch_name "日本語 feature")
assert_eq "CJK chars filtered, keeps ASCII" "feature" "$result"

# --- Edge Cases ---
echo ""
echo "## Edge Cases"

result=$(generate_branch_name "")
assert_eq "Empty string" "" "$result"

result=$(generate_branch_name "   ")
assert_eq "Whitespace only" "" "$result"

result=$(generate_branch_name "a an the to for")
assert_eq "All stop words triggers fallback" "a-an-the" "$result"

result=$(generate_branch_name "UPPERCASE DESCRIPTION HERE")
assert_eq "All uppercase input" "uppercase-description-here" "$result"

result=$(generate_branch_name "MixedCase Feature Description")
assert_eq "Mixed case input" "mixedcase-feature-description" "$result"

result=$(generate_branch_name "feature---with---dashes")
assert_eq "Multiple dashes collapsed, 'with' filtered" "feature-dashes" "$result"

result=$(generate_branch_name "  leading  and  trailing  spaces  ")
assert_eq "Extra whitespace handled ('and' not a stop word)" "leading-and-trailing-spaces" "$result"

# --- Fallback Logic ---
echo ""
echo "## Fallback Logic"

result=$(generate_branch_name "a i")
assert_eq "Short stop words triggers fallback" "a-i" "$result"

result=$(generate_branch_name "to be or not")
assert_eq "Mostly stop words triggers fallback" "not" "$result"

# --- Summary ---
echo ""
echo "=== Summary ==="
echo -e "Passed: ${GREEN}$PASSED${NC}"
echo -e "Failed: ${RED}$FAILED${NC}"

if [ $FAILED -gt 0 ]; then
    exit 1
fi
