#!/usr/bin/env bash
# scripts/checks/_test-branch-name.sh — unit test for the branch-name glob patterns.
#
# Run: ./scripts/checks/_test-branch-name.sh
# Exit 0 = all cases pass, non-zero = at least one regression.
#
# Bash `case` uses shell glob semantics, not regex. Recent PR-pilot-1 review
# claimed `feature/[a-z0-9-]*` accepts `feature/` (empty suffix) — that would
# be true in regex (where `*` means "zero or more of the preceding atom"), but
# in shell glob, `[a-z0-9-]` matches ONE char and `*` matches the rest. So
# `feature/` IS rejected. This test pins that behaviour so a future "fix" that
# changes the pattern breaks loudly.

set -uo pipefail

# Mirror the patterns from lefthook.yml + scripts/git-hooks/pre-commit exactly.
classify() {
    case "$1" in
        main|HEAD)                                             echo accept ;;
        version/[0-9]*.[0-9]*.[0-9]*)                          echo accept ;;
        feature/*/MP-[0-9]*)                                   echo accept ;;
        feature/[a-z0-9-]*)                                    echo accept ;;
        hotfix/[0-9]*.[0-9]*.[0-9]*.[0-9]*/MP-[0-9]*)          echo accept ;;
        MP-[0-9]*|feat/MP-[0-9]*-*)                            echo legacy ;;
        *)                                                     echo reject ;;
    esac
}

pass=0
fail=0
expect() {
    local branch="$1" want="$2"
    local got
    got=$(classify "$branch")
    if [ "$got" = "$want" ]; then
        pass=$((pass + 1))
    else
        fail=$((fail + 1))
        printf '  ✗ %-50s got=%s  want=%s\n' "$branch" "$got" "$want"
    fi
}

# Accept cases
expect "main"                                            accept
expect "HEAD"                                            accept
expect "version/1.0.0"                                   accept
expect "version/12.34.56"                                accept
expect "feature/cloud-init-clone"                        accept
expect "feature/a"                                       accept      # single char accepted
expect "feature/vm-hotplug/MP-1305-sdn"                  accept      # component branch w/ MP key
expect "hotfix/1.2.3.1/MP-1500-cve-bump"                 accept

# Reject cases
expect "feature/"                                        reject      # empty suffix — bug regression test
expect "version/1.0"                                     reject      # missing patch number
expect "version/1"                                       reject      # missing minor+patch
expect "hotfix/1.2.3/MP-1500"                            reject      # missing fourth segment
expect "hotfix/1.2.3.1/no-jira-key"                      reject      # missing MP key
expect "random-branch"                                   reject

# Legacy (warning-only during pilot)
expect "MP-1202"                                         legacy
expect "feat/MP-1202-pr-pilot-1"                         legacy

if [ "$fail" -eq 0 ]; then
    printf "✓ branch-name patterns: %d cases pass\n" "$pass"
    exit 0
else
    printf "✗ branch-name patterns: %d pass, %d fail\n" "$pass" "$fail"
    exit 1
fi
