interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / scripts / test_pr_gate.sh

201 lines · 12319 bytes Raw Blame History
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.
6set -u
7here="$(cd "$(dirname "$0")" && pwd)"
8gate="$here/pr-gate.sh"
9tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
10export GIT_AUTHOR_NAME=T GIT_AUTHOR_EMAIL=t@example.invalid GIT_COMMITTER_NAME=T GIT_COMMITTER_EMAIL=t@example.invalid
11export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
12export SLEEP_SECS=0
13FX="$tmp/fx"; mkdir -p "$FX" "$tmp/bin"; export FX
14
15cat > "$tmp/bin/gh" <<'S'
16#!/usr/bin/env bash
17d="$FX/$CASE"
18case "$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 ;;
38esac
39S
40cat > "$tmp/bin/crumb" <<'S'
41#!/usr/bin/env bash
42# retired: any call is a test failure (recorded, then exit 99)
43echo "$*" >> "$FX/crumb-called"
44exit 99
45S
46chmod +x "$tmp/bin/gh" "$tmp/bin/crumb"
47export PATH="$tmp/bin:$PATH" CRUMB="$tmp/bin/crumb"
48RETIRED='GATE crumb: SKIP retired 2026-11-07 \(DRA-97\); .crumb files are history'
49
50pass=0; bad=0
51ok() { pass=$((pass+1)); }
52no() { bad=$((bad+1)); echo "TEST FAIL: $1"; }
53run() { OUT=$("$gate" "$@" 2>&1); rc=$?; }
54expect() { # 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}
58has() { # has <label> <pattern> against $OUT
59 if echo "$OUT" | grep -qE -- "$2"; then ok; else no "$1 (missing /$2/): $OUT"; fi
60}
61hasnot() { 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]
64CLEANPR='{"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"}]}'
65fx() {
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)
74fx clean; export CASE=clean; expect 0 "clean PR passes dry run" repo1 7 --dry-run
75has "dry run prints pr PASS" 'GATE pr: PASS'
76has "local skipped without worktree" 'GATE local: SKIP no worktree given'
77has "merge skipped in dry run" 'GATE merge: SKIP dry run'
78has "crumb step retired without worktree" "$RETIRED"
79has "queue marks BEHIND PR as needing rebase" 'QUEUE #9 other BEHIND needs scripts/rebase-crumb.sh'
80has "queue shows clean PR plainly" 'QUEUE #10 third CLEAN$'
81hasnot "queue omits the PR itself" 'QUEUE #7 '
82check_nomerge() { if [ ! -e "$FX/$1/merge.cmd" ]; then ok; else no "$2"; fi; }
83check_nomerge clean "dry run must not call gh pr merge"
84
85fx behind '.mergeStateStatus="BEHIND"'; CASE=behind expect 1 "BEHIND refuses" repo1 7 --dry-run
86has "BEHIND reason" 'stale, rebase first'
87fx dirty '.mergeStateStatus="DIRTY"'; CASE=dirty expect 1 "DIRTY refuses" repo1 7 --dry-run
88fx blocked '.mergeStateStatus="BLOCKED"'; CASE=blocked expect 1 "BLOCKED refuses" repo1 7 --dry-run
89fx unstable '.mergeStateStatus="UNSTABLE"'; CASE=unstable expect 1 "UNSTABLE refuses" repo1 7 --dry-run
90fx conflicting '.mergeable="CONFLICTING"'; CASE=conflicting expect 1 "mergeable CONFLICTING refuses" repo1 7 --dry-run
91fx unknown '.mergeStateStatus="UNKNOWN"'; CASE=unknown expect 1 "UNKNOWN retries then refuses" repo1 7 --dry-run
92has "UNKNOWN reason" 'has not computed mergeability'
93fx hooks '.mergeStateStatus="HAS_HOOKS"'; CASE=hooks expect 0 "HAS_HOOKS passes in dry run" repo1 7 --dry-run
94CASE=hooks expect 1 "HAS_HOOKS refuses on a real merge" repo1 7
95check_nomerge hooks "HAS_HOOKS real run must not merge"
96fx draft '.isDraft=true'; CASE=draft expect 1 "draft refuses" repo1 7 --dry-run
97fx closed '.state="CLOSED"'; CASE=closed expect 1 "closed refuses" repo1 7 --dry-run
98fx wrongbase '.baseRefName="release"'; CASE=wrongbase expect 1 "non-main base refuses" repo1 7 --dry-run
99fx missing '.statusCheckRollup=[{"__typename":"CheckRun","name":"rust","conclusion":"SUCCESS"}]'
100CASE=missing expect 1 "required check absent from rollup refuses" repo1 7 --dry-run
101has "names the missing check" 'legacy-ci'
102fx failing '.statusCheckRollup[0].conclusion="FAILURE"'
103CASE=failing expect 1 "required check present but FAILURE refuses" repo1 7 --dry-run
104fx changes '.reviewDecision="CHANGES_REQUESTED"'; CASE=changes expect 1 "CHANGES_REQUESTED refuses" repo1 7 --dry-run
105fx reqrev '.reviewDecision="REVIEW_REQUIRED"'; CASE=reqrev expect 1 "REVIEW_REQUIRED refuses" repo1 7 --dry-run
106fx approved '.reviewDecision="APPROVED"'; CASE=approved expect 0 "APPROVED passes" repo1 7 --dry-run
107fx noprot; touch "$FX/noprot/prot404"; CASE=noprot expect 0 "404 on protection gives SKIP and passes" repo1 7 --dry-run
108has "no protection SKIP line" 'GATE checks: SKIP no branch protection'
109has "no protection warning" 'WARNING'
110fx noprotb '.mergeStateStatus="BEHIND"'; touch "$FX/noprotb/prot404"
111CASE=noprotb expect 1 "404 protection does not rescue a BEHIND PR" repo1 7 --dry-run
112
113# ---- merge cases
114fx merge; CASE=merge expect 0 "clean PR merges (non-dry-run)" repo1 7
115has "merge PASS with sha" 'GATE merge: PASS squash; merge commit cafebabe'
116if grep -q -- '--match-head-commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' "$FX/merge/merge.cmd"; then ok; else no "merge binds the inspected head"; fi
117fx moved
118echo "$CLEANPR" | jq '.headRefOid="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"' > "$FX/moved/updated.json"
119CASE=moved expect 1 "moving head refuses at merge" repo1 7
120has "moving head refuses merge" 'head branch changed'
121check_notmerged() { if [ ! -e "$FX/$1/merged-flag" ]; then ok; else no "$2"; fi; }
122check_notmerged moved "moving head was merged"
123fx unknownmoved '.mergeStateStatus="UNKNOWN"'
124echo "$CLEANPR" | jq '.headRefOid="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"' > "$FX/unknownmoved/updated.json"
125CASE=unknownmoved expect 1 "UNKNOWN refresh changing head refuses before merge" repo1 7
126has "UNKNOWN head changed reason" 'head changed during mergeability wait'
127check_notmerged unknownmoved "UNKNOWN refresh merged changed head"
128if 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
129fx adm; CASE=adm expect 0 "authorized admin merge" repo1 7 --admin-authorized-by "Drake, 2026-10-10, test"
130if grep -q -- '--admin' "$FX/adm/merge.cmd"; then ok; else no "--admin recorded when authorized"; fi
131has "authorization text printed" 'authorized by: Drake, 2026-10-10, test'
132fx admfail '.mergeStateStatus="BLOCKED"'; CASE=admfail expect 1 "--admin never overrides a BLOCKED PR" repo1 7 --admin-authorized-by "Drake, 2026-10-10, test"
133check_nomerge admfail "no merge call for BLOCKED even with authorization"
134fx notmerged; echo '{"state":"OPEN","mergedAt":null,"mergeCommit":null}' > "$FX/notmerged/merged.json"
135CASE=notmerged expect 1 "merge not confirmed is a FAIL" repo1 7
136has "reports not MERGED" 'not MERGED'
137CASE=clean expect 2 "bad usage exits 2" repo1
138CASE=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".
142mkrepo() {
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}
152lfx() { # lfx <case> : fixture whose headRefOid is the worktree HEAD
153 fx "$1" ".headRefOid=\"$(git -C "$W" rev-parse HEAD)\""
154}
155mkrepo l1; lfx l1; CASE=l1 expect 0 "local+crumb clean passes" repo1 7 --dry-run --worktree "$W"
156has "local PASS" 'GATE local: PASS'
157has "crumb step retired with worktree" "$RETIRED"
158
159mkrepo l2; echo dirt >> "$W/feat.txt"; lfx l2; CASE=l2 expect 1 "dirty worktree refuses" repo1 7 --dry-run --worktree "$W"
160has "dirty reason" 'worktree is dirty'
161
162mkrepo l3; git -C "$W" checkout -q -b other; lfx l3; CASE=l3 expect 1 "wrong branch refuses" repo1 7 --dry-run --worktree "$W"
163has "wrong branch reason" 'worktree is on other'
164
165mkrepo l4
166fx l4 '.headRefOid="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'; CASE=l4 expect 1 "local HEAD differs from GitHub head refuses" repo1 7 --dry-run --worktree "$W"
167has "oid mismatch reason" 'differs from GitHub head'
168
169mkrepo l5
170( cd "$tmp/l5/seed" && echo more > more.txt && git add -A && git commit -q -m "main moves" && git push -q origin main )
171lfx l5; CASE=l5 expect 1 "stale base refuses" repo1 7 --dry-run --worktree "$W"
172has "stale base points at rebase script" 'run scripts/rebase-crumb.sh'
173
174mkrepo l6; ( cd "$W" && echo src2 > src2.txt && git add -A && git commit -q --amend -m "mixed" )
175lfx l6; CASE=l6 expect 0 "HEAD commit mixing source and .crumb no longer refuses (rule 2 retired)" repo1 7 --dry-run --worktree "$W"
176has "mixed commit gives retired SKIP" "$RETIRED"
177hasnot "mixed commit not refused by rule 2" 'must be separate and last'
178
179mkrepo l7; lfx l7; CRUMB=/nonexistent/crumb CASE=l7 expect 0 "missing crumb tool does not matter" repo1 7 --dry-run --worktree "$W"
180has "missing crumb tool gives retired SKIP" "$RETIRED"
181hasnot "no crumb verify message" 'did not print OK'
182
183mkrepo l8 nocrumb; lfx l8; CASE=l8 expect 0 "repo without crumbs passes" repo1 7 --dry-run --worktree "$W"
184has "no crumbs gives retired SKIP" "$RETIRED"
185
186# crumbs exist but this PR does not touch them: HEAD may be a source commit
187mkrepo l9
188( cd "$W" && git reset -q --hard HEAD~1 && git push -q -f origin feat )
189lfx 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
192mkrepo l10; ( cd "$W" && git rm -q .crumb && git commit -q -m "delete crumb" && git push -q origin feat )
193lfx l10; CASE=l10 expect 0 "deleted .crumb does not refuse" repo1 7 --dry-run --worktree "$W"
194has "deleted .crumb gives retired SKIP" "$RETIRED"
195mkrepo l11; ( cd "$W" && echo later > later.txt && git add -A && git commit -q -m "source after crumb" && git push -q origin feat )
196lfx l11; CASE=l11 expect 0 "source after the crumb commit does not refuse" repo1 7 --dry-run --worktree "$W"
197
198if [ ! -e "$FX/crumb-called" ]; then ok; else no "crumb tool was called: $(cat "$FX/crumb-called")"; fi
199
200echo "PASS $pass FAIL $bad"
201[ "$bad" -eq 0 ]