interplane.io
Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.
Brought by drakestapleton
interplane.io / scripts / tests / check-claims.sh
| 1 | #!/bin/sh |
| 2 | # Cross-checks docs/CLAIMS.md against built pages. Shell and awk only. |
| 3 | # |
| 4 | # sh scripts/tests/check-claims.sh OUT [--hop FILE OUT...] compare; exit 0 when the table and the pages agree |
| 5 | # sh scripts/tests/check-claims.sh --print OUT [--hop FILE OUT...] print the rows the pages give, for docs/CLAIMS.md |
| 6 | # |
| 7 | # --hop FILE names the Hop release file (content/hop.release.txt, or the fixture) that the output |
| 8 | # directories after it were built with. A Hop sentence that prints one of its values (version, |
| 9 | # commit, rid, macos) is compared with that value written as {version}, {commit}, {rid} or {macos}, |
| 10 | # so one table is right for the fixtures and for a real release. |
| 11 | # |
| 12 | # OUT is a built site directory. One without llms.txt is read as hopbot.org. Pass several |
| 13 | # directories to cover the release build and the test-data build. |
| 14 | # |
| 15 | # Two things are compared, both ways: |
| 16 | # 1. Labelled sentences. Every block that carries a register label (class "reg rule", "reg check" or |
| 17 | # "reg build") must be a row of the claims table with the same page, register, words and links, |
| 18 | # and every row must be on a page. A "check" sentence without a link fails. |
| 19 | # 2. Hop sentences. Every block on a Hop page that carries class "hf" (a statement about the Hop |
| 20 | # app) must be a row of the Hop table, which records the source file and line behind it. |
| 21 | # 3. Universal words (every, everything, everyone, everybody, nobody, never, always, all, none) in |
| 22 | # the visible text of a page. Each use must be a row of the allowed-words table, with its count. |
| 23 | # What this cannot do: decide whether an unlabelled sentence states a fact. A person reads for that. |
| 24 | set -eu |
| 25 | ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) |
| 26 | PRINT=0 |
| 27 | if [ "${1:-}" = --print ]; then PRINT=1; shift; fi |
| 28 | [ "$#" -ge 1 ] || { echo 'usage: check-claims.sh [--print] OUT [--hop FILE OUT...]' >&2; exit 2; } |
| 29 | HOPF= |
| 30 | NEXT=0 |
| 31 | hopval() { [ -z "$HOPF" ] || sed -n "s/^$1 //p" "$HOPF" | head -n 1; } |
| 32 | T=$(mktemp -d) |
| 33 | trap 'rm -rf "$T"' 0 |
| 34 | trap 'exit 1' 1 2 15 |
| 35 | : > "$T/claims"; : > "$T/words"; : > "$T/nolink"; : > "$T/hop" |
| 36 | for OUT in "$@"; do |
| 37 | if [ "$NEXT" -eq 1 ]; then |
| 38 | NEXT=0; HOPF=$OUT |
| 39 | [ -f "$HOPF" ] || { echo "not a Hop release file: $HOPF" >&2; exit 2; } |
| 40 | continue |
| 41 | fi |
| 42 | if [ "$OUT" = --hop ]; then NEXT=1; continue; fi |
| 43 | [ -f "$OUT/manifest.sha256" ] || { echo "not a built site directory: $OUT" >&2; exit 2; } |
| 44 | PRE= |
| 45 | [ -f "$OUT/llms.txt" ] || PRE=hopbot.org |
| 46 | find "$OUT" -name index.html | sort | while IFS= read -r f; do |
| 47 | r=${f#"$OUT"}; r=${r%index.html} |
| 48 | awk -v route="$PRE$r" -v claims="$T/claims" -v words="$T/words" -v nolink="$T/nolink" -v hop="$T/hop" \ |
| 49 | -v eshape="$T/eshape" -v zshape="$T/zshape" \ |
| 50 | -v hcommit="$(hopval commit)" -v hrid="$(hopval rid)" -v hmacos="$(hopval macos)" -v hversion="$(hopval version)" ' |
| 51 | function swap(s, val, tok, i, out) { |
| 52 | if (val == "") return s |
| 53 | out = "" |
| 54 | while ((i = index(s, val)) > 0) { out = out substr(s, 1, i - 1) tok; s = substr(s, i + length(val)) } |
| 55 | return out s |
| 56 | } |
| 57 | function clean(s, out) { |
| 58 | out = "" |
| 59 | while (match(s, /<span translate="no">[^<]*<\/span>/)) { out = out substr(s, 1, RSTART - 1) substr(s, RSTART + 21, RLENGTH - 28); s = substr(s, RSTART + RLENGTH) } |
| 60 | s = out s |
| 61 | gsub(/ translate="no"/, "", s); gsub(/<[^>]*>/, " ", s); gsub(/[ \t\r\n]+/, " ", s); sub(/^ /, "", s); sub(/ $/, "", s); return s |
| 62 | } |
| 63 | { doc = doc $0 "\n" } |
| 64 | END { |
| 65 | nhf = 0 |
| 66 | rest = doc |
| 67 | while ((i = index(rest, "<span class=\"reg ")) > 0) { |
| 68 | rest = substr(rest, i + 17) |
| 69 | reg = substr(rest, 1, index(rest, "\"") - 1) |
| 70 | rest = substr(rest, index(rest, "</span>") + 7) |
| 71 | e = length(rest) + 1 |
| 72 | n = split("</p> </h3> </li>", stop, " ") |
| 73 | for (k = 1; k <= n; k++) { j = index(rest, stop[k]); if (j > 0 && j < e) e = j } |
| 74 | block = substr(rest, 1, e - 1) |
| 75 | links = ""; b = block |
| 76 | while ((h = index(b, "href=\"")) > 0) { b = substr(b, h + 6); u = substr(b, 1, index(b, "\"") - 1); links = links (links == "" ? "" : " ") u } |
| 77 | if (reg == "rule") ev = "Policy. This site cannot prove it." |
| 78 | else if (reg == "build") ev = "Not live." |
| 79 | else { ev = links; if (links == "") print route "\t" clean(block) >> nolink } |
| 80 | print route "\t" reg "\t" clean(block) "\t" ev >> claims |
| 81 | } |
| 82 | rest = doc |
| 83 | while ((i = index(rest, "hf\">")) > 0) { |
| 84 | rest = substr(rest, i + 4) |
| 85 | e = length(rest) + 1 |
| 86 | n = split("</p> </h3> </li> </h1>", stop, " ") |
| 87 | for (k = 1; k <= n; k++) { j = index(rest, stop[k]); if (j > 0 && j < e) e = j } |
| 88 | h = clean(substr(rest, 1, e - 1)) |
| 89 | h = swap(h, hcommit, "{commit}"); h = swap(h, hrid, "{rid}"); h = swap(h, hmacos, "{macos}"); h = swap(h, hversion, "{version}") |
| 90 | print h >> hop |
| 91 | nhf++ |
| 92 | } |
| 93 | body = doc |
| 94 | b0 = index(body, "<body"); if (b0 > 0) body = substr(body, b0) |
| 95 | text = tolower(clean(body)) |
| 96 | gsub(/[^a-z]+/, " ", text) |
| 97 | n = split(text, w, " ") |
| 98 | for (k = 1; k <= n; k++) if (w[k] ~ /^(every|everything|everyone|everybody|nobody|never|always|all|none)$/) c[w[k]]++ |
| 99 | for (x in c) print route "\t" x "\t" c[x] >> words |
| 100 | }' "$f" |
| 101 | done |
| 102 | done |
| 103 | sort -u "$T/claims" > "$T/claims.got" |
| 104 | sort -u "$T/words" > "$T/words.got" |
| 105 | sort -u "$T/hop" > "$T/hop.got" |
| 106 | if [ "$PRINT" -eq 1 ]; then |
| 107 | awk -F'\t' '{ print "| claim | " $1 " | " $2 " | " $3 " | " $4 " |" }' "$T/claims.got" |
| 108 | awk -F'\t' '{ print "| word | " $1 " | " $2 " | " $3 " | REASON |" }' "$T/words.got" |
| 109 | awk '{ print "| hop | " $0 " | SOURCE |" }' "$T/hop.got" |
| 110 | exit 0 |
| 111 | fi |
| 112 | DOC=$ROOT/docs/CLAIMS.md |
| 113 | [ -f "$DOC" ] || { echo 'docs/CLAIMS.md is missing' >&2; exit 1; } |
| 114 | # Rows must belong to the Markdown table whose header describes them. Comparing |
| 115 | # row contents alone accepts a moved block or a blank line that breaks the table. |
| 116 | awk ' |
| 117 | function bad(why) { print "FAIL claims table layout at line " NR ": " why; failed=1 } |
| 118 | { |
| 119 | compact=$0; gsub(/[ \t\r]/, "", compact) |
| 120 | kind="" |
| 121 | if (compact == "|kind|page|register|sentence|evidence|") kind="claim" |
| 122 | if (compact == "|kind|page|word|count|whyitisallowed|") kind="word" |
| 123 | if (compact == "|kind|sentence|sourceatthatcommit|") kind="hop" |
| 124 | if (kind != "") { |
| 125 | if (pending != "") bad("missing delimiter after header") |
| 126 | if (++headers[kind] != 1) bad("duplicate " kind " header") |
| 127 | pending=kind; active=""; next |
| 128 | } |
| 129 | if (pending != "") { |
| 130 | delimiter=compact; gsub(/:/, "", delimiter) |
| 131 | want=pending == "hop" ? "|---|---|---|" : "|---|---|---|---|---|" |
| 132 | if (delimiter != want) bad("header must be followed immediately by its delimiter") |
| 133 | active=pending; pending=""; next |
| 134 | } |
| 135 | if ($0 ~ /^[|][ \t]*(claim|word|hop)[ \t]*[|]/) { |
| 136 | split($0, cells, /[|]/); row=cells[2]; gsub(/[ \t]/, "", row) |
| 137 | if (active != row) bad(row " row is outside its matching contiguous table") |
| 138 | rows[row]++; next |
| 139 | } |
| 140 | active="" |
| 141 | } |
| 142 | END { |
| 143 | if (pending != "") bad("missing delimiter after final header") |
| 144 | split("claim word hop", kinds, " ") |
| 145 | for (i=1; i<=3; i++) if (headers[kinds[i]] != 1 || rows[kinds[i]] == 0) bad("missing or empty " kinds[i] " table") |
| 146 | exit failed ? 1 : 0 |
| 147 | } |
| 148 | ' "$DOC" >&2 || exit 1 |
| 149 | awk -F' [|] ' '/^[|] claim [|] / { e = $5; sub(/ [|]$/, "", e); print $2 "\t" $3 "\t" $4 "\t" e }' "$DOC" | sort -u > "$T/claims.want" |
| 150 | awk -F' [|] ' '/^[|] word [|] / { print $2 "\t" $3 "\t" $4 }' "$DOC" | sort -u > "$T/words.want" |
| 151 | awk -F' [|] ' '/^[|] hop [|] / { print $2 }' "$DOC" | sort -u > "$T/hop.want" |
| 152 | BAD=0 |
| 153 | if [ -n "$(awk -F' [|] ' '/^[|] hop [|] / { e = $3; sub(/ [|]$/, "", e); if (e !~ /:[0-9]/) print $2 }' "$DOC")" ]; then |
| 154 | echo 'FAIL a Hop row in docs/CLAIMS.md names no source file and line' >&2; BAD=1 |
| 155 | fi |
| 156 | if ! cmp -s "$T/hop.got" "$T/hop.want"; then |
| 157 | echo 'FAIL Hop sentences on the pages and the Hop table in docs/CLAIMS.md differ (< pages, > table):' >&2 |
| 158 | diff "$T/hop.got" "$T/hop.want" >&2 || true |
| 159 | BAD=1 |
| 160 | fi |
| 161 | if [ -s "$T/nolink" ]; then |
| 162 | echo 'FAIL a "what you can check today" sentence has no link:' >&2; cat "$T/nolink" >&2; BAD=1 |
| 163 | fi |
| 164 | if ! cmp -s "$T/claims.got" "$T/claims.want"; then |
| 165 | echo 'FAIL labelled sentences on the pages and the claims table in docs/CLAIMS.md differ (< pages, > table):' >&2 |
| 166 | diff "$T/claims.got" "$T/claims.want" >&2 || true |
| 167 | BAD=1 |
| 168 | fi |
| 169 | if ! cmp -s "$T/words.got" "$T/words.want"; then |
| 170 | echo 'FAIL universal words on the pages and the allowed-words table in docs/CLAIMS.md differ (< pages, > table):' >&2 |
| 171 | diff "$T/words.got" "$T/words.want" >&2 || true |
| 172 | BAD=1 |
| 173 | fi |
| 174 | [ "$BAD" -eq 0 ] || exit 1 |
| 175 | printf 'claims: %s labelled sentences, %s Hop sentences and %s allowed universal-word rows agree with docs/CLAIMS.md\n' "$(wc -l < "$T/claims.got" | tr -d ' ')" "$(wc -l < "$T/hop.got" | tr -d ' ')" "$(wc -l < "$T/words.got" | tr -d ' ')" |