Repository Public
1#!/bin/bash2# Starts a throw-away SP Browser backend on 127.0.0.1 for integration tests (never production data).3# scripts/test-backend.sh up → clones/updates the backend from spbgit, starts it, prints the env to export4# scripts/test-backend.sh down5# The instance uses a temporary database, open registration and no search provider key.6set -euo pipefail7CACHE="${SPB_TEST_BACKEND_DIR:-$HOME/Library/Caches/sp-browser-test-backend}"8PORT="${SPB_TEST_PORT:-8719}"9STATE="${TMPDIR:-/tmp}/spb-test-backend"10case "${1:-up}" in11 up)12 if [ -d "$CACHE/.git" ]; then git -C "$CACHE" pull -q --ff-only || true; else git clone -q gitsrv:srv/git/sp-browser-backend.git "$CACHE"; fi13 (cd "$CACHE" && [ -d node_modules ] || npm ci --omit=dev --silent)14 "$0" down >/dev/null 2>&1 || true15 rm -rf "$STATE"; mkdir -p "$STATE/data"16 (cd "$CACHE" && env -i PATH="$PATH" HOME="$HOME" PORT="$PORT" HOST=127.0.0.1 DATA_DIR="$STATE/data" \17 PUBLIC_ORIGIN=https://browser.test RP_ID=browser.test OPEN_REGISTRATION=true SERVER_SECRET=test ADMIN_TOKEN=test-admin \18 TAVILY_API_KEY= ANON_SEARCH=true AUTH_RATE_MULTIPLIER=1000 nohup node src/server.ts > "$STATE/server.log" 2>&1 < /dev/null & echo $! > "$STATE/pid")19 for _ in $(seq 1 40); do curl -sf "http://127.0.0.1:$PORT/api/health" >/dev/null && break; sleep 0.25; done20 curl -sf "http://127.0.0.1:$PORT/api/health" >/dev/null || { echo "backend did not start; see $STATE/server.log" >&2; exit 1; }21 echo "export SPB_TEST_BACKEND=http://127.0.0.1:$PORT SPB_TEST_ORIGIN=https://browser.test SPB_TEST_RP=browser.test SPB_TEST_DB=$STATE/data/sp-browser.sqlite"22 ;;23 down)24 [ -f "$STATE/pid" ] && kill "$(cat "$STATE/pid")" 2>/dev/null || true25 rm -f "$STATE/pid"26 # Whatever still listens on the test port is a previous test instance: stop it by PID (never by name).27 for pid in $(lsof -nP -tiTCP:"$PORT" -sTCP:LISTEN 2>/dev/null); do kill "$pid" 2>/dev/null || true; done28 for _ in $(seq 1 20); do lsof -nP -tiTCP:"$PORT" -sTCP:LISTEN >/dev/null 2>&1 || break; sleep 0.1; done29 ;;30 *) echo "usage: $0 up|down" >&2; exit 2;;31esac32