Repository Public
1#!/bin/bash2# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/backend-dev.sh4# Description: SP Music — backend-dev.5# A real SP Music backend for the app's functional tests, on this Mac: the cluster's code (a clone of sp-music.git6# in ~/dev/sp-music-testbed), its own PostgreSQL database, its own storage folder, real ffmpeg, real worker.7# Nothing here touches production.8# Scripts/backend-dev.sh start fresh database + library (synthetic audio only), prints the environment for tests9# Scripts/backend-dev.sh stop10# Scripts/backend-dev.sh env prints the environment of the running instance11set -euo pipefail12export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"13SELF="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"14BED="${SPMUSIC_TESTBED:-$HOME/dev/sp-music-testbed}"15PORT="${SPMUSIC_TEST_PORT:-8695}"16DB="sp_music_macdev"17STATE="/tmp/spmusic-backend-dev"18ENVF="$STATE/env"1920stop() {21 for f in "$STATE"/web.pid "$STATE"/worker.pid; do [ -f "$f" ] && kill "$(cat "$f")" 2>/dev/null || true; rm -f "$f"; done22 local p; p="$(lsof -tiTCP:"$PORT" -sTCP:LISTEN 2>/dev/null || true)"; [ -n "$p" ] && kill $p 2>/dev/null || true23}24web() { # (re)starts only the web process: used by tests that take the server down and bring it back25 cd "$BED"; set -a; . "$ENVF.server"; set +a26 PORT="$PORT" HOST=127.0.0.1 nohup node node_modules/tsx/dist/cli.mjs server.ts > "$STATE/web.log" 2>&1 & echo $! > "$STATE/web.pid"27 for _ in $(seq 1 80); do curl -fsS "http://127.0.0.1:$PORT/api/health" >/dev/null 2>&1 && return 0; sleep 0.25; done28 echo "server did not start"; tail -20 "$STATE/web.log"; return 129}3031case "${1:-}" in32stop) stop; echo stopped ;;33web-stop) [ -f "$STATE/web.pid" ] && kill "$(cat "$STATE/web.pid")" 2>/dev/null || true; rm -f "$STATE/web.pid" ;;34web-start) web ;;35env) cat "$ENVF" ;;36start)37 [ -d "$BED/node_modules" ] || { echo "testbed missing: clone sp-music.git into $BED and run pnpm install && next build"; exit 1; }38 stop; rm -rf "$STATE"; mkdir -p "$STATE/library" "$STATE/music"39 cat > "$ENVF.server" <<EOT40NODE_ENV=production41NEXT_TELEMETRY_DISABLED=142APP_URL=http://127.0.0.1:$PORT43APP_SECRET=macdev-secret-macdev-secret-macdev-secret-012345678944OWNER_EMAIL=owner@example.test45OWNER_NAME=Testeur46DATABASE_URL=postgres://127.0.0.1:5432/$DB47STORAGE_ROOT=$STATE/library48SETUP_URL_FILE=$STATE/setup-url.txt49RENDITIONS=auto50LYRICS=off51UPLOAD_MAX_FILE_MB=20052EOT53 dropdb --if-exists "$DB" 2>/dev/null; createdb "$DB"54 cd "$BED"; set -a; . "$ENVF.server"; set +a55 node_modules/.bin/tsx scripts/migrate.ts >/dev/null56 node_modules/.bin/tsx scripts/bootstrap.ts >/dev/null57 # The production build is made once per testbed revision (it needs the environment above).58 REV="$(git rev-parse HEAD)"59 if [ ! -f .next/BUILD_ID ] || [ "$(cat .next/.spmusic-rev 2>/dev/null)" != "$REV" ]; then node_modules/.bin/next build > "$STATE/build.log" 2>&1 || { tail -20 "$STATE/build.log"; exit 1; }; echo "$REV" > .next/.spmusic-rev; fi60 M="$STATE/music"; ff() { ffmpeg -v error -nostdin -y "$@"; }61 # Synthetic audio only: sine waves and noise. No recording of any kind.62 ff -f lavfi -i "color=c=0x8b3dff:s=640x640" -frames:v 1 "$M/cover-a.png"63 ff -f lavfi -i "color=c=0xff7a1a:s=640x640" -frames:v 1 "$M/cover-b.png"64 for i in 1 2 3 4; do65 ff -f lavfi -i "sine=frequency=$((220 * i)):duration=20:sample_rate=44100" -i "$M/cover-a.png" -map 0:a -map 1:v -ac 2 -c:a flac -c:v mjpeg -disposition:v attached_pic \66 -metadata title="Onde $i" -metadata artist="Les Sinusoïdes" -metadata album="Fréquences pures" -metadata album_artist="Les Sinusoïdes" -metadata track="$i/4" -metadata date=2024 -metadata genre="Électronique" "$M/a$i.flac"67 done68 ff -f lavfi -i "sine=frequency=330:duration=15" -i "$M/cover-b.png" -map 0:a -map 1:v -ac 2 -c:a libmp3lame -b:a 192k -c:v mjpeg -disposition:v attached_pic -id3v2_version 3 \69 -metadata title="Été compressé" -metadata artist="Autre Artiste" -metadata album="Formats variés" -metadata album_artist="Autre Artiste" -metadata track=1 -metadata date=2021 "$M/b1.mp3"70 ff -f lavfi -i "sine=frequency=550:duration=15" -ac 2 -c:a alac -metadata title="Sans perte Apple" -metadata artist="Autre Artiste" -metadata album="Formats variés" -metadata album_artist="Autre Artiste" -metadata track=2 -metadata date=2021 "$M/b2.m4a"71 ff -f lavfi -i "sine=frequency=660:duration=12:sample_rate=96000" -ac 2 -c:a flac -sample_fmt s32 -bits_per_raw_sample 24 -metadata title="Haute résolution" -metadata artist="Autre Artiste" -metadata album="Formats variés" -metadata album_artist="Autre Artiste" -metadata track=3 -metadata date=2021 "$M/b3.flac"72 # Incompressible and large (about 20 MB): the file used to cut a transfer in the middle.73 ff -f lavfi -i "anoisesrc=d=110:c=pink:r=44100:a=0.4" -ac 2 -c:a flac -metadata title="Long bruit rose" -metadata artist="Générateur" -metadata album="Gros fichiers" -metadata album_artist="Générateur" -metadata track=1 "$M/c1.flac"74 ff -f lavfi -i "anoisesrc=d=60:c=white:r=44100:a=0.3" -ac 2 -c:a flac -metadata title="Bruit blanc" -metadata artist="Générateur" -metadata album="Gros fichiers" -metadata album_artist="Générateur" -metadata track=2 "$M/c2.flac"75 nohup node node_modules/tsx/dist/cli.mjs src/worker/index.ts > "$STATE/worker.log" 2>&1 & echo $! > "$STATE/worker.pid"76 web77 node_modules/.bin/tsx scripts/import.ts "$M" --wait > "$STATE/import.log" 2>&1 || { tail -20 "$STATE/import.log"; exit 1; }78 cat > "$ENVF" <<EOT79export SPMUSIC_TEST_URL=http://127.0.0.1:$PORT80export SPMUSIC_TEST_DB=postgres://127.0.0.1:5432/$DB81export SPMUSIC_TEST_PORT=$PORT82export SPMUSIC_TEST_SCRIPT=$SELF83EOT84 echo "tracks: $(psql -Atc 'select count(*) from tracks' "$DATABASE_URL"), albums: $(psql -Atc 'select count(*) from albums' "$DATABASE_URL")"85 cat "$ENVF" ;;86*) echo "usage: $0 start|stop|env|web-stop|web-start"; exit 2 ;;87esac88