lore-forge/scripts/e2e-lore.sh

484 lines
23 KiB
Bash
Executable File

#!/usr/bin/env bash
# End-to-end test of lore-forge against a real, unmodified loreserver and lore CLI.
#
# scripts/e2e-lore.sh
#
# Runs the Lore release pinned in lore.lock, fetched by scripts/fetch-lore.sh. To test another
# build, set LORE_BIN_DIR to a directory holding its `loreserver` and `lore`.
# Everything runs on localhost in a fresh temporary directory, which is kept on failure, or
# always with KEEP_WORK=1.
#
# It checks the whole phase 1 path: first-run setup, a device login approved in the browser
# flow, repository create through the registry, push and clone with repository-scoped tokens,
# name lookup through UserService, refusals for a second user, a login with an API token, a
# service account logging in with client credentials before and after a grant, and logout.
#
# LORE_PORT (default 41337) moves loreserver's QUIC and gRPC port, and its HTTP port to
# LORE_PORT+2, so the test can run beside a deployment started by scripts/start.sh; FORGE_PORT
# does the same for lore-forge.
#
# Then it runs the desktop client's forge-service, linked to the pinned liblore, through
# scripts/desktop-e2e.py: sign-in, clone, the explorer's reads, submit and sync, then locks,
# lock-on-edit and autosync against bob in his own working copy. NO_DESKTOP=1 skips that.
#
# loreserver serves QUIC and gRPC over TLS, as in a deployment, with a certificate for localhost
# from a CA made for the run; Lore's clients and lore-forge trust it through SSL_CERT_FILE and
# reach loreserver as lores://localhost. NO_TLS=1 uses lore:// and plaintext gRPC instead.
#
# Last, it backs the deployment up with scripts/backup.sh, pushes another revision, restores
# the backup with scripts/restore.sh and checks the repository is back as it was.
#
# HOLD=1 keeps both servers running after the browser checks, for a look at the pages.
#
# SMOKE=1 stops after one user's login, create and push.
#
# Every login goes to a token store of its own (LORE_AUTH_PATH) whose key is kept in a file
# beside it (LORE_AUTH_STORE=fallback), so no run touches the user's own logins or asks the
# macOS keychain for anything.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
LORE_BIN_DIR="${LORE_BIN_DIR:-$("$ROOT/scripts/fetch-lore.sh")}"
LORE="$LORE_BIN_DIR/lore"
LORE_CLI="$LORE"
LORESERVER="$LORE_BIN_DIR/loreserver"
FORGE_PORT="${FORGE_PORT:-18080}"
FORGE="http://localhost:$FORGE_PORT"
LORE_PORT="${LORE_PORT:-41337}"
WORK="$(mktemp -d "${TMPDIR:-/tmp}/lore-forge-e2e.XXXXXX")"
PIDS=()
export LORE_AUTH_STORE=fallback
cleanup() {
local status=$?
for pid in ${PIDS[@]+"${PIDS[@]}"}; do kill "$pid" 2>/dev/null || true; done
# loreserver is not this shell's child, so `wait` does not cover it.
if [ -f "$WORK/loreserver.pid" ]; then
local lore_pid
lore_pid=$(cat "$WORK/loreserver.pid")
kill "$lore_pid" 2>/dev/null || true
for _ in $(seq 1 100); do kill -0 "$lore_pid" 2>/dev/null || break; sleep 0.1; done
fi
wait 2>/dev/null || true
if [ "$status" -ne 0 ]; then
echo "FAILED; logs and state kept in $WORK" >&2
elif [ -n "${KEEP_WORK:-}" ]; then
echo "logs and state kept in $WORK"
else
rm -rf "$WORK"
fi
}
trap cleanup EXIT
step() { printf '\n== %s\n' "$*"; }
fail() { echo "FAIL: $*" >&2; exit 1; }
wait_for() {
local what=$1 url=$2
for _ in $(seq 1 100); do
curl -fsS "$url" >/dev/null 2>&1 && return 0
sleep 0.2
done
fail "$what did not come up ($url)"
}
# The certificate is an EC key in SEC1 form ("EC PRIVATE KEY"), which is what Caddy writes for
# the Let's Encrypt certificates a deployment hands loreserver.
TRUST=()
TLS_ARGS=()
if [ -z "${NO_TLS:-}" ]; then
step "make a CA and a certificate for localhost"
TLS="$WORK/tls"
mkdir -p "$TLS" "$WORK/bin"
openssl ecparam -name prime256v1 -genkey -noout -out "$TLS/ca-key.pem"
openssl req -x509 -new -key "$TLS/ca-key.pem" -sha256 -days 2 -subj "/CN=lore-forge e2e CA" \
-addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign" \
-out "$TLS/ca.pem" 2>/dev/null
openssl ecparam -name prime256v1 -genkey -noout -out "$TLS/key.pem"
openssl req -new -key "$TLS/key.pem" -subj "/CN=localhost" -out "$TLS/server.csr" 2>/dev/null
printf 'subjectAltName=DNS:localhost,IP:127.0.0.1\nbasicConstraints=CA:FALSE\nextendedKeyUsage=serverAuth\n' >"$TLS/ext.cnf"
openssl x509 -req -in "$TLS/server.csr" -CA "$TLS/ca.pem" -CAkey "$TLS/ca-key.pem" -CAcreateserial \
-days 2 -sha256 -extfile "$TLS/ext.cnf" -out "$TLS/cert.pem" 2>/dev/null
grep -q "BEGIN EC PRIVATE KEY" "$TLS/key.pem" || fail "openssl did not write a SEC1 key"
TRUST=(env "SSL_CERT_FILE=$TLS/ca.pem")
TLS_ARGS=(--tls-cert "$TLS/cert.pem" --tls-key "$TLS/key.pem")
# Every lore command trusts the run's CA; nothing else does, so builds still reach crates.io.
printf '#!/bin/sh\nSSL_CERT_FILE=%s exec %s "$@"\n' "$TLS/ca.pem" "$LORE_CLI" >"$WORK/bin/lore"
chmod +x "$WORK/bin/lore"
LORE="$WORK/bin/lore"
LORE_REMOTE="lores://localhost:$LORE_PORT"
export LORE_FORGE_LORE_GRPC_URL="https://localhost:$LORE_PORT"
else
LORE_REMOTE="lore://localhost:$LORE_PORT"
export LORE_FORGE_LORE_GRPC_URL="http://127.0.0.1:$LORE_PORT"
fi
export LORE_FORGE_LORE_REMOTE="$LORE_REMOTE"
step "build lore-forge"
cargo build --quiet --manifest-path "$ROOT/Cargo.toml" -p lore-forge
FORGE_BIN="$ROOT/target/debug/lore-forge"
export LORE_FORGE_SECRET_KEY="$("$FORGE_BIN" generate-secret-key)"
export LORE_FORGE_PUBLIC_URL="$FORGE"
export LORE_FORGE_LORE_URL="https://localhost"
export LORE_FORGE_DATA_DIR="$WORK/forge-data"
export LORE_FORGE_LORE_HTTP_URL="http://127.0.0.1:$((LORE_PORT + 2))"
start_forge() {
${TRUST[@]+"${TRUST[@]}"} "$FORGE_BIN" serve --listen "127.0.0.1:$FORGE_PORT" >>"$WORK/forge.log" 2>&1 &
FORGE_PID=$!
PIDS+=("$FORGE_PID")
wait_for lore-forge "$FORGE/.well-known/openid-configuration"
}
step "start lore-forge on $FORGE"
start_forge
step "first-run setup in the browser flow"
JAR="$WORK/alice.cookies"
curl -fsS -c "$JAR" "$FORGE/setup" >/dev/null
csrf() { awk '$6 == "lf_csrf" { print $7 }' "$1"; }
curl -fsS -b "$JAR" -c "$JAR" -o /dev/null -H "Origin: $FORGE" \
--data-urlencode "csrf=$(csrf "$JAR")" \
--data-urlencode "username=alice" \
--data-urlencode "display_name=Alice Example" \
--data-urlencode "password=alice password" \
--data-urlencode "password_confirm=alice password" \
"$FORGE/setup"
grep -q lf_session "$JAR" || fail "setup did not sign alice in"
printf 'bob password\n' | "$FORGE_BIN" create-user --username bob --display-name "Bob Example" >/dev/null
step "start loreserver with the generated configuration"
mkdir -p "$WORK/lore-config"
"$FORGE_BIN" loreserver-config --store "$WORK/lore-store" --port "$LORE_PORT" --http-port "$((LORE_PORT + 2))" \
${TLS_ARGS[@]+"${TLS_ARGS[@]}"} --output "$WORK/lore-config/forge.toml"
# Started and stopped through these two scripts, which backup.sh also runs. The process ID is in
# loreserver.pid, since a restart from backup.sh is not this shell's child.
export E2E_WORK="$WORK" E2E_LORESERVER="$LORESERVER" E2E_HEALTH="http://127.0.0.1:$((LORE_PORT + 2))/health_check"
cat >"$WORK/bin-start-loreserver" <<'EOF'
#!/usr/bin/env bash
set -eu
log="$E2E_WORK/loreserver.log"
from=$(($(cat "$log" 2>/dev/null | wc -l) + 1))
"$E2E_LORESERVER" --config "$E2E_WORK/lore-config" --env forge >>"$log" 2>&1 &
pid=$!
echo "$pid" >"$E2E_WORK/loreserver.pid"
for _ in $(seq 1 150); do
curl -fsS "$E2E_HEALTH" >/dev/null 2>&1 && exit 0
kill -0 "$pid" 2>/dev/null || { tail -n +"$from" "$log" >&2; echo "loreserver exited" >&2; exit 1; }
sleep 0.2
done
echo "loreserver did not start" >&2
exit 1
EOF
cat >"$WORK/bin-stop-loreserver" <<'EOF'
#!/usr/bin/env bash
pid=$(cat "$E2E_WORK/loreserver.pid")
kill -TERM "$pid" 2>/dev/null || exit 0
for _ in $(seq 1 300); do kill -0 "$pid" 2>/dev/null || exit 0; sleep 0.1; done
echo "loreserver did not stop" >&2
exit 1
EOF
chmod +x "$WORK/bin-start-loreserver" "$WORK/bin-stop-loreserver"
"$WORK/bin-start-loreserver" || fail "loreserver did not start"
LORE_PID=$(cat "$WORK/loreserver.pid")
# Logs `$1` (a user with cookie jar `$2`) into the Lore CLI by approving the device code.
lore_login() {
local user=$1 jar=$2
export LORE_AUTH_PATH="$WORK/auth-$user"
"$LORE" auth login --no-browser "$LORE_REMOTE" >"$WORK/login-$user.log" 2>&1 &
local pid=$!
local code=""
for _ in $(seq 1 100); do
code=$(grep -oE 'user_code=[A-Z]{4}-[A-Z]{4}' "$WORK/login-$user.log" | head -1 | cut -d= -f2 || true)
[ -z "$code" ] && code=$(grep -oE '\b[BCDFGHJKLMNPQRSTVWXZ]{4}-[BCDFGHJKLMNPQRSTVWXZ]{4}\b' "$WORK/login-$user.log" | head -1 || true)
[ -n "$code" ] && break
kill -0 "$pid" 2>/dev/null || { cat "$WORK/login-$user.log" >&2; fail "lore auth login exited early"; }
sleep 0.2
done
[ -n "$code" ] || { cat "$WORK/login-$user.log" >&2; fail "no user code printed"; }
echo "user code $code"
curl -fsS -b "$jar" -c "$jar" "$FORGE/device?user_code=$code" | grep -q "$code" \
|| fail "device page did not show $code"
curl -fsS -b "$jar" -c "$jar" -H "Origin: $FORGE" \
--data-urlencode "csrf=$(csrf "$jar")" \
--data-urlencode "user_code=$code" \
--data-urlencode "decision=approve" \
"$FORGE/device" | grep -q "Client signed in" || fail "approval failed"
wait "$pid" || { cat "$WORK/login-$user.log" >&2; fail "lore auth login failed for $user"; }
}
step "alice: lore auth login"
lore_login alice "$JAR"
step "alice: create alice/demo, commit and push"
export LORE_AUTH_PATH="$WORK/auth-alice"
mkdir -p "$WORK/alice-work"
(
cd "$WORK/alice-work"
"$LORE" repository create "$LORE_REMOTE/alice/demo"
echo one >a.txt
"$LORE" stage a.txt
"$LORE" commit "First revision"
"$LORE" push
)
if [ -z "${NO_TLS:-}" ]; then
step "alice: a client that does not trust the certificate, or skips TLS, is refused"
if (cd "$WORK" && "$LORE_CLI" clone "$LORE_REMOTE/alice/demo" untrusted-clone) >"$WORK/untrusted.log" 2>&1; then
fail "a client without the CA cloned over lores://"
fi
if (cd "$WORK" && "$LORE" clone "lore://localhost:$LORE_PORT/alice/demo" plaintext-clone) >"$WORK/plaintext.log" 2>&1; then
fail "a lore:// clone worked against loreserver's TLS gRPC endpoint"
fi
# Only TLS tells the two apart from the clones that work, but check they failed connecting,
# not over permissions. The plaintext one reports either message, run to run.
for log in untrusted plaintext; do
grep -qE "Disconnected from server|connection closed" "$WORK/$log.log" \
|| { cat "$WORK/$log.log" >&2; fail "the $log clone failed for another reason"; }
done
echo "both failed to connect"
fi
if [ -n "${SMOKE:-}" ]; then
printf '\nPASS (smoke): one login, create and push against %s\n' "$("$LORESERVER" --version 2>/dev/null || echo loreserver)"
exit 0
fi
step "alice: auth info resolves names through UserService and shows the scoped token"
(cd "$WORK/alice-work" && "$LORE" auth info --with-access-token) >"$WORK/info.log" 2>&1
sed -E 's/eyJ[A-Za-z0-9_.-]+/<token>/g' "$WORK/info.log"
grep -q 'Alice Example' "$WORK/info.log" || fail "auth info did not resolve alice's name"
# The payload of the repository token Lore stored, which must carry a `resources` entry for
# alice/demo.
jwt_payload() {
local part
part=$(cut -d. -f2 | tr '_-' '/+')
while [ $((${#part} % 4)) -ne 0 ]; do part="$part="; done
printf '%s' "$part" | base64 -d
}
TOKEN=$(grep -oE 'eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+' "$WORK/info.log" | tail -1)
[ -n "$TOKEN" ] || fail "auth info printed no access token"
printf '%s' "$TOKEN" | jwt_payload | tee "$WORK/claims.json"
echo
grep -q '"resources":\[{"resource_id":"urc-' "$WORK/claims.json" || fail "the repository token has no resources claim"
step "alice: lore repository list comes from the registry"
"$LORE" repository list "$LORE_REMOTE" 2>&1 | tee "$WORK/list.log"
grep -q "alice/demo" "$WORK/list.log" || fail "alice/demo not listed"
step "alice: clone alice/demo elsewhere"
(cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" alice-clone)
[ "$(cat "$WORK/alice-clone/a.txt")" = "one" ] || fail "clone content differs"
step "alice: names outside a namespace are refused"
# Each create runs in an empty directory, so only the registry can refuse it.
refused_create() {
local name=$1 expect=$2 dir="$WORK/refused-${1//\//-}"
mkdir -p "$dir"
if (cd "$dir" && "$LORE" repository create "$LORE_REMOTE/$name" >"$dir.log" 2>&1); then
fail "created $name"
fi
grep -qiE "$expect" "$dir.log" || { cat "$dir.log" >&2; fail "$name was refused for another reason"; }
}
refused_create nobody/x "not found|nobody"
refused_create flat "owner/name|invalid"
step "bob: lore auth login"
BOB_JAR="$WORK/bob.cookies"
curl -fsS -c "$BOB_JAR" "$FORGE/login" >/dev/null
curl -fsS -b "$BOB_JAR" -c "$BOB_JAR" -o /dev/null -H "Origin: $FORGE" \
--data-urlencode "csrf=$(csrf "$BOB_JAR")" \
--data-urlencode "username=bob" \
--data-urlencode "password=bob password" \
"$FORGE/login"
lore_login bob "$BOB_JAR"
export LORE_AUTH_PATH="$WORK/auth-bob"
step "bob: may not create in alice's namespace, clone her repository, or see it listed"
mkdir -p "$WORK/bob-work"
refused_create alice/stolen "not authorized|permission|denied"
(cd "$WORK" && ! "$LORE" clone "$LORE_REMOTE/alice/demo" bob-clone 2>"$WORK/bob-clone.log") \
|| fail "bob cloned alice's repository"
"$LORE" repository list "$LORE_REMOTE" >"$WORK/bob-list.log" 2>&1 || true
! grep -q "alice/demo" "$WORK/bob-list.log" || fail "bob sees alice/demo"
step "bob: creates bob/own"
(cd "$WORK/bob-work" && "$LORE" repository create "$LORE_REMOTE/bob/own")
# Posts a form as alice (CSRF added) and prints the page that answers it.
alice_post() {
local path=$1; shift
local args=()
for pair in "$@"; do args+=(--data-urlencode "$pair"); done
curl -fsS -L -b "$JAR" -c "$JAR" -H "Origin: $FORGE" \
--data-urlencode "csrf=$(csrf "$JAR")" ${args[@]+"${args[@]}"} "$FORGE$path"
}
secret_in() { grep -oE 'lf_[A-Za-z0-9_-]+' | head -1; }
step "alice: an API token logs the CLI in without a browser"
TOKEN=$(alice_post /settings/tokens name=e2e scope=lore days=1 | secret_in)
[ -n "$TOKEN" ] || fail "no API token shown"
export LORE_AUTH_PATH="$WORK/auth-alice-token"
LORE_TOKEN="$TOKEN" "$LORE" auth login --token-type api-key --no-browser "$LORE_REMOTE" >"$WORK/login-token.log" 2>&1 \
|| { cat "$WORK/login-token.log" >&2; fail "API token login failed"; }
(cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" token-clone) >/dev/null
[ "$(cat "$WORK/token-clone/a.txt")" = "one" ] || fail "clone with the API token login differs"
step "alice: a service account logs in with client credentials"
ACCOUNT_PATH=$(curl -fsS -o /dev/null -w '%{redirect_url}' -b "$JAR" -c "$JAR" -H "Origin: $FORGE" \
--data-urlencode "csrf=$(csrf "$JAR")" --data-urlencode name=ci --data-urlencode "display_name=CI" \
"$FORGE/settings/service-accounts" | sed -E 's|^https?://[^/]+||; s|\?.*$||')
[ -n "$ACCOUNT_PATH" ] || fail "service account not created"
SECRET=$(alice_post "$ACCOUNT_PATH/tokens" name=e2e days=1 | secret_in)
[ -n "$SECRET" ] || fail "no service account secret shown"
export LORE_AUTH_PATH="$WORK/auth-ci"
if (cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" ci-denied) >/dev/null 2>&1; then
fail "a service account cloned before any grant"
fi
LORE_CLIENT_SECRET="$SECRET" "$LORE" auth login --client-id alice.ci "$LORE_REMOTE" >"$WORK/login-ci.log" 2>&1 \
|| { cat "$WORK/login-ci.log" >&2; fail "client credentials login failed"; }
if (cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" ci-early) >/dev/null 2>&1; then
fail "a service account cloned without a grant"
fi
alice_post /alice/demo/access kind=user name=alice.ci preset=reader >/dev/null
LORE_CLIENT_SECRET="$SECRET" "$LORE" auth login --client-id alice.ci "$LORE_REMOTE" >>"$WORK/login-ci.log" 2>&1 \
|| { cat "$WORK/login-ci.log" >&2; fail "client credentials login failed after the grant"; }
(cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" ci-clone) >/dev/null
[ "$(cat "$WORK/ci-clone/a.txt")" = "one" ] || fail "clone as the service account differs"
step "alice: the web browser reads files, history and diffs from loreserver"
export LORE_AUTH_PATH="$WORK/auth-alice"
(
cd "$WORK/alice-work"
echo two >a.txt
mkdir -p sub && echo three >sub/b.txt
"$LORE" stage a.txt sub/b.txt
"$LORE" commit "Second revision"
"$LORE" push
) >"$WORK/second.log" 2>&1
(
cd "$WORK/alice-work"
"$LORE" branch create feature
"$LORE" branch switch feature
echo art >c.txt
"$LORE" stage c.txt
"$LORE" commit "Feature work"
"$LORE" push
"$LORE" branch switch main
echo four >d.txt
"$LORE" stage d.txt
"$LORE" commit "Main moves on"
"$LORE" push
"$LORE" branch merge feature # commits the merge itself
"$LORE" push
) >"$WORK/branch.log" 2>&1
page() { curl -fsS -b "$1" "$FORGE$2" || fail "GET $2 failed"; }
page "$JAR" /alice/demo >"$WORK/files.html"
grep -q '>a.txt<' "$WORK/files.html" && grep -q '>sub<' "$WORK/files.html" || fail "the file list lacks a.txt or sub"
grep -q 'Second revision' "$WORK/files.html" || fail "the file list does not attribute the last change"
page "$JAR" "/alice/demo?path=a.txt" | grep -q '<td class="text">two</td>' || fail "a.txt does not show its content"
page "$JAR" "/alice/demo?path=sub%2Fb.txt" | grep -q '<td class="text">three</td>' || fail "sub/b.txt does not show its content"
page "$JAR" /alice/demo/graph >"$WORK/graph.html"
grep -q 'First revision' "$WORK/graph.html" && grep -q 'Second revision' "$WORK/graph.html" || fail "the graph lacks a revision"
grep -q '>alice<' "$WORK/graph.html" || fail "the graph does not name the author"
grep -q 'Feature work' "$WORK/graph.html" && grep -q '>feature<' "$WORK/graph.html" || fail "the graph lacks the feature branch"
grep -q 'width="4" height="4"' "$WORK/graph.html" || fail "the graph draws no merge"
# The revision link in the row whose message is "Second revision".
SECOND=$(perl -ne 'print "$1\n" while m{(/alice/demo/revision/[0-9a-f]{64})(?:(?!</tr>).)*?Second revision}g' "$WORK/graph.html" | head -1)
MERGE=$(grep -oE '/alice/demo/revision/[0-9a-f]{64}' "$WORK/graph.html" | head -1)
[ "$(page "$JAR" "$MERGE" | grep -oE 'href="/alice/demo/revision/[0-9a-f]{64}"' | sort -u | wc -l | tr -d ' ')" = 2 ] \
|| fail "the merge does not show two parents"
page "$JAR" "$SECOND" >"$WORK/revision.html"
grep -q -- '+two' "$WORK/revision.html" && grep -q -- '-one' "$WORK/revision.html" || fail "the revision page lacks the diff of a.txt"
grep -q 'sub/b.txt' "$WORK/revision.html" || fail "the revision page lacks the added file"
page "$JAR" "/alice/demo/tree?rev=${SECOND##*/}" | grep -q '>M</span>' || fail "the tree does not open the revision's changes"
for path in /alice/demo /alice/demo/graph "$SECOND"; do
status=$(curl -s -o /dev/null -w '%{http_code}' -b "$BOB_JAR" "$FORGE$path")
[ "$status" = 404 ] || fail "bob got $status for $path"
done
if [ -z "${NO_DESKTOP:-}" ]; then
step "desktop: forge-service signs in, clones, submits and syncs through liblore"
cargo build --quiet --manifest-path "$ROOT/desktop/Cargo.toml" -p forge-service
LIBLORE_DIR="$("$ROOT/scripts/fetch-lore.sh" --liblore)"
case "$(uname -s)" in Darwin) LIBLORE_FILE=liblore.dylib ;; *) LIBLORE_FILE=liblore.so ;; esac
mkdir -p "$WORK/desktop"
(
export LORE_FORGE_DESKTOP_DIR="$WORK/desktop"
export LORE_FORGE_LIBLORE="$LIBLORE_DIR/$LIBLORE_FILE"
export LORE_AUTH_PATH="$WORK/auth-desktop"
export RUST_LOG="${RUST_LOG:-info,forge_service=debug}"
[ -n "${TLS:-}" ] && export SSL_CERT_FILE="$TLS/ca.pem"
exec "$ROOT/desktop/target/debug/forge-service"
) >"$WORK/desktop/stdout.log" 2>&1 &
DESKTOP_PID=$!
PIDS+=("$DESKTOP_PID")
for _ in $(seq 1 100); do
[ -S "$WORK/desktop/service.sock" ] && break
kill -0 "$DESKTOP_PID" 2>/dev/null || { cat "$WORK/desktop/stdout.log" >&2; fail "forge-service exited"; }
sleep 0.2
done
# bob writes to alice/demo from his own working copy, for the lock checks.
alice_post /alice/demo/access kind=user name=bob preset=writer >/dev/null
(cd "$WORK" && LORE_AUTH_PATH="$WORK/auth-bob" "$LORE" clone "$LORE_REMOTE/alice/demo" bob-demo) >/dev/null
python3 -I "$ROOT/scripts/desktop-e2e.py" "$WORK/desktop/service.sock" "$FORGE" "$JAR" "$LORE_REMOTE" "$WORK" \
"$LORE" "$WORK/bob-demo" \
|| { tail -20 "$WORK/desktop/stdout.log" >&2; fail "the desktop service checks failed"; }
wait "$DESKTOP_PID" || fail "forge-service did not stop cleanly"
fi
if [ -n "${HOLD:-}" ]; then
echo "holding both servers for a look: $FORGE (alice / alice password), state in $WORK; Ctrl-C ends"
while kill -0 "$(cat "$WORK/loreserver.pid")" 2>/dev/null; do sleep 1; done
fi
step "backup: back up, push another revision, restore, and the repository is as it was"
export LORE_AUTH_PATH="$WORK/auth-alice"
# A fresh clone: the desktop checks moved alice/demo on past alice-work.
(cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" backup-work) >"$WORK/backup-clone.log" 2>&1 \
|| { cat "$WORK/backup-clone.log" >&2; fail "cloning for the backup check failed"; }
(
cd "$WORK/backup-work"
echo backed up >backed.txt
"$LORE" stage backed.txt
"$LORE" commit "Pushed just before the backup"
"$LORE" push
) >"$WORK/before-backup.log" 2>&1 || { cat "$WORK/before-backup.log" >&2; fail "the push before the backup failed"; }
LORE_FORGE_BIN="$FORGE_BIN" LORE_STORE="$WORK/lore-store" LORESERVER_BIN="$LORESERVER" \
LORESERVER_STOP="$WORK/bin-stop-loreserver" LORESERVER_START="$WORK/bin-start-loreserver" \
"$ROOT/scripts/backup.sh" "$WORK/backups" | tee "$WORK/backup.log"
SNAPSHOT="$WORK/backups/$(readlink "$WORK/backups/latest")"
[ -f "$SNAPSHOT/lore-forge.sqlite" ] && [ -d "$SNAPSHOT/lore-store" ] && [ -f "$SNAPSHOT/MANIFEST" ] \
|| fail "the backup lacks the database, the store or its manifest"
(
cd "$WORK/backup-work"
echo lost >lost.txt
"$LORE" stage lost.txt
"$LORE" commit "Pushed after the backup"
"$LORE" push
) >"$WORK/after-backup.log" 2>&1 || { cat "$WORK/after-backup.log" >&2; fail "the push after the backup failed"; }
"$WORK/bin-stop-loreserver"
kill "$FORGE_PID" && wait "$FORGE_PID" 2>/dev/null || true
LORE_STORE="$WORK/lore-store" "$ROOT/scripts/restore.sh" "$SNAPSHOT" | tee "$WORK/restore.log"
start_forge
"$WORK/bin-start-loreserver" || fail "loreserver did not start on the restored store"
(cd "$WORK" && "$LORE" clone "$LORE_REMOTE/alice/demo" restored-clone) >"$WORK/restored-clone.log" 2>&1 \
|| { cat "$WORK/restored-clone.log" >&2; fail "cloning the restored repository failed"; }
[ "$(cat "$WORK/restored-clone/backed.txt")" = "backed up" ] || fail "the restore lost the revision pushed before the backup"
[ ! -e "$WORK/restored-clone/lost.txt" ] || fail "the restore kept a revision pushed after the backup"
page "$JAR" /alice/demo | grep -q '>backed.txt<' || fail "the browser does not read the restored repository"
ls -d "$WORK"/lore-store.before-restore-* "$LORE_FORGE_DATA_DIR"/lore-forge.sqlite.before-restore-* >/dev/null \
|| fail "restore did not keep what it replaced"
echo "restored $(basename "$SNAPSHOT"): backed.txt is there, lost.txt is not"
step "alice: logout revokes the CLI login"
export LORE_AUTH_PATH="$WORK/auth-alice"
(cd "$WORK/alice-work" && "$LORE" auth logout)
grep -q "cli_login.revoke" <(sqlite3 "$LORE_FORGE_DATA_DIR/lore-forge.sqlite" "SELECT action FROM audit_log") \
|| fail "logout did not reach the revocation endpoint"
printf '\nPASS: lore-forge end to end against %s\n' "$("$LORESERVER" --version 2>/dev/null || echo loreserver)"