Files
karaoke-gen/scripts/local-stack.sh
T
Andrew Beveridge ☄️andClaude Opus 5.5 ce57b4b598 fix(review): Tap To Sync partial pass keeps word order; local stack never reaches prod (v0.288.1) (#1237)
## Summary

### 1. Tap To Sync: a pass that stops before the last word
(customer-facing)
If a pass stopped before the last word (auto-stop about a second after
the line end, or the user pressing Stop), the untapped words after the
last tapped one kept their **old** timings, and those could now start
*before* the last tapped word. On gen's fake fixture line ("Nothing here
was ever transcribed", words 15.5 to 19.5, next line at 20.0), taps at
16.0, 17.2, 18.4 and 19.6 followed by auto-stop left "ever" at 19.6 and
"transcribed" at a stale 18.74 to 19.5. The two words were out of order,
and the video rendered them out of order.

- `frontend/hooks/useManualSync.ts`: new pure
`commitPartialSyncPass(words, synced)` runs in `cleanupManualSync`,
which is both the auto-stop path and the Stop toggle. It clears (sets
start/end to null) every untapped word after the last tapped one that
starts before it, then caps the last tapped word's end before the next
still-timed word in the line. The existing untimed-lyrics guard then
blocks finishing and points the user at those words. This is the same
rule as nomad-mobile's review-core `commitTapSync`
(`packages/review-core/src/tapSync.ts`, branch
`feat/sess-20261030-1930-e2e-health`). It uses the hook's existing
`syncedIndicesRef`.
- Backend backstop, `backend/services/timing_sanitizer.py`
`sanitize_corrections`: within a line, a word that starts before the
previous word's **start** is now treated like any other invalid start.
Small overlaps (a start a few ms before the previous word's end, common
in AI transcriptions) are left exactly as on main, so they get no move,
no WARNING and no edit-modal toast. It is **moved** to the previous
word's end, not cleared. I chose moving because that is how the module
already repairs bad or missing starts, because a cleared word would just
be refilled at prev_end on the next sanitize, and because nulls would
crash the render (`find_missing_timings` only guards the preview). The
module logs one aggregated `WARNING` per submission (never ERROR, since
this is an expected and repaired condition).
- `frontend/lib/lyrics-review/sanitizeWordTimings.ts` (the frontend
mirror, run when the edit modal opens) applies the same ordering rule,
with the same exemption for small overlaps.

### 2. The local stack must not reach production
nomad-mobile's E2E runs gen web inside an Android emulator's Chrome.
- `components/google-analytics.tsx`: GA (`G-D0E63LB9FY`) is no longer
loaded when the page host is localhost, 127.0.0.1 or 10.0.2.2. The check
runs at runtime after mount, so it also covers CI, and the server render
and first client render match.
- `lib/api/autoCorrect.ts` and `lib/api/customLyrics.ts` now use
`API_BASE_URL` from `lib/api.ts` instead of `NEXT_PUBLIC_API_BASE ??
'https://api.nomadkaraoke.com'`. That env var is set nowhere. A local
page now calls its own backend through relative URLs, so it never sends
its bearer token to production.
- New `lib/local-host.ts` is the single list of local hostnames, shared
by `lib/api.ts` and GA. `lib/api.ts` now also treats 10.0.2.2 as local.
- `scripts/local-stack.sh` backend_env: blank `FLACFETCH_API_URL=` and
`FLACFETCH_API_KEY=`, so direnv can't point the local stack at the real
flacfetch.

Version is bumped to 0.288.1.

## Testing
Each change had a failing test written first.
- Frontend jest (hook, sanitizer, edit modals, GA, API clients,
api/local-mode, auto-correct, custom lyrics): 13 suites, **150 passed**.
- New tests: 3 partial-pass hook tests (auto-stop, Stop, and the
keep-and-cap case), 2 sanitizer tests (the stale-word move, and a 20 ms
overlap left untouched), 4 GA tests, and 8 API-base tests (local hosts
use relative URLs, the production host uses the production API).
- Backend pytest: `test_timing_sanitizer.py` +
`test_jobs_corrections.py` **20 passed** (3 new, including a 20 ms
overlap that is left untouched), and `test_routes_review.py` **114
passed**.
- `customLyrics.test.ts`'s AbortSignal test now asserts that the
caller's abort reaches fetch, not that the signal object is identical. A
relative URL makes `apiFetch` manage the request and wrap the signal.

## Notes
- The web hook has no view of the next line, so the cap only uses words
in the same line. The backend sanitizer and segment bounds handle the
rest.
- The render-time `SegmentResizer._sanitize_segment_timings` is
unchanged (it clamps to the window only).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_012RYLVSLS3eVDqPctfYBMTt



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved lyric timing cleanup when recording or editing stops before
the final word. Out-of-order timings are corrected while small overlaps
are preserved, helping keep lyrics synchronized without changing valid
timings.
* Corrected out-of-order word timings during lyric cleanup and editing.
* **Improvements**
* Local app use now works more consistently across supported addresses,
including Android emulators, with API requests routed appropriately.
  * Analytics scripts are now omitted on local pages.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 20:28:19 -04:00

187 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
# gen "local stack" for E2E: the REAL backend on the Firestore + GCS emulators, with
# fake workers (FAKE_WORKERS=1) that walk jobs through their real states, the
# /api/test/* router, captured email and fake Stripe checkout. Web Playwright
# (frontend/e2e/local-stack) and mobile Maestro flows run against it.
#
# Usage:
# scripts/local-stack.sh up [--frontend] start emulators + backend (+ Next dev server)
# scripts/local-stack.sh down stop everything
# scripts/local-stack.sh status show what's running
# scripts/local-stack.sh logs [backend|frontend]
# scripts/local-stack.sh serve emulators + backend in the FOREGROUND (Playwright webServer, CI)
# scripts/local-stack.sh env print the backend env (for running uvicorn yourself)
#
# Ports (override via env): LOCAL_STACK_API_PORT (8010), LOCAL_STACK_WEB_PORT (3010).
# Several stacks on one machine (the mobile app's parallel E2E lanes) share the Firestore and GCS emulators but each
# needs its own ports, LOCAL_STACK_PROJECT (the Firestore project, default test-project: fake-config and data are
# per project, so one stack's global knobs don't leak into another), LOCAL_STACK_GCS_BUCKET (default test-bucket:
# the test reset empties the bucket, so each stack needs its own) and LOCAL_STACK_STATE_DIR (pids and logs,
# default .local-stack). `down` with LOCAL_STACK_KEEP_EMULATORS=1 leaves the shared emulators running.
# The backend binds 0.0.0.0 so the Android emulator (10.0.2.2) and iOS simulator
# can reach it; set LOCAL_STACK_API_URL to the address devices should use.
# See docs/TESTING.md § "Local stack".
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
STATE_DIR="${LOCAL_STACK_STATE_DIR:-$PROJECT_ROOT/.local-stack}"
mkdir -p "$STATE_DIR"
API_PORT="${LOCAL_STACK_API_PORT:-8010}"
WEB_PORT="${LOCAL_STACK_WEB_PORT:-3010}"
API_URL="${LOCAL_STACK_API_URL:-http://127.0.0.1:$API_PORT}"
WEB_URL="${LOCAL_STACK_WEB_URL:-http://127.0.0.1:$WEB_PORT}"
ADMIN_TOKEN="${LOCAL_STACK_ADMIN_TOKEN:-local-stack-admin}"
backend_env() {
cat <<EOF
FAKE_WORKERS=1
ENVIRONMENT=development
FIRESTORE_EMULATOR_HOST=127.0.0.1:8080
STORAGE_EMULATOR_HOST=http://127.0.0.1:4443
GOOGLE_CLOUD_PROJECT=${LOCAL_STACK_PROJECT:-test-project}
GCS_BUCKET_NAME=${LOCAL_STACK_GCS_BUCKET:-test-bucket}
FIRESTORE_COLLECTION=jobs
ADMIN_TOKENS=$ADMIN_TOKEN
FRONTEND_URL=$WEB_URL
LOCAL_STACK_API_URL=$API_URL
PORT=$API_PORT
ENABLE_CLOUD_TASKS=false
CREDIT_EVAL_ENABLED=false
ACCOUNT_DELETION_ENABLED=1
REVIEW_AUDIO_PROXY=1
MAX_SIGNUPS_PER_IP=100000
BOARD_MAX_SIGNUPS_PER_IP=100000
LOGIN_CODES_PER_IP_PER_HOUR=100000
KJBOX_PARTNER_SECRET=${KJBOX_PARTNER_SECRET:-local-stack-secret}
DECIDE_GEN_PARTNER_SECRET=${DECIDE_GEN_PARTNER_SECRET:-local-stack-secret}
KARAOKE_DECIDE_API_URL=${KARAOKE_DECIDE_API_URL:-http://127.0.0.1:8030}
POSTMARK_SERVER_TOKEN=
STRIPE_SECRET_KEY=
FLACFETCH_API_URL=
FLACFETCH_API_KEY=
EOF
}
export_backend_env() {
while IFS= read -r line; do
export "${line?}"
done < <(backend_env)
}
is_running() {
local pidfile="$STATE_DIR/$1.pid"
[[ -f "$pidfile" ]] && kill -0 "$(cat "$pidfile")" 2>/dev/null
}
wait_for() {
local url="$1" name="$2" tries="${3:-90}"
for _ in $(seq 1 "$tries"); do
if curl -sf "$url" >/dev/null 2>&1; then
echo "✅ $name ready ($url)"
return 0
fi
sleep 1
done
echo "❌ $name did not become ready at $url"
return 1
}
start_backend() {
if is_running backend; then
echo "⚠️ backend already running (pid $(cat "$STATE_DIR/backend.pid"))"
return
fi
export_backend_env
(cd "$PROJECT_ROOT" && poetry run python "$SCRIPT_DIR/seed-local-data.py")
echo "🌐 Starting backend on :$API_PORT (FAKE_WORKERS=1)"
(
cd "$PROJECT_ROOT"
nohup poetry run uvicorn backend.main:app --host 0.0.0.0 --port "$API_PORT" \
> "$STATE_DIR/backend.log" 2>&1 &
echo $! > "$STATE_DIR/backend.pid"
)
if ! wait_for "http://127.0.0.1:$API_PORT/api/test/status" backend 120; then
tail -40 "$STATE_DIR/backend.log"
exit 1
fi
}
start_frontend() {
if is_running frontend; then
echo "⚠️ frontend already running (pid $(cat "$STATE_DIR/frontend.pid"))"
return
fi
echo "🖥️ Starting Next dev server on :$WEB_PORT (BACKEND_URL=$API_URL)"
(
cd "$PROJECT_ROOT/frontend"
BACKEND_URL="http://127.0.0.1:$API_PORT" nohup npx next dev -p "$WEB_PORT" -H 127.0.0.1 \
> "$STATE_DIR/frontend.log" 2>&1 &
echo $! > "$STATE_DIR/frontend.pid"
)
wait_for "$WEB_URL/" frontend 180
}
stop_pid() {
local name="$1" pidfile="$STATE_DIR/$1.pid"
if is_running "$name"; then
local pid
pid="$(cat "$pidfile")"
# Kill the process group too (poetry/npx spawn children).
pkill -TERM -P "$pid" 2>/dev/null || true
kill "$pid" 2>/dev/null || true
echo "🛑 stopped $name (pid $pid)"
fi
rm -f "$pidfile"
}
cmd="${1:-}"
shift || true
case "$cmd" in
up)
"$SCRIPT_DIR/start-emulators.sh"
start_backend
if [[ "${1:-}" == "--frontend" ]]; then
start_frontend
fi
echo ""
echo "Local stack up:"
echo " API $API_URL (docs: $API_URL/docs, test API: $API_URL/api/test/status)"
is_running frontend && echo " Web $WEB_URL"
echo " Admin X-Admin-Token: $ADMIN_TOKEN"
echo " Logs scripts/local-stack.sh logs backend"
;;
down)
stop_pid frontend
stop_pid backend
[[ "${LOCAL_STACK_KEEP_EMULATORS:-}" == 1 ]] || "$SCRIPT_DIR/stop-emulators.sh" || true
;;
serve)
"$SCRIPT_DIR/start-emulators.sh"
export_backend_env
(cd "$PROJECT_ROOT" && poetry run python "$SCRIPT_DIR/seed-local-data.py")
cd "$PROJECT_ROOT"
echo "🌐 Backend on :$API_PORT (FAKE_WORKERS=1), logging to $STATE_DIR/backend.log"
exec poetry run uvicorn backend.main:app --host 0.0.0.0 --port "$API_PORT" \
> "$STATE_DIR/backend.log" 2>&1
;;
status)
for name in backend frontend; do
if is_running "$name"; then echo "$name: running (pid $(cat "$STATE_DIR/$name.pid"))"; else echo "$name: stopped"; fi
done
curl -sf "http://127.0.0.1:$API_PORT/api/test/status" && echo || true
;;
logs)
tail -f "$STATE_DIR/${1:-backend}.log"
;;
env)
backend_env
;;
*)
sed -n '2,20p' "$0"
exit 1
;;
esac