interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / store / tests / compare-controls.sh

155 lines · 8915 bytes Raw Blame History
1#!/usr/bin/env bash
2# Controls for `compare.py radicle` (review-b1 finding 1). Each control builds two tiny fetch folders
3# (MAIN and STORE, the index.tsv + body files that `compare.py fetch` writes) in a temporary folder
4# and runs the comparator on them; nothing is fetched and no server is started.
5# sh store/tests/compare-controls.sh [OLD_REV]
6# The table gives the expected and observed exit of each run; the script exits 0 only when every
7# observed exit is the expected one. OLD_REV (default babf1706) is the commit whose compare.py is run on
8# the controls marked "old": they show what the earlier per-line presence test accepted.
9set -u
10D=$(cd "$(dirname "$0")" && pwd)
11OLD_REV=${1:-babf1706080e75dff2f8dd412e0d62ecbc538d44}
12T=$(mktemp -d)
13trap 'rm -rf "$T"' EXIT
14git -C "$D" show "$OLD_REV:store/tests/compare.py" > "$T/compare-old.py" || { echo "cannot read compare.py at $OLD_REV"; exit 2; }
15
16python3 - "$T" <<'EOF'
17import hashlib, os, sys
18T = sys.argv[1]
19O = "drakestapleton"
20RID, RID2 = "z3uek4Z9YSEAtFXmedDzEG4xvD4XR", "z44GqrPVAPjZkhoD2MWVjqy18ighN"
21GID, GID2, GID3 = "ab" * 32, "cd" * 32, "ef" * 32
22KEY = "SHA256:VY3HfHMNlI1gw6laxupXOx2vj2zSOOjYcQS1sC3S6t0"
23NAV = ('<div class="forge-row"><nav class="forge-nav" aria-label="Forge"><a href="/explore">Explore</a>'
24 '<a href="/open/">Open builds</a><a href="/research">Research</a></nav></div>')
25FOOT_A = ('<p class="foot-fine">INTERPLANE Forge: agents express intent; the forge retains authority. Built on Radicle'
26 ' (Heartwood): repositories are Radicle projects served by forged, a seeded-Omega web server, with no'
27 ' JavaScript.</p>')
28FOOT_B = ('<p class="foot-fine">INTERPLANE Forge: agents express intent; the forge retains authority. Repositories are'
29 " plain Git kept in Interplane's own signed store, served by forged, a seeded-Omega web server, with no"
30 ' JavaScript.</p>')
31C = '<code translate="no">'
32DEV_A = [
33 "<dt>RID</dt><dd>The Radicle ID: the project's permanent name on Radicle, the open network that stores and signs"
34 " it. This one is " + C + "rad:" + RID + "</code>.</dd>",
35 "<dt>Delegate</dt><dd>A key that may sign the project's official versions. This forge lists the project under "
36 + O + "; " + C + "rad inspect rad:" + RID + " --delegates</code> prints the project's own list.</dd>",
37 "<dt>Seed</dt><dd>A Radicle node that keeps a copy of the project and serves it to others. The count below is"
38 " read from this node's routing table.</dd>"]
39DEV_B = [
40 "<dt>ID</dt><dd>The project's permanent id on Interplane: the SHA-256 of its first signed record. This one is "
41 + C + 'git:<span translate="no">' + GID + "</span></code>. Its old Radicle ID, kept only so that old links still"
42 " work: " + C + "rad:" + RID + "</code>.</dd>",
43 "<dt>Owner</dt><dd>The key that signs every record of the project. This forge lists the project under " + O +
44 "; its key is " + C + KEY + "</code>.</dd>"]
45ARCH = ('<dt>Source archive of main</dt><dd><a href="/%s/hop/archive/refs/heads/main.zip">zip</a></dd></dl></div>' % O)
46PANEL_A = ('<div class="clone"><h3>Get this repository</h3><dl><dt>Radicle</dt><dd>' + C + "rad clone rad:" + RID +
47 "</code></dd><dt>Git over HTTPS</dt><dd>" + C + "git clone https://www.aienos.com/rad/" + RID +
48 ".git</code></dd>" + ARCH)
49PANEL_B = ('<div class="clone"><h3>Get this repository</h3><dl><dt>Git over HTTPS</dt><dd>' + C +
50 "git clone https://interplane.io/projects/hop/repo.git</code></dd>" + ARCH)
51CONTRIB = ('<p class="meta">One command for agents: ' + C + "forge-contribute " + RID +
52 " DIR BRANCH</code> opens a Radicle patch.</p>")
53
54
55def item(rid, tags="[]", name="hop"):
56 return ('{"owner":"%s","name":"%s","rid":"%s","path":"/%s/%s","tags":%s,"default_branch":"main"}'
57 % (O, name, rid, O, name, tags))
58
59
60def repo_json(rid, store):
61 clone = ('"clone":{"rad":null,"rad_clone":null,"git_https":"https://interplane.io/projects/hop/repo.git"}'
62 if store else '"clone":{"rad":"rad:%s","rad_clone":"rad clone rad:%s","git_https":'
63 '"https://www.aienos.com/rad/%s.git"}' % (RID, RID, RID))
64 return "{\n" + item(rid)[1:-1] + ",\n" + clone + "\n}\n"
65
66
67def html(*lines):
68 return "\n".join(["<!doctype html>", "<html><body>", *lines, "</body></html>", ""])
69
70
71def pages(store):
72 rid = "git:" + GID if store else "rad:" + RID
73 rid2 = "git:" + GID2 if store else "rad:" + RID2
74 return { # (path, kind): (status, content type, body)
75 ("/", "html"): (200, "text/html; charset=utf-8",
76 html(NAV.replace('<a href="/open/">Open builds</a>', "") if store else NAV, "<p>Home</p>",
77 FOOT_B if store else FOOT_A)),
78 ("/", "json"): (200, "application/json", "[\n" + item(rid) + ",\n" + item(rid2, name="core") + "\n]\n"),
79 ("/%s/hop" % O, "html"): (200, "text/html; charset=utf-8",
80 html(*(DEV_B if store else DEV_A), PANEL_B if store else PANEL_A, "<p>same</p>")),
81 ("/%s/hop" % O, "json"): (200, "application/json", repo_json(rid, store)),
82 ("/%s/hop/tags/1" % O, "json"): (200, "application/json",
83 '{"schema":"forge/tags/1","repo":' + item(rid) + ',"count":0}\n'),
84 ("/%s/hop/archive/refs/heads/main.zip" % O, "html"): (200, "application/zip", "PK\x03\x04 same bytes"),
85 ("/%s/hop/raw/main/README.md" % O, "html"): (200, "text/plain; charset=utf-8", "readme\n"),
86 }
87
88
89def mutate(control, p):
90 k = ("/%s/hop/tags/1" % O, "json")
91 if control == "b": # a non-id JSON field changed, on a one-line body that also holds an id
92 st, ct, body = p[k]
93 p[k] = (st, ct, body.replace('"tags":[]', '"tags":["v1","v2","v3"]'))
94 elif control == "c": # status differs
95 p[k] = (404, p[k][1], p[k][2])
96 elif control == "d": # a binary page differs
97 z = ("/%s/hop/archive/refs/heads/main.zip" % O, "html")
98 p[z] = (p[z][0], p[z][1], "PK\x03\x04 other bytes")
99 elif control == "e": # footer replaced by other text that carries a store id
100 h = ("/", "html")
101 p[h] = (p[h][0], p[h][1], p[h][2].replace(FOOT_B, "<p>Nothing to see; git:" + GID3 + "</p>"))
102 elif control == "f": # one Radicle id given two different store ids on two pages
103 j = ("/", "json")
104 p[j] = (p[j][0], p[j][1], p[j][2].replace("git:" + GID, "git:" + GID3, 1))
105 elif control == "g": # the clone panel names another project's repository
106 h = ("/%s/hop" % O, "html")
107 p[h] = (p[h][0], p[h][1], p[h][2].replace("projects/hop/repo.git", "projects/other/repo.git"))
108 elif control == "h": # a MAIN-only Radicle line with no STORE counterpart (the kept run's agent line)
109 return "main-extra"
110 return None
111
112
113def write(d, p):
114 os.makedirs(d)
115 idx = []
116 for (path, kind), (st, ct, body) in p.items():
117 b = body.encode("utf-8")
118 name = hashlib.sha256((kind + " " + path).encode()).hexdigest()[:32]
119 open(os.path.join(d, name), "wb").write(b)
120 idx.append("\t".join([name, kind, path, str(st), ct, "", str(len(b)), hashlib.sha256(b).hexdigest()]))
121 open(os.path.join(d, "index.tsv"), "w", encoding="utf-8").write("\n".join(idx) + "\n")
122
123
124for control in "abcdefgh":
125 main, store = pages(False), pages(True)
126 if mutate(control, store) == "main-extra":
127 h = ("/%s/hop" % O, "html")
128 main[h] = (main[h][0], main[h][1], main[h][2].replace("<p>same</p>", CONTRIB + "\n<p>same</p>"))
129 write(os.path.join(T, control, "main"), main)
130 write(os.path.join(T, control, "store"), store)
131EOF
132[ -d "$T/h/store" ] || { echo "fixtures not written"; exit 2; }
133
134fail=0
135printf '%-8s %-5s %-70s %-8s %s\n' control code what expected observed
136run() { # run CONTROL CODE EXPECTED WHAT
137 if [ "$2" = old ]; then py="$T/compare-old.py"; else py="$D/compare.py"; fi
138 python3 "$py" radicle "$T/$1/main" "$T/$1/store" > "$T/$1.$2.out" 2>&1
139 got=$?
140 [ "$got" = "$3" ] || fail=1
141 printf '%-8s %-5s %-70s %-8s %s%s\n' "$1" "$2" "$4" "$3" "$got" "$([ "$got" = "$3" ] || echo ' MISMATCH')"
142 { grep -m3 '^BAD' "$T/$1.$2.out"; tail -1 "$T/$1.$2.out"; } | cut -c1-200 | sed 's/^/ /'
143}
144run a new 0 "identical apart from id forms and the listed rules"
145run b new 1 'tags [] vs three tags in a one-line JSON body with an id'
146run b old 0 "same, earlier presence test (the defect: it passes)"
147run c new 1 "status 200 vs 404"
148run d new 1 "binary (application/zip) page bytes differ"
149run e new 1 "footer replaced by other text carrying git:<hex>"
150run e old 0 "same, earlier presence test"
151run f new 1 "one Radicle id mapped to two store ids"
152run g new 1 "clone panel names another project's repository"
153run h new 1 "MAIN-only Radicle agent line, no STORE counterpart"
154[ "$fail" = 0 ] && echo "controls: all exits as expected" || echo "controls: MISMATCH"
155exit "$fail"