interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / collab / tests / native-issues-store.sh

224 lines · 12416 bytes Raw Blame History
1#!/usr/bin/env bash
2# Loopback check for the imported issue views in the signed-store build.
3. patch/tests/lib.sh
4set -e
5trap 'printf "FAIL issue test at line %s, fixture %s\n" "$LINENO" "$T" >&2' ERR
6trap 'set +e; stop; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT
7store="$T/data/issues/$OWNER/fixrepo"
8for row in "0123456789abcdef open 3" "01234fedcba98765 closed 4"; do
9 read -r id state number <<< "$row"
10 mkdir -p "$store/$id"
11 printf '%s\n' "$id" >> "$store/index"
12 printf 'Fixture issue %s' "$number" > "$store/$id/title"
13 printf 'Body with **Markdown** and a [relative link](README.md)\n' > "$store/$id/description"
14 printf '%s\n' "$state" > "$store/$id/status"
15 printf 'number %s\nlogin octo\nopened 2026-01-01T00:00:00Z\nclosed -\nreason -\n' "$number" > "$store/$id/gh"
16 printf 'bug\n' > "$store/$id/labels"
17 : > "$store/$id/thread"
18 printf '{"kind":"github-import","owner":"fixo","repo":"fixrepo","number":%s,"url":"https://github.com/fixo/fixrepo/issues/%s"}\n' "$number" "$number" > "$store/$id/source.json"
19done
20start
21test "$(code GET "$R/issues")" = 200
22grep -q 'Fixture issue 3' "$T/body"
23! grep -q 'Fixture issue 4' "$T/body"
24echo 'PASS default issue list shows only open items'
25test "$(code GET "$R/issues?state=all" -H 'Accept: application/json')" = 200
26jq -e '.schema == "forge/issues/1" and (.issues | length == 2)' "$T/body" >/dev/null
27echo 'PASS JSON issue list returns both states'
28test "$(code GET "$R/issues/012345" -H 'Accept: application/json')" = 200
29jq -e '.schema == "forge/issue/1" and .title == "Fixture issue 3" and .author == null and .imported.login == "octo" and .labels == ["bug"]' "$T/body" >/dev/null
30echo 'PASS JSON detail resolves a unique prefix and preserves source attribution and labels'
31test "$(code GET "$R/issues/01234fedcba98765")" = 200
32grep -q 'Imported from GitHub' "$T/body"
33grep -q '<strong>Markdown</strong>' "$T/body"
34echo 'PASS HTML issue detail renders provenance and Markdown'
35test "$(code GET "$R/issues/01234")" = 404
36test "$(code GET "$R/issues/ffffffff")" = 404
37test "$(code POST "$R/issues/0123456789abcdef/compose")" = 404
38test "$(code GET "/$OWNER/unknown/issues")" = 404
39echo 'PASS ambiguous or missing issues, the retired preview endpoint, and unknown repositories are refused'
40test "$(code GET "$R/patches" -H 'Accept: application/json')" = 200
41jq -e '.schema == "forge/patch-list/1"' "$T/body" >/dev/null
42echo 'PASS patch and issue routes coexist in the same build'
43
44fixture="$store/0123456789abcdef"
45printf 'comment 2026-01-01T00:00:00Z octo 4\nNote\ncomment 2026-01-01T00:00:00Z octo 9999999999999999999\n' > "$fixture/thread"
46sed -i 's/^number .*/number invalid/' "$fixture/gh"
47test "$(code GET "$R/issues/0123456789abcdef" -H 'Accept: application/json')" = 200
48jq -e '.imported.number == 0 and (.comments | length == 2) and .comments[1].body == "Note"' "$T/body" >/dev/null
49echo 'PASS invalid import numbers and overlong thread lengths preserve valid records and valid JSON'
50printf 'opened 2026-01-01T<test>00Z\n' > "$fixture/gh"
51test "$(code GET "$R/issues/0123456789abcdef")" = 200
52! grep -Fq '<test>' "$T/body"
53grep -Fq "&lt;test" "$T/body"
54echo 'PASS imported time text is escaped in the issue page'
55
56test "$(code GET "$R/issues/new")" = 200
57grep -q 'Sign in to open an issue' "$T/body"
58test "$(code POST "$R/issues/new" --data 'title=Unsigned')" = 303
59test "$(hdr location)" = /login
60test "$(code POST "$R/issues/0123456789abcdef/comments" --data 'body=Unsigned')" = 303
61test "$(wc -l < "$store/index")" = 2
62echo 'PASS sign-in controls protect both write routes'
63
64newkey writer
65signin writer 10.4.20.1
66writer=$DID
67issue_cookie="Cookie: __Host-forge_session=$SES"
68test "$(code GET "$R/issues/new" -H "$issue_cookie")" = 200
69test "$(field csrf)" = "$TOK"
70request=$(field request)
71test "${#request}" = 32
72test "$(code POST "$R/issues/new" -H "$issue_cookie" --data-urlencode "request=$request" --data-urlencode 'title=Missing CSRF')" = 403
73test "$(code POST "$R/issues/new" -H "$issue_cookie" -H 'Origin: https://elsewhere.example' --data-urlencode "csrf=$TOK" --data-urlencode "request=$request" --data-urlencode 'title=Foreign origin')" = 403
74test "$(code POST "$R/issues/new" -H "$issue_cookie" --data-urlencode "csrf=$TOK" --data-urlencode "request=$request" --data-urlencode $'title=first\nsecond')" = 400
75test "$(code POST "$R/issues/new" -H "$issue_cookie" --data-urlencode "csrf=$TOK" --data-urlencode "request=$request" --data-urlencode 'title=Oversized' --data-urlencode "body=$(head -c 16001 /dev/zero | tr '\0' a)")" = 400
76test "$(wc -l < "$store/index")" = 2
77echo 'PASS missing CSRF, foreign origin, multiline title and oversized body refuse writes'
78
79openissue() {
80 code POST "$R/issues/new" -H "$issue_cookie" --data-urlencode "csrf=$TOK" \
81 --data-urlencode "request=$request" --data-urlencode "title=$1" --data-urlencode $'body=This is **native**.\n\n<script>alert(1)</script>\n\n[Unsafe](javascript:alert(1))'
82}
83test "$(openissue 'Native issue <proof>')" = 303
84issue=$(hdr location)
85id=${issue##*/}
86test "${#id}" = 16
87test "$(openissue 'Native issue <proof>')" = 303
88test "$(hdr location)" = "$issue"
89test "$(openissue 'Changed title')" = 409
90test "$(wc -l < "$store/index")" = 3
91test "$(code GET "$issue" -H "$issue_cookie")" = 200
92grep -q 'Native issue &lt;proof&gt;' "$T/body"
93grep -q '<strong>native</strong>' "$T/body"
94if grep -qE '<script>|href="javascript:' "$T/body"; then false; fi
95grep -q 'Post comment' "$T/body"
96test "$(hdr x-accel-expires)" = 0
97test "$(code GET "$R/issues" -H 'Accept: application/json')" = 200
98jq -e --arg did "$writer" --arg id "$id" --arg new "$R/issues/new" '
99 .new == $new and any(.issues[]; .id == $id and .author.did == $did and .imported == null)
100' "$T/body" >/dev/null
101echo 'PASS issue creation, stable retries, conflicting retry, HTML and native list attribution'
102
103# Simulate the crash window after the issue directory is published but before its index is replaced.
104sed -i "/^$id$/d" "$store/index"
105test "$(code GET "$issue")" = 404
106test "$(openissue 'Native issue <proof>')" = 303
107test "$(hdr location)" = "$issue"
108test "$(grep -c "^$id$" "$store/index")" = 1
109flock -x "$store/.write.lock" -c 'echo held; read -r _' < <(sleep 2) > "$T/lock" &
110holder=$!
111for _ in $(seq 100); do [ -s "$T/lock" ] && break; sleep .01; done
112test "$(cat "$T/lock")" = held
113test "$(openissue 'Native issue <proof>')" = 503
114test "$(hdr retry-after)" = 1
115wait "$holder" || :
116test "$(openissue 'Native issue <proof>')" = 303
117echo 'PASS an interrupted creation is recovered and a competing writer can retry when the lock clears'
118
119newkey commenter
120signin commenter 10.4.20.2
121commenter=$DID
122issue_cookie="Cookie: __Host-forge_session=$SES"
123test "$(code GET "$issue" -H "$issue_cookie")" = 200
124request=$(field request)
125test "${#request}" = 32
126comment() {
127 code POST "$1/comments" -H "$issue_cookie" --data-urlencode "csrf=$TOK" \
128 --data-urlencode "request=$request" --data-urlencode "body=$2" --data-urlencode 'author=forged'
129}
130test "$(comment "$issue" ' ')" = 400
131test "$(comment "$R/issues/${id:0:8}" 'Prefix')" = 404
132test "$(comment "$R/issues/ffffffffffffffff" 'Missing')" = 404
133test "$(comment "$issue" 'Posted **directly** via Interplane.')" = 303
134test "$(hdr location)" = "$issue"
135test "$(comment "$issue" 'Posted **directly** via Interplane.')" = 303
136test "$(comment "$issue" 'Conflicting retry')" = 409
137test "$(code GET "$issue" -H 'Accept: application/json')" = 200
138jq -e --arg writer "$writer" --arg commenter "$commenter" '
139 .author.did == $writer and .imported == null and (.comments | length == 2)
140 and .comments[0].author.did == $writer and .comments[1].author.did == $commenter
141 and .comments[1].body == "Posted **directly** via Interplane."
142' "$T/body" >/dev/null
143echo 'PASS comment writes preserve both authors and deduplicate retries'
144
145old="$R/issues/0123456789abcdef"
146printf 'number 3\nlogin octo\nopened 2026-01-01T00:00:00Z\nclosed -\nreason -\n' > "$fixture/gh"
147test "$(comment "$old" 'This must not disappear after a corrupt record.')" = 409
148printf 'comment 2026-01-01T00:00:00Z octo 4\nNote\n' > "$fixture/thread"
149test "$(comment "$old" 'Native reply to an imported issue.')" = 303
150test "$(code GET "$old" -H 'Accept: application/json')" = 200
151jq -e --arg did "$commenter" '
152 .imported.login == "octo" and .author == null and .comments[1].author == null
153 and .comments[1].login == "octo" and .comments[2].author.did == $did
154' "$T/body" >/dev/null
155echo 'PASS imported authorship survives native comments and corrupt threads refuse edits'
156
157mkdir "$store/$id/.thread.tmp"
158request=0123456789abcdef0123456789abcdef
159test "$(comment "$issue" 'Should not publish.')" = 500
160rmdir "$store/$id/.thread.tmp"
161test "$(code GET "$issue" -H 'Accept: application/json')" = 200
162jq -e '(.comments | length) == 2' "$T/body" >/dev/null
163test "$(comment "$issue" 'Successful retry after I/O error.')" = 303
164stop || :
165start
166test "$(code GET "$issue" -H 'Accept: application/json')" = 200
167jq -e '.comments[2].body == "Successful retry after I/O error."' "$T/body" >/dev/null
168echo 'PASS failed replacement leaves the thread intact and a successful retry survives restart'
169
170# A full first page must keep the newest item visible and preserve a path to older imports.
171for u in $(seq 1 30); do
172 other=$(printf '12345678%08x' "$u")
173 mkdir "$store/$other"
174 printf 'open\n' > "$store/$other/status"
175 printf '%s\n' "$other" >> "$store/index"
176done
177test "$(code GET "$R/issues?state=all&page=1" -H 'Accept: application/json')" = 200
178jq -e '.page == 1 and (.issues | length == 30) and .next != null and .prev == null' "$T/body" >/dev/null
179test "$(code GET "$R/issues?state=all&page=2" -H 'Accept: application/json')" = 200
180jq -e --arg id "$id" '.page == 2 and .prev != null and .next == null and any(.issues[]; .id == $id)' "$T/body" >/dev/null
181echo 'PASS issue list pagination bounds thread reads and older records stay reachable'
182
183test "$(code GET "$R/issues?state=all" -H 'Accept: application/json')" = 200
184jq -e '.page == null and (.issues | length == 33) and .issues[0].id == "0123456789abcdef"' "$T/body" >/dev/null
185test "$(curl -sI -o /dev/null -w '%{http_code}' "$B$issue")" = 200
186echo 'PASS unpaged JSON retains prior index order and HEAD still serves the issue'
187
188json=(-H 'Accept: application/json')
189test "$(code GET "$R/issues/new" "${json[@]}")" = 303
190test "$(code GET "$issue/comments" "${json[@]}")" = 303
191newkey agent
192signin agent 10.4.20.3
193issue_cookie="Cookie: __Host-forge_session=$SES"
194test "$(code GET "$R/issues/new" "${json[@]}" -H "$issue_cookie")" = 200
195jq -e --arg route "$R/issues/new" '.schema == "forge/issue-form/1" and .action == $route' "$T/body" >/dev/null
196test "$(hdr x-accel-expires)" = 0
197request=$(jq -r .request "$T/body")
198test "$(jq -r .csrf "$T/body")" = "$TOK"
199agentopen() {
200 code POST "$R/issues/new" "${json[@]}" -H "$issue_cookie" -H "X-Real-IP: $IP" --data-urlencode "csrf=$TOK" \
201 --data-urlencode "request=$request" --data-urlencode 'title=Issue from the JSON form'
202}
203test "$(agentopen)" = 201
204issue=$(jq -er 'select(.schema == "forge/issue-write/1" and .applied == true).path' "$T/body")
205test "$(agentopen)" = 200
206jq -e --arg path "$issue" '.path == $path and .applied == false' "$T/body" >/dev/null
207test "$(code GET "$issue/comments" "${json[@]}" -H "$issue_cookie")" = 200
208jq -e --arg route "$issue/comments" '.schema == "forge/issue-form/1" and .action == $route' "$T/body" >/dev/null
209request=$(jq -r .request "$T/body")
210agentcomment() {
211 code POST "$issue/comments" "${json[@]}" -H "$issue_cookie" -H "X-Real-IP: $IP" --data-urlencode "csrf=$1" \
212 --data-urlencode "request=$request" --data-urlencode 'body=Comment from the JSON form'
213}
214test "$(agentcomment wrong)" = 403
215jq -e '.schema == "forge/error/1" and .status == 403' "$T/body" >/dev/null
216test "$(agentcomment "$TOK")" = 201
217jq -e --arg path "$issue" '.path == $path and .applied == true' "$T/body" >/dev/null
218test "$(agentcomment "$TOK")" = 200
219jq -e '.applied == false' "$T/body" >/dev/null
220test "$(code GET "$issue" "${json[@]}")" = 200
221jq -e '(.comments | length) == 2 and .comments[1].body == "Comment from the JSON form"' "$T/body" >/dev/null
222test "$(code GET /agents.json)" = 200
223jq -e '[.routes[] | select((.schema == "forge/issue-form/1" and .method == "GET" or .schema == "forge/issue-write/1" and .method == "POST") and .auth == "session")] | length == 4' "$T/body" >/dev/null
224echo 'PASS agents can fetch both forms, post, get a JSON result, and replay without parsing HTML'