interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / collab / tests / links.sh

76 lines · 5758 bytes Raw Blame History
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.
11untr() { 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; }
12set -u
13H=$(cd "$(dirname "$0")" && pwd)
14export PORT=${LINKS_PORT:-18497} CORES=${CORES:-14}
15T=$(mktemp -d /tmp/cplinks.XXXXXX)
16W=$T/w
17PASS=0; FAIL=0; PID=
18ok() { PASS=$((PASS+1)); echo "PASS $*"; }
19bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
20has() { grep -qF -- "$1" "$T/body"; }
21trap '[ -n "$PID" ] && { kill $PID 2>/dev/null; wait $PID 2>/dev/null; }; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT
22
23export RAD_HOME=$T/rad RAD_PASSPHRASE=
24rad auth --alias cplinks < /dev/null > /dev/null 2>&1 || { echo "rad auth failed"; exit 1; }
25git 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; }
29git config user.name t; git config user.email t@t.invalid
30RID=$(rad . | sed 's/^rad://')
31printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| cplinks | rad:%s | main |\n' "$RID" > "$T/rids.md"
32
33mkdir -p "$T/data" "$T/social"
34DATA=$T/data/ STORE=$RAD_HOME/storage/ RIDS=$T/rids.md SEDX="${MUTX:-}" "$H/tbuild.sh" "$T/forged" > /dev/null || exit 1
35FORGE_SOCIAL_DIR=$T/social/ taskset -c "$CORES" nice -n 10 "$T/forged" 2>> "$T/forged.err" &
36PID=$!
37U="http://127.0.0.1:$PORT"
38for i in $(seq 50); do curl -s -o /dev/null "$U/" && break; sleep 0.1; done
39kill -0 $PID 2> /dev/null || { echo "test forged did not start: $(cat "$T/forged.err")"; exit 1; }
40R=/drakestapleton/cplinks
41get() { c=$(curl -s -m 20 -o "$T/body" "$U$R/$1" -w '%{http_code}'); untr "$T/body"; printf %s "$c"; }
42ids() { get "$1" > /dev/null; grep -o "$R/$1/[0-9a-f]\{40\}\"" "$T/body" | sed 's|.*/||; s/"//' | sort -u; }
43
44rad issue open --title "First bug" --description "one" --no-announce > /dev/null 2>&1 || { echo "rad issue open failed"; exit 1; }
45I1=$(ids issues)
46rad issue open --title "Second bug" --description "two" --no-announce > /dev/null 2>&1 || { echo "rad issue open failed"; exit 1; }
47I2=$(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
51patch() { # 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}
55L=$(patch l l.txt "Fix the first bug" "Closes issue/${I1:0:8}, see notes.")
56N=$(patch u u.txt "Unrelated work" "Mentions tissue/${I1:0:8} and issue/${I1:0:6} and issue/zzzzzzzz only.")
57C=$(patch c c.txt "Third change" "No issue named here.")
58rad 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
61c=$(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")"
63has 'Linked patches' && ! has "$R/patches/$N\"" && ok "NEG unrelated patch U not linked to issue 1" || bad "patch U linked to issue 1"
64has '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"
65get "issues/$I2" > /dev/null
66has "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")"
67get "patches/$L" > /dev/null
68has "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")"
69c=$(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")"
71get "patches/$C" > /dev/null
72has "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")"
73curl -s -o "$T/body" -H 'X-Forge-Base: /forge/' "$U$R/issues/$I1"; untr "$T/body"
74has "href=\"/forge$R/patches/$L\"" && ok "links follow X-Forge-Base /forge/" || bad "/forge/ link"
75echo "collab links: $PASS passed, $FAIL failed"
76[ "$FAIL" = 0 ]