#!/usr/bin/env bash
# Tests for check.sh — exercises valid/invalid branch+title matrices.
set -euo pipefail

SCRIPT="$(cd "$(dirname "$0")/.." && pwd)/check.sh"
PASS=0
FAIL=0

check() {
  local desc="$1" want_fail="$2" branch="$3" title="$4" author="$5"
  local out rc=0
  out=$(HEAD_BRANCH="$branch" PR_TITLE="$title" PR_AUTHOR="$author" bash "$SCRIPT" 2>&1) || rc=$?
  local got_lines=0
  echo "$out" | grep -qE "^FAIL" && got_lines=1 || true
  if [ "$rc" -eq "$want_fail" ] && [ "$got_lines" -eq "$want_fail" ]; then
    echo "PASS [$desc]"
    PASS=$((PASS + 1))
  else
    echo "FAIL [$desc]: expected exit=$want_fail fail-lines=$want_fail, got exit=$rc fail-lines=$got_lines"
    echo "  output: $out"
    FAIL=$((FAIL + 1))
  fi
}

# break-glass
check "dfritz exempt — invalid branch"  0 "totally/wrong-branch" "no prefix"               "dfritz"
check "dfritz exempt — mismatch"        0 "dev/bug-abc/thing"    "[bug-xyz] Thing"          "dfritz"

# valid role/bug — all roster handles
check "dev/bug matching title"          0 "dev/bug-x7k2m9/fix-terminal-resize" "[bug-x7k2m9] Fix terminal resize"  "dev"
check "architect/bug matching title"    0 "architect/bug-abc123/refactor-auth"  "[bug-abc123] Refactor auth"        "architect"
check "ops/bug matching title"          0 "ops/bug-zz9/deploy-tweak"            "[bug-zz9] Deploy tweak"            "ops"
check "security/bug matching title"     0 "security/bug-s1a2/patch-cve"         "[bug-s1a2] Patch CVE"              "security"
check "perf/bug matching title"         0 "perf/bug-p0p0/reduce-latency"        "[bug-p0p0] Reduce latency"         "perf"
check "ux/bug matching title"           0 "ux/bug-u1u1/polish-modal"            "[bug-u1u1] Polish modal"           "ux"
check "support/bug matching title"      0 "support/bug-sup9/clarify-error"      "[bug-sup9] Clarify error"          "support"

# valid chore form
check "chore — no title prefix"         0 "chore/bump-deps"   "Bump dependency versions"  "dev"
check "chore — plain title"             0 "chore/fix-a-typo"  "Fix typo in README"         "ops"

# fail: role/bug branch but no [bug-id] title prefix
check "role/bug no title prefix"        1 "dev/bug-x7k2m9/fix-resize"     "Fix resize"                  "dev"

# fail: bug-id mismatch between branch and title
check "bug-id mismatch"                 1 "dev/bug-x7k2m9/fix-resize"     "[bug-zzzzz1] Fix resize"     "dev"

# fail: chore branch with [bug-id] title prefix
check "chore with bug-id title"         1 "chore/bump-deps"                "[bug-abc123] Bump deps"      "dev"

# fail: invalid branch forms
check "invalid — no role prefix"        1 "feature/foo-bar"                "Add feature"                 "dev"
check "invalid — missing bug segment"   1 "dev/fix-something"              "Fix something"               "dev"
check "invalid — unknown role"          1 "unknown/bug-abc/thing"          "[bug-abc] Thing"             "dev"
check "invalid — uppercase bug-id"      1 "dev/bug-ABC123/thing"           "[bug-ABC123] Thing"          "dev"
check "invalid — empty kebab"           1 "dev/bug-x7k2m9/"                "[bug-x7k2m9] No kebab"       "dev"
check "invalid — no bug prefix on seg"  1 "dev/x7k2m9/fix-resize"          "Fix resize"                  "dev"

echo ""
echo "Results: ${PASS} passed, ${FAIL} failed"
[ "${FAIL}" -eq 0 ]
