aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / scripts / test_pr_gate.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Offline test for pr-gate.sh. A fake `gh` on PATH answers from fixture JSON chosen by $CASE and |
| 3 | # records any merge command line. Local and crumb steps use real temporary git repos (bare origin |
| 4 | # plus worktree). The crumb step is retired (DRA-97): a `crumb` stub that exits 99 is on PATH and in |
| 5 | # $CRUMB, and the test proves it is never called. No network, no auth. |
| 6 | set -u |
| 7 | here="$(cd "$(dirname "$0")" && pwd)" |
| 8 | gate="$here/pr-gate.sh" |
| 9 | tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT |
| 10 | export GIT_AUTHOR_NAME=T GIT_AUTHOR_EMAIL=t@example.invalid GIT_COMMITTER_NAME=T GIT_COMMITTER_EMAIL=t@example.invalid |
| 11 | export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null |
| 12 | export SLEEP_SECS=0 |
| 13 | FX="$tmp/fx"; mkdir -p "$FX" "$tmp/bin"; export FX |
| 14 | |
| 15 | cat > "$tmp/bin/gh" <<'S' |
| 16 | #!/usr/bin/env bash |
| 17 | d="$FX/$CASE" |
| 18 | case "$1 $2" in |
| 19 | "pr view") |
| 20 | if echo "$*" | grep -q mergeCommit && [ -e "$d/merged-flag" ]; then cat "$d/merged.json" |
| 21 | elif [ -e "$d/updated.json" ] && [ -e "$d/viewed" ]; then cat "$d/updated.json" |
| 22 | else |
| 23 | touch "$d/viewed" |
| 24 | cat "$d/pr.json" |
| 25 | fi ;; |
| 26 | "pr list") cat "$d/list.json" ;; |
| 27 | "pr merge") |
| 28 | echo "$*" > "$d/merge.cmd" |
| 29 | if [ -e "$d/updated.json" ]; then |
| 30 | want=$(jq -r .headRefOid "$d/updated.json") |
| 31 | [[ " $* " == *" --match-head-commit $want "* ]] || { echo "head branch changed" >&2; exit 1; } |
| 32 | fi |
| 33 | touch "$d/merged-flag" ;; |
| 34 | api\ repos/*) |
| 35 | if [ -e "$d/prot404" ]; then echo "gh: Not Found (HTTP 404)" >&2; exit 1; fi |
| 36 | cat "$d/prot.json" ;; |
| 37 | *) echo "fake gh: unhandled: $*" >&2; exit 2 ;; |
| 38 | esac |
| 39 | S |
| 40 | cat > "$tmp/bin/crumb" <<'S' |
| 41 | #!/usr/bin/env bash |
| 42 | # retired: any call is a test failure (recorded, then exit 99) |
| 43 | echo "$*" >> "$FX/crumb-called" |
| 44 | exit 99 |
| 45 | S |
| 46 | chmod +x "$tmp/bin/gh" "$tmp/bin/crumb" |
| 47 | export PATH="$tmp/bin:$PATH" CRUMB="$tmp/bin/crumb" |
| 48 | RETIRED='GATE crumb: SKIP retired 2026-11-07 \(DRA-97\); .crumb files are history' |
| 49 | |
| 50 | pass=0; bad=0 |
| 51 | ok() { pass=$((pass+1)); } |
| 52 | no() { bad=$((bad+1)); echo "TEST FAIL: $1"; } |
| 53 | run() { OUT=$("$gate" "$@" 2>&1); rc=$?; } |
| 54 | expect() { # expect <rc> <label> <gate args...> |
| 55 | w=$1; l=$2; shift 2; run "$@" |
| 56 | if [ "$rc" = "$w" ]; then ok; else no "$l (rc=$rc, want $w): $OUT"; fi |
| 57 | } |
| 58 | has() { # has <label> <pattern> against $OUT |
| 59 | if echo "$OUT" | grep -qE -- "$2"; then ok; else no "$1 (missing /$2/): $OUT"; fi |
| 60 | } |
| 61 | hasnot() { if echo "$OUT" | grep -qE -- "$2"; then no "$1 (unexpected /$2/): $OUT"; else ok; fi; } |
| 62 | |
| 63 | # fixture builder: fx <case> [jq filter applied to the clean PR] |
| 64 | CLEANPR='{"number":7,"state":"OPEN","isDraft":false,"baseRefName":"main","headRefName":"feat","headRefOid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","mergeable":"MERGEABLE","mergeStateStatus":"CLEAN","reviewDecision":"","statusCheckRollup":[{"__typename":"CheckRun","name":"rust","status":"COMPLETED","conclusion":"SUCCESS"},{"__typename":"StatusContext","context":"legacy-ci","state":"SUCCESS"}]}' |
| 65 | fx() { |
| 66 | mkdir -p "$FX/$1" |
| 67 | echo "$CLEANPR" | jq "${2:-.}" > "$FX/$1/pr.json" |
| 68 | echo '["rust","legacy-ci"]' > "$FX/$1/prot.json" |
| 69 | echo '[{"number":7,"headRefName":"feat","mergeStateStatus":"CLEAN"},{"number":9,"headRefName":"other","mergeStateStatus":"BEHIND"},{"number":10,"headRefName":"third","mergeStateStatus":"CLEAN"}]' > "$FX/$1/list.json" |
| 70 | echo '{"state":"MERGED","mergedAt":"2026-10-10T00:00:00Z","mergeCommit":{"oid":"cafebabecafebabecafebabecafebabecafebabe"}}' > "$FX/$1/merged.json" |
| 71 | } |
| 72 | |
| 73 | # ---- GitHub-side cases (no worktree) |
| 74 | fx clean; export CASE=clean; expect 0 "clean PR passes dry run" repo1 7 --dry-run |
| 75 | has "dry run prints pr PASS" 'GATE pr: PASS' |
| 76 | has "local skipped without worktree" 'GATE local: SKIP no worktree given' |
| 77 | has "merge skipped in dry run" 'GATE merge: SKIP dry run' |
| 78 | has "crumb step retired without worktree" "$RETIRED" |
| 79 | has "queue marks BEHIND PR as needing rebase" 'QUEUE #9 other BEHIND needs scripts/rebase-crumb.sh' |
| 80 | has "queue shows clean PR plainly" 'QUEUE #10 third CLEAN$' |
| 81 | hasnot "queue omits the PR itself" 'QUEUE #7 ' |
| 82 | check_nomerge() { if [ ! -e "$FX/$1/merge.cmd" ]; then ok; else no "$2"; fi; } |
| 83 | check_nomerge clean "dry run must not call gh pr merge" |
| 84 | |
| 85 | fx behind '.mergeStateStatus="BEHIND"'; CASE=behind expect 1 "BEHIND refuses" repo1 7 --dry-run |
| 86 | has "BEHIND reason" 'stale, rebase first' |
| 87 | fx dirty '.mergeStateStatus="DIRTY"'; CASE=dirty expect 1 "DIRTY refuses" repo1 7 --dry-run |
| 88 | fx blocked '.mergeStateStatus="BLOCKED"'; CASE=blocked expect 1 "BLOCKED refuses" repo1 7 --dry-run |
| 89 | fx unstable '.mergeStateStatus="UNSTABLE"'; CASE=unstable expect 1 "UNSTABLE refuses" repo1 7 --dry-run |
| 90 | fx conflicting '.mergeable="CONFLICTING"'; CASE=conflicting expect 1 "mergeable CONFLICTING refuses" repo1 7 --dry-run |
| 91 | fx unknown '.mergeStateStatus="UNKNOWN"'; CASE=unknown expect 1 "UNKNOWN retries then refuses" repo1 7 --dry-run |
| 92 | has "UNKNOWN reason" 'has not computed mergeability' |
| 93 | fx hooks '.mergeStateStatus="HAS_HOOKS"'; CASE=hooks expect 0 "HAS_HOOKS passes in dry run" repo1 7 --dry-run |
| 94 | CASE=hooks expect 1 "HAS_HOOKS refuses on a real merge" repo1 7 |
| 95 | check_nomerge hooks "HAS_HOOKS real run must not merge" |
| 96 | fx draft '.isDraft=true'; CASE=draft expect 1 "draft refuses" repo1 7 --dry-run |
| 97 | fx closed '.state="CLOSED"'; CASE=closed expect 1 "closed refuses" repo1 7 --dry-run |
| 98 | fx wrongbase '.baseRefName="release"'; CASE=wrongbase expect 1 "non-main base refuses" repo1 7 --dry-run |
| 99 | fx missing '.statusCheckRollup=[{"__typename":"CheckRun","name":"rust","conclusion":"SUCCESS"}]' |
| 100 | CASE=missing expect 1 "required check absent from rollup refuses" repo1 7 --dry-run |
| 101 | has "names the missing check" 'legacy-ci' |
| 102 | fx failing '.statusCheckRollup[0].conclusion="FAILURE"' |
| 103 | CASE=failing expect 1 "required check present but FAILURE refuses" repo1 7 --dry-run |
| 104 | fx changes '.reviewDecision="CHANGES_REQUESTED"'; CASE=changes expect 1 "CHANGES_REQUESTED refuses" repo1 7 --dry-run |
| 105 | fx reqrev '.reviewDecision="REVIEW_REQUIRED"'; CASE=reqrev expect 1 "REVIEW_REQUIRED refuses" repo1 7 --dry-run |
| 106 | fx approved '.reviewDecision="APPROVED"'; CASE=approved expect 0 "APPROVED passes" repo1 7 --dry-run |
| 107 | fx noprot; touch "$FX/noprot/prot404"; CASE=noprot expect 0 "404 on protection gives SKIP and passes" repo1 7 --dry-run |
| 108 | has "no protection SKIP line" 'GATE checks: SKIP no branch protection' |
| 109 | has "no protection warning" 'WARNING' |
| 110 | fx noprotb '.mergeStateStatus="BEHIND"'; touch "$FX/noprotb/prot404" |
| 111 | CASE=noprotb expect 1 "404 protection does not rescue a BEHIND PR" repo1 7 --dry-run |
| 112 | |
| 113 | # ---- merge cases |
| 114 | fx merge; CASE=merge expect 0 "clean PR merges (non-dry-run)" repo1 7 |
| 115 | has "merge PASS with sha" 'GATE merge: PASS squash; merge commit cafebabe' |
| 116 | if grep -q -- '--match-head-commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' "$FX/merge/merge.cmd"; then ok; else no "merge binds the inspected head"; fi |
| 117 | fx moved |
| 118 | echo "$CLEANPR" | jq '.headRefOid="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"' > "$FX/moved/updated.json" |
| 119 | CASE=moved expect 1 "moving head refuses at merge" repo1 7 |
| 120 | has "moving head refuses merge" 'head branch changed' |
| 121 | check_notmerged() { if [ ! -e "$FX/$1/merged-flag" ]; then ok; else no "$2"; fi; } |
| 122 | check_notmerged moved "moving head was merged" |
| 123 | fx unknownmoved '.mergeStateStatus="UNKNOWN"' |
| 124 | echo "$CLEANPR" | jq '.headRefOid="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"' > "$FX/unknownmoved/updated.json" |
| 125 | CASE=unknownmoved expect 1 "UNKNOWN refresh changing head refuses before merge" repo1 7 |
| 126 | has "UNKNOWN head changed reason" 'head changed during mergeability wait' |
| 127 | check_notmerged unknownmoved "UNKNOWN refresh merged changed head" |
| 128 | if grep -q -- '--squash --delete-branch' "$FX/merge/merge.cmd" && ! grep -q -- '--admin' "$FX/merge/merge.cmd"; then ok; else no "merge command has squash+delete-branch and no --admin: $(cat "$FX/merge/merge.cmd" 2>&1)"; fi |
| 129 | fx adm; CASE=adm expect 0 "authorized admin merge" repo1 7 --admin-authorized-by "Drake, 2026-10-10, test" |
| 130 | if grep -q -- '--admin' "$FX/adm/merge.cmd"; then ok; else no "--admin recorded when authorized"; fi |
| 131 | has "authorization text printed" 'authorized by: Drake, 2026-10-10, test' |
| 132 | fx admfail '.mergeStateStatus="BLOCKED"'; CASE=admfail expect 1 "--admin never overrides a BLOCKED PR" repo1 7 --admin-authorized-by "Drake, 2026-10-10, test" |
| 133 | check_nomerge admfail "no merge call for BLOCKED even with authorization" |
| 134 | fx notmerged; echo '{"state":"OPEN","mergedAt":null,"mergeCommit":null}' > "$FX/notmerged/merged.json" |
| 135 | CASE=notmerged expect 1 "merge not confirmed is a FAIL" repo1 7 |
| 136 | has "reports not MERGED" 'not MERGED' |
| 137 | CASE=clean expect 2 "bad usage exits 2" repo1 |
| 138 | CASE=clean expect 2 "non-numeric PR exits 2" repo1 abc |
| 139 | |
| 140 | # ---- local and crumb cases with real git repos |
| 141 | # mkrepo <name> [nocrumb]: main has src.txt (+ .crumb); branch feat = commit "feat", then (crumbs) commit "crumb compile". |
| 142 | mkrepo() { |
| 143 | d="$tmp/$1"; mkdir -p "$d"; git init -q --bare -b main "$d/origin.git"; git init -q -b main "$d/seed" |
| 144 | ( cd "$d/seed" && echo base > src.txt && { [ "${2:-}" = nocrumb ] || echo c0 > .crumb; } && git add -A && git commit -q -m base \ |
| 145 | && git remote add origin "$d/origin.git" && git push -q origin main ) |
| 146 | git clone -q "$d/origin.git" "$d/w" |
| 147 | ( cd "$d/w" && git checkout -q -b feat && echo feat > feat.txt && git add -A && git commit -q -m feat |
| 148 | if [ "${2:-}" != nocrumb ]; then echo c1 > .crumb && git add -A && git commit -q -m "crumb compile"; fi |
| 149 | git push -q origin feat ) |
| 150 | W="$d/w" |
| 151 | } |
| 152 | lfx() { # lfx <case> : fixture whose headRefOid is the worktree HEAD |
| 153 | fx "$1" ".headRefOid=\"$(git -C "$W" rev-parse HEAD)\"" |
| 154 | } |
| 155 | mkrepo l1; lfx l1; CASE=l1 expect 0 "local+crumb clean passes" repo1 7 --dry-run --worktree "$W" |
| 156 | has "local PASS" 'GATE local: PASS' |
| 157 | has "crumb step retired with worktree" "$RETIRED" |
| 158 | |
| 159 | mkrepo l2; echo dirt >> "$W/feat.txt"; lfx l2; CASE=l2 expect 1 "dirty worktree refuses" repo1 7 --dry-run --worktree "$W" |
| 160 | has "dirty reason" 'worktree is dirty' |
| 161 | |
| 162 | mkrepo l3; git -C "$W" checkout -q -b other; lfx l3; CASE=l3 expect 1 "wrong branch refuses" repo1 7 --dry-run --worktree "$W" |
| 163 | has "wrong branch reason" 'worktree is on other' |
| 164 | |
| 165 | mkrepo l4 |
| 166 | fx l4 '.headRefOid="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'; CASE=l4 expect 1 "local HEAD differs from GitHub head refuses" repo1 7 --dry-run --worktree "$W" |
| 167 | has "oid mismatch reason" 'differs from GitHub head' |
| 168 | |
| 169 | mkrepo l5 |
| 170 | ( cd "$tmp/l5/seed" && echo more > more.txt && git add -A && git commit -q -m "main moves" && git push -q origin main ) |
| 171 | lfx l5; CASE=l5 expect 1 "stale base refuses" repo1 7 --dry-run --worktree "$W" |
| 172 | has "stale base points at rebase script" 'run scripts/rebase-crumb.sh' |
| 173 | |
| 174 | mkrepo l6; ( cd "$W" && echo src2 > src2.txt && git add -A && git commit -q --amend -m "mixed" ) |
| 175 | lfx l6; CASE=l6 expect 0 "HEAD commit mixing source and .crumb no longer refuses (rule 2 retired)" repo1 7 --dry-run --worktree "$W" |
| 176 | has "mixed commit gives retired SKIP" "$RETIRED" |
| 177 | hasnot "mixed commit not refused by rule 2" 'must be separate and last' |
| 178 | |
| 179 | mkrepo l7; lfx l7; CRUMB=/nonexistent/crumb CASE=l7 expect 0 "missing crumb tool does not matter" repo1 7 --dry-run --worktree "$W" |
| 180 | has "missing crumb tool gives retired SKIP" "$RETIRED" |
| 181 | hasnot "no crumb verify message" 'did not print OK' |
| 182 | |
| 183 | mkrepo l8 nocrumb; lfx l8; CASE=l8 expect 0 "repo without crumbs passes" repo1 7 --dry-run --worktree "$W" |
| 184 | has "no crumbs gives retired SKIP" "$RETIRED" |
| 185 | |
| 186 | # crumbs exist but this PR does not touch them: HEAD may be a source commit |
| 187 | mkrepo l9 |
| 188 | ( cd "$W" && git reset -q --hard HEAD~1 && git push -q -f origin feat ) |
| 189 | lfx l9; CASE=l9 expect 0 "PR that does not touch crumbs may end on a source commit" repo1 7 --dry-run --worktree "$W" |
| 190 | |
| 191 | # a PR that deletes a .crumb, or changes source after its crumb commit (stale crumbs), is not refused |
| 192 | mkrepo l10; ( cd "$W" && git rm -q .crumb && git commit -q -m "delete crumb" && git push -q origin feat ) |
| 193 | lfx l10; CASE=l10 expect 0 "deleted .crumb does not refuse" repo1 7 --dry-run --worktree "$W" |
| 194 | has "deleted .crumb gives retired SKIP" "$RETIRED" |
| 195 | mkrepo l11; ( cd "$W" && echo later > later.txt && git add -A && git commit -q -m "source after crumb" && git push -q origin feat ) |
| 196 | lfx l11; CASE=l11 expect 0 "source after the crumb commit does not refuse" repo1 7 --dry-run --worktree "$W" |
| 197 | |
| 198 | if [ ! -e "$FX/crumb-called" ]; then ok; else no "crumb tool was called: $(cat "$FX/crumb-called")"; fi |
| 199 | |
| 200 | echo "PASS $pass FAIL $bad" |
| 201 | [ "$bad" -eq 0 ] |