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