interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / server / tests / counts.sh
| 1 | #!/usr/bin/env bash |
| 2 | # server/tests/counts.sh: host gates for the JSON form (forge/explore/1, forge/repo/1) and the real |
| 3 | # clone / download / seeding counts of the repo home (card t_2ace6336, docs/API.md). |
| 4 | # bash server/tests/counts.sh (spark; FORGED_CPUS default 12-14; ports FORGE_TEST_PORT 18487 and +1) |
| 5 | # Builds two trial copies of forged: HEAD of this tree and the BASE commit (FORGE_TEST_BASE, required: there is |
| 6 | # no default), with the data dir, the port, the two nginx logs, the node-db stamp, rad and |
| 7 | # getent redirected into a temp dir (rad and getent are the fakes in server/tests/fake/). Fixture logs |
| 8 | # hold counted lines and negative controls (our own addresses, the home address, bots, 404, POST |
| 9 | # variants, a non-public RID, 304, method 3). Checks: |
| 10 | # counts: HTML header and JSON equal the fixture's expected numbers; a log that grows is read from |
| 11 | # where it stopped; a restart replays the journal without counting twice; a rotation (<log> moved |
| 12 | # to <log>.1, new <log>) counts the rest of .1 and the new file; a torn journal batch is ignored; |
| 13 | # nothing is written when the logs did not change; rad failing shows "not measured" / null; with the |
| 14 | # rotated log gone, a restart keeps the counts from the journal alone. |
| 15 | # JSON: valid (python3 -m json.tool) under X-Forge-Base / and /forge/, schema names, paths built |
| 16 | # from the base, last_commit sha and time equal git (oracle) for every public repo, file names |
| 17 | # equal git ls-tree, Accept negotiation, .json suffix, 405 on POST, 404 for denied / unknown .json. |
| 18 | # HTML: every page's <main> equals the BASE build's apart from the known additions listed in |
| 19 | # section 8 (the alternate link, the three count lines, and the README-first project page, whose |
| 20 | # lines are compared as a sorted list after its new header and section lines are removed). |
| 21 | # FORGE_TEST_MUTANT=<sed script> applies a mutant to the HEAD copy (server/tests/counts-mutants.sh). |
| 22 | # Read-only on real storage; never touches 18480, ~/forge-data, the real logs or port 18091. |
| 23 | set -u |
| 24 | cd "$(dirname "$0")/../.." |
| 25 | ROOT=$(pwd) |
| 26 | CORES=${FORGED_CPUS:-12-14} |
| 27 | PORT=${FORGE_TEST_PORT:-18487} |
| 28 | PORTB=$((PORT + 1)) |
| 29 | # The base has no default: on a line that holds later page changes the edge is the wrong base and one |
| 30 | # HTML check then fails for a reason that is not in the head (review of 56990a44, check 9). For |
| 31 | # integrate/one the base is 5864d34282ec18914c0f2e274015ae3bafc50d47 (cb98401, the line before |
| 32 | # json-counts, plus forge/simplify at 21196c82; kept in ~/work/forge/web as refs/integrate-one/counts-base). |
| 33 | if [ -z "${FORGE_TEST_BASE:-}" ]; then echo "NOT_RUN: FORGE_TEST_BASE is not set: name the commit whose pages this head must equal (see the note above this line)"; exit 3; fi |
| 34 | BASE=$FORGE_TEST_BASE |
| 35 | # A fresh clone of the Radicle project does not hold that base (bay 6 at 3edbe5aa: NOT_RUN). Fetch the |
| 36 | # objects of refs/integrate-one/counts-base from FORGE_TEST_BASE_REPO (default ~/work/forge/web; read |
| 37 | # only, no ref is written here), then check again. |
| 38 | if ! git rev-parse -q --verify "$BASE^{commit}" > /dev/null; then |
| 39 | BR=${FORGE_TEST_BASE_REPO:-$HOME/work/forge/web} |
| 40 | [ -d "$BR" ] && git fetch -q --no-tags "$BR" refs/integrate-one/counts-base 2> /dev/null \ |
| 41 | && echo "base $BASE fetched from $BR refs/integrate-one/counts-base" |
| 42 | fi |
| 43 | git rev-parse -q --verify "$BASE^{commit}" > /dev/null || { echo "NOT_RUN: base $BASE is not a commit of this repository"; exit 3; } |
| 44 | OMC=$HOME/work/flux/omega-mv/seed/out/omc |
| 45 | PY=/usr/bin/python3 |
| 46 | STORE=$HOME/.radicle/storage |
| 47 | R=drakestapleton/cuda-crack |
| 48 | RID=z631r2JJiVT8it4tST7RK24hpowi |
| 49 | DENY=z2ft64NFLsKzWttCXw6NWNbRQLggQ |
| 50 | NSID=z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK |
| 51 | T=$(mktemp -d) |
| 52 | pid=; pidb= |
| 53 | pass=0; fail=0 |
| 54 | ok() { pass=$((pass+1)); echo "PASS $*"; } |
| 55 | bad() { fail=$((fail+1)); echo "FAIL $*"; } |
| 56 | check() { if [ "$1" = "$2" ]; then ok "$3 ($1)"; else bad "$3: want '$2', got '$1'"; fi; } |
| 57 | cleanup() { [ -n "$pid" ] && kill "$pid" 2>/dev/null; [ -n "$pidb" ] && kill "$pidb" 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"; } |
| 58 | trap cleanup EXIT |
| 59 | mkdir -p "$T/bin" "$T/ng" "$T/data/logs" "$T/datab/logs" "$T/base" |
| 60 | cp server/tests/fake/rad server/tests/fake/getent "$T/bin/" |
| 61 | chmod 755 "$T/bin/rad" "$T/bin/getent" |
| 62 | : > "$T/ng/wal" |
| 63 | |
| 64 | # ---- builds: compose like run.sh, then redirect paths and port in the test copy only |
| 65 | compose() { # dir out port data |
| 66 | (cd "$1" && { printf 'om2\n'; cat $(./run.sh units); }) | sed \ |
| 67 | -e "s|^text tfdata \".*\"|text tfdata \"$4/\"|" \ |
| 68 | -e "s|^text tfzp0 \".*\"|text tfzp0 \"$T/ng/dl.log\"|" \ |
| 69 | -e "s|^text tfzp1 \".*\"|text tfzp1 \"$T/ng/access.log\"|" \ |
| 70 | -e "s|^text tfzwal \".*\"|text tfzwal \"$T/ng/wal\"|" \ |
| 71 | -e "s|^text tfzrad \".*\"|text tfzrad \"$T/bin/rad\"|" \ |
| 72 | -e "s|^text tfzgent \".*\"|text tfzgent \"$T/bin/getent\"|" \ |
| 73 | -e "s/18480/$3/g" > "$2.om.0" |
| 74 | if [ "$1" = "$ROOT" ] && [ -n "${FORGE_TEST_MUTANT:-}" ]; then sed -e "$FORGE_TEST_MUTANT" "$2.om.0" > "$2.om"; else mv "$2.om.0" "$2.om"; fi |
| 75 | [ "$1" = "$ROOT" ] && [ -n "${FORGE_TEST_MUTANT:-}" ] && { cmp -s "$2.om.0" "$2.om" && { echo "MUTANT did not apply"; exit 3; }; echo "MUTANT: $FORGE_TEST_MUTANT"; } |
| 76 | taskset -c "$CORES" nice -n 10 "$OMC" < "$2.om" > "$2" 2> "$2.err" || { echo "BUILD FAILED $1: $(cat "$2.err")"; exit 1; } |
| 77 | chmod 755 "$2" |
| 78 | } |
| 79 | git archive "$BASE" | tar -x -C "$T/base" |
| 80 | compose "$ROOT" "$T/forged" "$PORT" "$T/data" |
| 81 | compose "$T/base" "$T/forged.base" "$PORTB" "$T/datab" |
| 82 | echo "built head $(sha256sum < "$T/forged" | cut -c1-16), base $BASE $(git rev-parse --short "$BASE") $(sha256sum < "$T/forged.base" | cut -c1-16)" |
| 83 | |
| 84 | start() { taskset -c "$CORES" nice -n 10 "$T/forged" 2>> "$T/forged.err" & pid=$!; up "$PORT"; } |
| 85 | stop() { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=; } |
| 86 | up() { for i in $(seq 100); do curl -s -o /dev/null "http://127.0.0.1:$1/explore" && return 0; sleep 0.1; done; echo "server on $1 did not start"; cat "$T/forged.err"; exit 1; } |
| 87 | # get PORT BASE PATH [curl args] -> body $T/b, headers $T/h, prints the status |
| 88 | # get: an HTML body is read without the send-time translation hints of server/tr.om (translate="no" |
| 89 | # attributes and <span translate="no"> around hashes and ids; server/tests/translate_check.py strip), |
| 90 | # so the count lines and the pages compare as text with the BASE build, which adds none |
| 91 | get() { |
| 92 | local p=$1 b=$2 u=$3 c; shift 3 |
| 93 | c=$(curl -s -o "$T/b" -D "$T/h" -w '%{http_code}' -H "X-Forge-Base: $b" -H "X-Real-IP: 127.0.0.1" "$@" "http://127.0.0.1:$p/$u") |
| 94 | if grep -qi '^content-type: text/html' "$T/h"; then python3 server/tests/translate_check.py strip "$T/b" "$T/b.s" && mv "$T/b.s" "$T/b"; fi |
| 95 | printf '%s' "$c" |
| 96 | } |
| 97 | ctype() { tr -d '\r' < "$T/h" | sed -n 's/^[Cc]ontent-[Tt]ype: //p' | tail -1; } |
| 98 | jv() { jq -r "$1" "$T/b"; } |
| 99 | |
| 100 | # ---- fixture logs (TEST-NET addresses; 198.51.100.7 is the fake home address) |
| 101 | Q='/info/refs?service=git-upload-pack' |
| 102 | cat > "$T/ng/dl.log" <<EOF |
| 103 | 1794162545.332 203.0.113.5 200 "/rad/$RID.git$Q" "git/2.56.0" |
| 104 | 1794162546.100 203.0.113.6 200 "/rad/$RID.git/$NSID$Q" "git/2.43.0" |
| 105 | 1794162547.000 192.168.1.9 200 "/rad/$RID.git$Q" "git/2.43.0" |
| 106 | 1794162548.000 198.51.100.7 200 "/rad/$RID.git$Q" "git/2.43.0" |
| 107 | 1794162549.000 203.0.113.7 200 "/rad/$RID.git$Q" "SofyaBot/1.0 (+https://sofya.co/bot)" |
| 108 | 1794162550.000 203.0.113.8 404 "/rad/$RID.git$Q" "git/2.43.0" |
| 109 | 1794162551.000 203.0.113.9 200 "/rad/$DENY.git$Q" "git/2.43.0" |
| 110 | 1794162552.000 203.0.113.10 200 "/code/git/cuda-crack.git$Q" "git/2.43.0" |
| 111 | 1794162553.000 100.100.1.1 200 "/rad/$RID.git$Q" "git/2.43.0" |
| 112 | 1794162554.000 100.200.1.1 200 "/rad/$RID.git$Q" "git/2.43.0" |
| 113 | 1794162555.000 203.0.113.11 200 "/code/zip/cuda-crack.zip" "curl/8.5.0" |
| 114 | EOF |
| 115 | cat > "$T/ng/access.log" <<EOF |
| 116 | 127.0.0.1 - - [08/Nov/2026:05:48:05 -0600] "GET / HTTP/2.0" 301 162 "-" "curl/8.5.0" |
| 117 | 127.0.0.1 - - [08/Nov/2026:05:57:35 -0600] "POST /rad/$RID.git/git-upload-pack HTTP/2.0" 200 145 "-" "git/2.43.0" |
| 118 | 127.0.0.1 - - [08/Nov/2026:05:57:37 -0600] "POST /rad/$RID.git/git-upload-pack HTTP/2.0" 200 133032806 "-" "git/2.43.0" |
| 119 | 127.0.0.1 - - [08/Nov/2026:06:00:00 -0600] "POST /rad/$RID.git/$NSID/git-upload-pack HTTP/1.1" 200 65 "-" "git/2.43.0" |
| 120 | 127.0.0.1 - - [08/Nov/2026:06:00:01 -0600] "POST /rad/$RID.git/git-upload-pack HTTP/2.0" 404 3154 "-" "git/2.43.0" |
| 121 | 127.0.0.1 - - [08/Nov/2026:06:00:02 -0600] "POST /rad/$DENY.git/git-upload-pack HTTP/2.0" 200 65 "-" "git/2.43.0" |
| 122 | 127.0.0.1 - - [08/Nov/2026:06:00:03 -0600] "GET /rad/$RID.git$Q HTTP/2.0" 200 148 "-" "git/2.43.0" |
| 123 | 127.0.0.1 - - [08/Nov/2026:06:00:04 -0600] "POST /rad/$RID.git/git-upload-pack-x HTTP/2.0" 200 65 "-" "git/2.43.0" |
| 124 | EOF |
| 125 | A=/drakestapleton/cuda-crack |
| 126 | cat > "$T/data/logs/forged.log" <<EOF |
| 127 | 2026-11-07T11:02:15Z start /home/drakestapleton/work/forge/web/server/forged (cpus 4-5 nice 10) |
| 128 | forged: reloaded, public repos 28 |
| 129 | 1794049338 127.0.0.1 1 / 200 13440 0 |
| 130 | 1794049400 203.0.113.5 1 $A/archive/refs/heads/main.zip 200 1000 5 |
| 131 | 1794049401 203.0.113.5 1 $A/archive/refs/heads/main.tar.gz 304 0 1 |
| 132 | 1794049402 127.0.0.1 1 $A/archive/refs/heads/main.zip 200 1000 5 |
| 133 | 1794049403 198.51.100.7 1 $A/archive/refs/heads/main.zip 200 1000 5 |
| 134 | 1794049404 203.0.113.6 1 $A/releases/download/v1/cuda-crack-v1.zip 200 1000 5 |
| 135 | 1794049405 203.0.113.6 3 $A/archive/refs/heads/main.zip 200 0 1 |
| 136 | 1794049406 203.0.113.6 1 /drakestapleton/omega-pearl/archive/refs/heads/main.zip 200 1000 5 |
| 137 | 1794049407 203.0.113.6 1 $A/tree/main 200 1000 5 |
| 138 | EOF |
| 139 | cp "$T/data/logs/forged.log" "$T/datab/logs/forged.log" |
| 140 | iso() { TZ=UTC date -u -d "@$1" +%Y-%m-%dT%H:%M:%SZ; } # oracle for the since times |
| 141 | S0=$(iso 1794162545); S2=$(iso 1794049338) |
| 142 | |
| 143 | counts() { # base -> "fetches posts downloads archives releases seeds" from the JSON (posts and seeds: retired, null) |
| 144 | get "$PORT" "$1" "$R.json" > /dev/null |
| 145 | jq -r '[.counts.git_fetches.count, .counts.upload_pack_requests.count, .counts.downloads.count, .counts.downloads.archives, .counts.downloads.release_assets, .counts.seeds.count] | map(tostring) | join(" ")' "$T/b" |
| 146 | } |
| 147 | |
| 148 | # A torn FIRST batch must not retain its uncommitted since marker after its bytes are discarded. |
| 149 | printf 'e g 1794162545 %s\ns 0 1794000000\n' "$RID" > "$T/data/counts.log" # a since that differs from the log's: it must not survive |
| 150 | start |
| 151 | # ---- 1. first read of the fixtures |
| 152 | check "$(counts /)" "4 null 2 1 1 null" "counts from the fixture logs (fetches downloads archives releases; posts and seeds retired)" |
| 153 | check "$(jv .counts.git_fetches.since)" "$S0" "git_fetches since = first line of the download log" |
| 154 | check "$(jv .counts.upload_pack_requests.since)" "null" "upload_pack: source retired, since is null" |
| 155 | check "$(jv .counts.downloads.since)" "$S2" "downloads since = first access line of forged.log" |
| 156 | stop; start |
| 157 | check "$(counts /)" "4 null 2 1 1 null" "restart after recovery of the torn first journal batch" |
| 158 | check "$(jv .counts.git_fetches.since)" "$S0" "since survives recovery of the torn first batch" |
| 159 | check "$(jv .counts.seeds.as_of)" "null" "seeds: source retired, as_of is null" |
| 160 | c=$(get "$PORT" /forge/ "$R"); check "$c" 200 "GET /forge/ repo home" |
| 161 | grep -q 'Clones and fetches over HTTPS: <b>4</b> by visitors' "$T/b" && ok "HTML: 4 visitor fetches" || bad "HTML fetch line: $(grep -o 'Clones and fetches[^<]*<b>[^<]*' "$T/b")" |
| 162 | grep -q "<b>not measured</b> <code>git-upload-pack</code> requests from all clients (the source of this count is retired)" "$T/b" && ok "HTML: upload-pack requests not measured (source retired)" || bad "HTML upload-pack line" |
| 163 | grep -q "Downloads: <b>2</b> by visitors (1 source archives, 1 release assets" "$T/b" && ok "HTML: 2 downloads (1 + 1)" || bad "HTML downloads: $(grep -o 'Downloads:.\{0,80\}' "$T/b")" |
| 164 | grep -q "since ${S0%%T*})" "$T/b" && grep -q "since ${S2%%T*})" "$T/b" && ok "HTML: each count names its since date" || bad "HTML since dates" |
| 165 | grep -q "Seeding: <b>not measured</b> (the source of this count is retired)" "$T/b" && ok "HTML: seeding not measured (source retired)" || bad "HTML seeds" |
| 166 | grep -q "<link rel=\"alternate\" type=\"application/json\" href=\"/forge/$R.json\">" "$T/b" && ok "repo home links its JSON form" || bad "repo home alternate link" |
| 167 | J=$(grep -c '^e ' "$T/data/counts.log"); check "$J" 9 "journal holds the 9 counted events" |
| 168 | sz=$(stat -c %s "$T/data/counts.log") |
| 169 | counts / > /dev/null; check "$(stat -c %s "$T/data/counts.log")" "$sz" "nothing new in the logs: the journal is not written" |
| 170 | echo "1794049500 127.0.0.1 1 $A 200 1 1" >> "$T/data/logs/forged.log" |
| 171 | counts / > /dev/null; check "$(stat -c %s "$T/data/counts.log")" "$sz" "a log grew by an uncounted line: the journal is not written" |
| 172 | |
| 173 | # ---- 2. growth is read from where it stopped |
| 174 | echo "1794162600.000 203.0.113.20 200 \"/rad/$RID.git$Q\" \"git/2.43.0\"" >> "$T/ng/dl.log" |
| 175 | echo "127.0.0.1 - - [08/Nov/2026:07:00:00 -0600] \"POST /rad/$RID.git/git-upload-pack HTTP/2.0\" 200 65 \"-\" \"git/2.43.0\"" >> "$T/ng/access.log" |
| 176 | echo "1794049600 203.0.113.21 1 $A/archive/refs/tags/v1.tar.gz 200 1000 5" >> "$T/data/logs/forged.log" |
| 177 | check "$(counts /forge/)" "5 null 3 2 1 null" "one more line in each log counts once" |
| 178 | printf '1794162601.000 203.0.113.22 200 "/rad/%s.git%s" "git' "$RID" "$Q" >> "$T/ng/dl.log" |
| 179 | check "$(counts /)" "5 null 3 2 1 null" "a half-written last line is not counted yet" |
| 180 | printf '/2.43.0"\n' >> "$T/ng/dl.log" |
| 181 | check "$(counts /)" "6 null 3 2 1 null" "the line counts once it is complete" |
| 182 | |
| 183 | # ---- 3. restart: journal replay, no double count |
| 184 | stop; start |
| 185 | check "$(counts /)" "6 null 3 2 1 null" "restart replays the journal without counting twice" |
| 186 | |
| 187 | # ---- 4. rotation: rest of <log>.1, then the new <log> |
| 188 | echo "1794162700.000 203.0.113.30 200 \"/rad/$RID.git$Q\" \"git/2.43.0\"" >> "$T/ng/dl.log" |
| 189 | mv "$T/ng/dl.log" "$T/ng/dl.log.1" |
| 190 | echo "1794249000.000 203.0.113.31 200 \"/rad/$RID.git$Q\" \"git/2.43.0\"" > "$T/ng/dl.log" |
| 191 | check "$(counts /)" "8 null 3 2 1 null" "after a rotation: the rest of .1 and the new log count once each" |
| 192 | check "$(jv .counts.git_fetches.since)" "$S0" "since stays the start of the oldest log read" |
| 193 | stop; start |
| 194 | check "$(counts /)" "8 null 3 2 1 null" "restart after the rotation" |
| 195 | # ---- 5. a torn batch at the end of the journal is ignored |
| 196 | printf 'e g 1794162999 %s\ne g 1794163000 %s\n' "$RID" "$RID" >> "$T/data/counts.log" |
| 197 | stop; start |
| 198 | check "$(counts /)" "8 null 3 2 1 null" "a journal batch without its cursor line is ignored" |
| 199 | # An abandoned batch must not be committed by the NEXT real batch's cursor. |
| 200 | echo "1794249001.000 203.0.113.32 200 \"/rad/$RID.git$Q\" \"git/2.43.0\"" >> "$T/ng/dl.log" |
| 201 | check "$(counts /)" "9 null 3 2 1 null" "new event after a torn journal batch" |
| 202 | stop; start |
| 203 | check "$(counts /)" "9 null 3 2 1 null" "later cursor cannot resurrect abandoned journal events" |
| 204 | # Also discard a byte-torn event line before a later append. |
| 205 | printf 'e g 1794163000 z63' >> "$T/data/counts.log" |
| 206 | stop; start |
| 207 | check "$(counts /)" "9 null 3 2 1 null" "partial journal line is ignored" |
| 208 | echo "1794249002.000 203.0.113.33 200 \"/rad/$RID.git$Q\" \"git/2.43.0\"" >> "$T/ng/dl.log" |
| 209 | check "$(counts /)" "10 null 3 2 1 null" "new event after a partial journal line" |
| 210 | stop; start |
| 211 | check "$(counts /)" "10 null 3 2 1 null" "partial journal line cannot swallow the next event" |
| 212 | # ---- 5b. the old logs are gone (rotated away and compressed): the counts stay, from the journal |
| 213 | mv "$T/ng/dl.log.1" "$T/ng/dl.log.2.gone" |
| 214 | stop; start |
| 215 | check "$(counts /)" "10 null 3 2 1 null" "older logs gone: the counts come from the journal" |
| 216 | |
| 217 | # ---- 7. JSON form under both bases |
| 218 | for b in / /forge/; do |
| 219 | for u in explore.json "$R.json"; do |
| 220 | c=$(get "$PORT" "$b" "$u"); check "$c" 200 "GET $b$u" |
| 221 | case "$(ctype)" in "application/json; charset=utf-8") ok "$b$u content type";; *) bad "$b$u content type '$(ctype)'";; esac |
| 222 | $PY -m json.tool "$T/b" > /dev/null && ok "$b$u is valid JSON (python3 -m json.tool)" || bad "$b$u is not valid JSON" |
| 223 | done |
| 224 | get "$PORT" "$b" explore.json > /dev/null |
| 225 | check "$(jv .schema)" "forge/explore/1" "explore schema ($b)" |
| 226 | check "$(jv .base)" "$b" "explore base ($b)" |
| 227 | check "$(jv '.repos | length')" "$(jv .count)" "explore count = number of repos ($b)" |
| 228 | check "$(jv "[.repos[] | select(.name == \"cuda-crack\")][0].path")" "${b}$R" "explore path built from the base ($b)" |
| 229 | check "$(jv "[.repos[] | select(.name == \"cuda-crack\")][0].rid")" "$RID" "explore id is the bare RID ($b)" |
| 230 | get "$PORT" "$b" "$R.json" > /dev/null |
| 231 | check "$(jv .schema)" "forge/repo/1" "repo schema ($b)" |
| 232 | check "$(jv .json)" "${b}$R.json" "repo json link ($b)" |
| 233 | check "$(jv .archives.zip)" "${b}$R/archive/refs/heads/main.zip" "repo zip link ($b)" |
| 234 | check "$(jv .clone.git_https)" "null" "no public Git URL: git_https is null ($b)" |
| 235 | check "$(jv .clone.rad_clone)" "null" "no clone command: rad_clone is null ($b)" |
| 236 | check "$(jv .clone.rad)" "$RID" "clone id is the bare RID ($b)" |
| 237 | check "$(jv .readme.raw)" "${b}$R/raw/main/README.md" "repo readme raw link ($b)" |
| 238 | done |
| 239 | # every public repo: last_commit sha and time equal git; the subject equals git (valid UTF-8) |
| 240 | get "$PORT" / explore.json > /dev/null |
| 241 | n=$(jv '.repos | length'); bad_lc=0 |
| 242 | for k in $(seq 0 $((n - 1))); do |
| 243 | rid=$(jv ".repos[$k].rid"); br=$(jv ".repos[$k].default_branch") |
| 244 | ref=refs/heads/$br |
| 245 | git --git-dir="$STORE/$rid" rev-parse -q --verify "$ref^{commit}" > /dev/null 2>&1 || ref=refs/namespaces/$NSID/refs/heads/$br |
| 246 | want=$(git --git-dir="$STORE/$rid" log -1 --format='%H %ct' "$ref" 2> /dev/null) |
| 247 | wsha=${want% *}; wt=$(iso "${want#* }"); wsub=$(git --git-dir="$STORE/$rid" log -1 --format=%s "$ref" 2> /dev/null) |
| 248 | got="$(jv ".repos[$k].last_commit.sha") $(jv ".repos[$k].last_commit.time")" |
| 249 | gsub=$(jv ".repos[$k].last_commit.subject") |
| 250 | if [ "$got" != "$wsha $wt" ] || [ "$gsub" != "$wsub" ]; then bad_lc=$((bad_lc+1)); echo " mismatch $(jv ".repos[$k].name"): '$got' '$gsub' vs '$wsha $wt' '$wsub'"; fi |
| 251 | done |
| 252 | check "$bad_lc" 0 "last_commit sha, ISO time and subject equal git for all $n public repos" |
| 253 | get "$PORT" / "$R.json" > /dev/null |
| 254 | check "$(jv '[.files[].name] | join("/")')" "$(git --git-dir="$STORE/$RID" ls-tree --name-only refs/heads/main | paste -sd/)" "repo files equal git ls-tree" |
| 255 | check "$(jv '.files_truncated')" "false" "files_truncated false" |
| 256 | # Accept negotiation |
| 257 | c=$(get "$PORT" /forge/ explore -H 'Accept: application/json'); check "$c $(ctype)" "200 application/json; charset=utf-8" "/explore with Accept: application/json" |
| 258 | c=$(get "$PORT" /forge/ "" -H 'Accept: application/json'); check "$(jv .schema)" "forge/explore/1" "/ with Accept: application/json" |
| 259 | c=$(get "$PORT" /forge/ "$R" -H 'Accept: application/json'); check "$(jv .schema)" "forge/repo/1" "repo home with Accept: application/json" |
| 260 | c=$(get "$PORT" /forge/ "$R" -H 'Accept: text/html,application/xhtml+xml,application/json;q=0.9,*/*;q=0.8'); check "$(ctype)" "text/html; charset=utf-8" "browser Accept that names text/html stays HTML" |
| 261 | c=$(get "$PORT" /forge/ "$R"); check "$(ctype)" "text/html; charset=utf-8" "no Accept header stays HTML" |
| 262 | # negative controls |
| 263 | check "$(get "$PORT" / drakestapleton/omega-pearl.json)" 404 "NEG denied repo .json is 404" |
| 264 | check "$(get "$PORT" / drakestapleton/aien-crypto.json)" 404 "NEG denied repo aien-crypto.json is 404" |
| 265 | check "$(get "$PORT" / drakestapleton/no-such-repo.json)" 404 "NEG unknown repo .json is 404" |
| 266 | check "$(get "$PORT" / drakestapleton/omega-pearl -H 'Accept: application/json')" 404 "NEG denied repo with Accept JSON is 404" |
| 267 | check "$(get "$PORT" / "rad:$DENY.json")" 404 "NEG denied RID .json is 404" |
| 268 | check "$(get "$PORT" / "$R.json/x")" 404 "NEG .json with more path is 404" |
| 269 | check "$(get "$PORT" / explore.json -X POST)" 405 "NEG POST /explore.json is 405" |
| 270 | check "$(get "$PORT" / "$R.json" -X POST)" 405 "NEG POST repo .json is 405" |
| 271 | # since card t_6b17c31a every page with a JSON form names its own URL (server/tests/jsonpages.sh walks them all) |
| 272 | get "$PORT" / "$R/tree/main" > /dev/null; grep -q "<link rel=\"alternate\" type=\"application/json\" href=\"/$R/tree/main\">" "$T/b" && ok "tree page names its own URL as the JSON alternate" || bad "tree page lacks its own JSON alternate" |
| 273 | |
| 274 | # ---- 8. HTML unchanged against the BASE build, apart from the known additions |
| 275 | taskset -c "$CORES" nice -n 10 "$T/forged.base" 2>> "$T/forged.base.err" & pidb=$!; up "$PORTB" |
| 276 | # The site icon link (ham/forge-favicon) is the third addition: every page head names <base>assets/favicon.svg. |
| 277 | strip() { sed -z -e 's|<link rel="alternate" type="application/json" href="[^"]*">||' -e 's|<link rel="icon" type="image/svg+xml" href="[^"]*assets/favicon.svg">||' -e 's|\n<p class="meta">Clones and fetches over HTTPS: [^\n]*\n<p class="meta">Downloads: [^\n]*\n<p class="meta">\(Radicle\|Seeding\): [^\n]*\n||' "$1"; } |
| 278 | # The fourth (one site, server/tests/sitechrome.sh): the interplane.io header and footer replace the |
| 279 | # forge's, so only the page content, <main> to </main>, is compared with the base build. |
| 280 | mainpart() { sed -n '/^<main[ >]/,/^<\/main>$/p' | sed '1s/^<main[^>]*>$/<main>/'; } |
| 281 | # The fifth (browse/b_md.om): an HTML comment line renders nothing, where the base build showed it |
| 282 | # escaped as the first or last line of a paragraph (the README language bar); removed from the base side. |
| 283 | nocomment() { sed -z -e 's|<p><!-- [^\n]*-->\n|<p>|g' -e 's|\n<!-- [^\n]*--></p>|</p>|g'; } |
| 284 | # The sixth (ForgeReadmeFirst, server/tests/readmefirst.sh): a new project header and tabs on every repo |
| 285 | # page; the repo home shows the README first and keeps its old lines, unchanged, in a "For developers" |
| 286 | # section; repo list rows carry a state line and keep their records; README links that leave the |
| 287 | # site carry class "ext". pjpre removes the head's new section lines and the three count lines, which |
| 288 | # moved from the header into that section (the base side loses them with its header; their numbers are |
| 289 | # checked by the earlier sections); pjcanon, on both sides, removes the project header and tabs, the |
| 290 | # explore introduction and the forge home box, reduces each repo list row to its records (link, |
| 291 | # description, last commit, stars) and drops the "ext" class; the lines are then compared as sorted |
| 292 | # lists, because the home moved its lines below the README. Every other line must still be equal. |
| 293 | pjpre() { sed -e '/^<section class="pj-dev"/,/^<\/dl>$/d' -e '/^<p class="pj-recs">/,/^<\/section>$/d' -e '/^<p class="pj-nordme">/d' -e '/^<p class="meta">Clones and fetches over HTTPS: /d' -e '/^<p class="meta">Downloads: /d' -e '/^<p class="meta">\(Radicle\|Seeding\): /d' "$1"; } |
| 294 | pjcanon() { |
| 295 | sed -E -e '/^<section class="rhead/,/^<nav class="tabs/d' -e '/^<h1>Explore repositories<\/h1>/d' -e '/^<section class="box"><h1>INTERPLANE Forge<\/h1>/d' \ |
| 296 | -e 's|^<li><span class="stars" title="stars">★ ([0-9]+)</span><a class="rname" href="([^"]*)">[^<]*</a><p class="desc">(.*)</p><p class="meta">Last commit: (.*)</p></li>$|ROW \2 \3 \4 \1|' \ |
| 297 | -e 's|^<li class="pj-row"><a class="rname" href="([^"]*)">[^<]*</a><p class="desc">(.*)</p><p class="state [^"]*">.*</p><p class="meta">Last commit: (.*) · <span class="stars" title="stars">★ ([0-9]+)</span></p></li>$|ROW \1 \2 \3 \4|' \ |
| 298 | -e 's| class="ext" rel="nofollow| rel="nofollow|g' -e 's|(href="mailto:[^"]*") rel="nofollow noopener noreferrer">|\1>|g' | LC_ALL=C sort |
| 299 | } |
| 300 | same=0; diff=0 |
| 301 | for b in / /forge/; do |
| 302 | for u in explore "" drakestapleton "$R" "$R/tree/main" "$R/blob/main/README.md" "$R/commits/main" "$R/branches" "$R/tags" "$R/releases" "$R/patches" "$R/issues" "search?q=forge" "$R/stargazers" "no-such" "drakestapleton/omega-pearl"; do |
| 303 | get "$PORT" "$b" "$u" > /dev/null; pjpre "$T/b" > "$T/b.pj"; strip "$T/b.pj" | mainpart | pjcanon > "$T/new"; cp "$T/b" "$T/new.raw" |
| 304 | get "$PORTB" "$b" "$u" > /dev/null; mainpart < "$T/b" | nocomment | pjcanon > "$T/old" |
| 305 | [ -s "$T/new" ] && [ -s "$T/old" ] || bad "$b$u: no <main> to compare" |
| 306 | if cmp -s "$T/new" "$T/old"; then same=$((same+1)); else diff=$((diff+1)); echo " differs: $b$u"; diff "$T/old" "$T/new" | head -5; fi |
| 307 | case "$u" in explore|"") grep -q '<link rel="alternate" type="application/json" href="' "$T/new.raw" || bad "$b$u lacks the alternate link";; esac |
| 308 | done |
| 309 | done |
| 310 | check "$diff" 0 "HTML content (<main>) equal to the base build on $same pages (explore and repo home after removing the additions)" |
| 311 | stop |
| 312 | echo "counts.sh: $pass passed, $fail failed" |
| 313 | [ "$fail" = 0 ] |