Repository Public
1#!/usr/bin/env bash2# SP Drive iOS3# File: Scripts/test-server.sh4# Description: Disposable SP Drive backend for the automated tests (own clone, database, data directory and port on m5ma).5# Never touches production (~/apps/sp-drive, database sp_drive) nor the macOS session's testbed.6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9#10# Scripts/test-server.sh up clone/refresh the backend (branch $SPDRIVE_BACKEND_REF), fresh database and data, start it on11# m5ma:8663, open an SSH tunnel to localhost:8663, write test credentials12# Scripts/test-server.sh creds two fresh accounts-with-device-tokens (the server keeps running)13# Scripts/test-server.sh tunnel reopen the SSH tunnel14# Scripts/test-server.sh down stop the server and the tunnel15set -euo pipefail16NODE="${SPDRIVE_TEST_NODE:-m5ma}"17REF="${SPDRIVE_BACKEND_REF:-next}" # SP Drive 2.0 backend (API-NEXT.md); `native-clients` = the 1.0 server18PORT=866319CONF="$HOME/.config/spdrive-ios-test"20mkdir -p "$CONF" && chmod 700 "$CONF"2122tunnel_down() { if [ -f "$CONF/tunnel.pid" ]; then kill "$(cat "$CONF/tunnel.pid")" 2>/dev/null || true; rm -f "$CONF/tunnel.pid"; fi; }23tunnel_up() {24 tunnel_down25 for p in $(lsof -nP -iTCP:$PORT -sTCP:LISTEN -t 2>/dev/null); do kill "$p" 2>/dev/null || true; done26 # Its own connection (no shared ControlMaster, which would close the forward when it expires).27 ssh -f -N -o ControlMaster=no -o ControlPath=none -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -L "$PORT:127.0.0.1:$PORT" "$NODE"28 lsof -nP -iTCP:$PORT -sTCP:LISTEN -t | head -1 > "$CONF/tunnel.pid"29}3031creds() {32 ssh "$NODE" 'bash -s' <<'REMOTE' > "$CONF/credentials.env"33set -euo pipefail34export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"35DB=sp_drive_iostest36tok() { python3 -c "import secrets;print(secrets.token_urlsafe(32))"; }37sha() { printf 'spd-token-v1:%s' "$1" | shasum -a 256 | cut -d" " -f1; }38SUFFIX=$(python3 -c 'import secrets;print(secrets.token_hex(4))')39mk() { # $1 = label, $2 = role ; prints the device token of a brand-new account with an empty Drive40 local T; T=$(tok); local E="ios-$1-$SUFFIX@example.test"41 psql -q -d "$DB" -v ON_ERROR_STOP=1 -v th="$(sha "$T")" -v email="$E" -v name="Test $1" -v role="${2:-user}" >/dev/null <<'SQL'42with u as (insert into users (email, display_name, role, webauthn_handle) values (:'email', :'name', :'role', gen_random_bytes(32)) returning id),43 q as (insert into quotas (user_id, limit_bytes) select id, 2147483648 from u),44 r as (insert into folders (owner_id, parent_id, name, name_key) select id, null, '', '' from u)45insert into sessions (user_id, token_hash, method, kind, device_name, expires_at, reauth_at, ip, user_agent)46 select id, :'th', 'passkey', 'device', 'SP Drive iOS tests', now() + interval '30 days', now(), '127.0.0.1', 'SPDrive-iOS tests' from u;47SQL48 printf %s "$T"49}50psql -q -d "$DB" -c "create extension if not exists pgcrypto" >/dev/null51# A: engine tests (administrator, so the admin view can be decoded) · B: simulator · C: « someone else » for grants52printf 'SPDRIVE_TEST_URL=http://localhost:8663\nSPDRIVE_TEST_TOKEN=%s\nSPDRIVE_TEST_TOKEN_B=%s\nSPDRIVE_TEST_TOKEN_C=%s\n' "$(mk A admin)" "$(mk B)" "$(mk C)"53printf 'SPDRIVE_TEST_EMAIL_A=ios-A-%s@example.test\nSPDRIVE_TEST_EMAIL_B=ios-B-%s@example.test\nSPDRIVE_TEST_EMAIL_C=ios-C-%s@example.test\n' "$SUFFIX" "$SUFFIX" "$SUFFIX"54REMOTE55 chmod 600 "$CONF/credentials.env"56 echo "credentials written to $CONF/credentials.env"57}5859case "${1:-}" in60up)61 ssh "$NODE" 'cat > /tmp/spdrive-ios-test-up.sh' <<'REMOTE'62set -euo pipefail63export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"64D=~/dev/sp-drive-ios-test; DATA=~/dev/sp-drive-ios-test-data; DB=sp_drive_iostest65LLM=8665 # fake OpenAI-compatible endpoint (scripts/fake-llm.mjs of the backend — NOT a model) standing in for the `maclustr` provider66for p in $(lsof -nP -iTCP:$PORT -sTCP:LISTEN -t 2>/dev/null) $(lsof -nP -iTCP:$LLM -sTCP:LISTEN -t 2>/dev/null); do kill "$p" 2>/dev/null || true; done67if [ -d "$D/.git" ]; then git -C "$D" fetch -q origin && git -C "$D" checkout -q -f --detach "origin/$REF"; else git clone -q gitsrv:srv/git/sp-drive.git "$D" && git -C "$D" checkout -q --detach "origin/$REF"; fi68cd "$D"; echo "backend: $(git log --oneline -1)"69pnpm install --frozen-lockfile --silent >/dev/null 2>&1 || true # pnpm 11 exits non-zero on the ignored esbuild build script; tsx works without it70[ -x node_modules/.bin/tsx ] || { echo "dependencies missing"; exit 1; }71rm -rf "$DATA"; mkdir -p "$DATA"72dropdb --if-exists "$DB"; createdb "$DB"73cat > "$DATA/env.sh" <<ENV74export DATABASE_URL=postgres://127.0.0.1/$DB SPD_ORIGIN=http://localhost:$PORT SPD_RP_ID=localhost PORT=$PORT HOST=127.0.0.175export SPD_DATA_DIR=$DATA/data SPD_MASTER_KEY_FILE=$DATA/master.key SPD_INLINE_WORKER=on SPD_APP_DIR=$D SPD_AV_MODE=optional SPD_CLAMD_SOCKET=76export SPD_APPLE_APP_IDS=3YM54G49SN.ai.spboucher.spdrive.ios NODE_ENV=production77export SPD_AI_ENV_FILE=$DATA/ai.env SPD_BACKUP_DIR=$DATA/backups SPD_TRUST_PROXY=127.0.0.1 # the engine tests give each simulated device its own X-Forwarded-For78ENV79if [ -f scripts/fake-llm.mjs ]; then80 printf 'SPD_AI_MACLUSTR_URL=http://127.0.0.1:%s/v1\nSPD_AI_MACLUSTR_KEY=test-fake\nSPD_AI_MACLUSTR_MODEL=fake-qwen\nSPD_AI_MACLUSTR_MODELS=fake-qwen|Faux Qwen (test)|Alibaba, fake-mistral|Faux Mistral (test)|Mistral AI\n' "$LLM" > "$DATA/ai.env"81 nohup node scripts/fake-llm.mjs "$LLM" > "$DATA/llm.log" 2>&1 < /dev/null &82fi83. "$DATA/env.sh"84node --import tsx services/api/src/cli.ts init-key >/dev/null85[ -f scripts/textkit-build.sh ] && zsh scripts/textkit-build.sh | tail -1 # text extraction + OCR helper (2.0)86node --import tsx services/api/src/cli.ts migrate >/dev/null 2>&1 || true # 1.0 servers migrate at start87nohup node --import tsx services/api/src/main.ts > "$DATA/api.log" 2>&1 < /dev/null &88for _ in $(seq 1 60); do curl -fsS "http://127.0.0.1:$PORT/api/v1/health/live" >/dev/null 2>&1 && break; sleep 0.5; done89curl -fsS "http://127.0.0.1:$PORT/api/v1/health/live" || { tail -20 "$DATA/api.log"; exit 1; }; echo90curl -fsS "http://127.0.0.1:$PORT/api/v1/meta" || true; echo91REMOTE92 ssh "$NODE" "REF='$REF' PORT='$PORT' bash /tmp/spdrive-ios-test-up.sh < /dev/null"93 tunnel_up; creds94 curl -fsS "http://localhost:$PORT/api/v1/health/live"; echo ;;95creds) creds ;;96tunnel) tunnel_up; curl -fsS "http://localhost:$PORT/api/v1/health/live"; echo ;;97down)98 tunnel_down99 ssh "$NODE" "for p in \$(lsof -nP -iTCP:$PORT -sTCP:LISTEN -t 2>/dev/null) \$(lsof -nP -iTCP:8665 -sTCP:LISTEN -t 2>/dev/null); do kill \"\$p\" 2>/dev/null || true; done; echo stopped" ;;100*) echo "usage: $0 up|creds|tunnel|down"; exit 2 ;;101esac102