6361 bytes
#!/bin/sh
set -eu
source=$(git rev-parse --show-toplevel)
test -z "$(git -C "$source" status --porcelain)"
commit=$(git -C "$source" rev-parse HEAD)
tmp=$(mktemp -d)
port_a=${ANBOT_TEST_PORT_A:-18787}
port_b=${ANBOT_TEST_PORT_B:-18788}
a_pid=
b_pid=
publish_pid=
cleanup() {
for pid in "$a_pid" "$b_pid" "$publish_pid"; do
if [ -n "$pid" ]; then
kill -KILL "$pid" 2>/dev/null || :
wait "$pid" 2>/dev/null || :
fi
done
rm -rf "$tmp"
}
trap cleanup EXIT
trap 'exit 1' INT TERM
log_command() {
printf '+ ' >&2
printf '%s ' "$@" >&2
printf '\n' >&2
}
run() {
log_command "$@"
"$@"
}
start() {
log_command "$@"
exec "$@"
}
await_file() {
attempts=0
until test -s "$1"; do
attempts=$((attempts + 1))
if [ "$attempts" -ge 200 ]; then
printf 'Timed out waiting for %s\n' "$1" >&2
return 1
fi
sleep 0.1
done
}
json_field() {
node -p 'JSON.parse(require("fs").readFileSync(0, "utf8"))[process.argv[1]]' "$1"
}
run git clone --no-local "$source" "$tmp/source"
run git -C "$tmp/source" checkout -B main "$commit"
mkdir "$tmp/runner"
run git -C "$tmp/source" archive --output="$tmp/source.tar" HEAD
run tar -xf "$tmp/source.tar" -C "$tmp/runner"
a_cli="$tmp/runner/anbot.mjs"
a_data="$tmp/data-a"
b_data="$tmp/data-b"
a_publication=$(run node "$a_cli" publish --data "$a_data" --source "$tmp/source" --branch main --port "$port_a")
printf 'A publication %s\n' "$a_publication"
a_url=$(printf '%s' "$a_publication" | json_field cloneUrl)
start node "$a_cli" serve --data "$a_data" --port "$port_a" >"$tmp/a-first.url" &
a_pid=$!
await_file "$tmp/a-first.url"
test "$(cat "$tmp/a-first.url")" = "$a_url"
kill "$a_pid"
wait "$a_pid" 2>/dev/null || :
a_pid=
rm -rf "$a_data/cache" "$tmp/source"
start node "$a_cli" serve --data "$a_data" --port "$port_a" >"$tmp/a.url" &
a_pid=$!
await_file "$tmp/a.url"
test "$(cat "$tmp/a.url")" = "$a_url"
run git clone "$a_url" "$tmp/node-b"
test "$(git -C "$tmp/node-b" rev-parse HEAD)" = "$commit"
a_author=$(node --input-type=module - "$a_cli" "$a_data" <<'JS'
import { pathToFileURL } from 'node:url'
const storage = new URL('./storage.mjs', pathToFileURL(process.argv[2]))
const { loadIdentity, recordPath } = await import(storage)
console.log(recordPath((await loadIdentity(process.argv[3])).author))
JS
)
test "$(run git ls-remote "${a_url%.git}/$a_author.git" HEAD | cut -f1)" = "$commit"
b_cli="$tmp/node-b/anbot.mjs"
b_publication=$(run node "$b_cli" publish --data "$b_data" --source "$tmp/node-b" --branch main --port "$port_b")
printf 'B publication %s\n' "$b_publication"
b_url=$(printf '%s' "$b_publication" | json_field cloneUrl)
test "${a_url##*/}" != "${b_url##*/}"
kill "$a_pid"
wait "$a_pid" 2>/dev/null || :
a_pid=
start node "$b_cli" serve --data "$b_data" --port "$port_b" >"$tmp/b-first.url" &
b_pid=$!
await_file "$tmp/b-first.url"
test "$(cat "$tmp/b-first.url")" = "$b_url"
b_hash=${b_url##*/}
b_prefix=$(printf '%s' "$b_hash" | cut -c1-12)
b_short_url="${b_url%/*}/$b_prefix.git"
run git clone "$b_short_url" "$tmp/second-generation"
test "$(git -C "$tmp/second-generation" rev-parse HEAD)" = "$commit"
if run git ls-remote "${b_url%.git}suffix.git" HEAD; then
printf 'An unknown repository hash was accepted\n' >&2
exit 1
fi
if run git -C "$tmp/second-generation" push origin main; then
printf 'The read-only server accepted git push\n' >&2
exit 1
fi
printf 'Development on node B\n' >"$tmp/node-b/development.txt"
run git -C "$tmp/node-b" add development.txt
run git -C "$tmp/node-b" -c user.name=Acceptance -c user.email=test@example.invalid commit -m "Test development on B"
development=$(git -C "$tmp/node-b" rev-parse HEAD)
test "$development" != "$commit"
# Pause the publisher after fsck succeeds and before the selection is written.
mkdir "$tmp/bin"
cat >"$tmp/bin/git" <<'SH'
#!/bin/sh
"$ANBOT_REAL_GIT" "$@" || exit "$?"
if [ "${5:-}" = fsck ]; then
kill -STOP "$PPID"
printf 'ready\n' >"$ANBOT_PAUSED"
fi
SH
chmod +x "$tmp/bin/git"
log_command node "$b_cli" publish --data "$b_data" --source "$tmp/node-b" --branch main --port "$port_b"
ANBOT_REAL_GIT=$(command -v git) ANBOT_PAUSED="$tmp/paused" PATH="$tmp/bin:$PATH" \
node "$b_cli" publish --data "$b_data" --source "$tmp/node-b" --branch main --port "$port_b" &
publish_pid=$!
await_file "$tmp/paused"
kill -KILL "$publish_pid"
wait "$publish_pid" 2>/dev/null || :
publish_pid=
kill "$b_pid"
wait "$b_pid" 2>/dev/null || :
b_pid=
rm -rf "$b_data/cache"
start node "$b_cli" serve --data "$b_data" --port "$port_b" >"$tmp/b.url" &
b_pid=$!
await_file "$tmp/b.url"
run git clone "$b_url" "$tmp/after-interruption"
test "$(git -C "$tmp/after-interruption" rev-parse HEAD)" = "$commit"
updated=$(run node "$b_cli" publish --data "$b_data" --source "$tmp/node-b" --branch main --port "$port_b")
printf 'B updated publication %s\n' "$updated"
test "$(printf '%s' "$updated" | json_field cloneUrl)" = "$b_url"
run git -C "$tmp/second-generation" fetch origin
test "$(git -C "$tmp/second-generation" rev-parse origin/main)" = "$development"
run git -C "$tmp/second-generation" merge-base --is-ancestor "$commit" "$development"
run git clone "$b_url" "$tmp/after-retry"
test "$(git -C "$tmp/after-retry" rev-parse HEAD)" = "$development"
# Forge a valid block filename with a different body in a disposable store.
cp -R "$b_data" "$tmp/corrupt"
rm -rf "$tmp/corrupt/cache"
node --input-type=module - "$tmp/corrupt" <<'JS'
import { readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
const root = process.argv[2]
const { updateId } = JSON.parse(readFileSync(join(root, 'selected.json')))
const recordName = Buffer.from(updateId, 'base64').toString('base64url')
const record = JSON.parse(readFileSync(join(root, 'records', recordName)))
const bundle = JSON.parse(record.content).bundle.id
const manifest = JSON.parse(readFileSync(join(root, 'blocks', bundle)))
const chunkPath = join(root, 'blocks', manifest.chunks[0])
const chunk = readFileSync(chunkPath)
chunk[0] ^= 1
writeFileSync(chunkPath, chunk)
JS
if run node "$b_cli" serve --data "$tmp/corrupt" --port "$port_b" 2>"$tmp/corrupt.err"; then
printf 'The server accepted a corrupt block\n' >&2
exit 1
fi
grep -F 'Corrupt AndFS block' "$tmp/corrupt.err"
printf 'PASS: %s survived two generations and interruption. Fetch and clone got %s.\n' "$commit" "$development"
Git ยท Social feed