interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / collab / tests / links.sh
| 1 | #!/bin/bash |
| 2 | # collab/tests/links.sh: HOST test of issue <-> patch links (docs/FEATURES.md "Issue links: exact rule"). |
| 3 | # Like compose.sh, everything is private to a temp dir: a throwaway Radicle profile (RAD_HOME) with a |
| 4 | # private repo made with heartwood's rad, and a test forged on PORT (default 18497) reading that store. |
| 5 | # Issue I1 is named by patch L in its description; issue I2 by a comment on patch C; patch U only has |
| 6 | # near misses (a letter before issue/, a 6-digit prefix, no hex) and must link nothing. MUTX adds one |
| 7 | # sed expression to the build (a mutant): with it the test must fail. |
| 8 | # Usage: collab/tests/links.sh (CORES default 14; KEEP=1 keeps the temp dir) |
| 9 | # untr FILE: forged adds translation hints while it sends an HTML page (server/tr.om); the checks here |
| 10 | # read pages without them (server/tests/translate.sh gates the hints). Other bodies are left as sent. |
| 11 | untr() { case "$(head -c 15 "$1" 2>/dev/null)" in '<!doctype html'*) sed -i 's/<span translate="no">\([^<]*\)<\/span>/\1/g; s/ translate="no"//g' "$1" ;; esac; } |
| 12 | set -u |
| 13 | H=$(cd "$(dirname "$0")" && pwd) |
| 14 | export PORT=${LINKS_PORT:-18497} CORES=${CORES:-14} |
| 15 | T=$(mktemp -d /tmp/cplinks.XXXXXX) |
| 16 | W=$T/w |
| 17 | PASS=0; FAIL=0; PID= |
| 18 | ok() { PASS=$((PASS+1)); echo "PASS $*"; } |
| 19 | bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } |
| 20 | has() { grep -qF -- "$1" "$T/body"; } |
| 21 | trap '[ -n "$PID" ] && { kill $PID 2>/dev/null; wait $PID 2>/dev/null; }; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT |
| 22 | |
| 23 | export RAD_HOME=$T/rad RAD_PASSPHRASE= |
| 24 | rad auth --alias cplinks < /dev/null > /dev/null 2>&1 || { echo "rad auth failed"; exit 1; } |
| 25 | git init -q -b main "$W" && cd "$W" && echo hello > a.txt && git add a.txt \ |
| 26 | && git -c user.name=t -c user.email=t@t.invalid commit -qm init \ |
| 27 | && rad init --private --name cplinks --description "links test" --default-branch main --no-confirm --no-seed > /dev/null 2>&1 \ |
| 28 | || { echo "rad init failed"; exit 1; } |
| 29 | git config user.name t; git config user.email t@t.invalid |
| 30 | RID=$(rad . | sed 's/^rad://') |
| 31 | printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| cplinks | rad:%s | main |\n' "$RID" > "$T/rids.md" |
| 32 | |
| 33 | mkdir -p "$T/data" "$T/social" |
| 34 | DATA=$T/data/ STORE=$RAD_HOME/storage/ RIDS=$T/rids.md SEDX="${MUTX:-}" "$H/tbuild.sh" "$T/forged" > /dev/null || exit 1 |
| 35 | FORGE_SOCIAL_DIR=$T/social/ taskset -c "$CORES" nice -n 10 "$T/forged" 2>> "$T/forged.err" & |
| 36 | PID=$! |
| 37 | U="http://127.0.0.1:$PORT" |
| 38 | for i in $(seq 50); do curl -s -o /dev/null "$U/" && break; sleep 0.1; done |
| 39 | kill -0 $PID 2> /dev/null || { echo "test forged did not start: $(cat "$T/forged.err")"; exit 1; } |
| 40 | R=/drakestapleton/cplinks |
| 41 | get() { c=$(curl -s -m 20 -o "$T/body" "$U$R/$1" -w '%{http_code}'); untr "$T/body"; printf %s "$c"; } |
| 42 | ids() { get "$1" > /dev/null; grep -o "$R/$1/[0-9a-f]\{40\}\"" "$T/body" | sed 's|.*/||; s/"//' | sort -u; } |
| 43 | |
| 44 | rad issue open --title "First bug" --description "one" --no-announce > /dev/null 2>&1 || { echo "rad issue open failed"; exit 1; } |
| 45 | I1=$(ids issues) |
| 46 | rad issue open --title "Second bug" --description "two" --no-announce > /dev/null 2>&1 || { echo "rad issue open failed"; exit 1; } |
| 47 | I2=$(ids issues | grep -v "$I1") |
| 48 | [ ${#I1} = 40 ] && [ ${#I2} = 40 ] && ok "two issues on the forge" || { bad "issue ids '$I1' '$I2'"; exit 1; } |
| 49 | [ "${I1:0:6}" != "${I2:0:6}" ] || { echo "NOT_RUN: the two issue ids share 6 hex digits; rerun"; exit 1; } |
| 50 | |
| 51 | patch() { # BRANCH FILE TITLE DESCRIPTION -> patch id on stdout |
| 52 | git checkout -q main && git checkout -qb "$1" && echo "$1" > "$2" && git add "$2" && git commit -qm "$3" \ |
| 53 | && git push -o "patch.message=$3" -o "patch.message=$4" rad HEAD:refs/patches 2>&1 | grep -o '[0-9a-f]\{40\}' | head -1 |
| 54 | } |
| 55 | L=$(patch l l.txt "Fix the first bug" "Closes issue/${I1:0:8}, see notes.") |
| 56 | N=$(patch u u.txt "Unrelated work" "Mentions tissue/${I1:0:8} and issue/${I1:0:6} and issue/zzzzzzzz only.") |
| 57 | C=$(patch c c.txt "Third change" "No issue named here.") |
| 58 | rad patch comment "$C" --message "Also fixes (issue/$I2)" --repo "rad:$RID" > /dev/null 2>&1 || bad "rad patch comment" |
| 59 | [ ${#L} = 40 ] && [ ${#N} = 40 ] && [ ${#C} = 40 ] && ok "three patches pushed" || { bad "patch ids '$L' '$N' '$C'"; exit 1; } |
| 60 | |
| 61 | c=$(get "issues/$I1") |
| 62 | [ "$c" = 200 ] && has 'Linked patches' && has "href=\"$R/patches/$L\">Fix the first bug</a>" && ok "issue 1 lists patch L (description link)" || bad "issue 1 page ($c): $(grep -o 'Linked patches.\{0,300\}' "$T/body")" |
| 63 | has 'Linked patches' && ! has "$R/patches/$N\"" && ok "NEG unrelated patch U not linked to issue 1" || bad "patch U linked to issue 1" |
| 64 | has 'Linked patches' && ! has "$R/patches/$C\"" && ok "NEG patch C (names issue 2) not linked to issue 1" || bad "patch C linked to issue 1" |
| 65 | get "issues/$I2" > /dev/null |
| 66 | has "href=\"$R/patches/$C\">Third change</a>" && ! has "$R/patches/$L\"" && ok "issue 2 lists only patch C (comment link)" || bad "issue 2 page: $(grep -o 'Linked patches.\{0,300\}' "$T/body")" |
| 67 | get "patches/$L" > /dev/null |
| 68 | has "Linked issues: <a href=\"$R/issues/$I1\"><code>${I1:0:8}</code></a></p>" && ok "patch L lists issue 1" || bad "patch L: $(grep -o 'Linked issue.\{0,200\}' "$T/body")" |
| 69 | c=$(get "patches/$N") |
| 70 | [ "$c" = 200 ] && has 'No linked issue' && ! has "$R/issues/$I1\"" && ok "NEG patch U lists no issue (letter before issue/, short prefix, no hex)" || bad "patch U ($c): $(grep -o 'Linked issue.\{0,200\}' "$T/body")" |
| 71 | get "patches/$C" > /dev/null |
| 72 | has "Linked issues: <a href=\"$R/issues/$I2\"><code>${I2:0:8}</code></a></p>" && ok "patch C lists issue 2 (from its comment)" || bad "patch C: $(grep -o 'Linked issue.\{0,200\}' "$T/body")" |
| 73 | curl -s -o "$T/body" -H 'X-Forge-Base: /forge/' "$U$R/issues/$I1"; untr "$T/body" |
| 74 | has "href=\"/forge$R/patches/$L\"" && ok "links follow X-Forge-Base /forge/" || bad "/forge/ link" |
| 75 | echo "collab links: $PASS passed, $FAIL failed" |
| 76 | [ "$FAIL" = 0 ] |