Repository Public
1#!/usr/bin/env bash2# SP Music iOS3# File: Scripts/test-server.sh4# Description: Disposable backend for the automated tests (own clone, database, port).5#6# Author: Simon-Pierre Boucher7# Contact: contact@spboucher.ai8# SP Music iOS — disposable backend for the automated tests.9#10# Scripts/test-server.sh up clone/refresh the backend (branch $SPMUSIC_BACKEND_REF), fresh database, synthetic11# library (sine waves, every supported container), start it on m5ma:8692, open an SSH12# tunnel to localhost:8692, write test credentials to ~/.config/spmusic-ios-test/13# Scripts/test-server.sh creds new session token + recovery codes (the server keeps running)14# Scripts/test-server.sh down stop the server and the tunnel15#16# Never touches production: its own clone (~/dev/sp-music-ios-test), database (sp_music_iostest), storage and port.17# No commercial recording is involved: the audio is generated by ffmpeg.18set -euo pipefail19NODE="${SPMUSIC_TEST_NODE:-m5ma}"20REF="${SPMUSIC_BACKEND_REF:-native-api}"21PORT=869222CONF="$HOME/.config/spmusic-ios-test"23mkdir -p "$CONF" && chmod 700 "$CONF"2425tunnel_down() { if [ -f "$CONF/tunnel.pid" ]; then kill "$(cat "$CONF/tunnel.pid")" 2>/dev/null || true; rm -f "$CONF/tunnel.pid"; fi; }26tunnel_up() {27 tunnel_down28 for p in $(lsof -nP -iTCP:$PORT -sTCP:LISTEN -t 2>/dev/null); do kill "$p" 2>/dev/null || true; done29 # Its own connection (no shared ControlMaster, which would close the forward when it expires).30 ssh -f -N -o ControlMaster=no -o ControlPath=none -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -L "$PORT:127.0.0.1:$PORT" "$NODE"31 lsof -nP -iTCP:$PORT -sTCP:LISTEN -t | head -1 > "$CONF/tunnel.pid"32}3334creds() {35 ssh "$NODE" 'bash -s' <<'REMOTE' > "$CONF/credentials.env"36set -euo pipefail37export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"38DB=sp_music_iostest39tok() { python3 -c "import secrets;print(secrets.token_urlsafe(32))"; }40sha() { printf %s "$1" | shasum -a 256 | cut -d" " -f1; }41TOKEN=$(tok); CSRF=$(tok); CODE1=$(python3 -c "import secrets;h=secrets.token_hex(6);print(h[:4]+'-'+h[4:8]+'-'+h[8:])"); CODE2=$(python3 -c "import secrets;h=secrets.token_hex(6);print(h[:4]+'-'+h[4:8]+'-'+h[8:])")42psql -q -d "$DB" -v ON_ERROR_STOP=1 -v th="$(sha "$TOKEN")" -v csrf="$CSRF" -v c1="$(sha "$CODE1")" -v c2="$(sha "$CODE2")" >/dev/null <<'SQL'43INSERT INTO sessions (user_id, token_hash, csrf_token, method, user_agent, ip, expires_at)44 SELECT id, :'th', :'csrf', 'device', 'SPMusic-iOS tests', 'local', now() + interval '30 days' FROM users WHERE is_owner;45INSERT INTO recovery_codes (user_id, code_hash) SELECT id, :'c1' FROM users WHERE is_owner;46INSERT INTO recovery_codes (user_id, code_hash) SELECT id, :'c2' FROM users WHERE is_owner;47DELETE FROM rate_limits;48SQL49printf 'SPMUSIC_TEST_URL=http://localhost:8692\nSPMUSIC_TEST_SESSION=%s\nSPMUSIC_TEST_RECOVERY_1=%s\nSPMUSIC_TEST_RECOVERY_2=%s\n' "$TOKEN" "$CODE1" "$CODE2"50REMOTE51 chmod 600 "$CONF/credentials.env"52 echo "credentials written to $CONF/credentials.env"53}5455case "${1:-}" in56up)57 # The script is copied first and run with no stdin: a command that reads stdin can then not swallow its remaining lines.58 ssh "$NODE" 'cat > /tmp/spmusic-ios-test-up.sh' <<'REMOTE'59set -euo pipefail60export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"61D=~/dev/sp-music-ios-test; DATA=~/dev/sp-music-ios-test-data; DB=sp_music_iostest62for p in $(lsof -nP -iTCP:$PORT -sTCP:LISTEN -t 2>/dev/null); do kill "$p" 2>/dev/null || true; done63[ -f "$DATA/worker.pid" ] && kill "$(cat "$DATA/worker.pid")" 2>/dev/null || true64if [ -d "$D/.git" ]; then git -C "$D" fetch -q origin && git -C "$D" checkout -q --detach "origin/$REF"; else git clone -q gitsrv:srv/git/sp-music.git "$D" && git -C "$D" checkout -q --detach "origin/$REF"; fi65cd "$D"; echo "backend: $(git log --oneline -1)"66[ -d node_modules ] || pnpm install --frozen-lockfile --silent67rm -rf "$DATA"; mkdir -p "$DATA/library" "$DATA/fixtures"68dropdb --if-exists "$DB"; createdb "$DB"69cat > .env <<ENV70NODE_ENV=production71APP_URL=http://localhost:$PORT72PORT=$PORT73HOST=127.0.0.174APP_SECRET=ios-test-secret-ios-test-secret-0123456789abcdef75OWNER_EMAIL=owner@example.test76OWNER_NAME=Propriétaire77DATABASE_URL=postgres://127.0.0.1:5432/$DB78STORAGE_ROOT=$DATA/library79SETUP_URL_FILE=$DATA/setup-url.txt80RENDITIONS=auto81LYRICS=off82APPLE_APP_IDS=3YM54G49SN.ai.spboucher.spmusic,3YM54G49SN.ai.spboucher.spmusic.ios83ENV84F="$DATA/fixtures"; ff() { ffmpeg -v error -nostdin -y "$@"; }85ff -f lavfi -i "gradients=s=800x800:c0=0xff6a3d:c1=0xffb347:x0=0:y0=0:x1=800:y1=800" -frames:v 1 "$F/cover-a.png"86ff -f lavfi -i "gradients=s=800x800:c0=0x38bdf8:c1=0x5eead4:x0=0:y0=0:x1=800:y1=800" -frames:v 1 "$F/cover-b.png"87ff -f lavfi -i "gradients=s=800x800:c0=0xa78bfa:c1=0xf0abfc:x0=0:y0=0:x1=800:y1=800" -frames:v 1 "$F/cover-c.png"88# Album A — FLAC 16/44.1, six tracks of 40 s (long enough to seek, scrub and background).89mkdir -p "$F/a" "$F/b" "$F/c"90i=0; for f in 220 262 330 392 440 523; do i=$((i+1))91 ff -f lavfi -i "sine=frequency=$f:duration=40" -i "$F/cover-a.png" -map 0:a -map 1:v -c:a flac -sample_fmt s16 -ar 44100 -ac 2 -c:v mjpeg -disposition:v attached_pic \92 -metadata title="Onde $f Hz" -metadata artist="Les Sinusoïdes" -metadata album="Fréquences" -metadata album_artist="Les Sinusoïdes" -metadata track="$i/6" -metadata date=2024 -metadata genre="Électronique" "$F/a/0$i.flac"93done94# Album B — one track per container/codec the library may hold.95mk() { ff -f lavfi -i "sine=frequency=$1:duration=30" -i "$F/cover-b.png" -map 0:a "${@:4}" -metadata title="$2" -metadata artist="Formats Réunis" -metadata album="Tous les formats" -metadata album_artist="Formats Réunis" -metadata track="$3" -metadata date=2025 -metadata genre="Test" "$F/b/$3-$2.$EXT"; }96EXT=flac mk 300 "FLAC 24-48" 1 -map 1:v -c:a flac -sample_fmt s32 -bits_per_raw_sample 24 -ar 48000 -ac 2 -c:v mjpeg -disposition:v attached_pic97EXT=flac mk 320 "FLAC 24-96" 2 -c:a flac -sample_fmt s32 -bits_per_raw_sample 24 -ar 96000 -ac 298EXT=mp3 mk 340 "MP3 320" 3 -c:a libmp3lame -b:a 320k -ar 44100 -ac 299EXT=m4a mk 360 "AAC 256" 4 -c:a aac -b:a 256k -ar 44100 -ac 2100EXT=m4a mk 380 "ALAC 16-44" 5 -c:a alac -ar 44100 -ac 2101EXT=wav mk 400 "WAV 16-44" 6 -c:a pcm_s16le -ar 44100 -ac 2102EXT=aiff mk 420 "AIFF 16-44" 7 -c:a pcm_s16be -ar 44100 -ac 2103# Album C — another artist, three short tracks (playlist and queue tests).104i=0; for f in 600 700 800; do i=$((i+1))105 ff -f lavfi -i "sine=frequency=$f:duration=12" -i "$F/cover-c.png" -map 0:a -map 1:v -c:a flac -ar 44100 -ac 2 -c:v mjpeg -disposition:v attached_pic \106 -metadata title="Éclat $i" -metadata artist="Hélène Côté" -metadata album="Aurores" -metadata album_artist="Hélène Côté" -metadata track="$i/3" -metadata date=2023 -metadata genre="Ambiant" "$F/c/0$i.flac"107done108# One large incompressible file (white noise, ~40 MB) so that pause, resume and progress can be observed.109mkdir -p "$F/d"110ff -f lavfi -i "anoisesrc=d=240:c=white:r=44100:a=0.05" -ac 2 -c:a flac -metadata title="Bruit blanc" -metadata artist="Les Sinusoïdes" -metadata album="Gros fichier" -metadata album_artist="Les Sinusoïdes" -metadata track=1 -metadata date=2026 "$F/d/01.flac"111node_modules/.bin/tsx scripts/migrate.ts112node_modules/.bin/tsx scripts/bootstrap.ts113node_modules/.bin/next build > "$DATA/build.log" 2>&1 || { tail -30 "$DATA/build.log"; exit 1; }114nohup node node_modules/tsx/dist/cli.mjs server.ts > "$DATA/web.log" 2>&1 &115nohup node node_modules/tsx/dist/cli.mjs src/worker/index.ts > "$DATA/worker.log" 2>&1 & echo $! > "$DATA/worker.pid"116for _ in $(seq 1 60); do curl -fsS "http://127.0.0.1:$PORT/api/health" >/dev/null 2>&1 && break; sleep 0.5; done117curl -fsS "http://127.0.0.1:$PORT/api/health"; echo118node_modules/.bin/tsx scripts/import.ts "$F/a" "$F/b" "$F/c" "$F/d" --wait119# The ALAC and AIFF renditions are prepared by the worker after import: wait for them.120for _ in $(seq 1 60); do n=$(psql -Atc "select count(*) from tracks where compat_state in ('queued','working')" "$DB"); [ "$n" = 0 ] && break; sleep 1; done121psql -Atc "select 'tracks', count(*) from tracks union all select 'albums', count(*) from albums union all select 'renditions', count(*) from media_assets where kind='rendition'" "$DB"122REMOTE123 ssh "$NODE" "REF='$REF' PORT='$PORT' bash /tmp/spmusic-ios-test-up.sh < /dev/null"124 tunnel_up; creds125 curl -fsS "http://localhost:$PORT/api/health"; echo ;;126creds) creds ;;127tunnel) tunnel_up; curl -fsS "http://localhost:$PORT/api/health"; echo ;;128down)129 tunnel_down130 ssh "$NODE" 'for p in $(lsof -nP -iTCP:8692 -sTCP:LISTEN -t 2>/dev/null); do kill "$p" 2>/dev/null || true; done; [ -f ~/dev/sp-music-ios-test-data/worker.pid ] && kill "$(cat ~/dev/sp-music-ios-test-data/worker.pid)" 2>/dev/null || true; echo stopped' ;;131*) echo "usage: $0 up|creds|tunnel|down"; exit 2 ;;132esac133