← back to NationalPaperHangers

scripts/run-e2e-prod-monitor.sh

74 lines

#!/usr/bin/env bash
#
# Runs the NPH e2e suite against live prod on a schedule.
# If any test fails, emails Steve via George with the tail of the output.
# Runs cleanly when prod is healthy; goes loud when something breaks.
#
# Triggered by ~/Library/LaunchAgents/com.steve.nph-e2e-prod.plist (every 6h).

set -u
PROJECT="$HOME/Projects/NationalPaperHangers"
LOG_DIR="$HOME/Library/Logs"
LOG="$LOG_DIR/nph-e2e-prod.log"
mkdir -p "$LOG_DIR"

cd "$PROJECT" || exit 1

TS=$(date -u +%Y-%m-%dT%H:%M:%SZ)
echo "" >> "$LOG"
echo "═══ run @ $TS ═══" >> "$LOG"

# Path needs Homebrew node etc.
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"

# Run the suite, capture both streams
OUT=$(BASE=https://nationalpaperhangers.com npm run test:e2e 2>&1)
EXIT=$?
echo "$OUT" >> "$LOG"
echo "exit=$EXIT" >> "$LOG"

# Parse summary line
SUMMARY=$(echo "$OUT" | grep -E '^\s+TOTAL' | tail -1 | sed 's/^[[:space:]]*//')

if [ "$EXIT" -ne 0 ]; then
  # Build the JSON payload via Python with stdin (no shell-quoting hell).
  PAYLOAD=$(mktemp)
  TS_LOCAL="$TS" SUMMARY_TXT="$SUMMARY" LOG_PATH="$LOG" HOST="$(hostname -s)" RUN_OUT="$OUT" \
    python3 - <<'PY' >"$PAYLOAD" 2>>"$LOG" || true
import json, os, html
out = os.environ.get('RUN_OUT','')
ts = os.environ.get('TS_LOCAL','')
summary = os.environ.get('SUMMARY_TXT','(no summary)')
log_path = os.environ.get('LOG_PATH','')
host = os.environ.get('HOST','')
tail_html = html.escape('\n'.join(out.splitlines()[-50:]))
body = (
  f"<p style='font-family:system-ui;color:#0a0a0a'><b>NPH e2e prod-monitor failed</b></p>"
  f"<p style='font-size:13px;color:#666'>Run @ {html.escape(ts)} UTC · summary: <code>{html.escape(summary)}</code></p>"
  f"<pre style='background:#0a0a0a;color:#f3f1ea;padding:14px 18px;font-size:11px;line-height:1.5;overflow-x:auto;border-radius:8px'>{tail_html}</pre>"
  f"<p style='font-size:11px;color:#888'>Full log: {html.escape(log_path)} · machine: {html.escape(host)}</p>"
)
print(json.dumps({
  'to': 'steve@designerwallcoverings.com',
  'subject': 'NPH e2e prod-monitor — FAIL',
  'body': body,
  'account': 'steve-office'
}))
PY

  # Send via George (Basic Auth from MEMORY.md: admin:DWSecure2024!)
  curl -s -X POST -H 'content-type: application/json' \
    -u 'admin:DWSecure2024!' \
    --data-binary "@$PAYLOAD" \
    http://localhost:9850/api/send \
    >>"$LOG" 2>&1
  rm -f "$PAYLOAD"
fi

# Trim log to last 5000 lines (keep ~few weeks of runs)
if [ -f "$LOG" ] && [ "$(wc -l < "$LOG")" -gt 5000 ]; then
  tail -5000 "$LOG" > "$LOG.tmp" && mv "$LOG.tmp" "$LOG"
fi

exit 0  # always 0 — failures are reported via email, launchd doesn't need to "fail"