#!/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 FIXTURES=$(mktemp -d) trap 'rm -rf "${FIXTURES}"' EXIT git -C "${FIXTURES}" init -q git -C "${FIXTURES}" config user.name "Test Agent" git -C "${FIXTURES}" config user.email "test@noreply.fritzlab.net" git -C "${FIXTURES}" commit --allow-empty -q -m "base" BASE=$(git -C "${FIXTURES}" rev-parse HEAD) git -C "${FIXTURES}" commit --allow-empty -q -m "valid change" -m "Authored-By: Codex (GPT-5) " GOOD_HEAD=$(git -C "${FIXTURES}" rev-parse HEAD) canonical_body() { local bug="$1" printf '## Tracking\n- Fixes %s — [%s](https://agenthub.fritzlab.net/%s)\n\n## Attribution\n- Authored-By: Codex (GPT-5) \n' "$bug" "$bug" "$bug" } check() { local desc="$1" want_fail="$2" branch="$3" title="$4" author="$5" local bug body bug=$(printf '%s\n' "$branch" | sed -nE 's|^[^/]+/(bug-[a-z0-9]+)/.*|\1|p') body=$(canonical_body "${bug:-bug-test}") local out rc=0 out=$(cd "${FIXTURES}" && HEAD_BRANCH="$branch" PR_TITLE="$title" PR_AUTHOR="$author" \ PR_BODY="$body" BASE_SHA="$BASE" HEAD_SHA="$GOOD_HEAD" 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 } check_contract() { local desc="$1" body="$2" base="$3" head="$4" diagnostic="$5" local out rc=0 out=$(cd "${FIXTURES}" && HEAD_BRANCH="architect/bug-x7k2m9/contract" \ PR_TITLE="[bug-x7k2m9] Enforce contract" PR_AUTHOR="architect" \ PR_BODY="$body" BASE_SHA="$base" HEAD_SHA="$head" bash "$SCRIPT" 2>&1) || rc=$? if [ "$rc" -eq 1 ] && printf '%s\n' "$out" | grep -Fq "$diagnostic"; then echo "PASS [$desc]" PASS=$((PASS + 1)) else echo "FAIL [$desc]: expected exit=1 and diagnostic '$diagnostic', got exit=$rc" 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" # Bug-backed PR body contract. These mirror the omissions in agenthub#779. BODY=$(canonical_body "bug-x7k2m9") check_contract "raw Fixes only rejects missing link and attribution" \ $'## Tracking\n- Fixes bug-x7k2m9' "$BASE" "$GOOD_HEAD" "must link https://agenthub.fritzlab.net/bug-x7k2m9" check_contract "tracking rejects linked Fixes without literal token" \ $'## Tracking\n- Fixes [bug-x7k2m9](https://agenthub.fritzlab.net/bug-x7k2m9)\n\n## Attribution\n- Authored-By: Codex (GPT-5) ' \ "$BASE" "$GOOD_HEAD" "must contain the literal token 'Fixes bug-x7k2m9'" check_contract "tracking rejects wrong Bug URL" \ $'## Tracking\n- Fixes bug-x7k2m9 — https://agenthub.fritzlab.net/bug-other\n\n## Attribution\n- Authored-By: Codex (GPT-5) ' \ "$BASE" "$GOOD_HEAD" "must link https://agenthub.fritzlab.net/bug-x7k2m9" check_contract "attribution must be in its own section" \ $'## Tracking\n- Fixes bug-x7k2m9 — https://agenthub.fritzlab.net/bug-x7k2m9\n- Authored-By: Codex (GPT-5) ' \ "$BASE" "$GOOD_HEAD" "## Attribution must contain" git -C "${FIXTURES}" commit --allow-empty -q -m "unwatermarked change" BAD_HEAD=$(git -C "${FIXTURES}" rev-parse HEAD) check_contract "agenthub 779 commits reject missing watermark" "$BODY" "$GOOD_HEAD" "$BAD_HEAD" "must end with an Authored-By" git -C "${FIXTURES}" commit --allow-empty -q -m $'misplaced trailer\nAuthored-By: Codex (GPT-5) ' MISPLACED_HEAD=$(git -C "${FIXTURES}" rev-parse HEAD) check_contract "commit watermark requires blank separator" "$BODY" "$BAD_HEAD" "$MISPLACED_HEAD" "must end with an Authored-By" git -C "${FIXTURES}" commit --allow-empty -q -m "trailer not final" -m $'Authored-By: Codex (GPT-5) \nextra text' NONFINAL_HEAD=$(git -C "${FIXTURES}" rev-parse HEAD) check_contract "commit watermark must be final trailer" "$BODY" "$MISPLACED_HEAD" "$NONFINAL_HEAD" "must end with an Authored-By" check_contract "missing commit range rejects" "$BODY" "$GOOD_HEAD" "$GOOD_HEAD" "contains no PR commits" check_contract "invalid SHA rejects before git" "$BODY" "not-a-sha" "$GOOD_HEAD" "lowercase hexadecimal" echo "" echo "Results: ${PASS} passed, ${FAIL} failed" [ "${FAIL}" -eq 0 ]