fix(testing): smoke-recovery.sh — correct userData path, http.server scenarios, missing helper

Three issues found while running the script for the first time:

1. userData path was hardcoded to ~/Library/Application Support/Backspace.
   Electron actually uses ~/Library/Application Support/@backspace/desktop
   (derived from package.json#name when app.name is not set explicitly).
   Scenario 2's bad URL was being written to the wrong location, so the app
   never loaded it and recovery never entered. Path corrected.

2. Scenario 4's VITE_FORCE_BOOT_STALL approach didn't actually stall anything
   in practice: the desktop loads the user's REMOTE instance URL, which
   serves its own web bundle from the server. The locally rebuilt stalled
   bundle is never used at runtime. Replaced with a tiny static HTML page
   served via `python3 -m http.server` and pointed at via BACKSPACE_URL.
   The page has no window.backspace integration, so it never pings,
   guaranteeing the boot timer fires. No web/desktop rebuilds needed.

3. Scenario 13 (positive control: rendererReady ping disarms the timer)
   used assert_no_log which was never defined. The function is now defined
   alongside wait_for_log. Scenario 13 now correctly verifies that a page
   that DOES call rendererReady never triggers recovery.

All three automated scenarios now pass:
  ✓ Scenario 2:  load-failed recovery entered
  ✓ Scenario 4:  renderer-stalled recovery entered
  ✓ Scenario 13: boot timer correctly disarmed by rendererReady ping
This commit is contained in:
Jannis Braun
2026-05-03 14:02:05 +02:00
parent 833dedd4a1
commit 309484dca9
+154 -70
View File
@@ -3,34 +3,38 @@
# Automated smoke tests for the Electron Recovery Mode subsystem. # Automated smoke tests for the Electron Recovery Mode subsystem.
# Covers scenarios that don't require GUI interaction or sudo. # Covers scenarios that don't require GUI interaction or sudo.
# #
# Manual scenarios (run separately, see docs/superpowers/specs/...): # Automated scenarios (this script):
# 1 network failure (needs sudo to toggle Wi-Fi) # 2 bad URL → load-failed recovery
# 3 renderer crash via DevTools # 4 boot stall (static page that never pings) → renderer-stalled recovery
# 5,6 native notification visibility # 13 positive control: rendererReady ping disarms boot timer (no recovery)
# 7 tray menu interaction #
# 8,9 recovery button clicks # Manual scenarios (run yourself in front of the UI — see spec §9):
# 1 network failure (needs sudo to toggle Wi-Fi)
# 3 renderer crash via DevTools (process.crash())
# 5,6 native notification visible/not-visible (OS-level rendering)
# 7 tray menu interaction
# 8,9 recovery button clicks (Reload success / re-fail)
# 10 corrupt recovery.html (requires asar manipulation) # 10 corrupt recovery.html (requires asar manipulation)
# 11 force-kill recovery # 11 force-kill + recovery + Restart-to-Install (verify electron-updater behavior)
# 12 hidden autostart # 12 hidden autostart + boot failure
# 14 SPA navigation # 14 SPA navigation (logged-in session, channel switches don't trip timer)
# 15 real Windows-incident reproduction (needs Vite running) # 15 real Windows-incident reproduction (build N, edit web bundle, run Vite)
# #
# Automated scenarios: # Strategy notes:
# 2 bad URL → load-failed # - Scenarios 4 and 13 use a local Python http.server serving a tiny static page,
# 4 boot stall (VITE_FORCE_BOOT_STALL build) → renderer-stalled # pointed at via BACKSPACE_URL. This bypasses the need to build a stalled web
# 13 ErrorBoundary path doesn't trigger boot timer (negative test — see note below) # bundle: scenario 4 omits the rendererReady call; scenario 13 includes it.
# # - Scenario 2 writes a bad URL to instance-url.json and lets the normal load
# Note on scenario 13: The VITE_FORCE_BOOT_STALL gate suppresses the ErrorBoundary # path fail (did-fail-load → load-failed recovery).
# ping as well, so an isolated scenario-13 negative test (ErrorBoundary DOES ping # - All scenarios grep stderr for [recovery] entered: ... lines emitted by
# in normal builds) requires a separate non-stalled build run. This script covers # enterRecoveryMode in recovery.ts.
# the stalled-build scenario; the positive scenario-13 case is tested manually.
set -euo pipefail set -euo pipefail
# ─── Config ────────────────────────────────────────────────────────────── # ─── Config ──────────────────────────────────────────────────────────────
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
APP_BINARY="$REPO_ROOT/packages/desktop/dist-electron/mac-arm64/Backspace.app/Contents/MacOS/Backspace" APP_BINARY="$REPO_ROOT/packages/desktop/dist-electron/mac-arm64/Backspace.app/Contents/MacOS/Backspace"
USER_DATA_DIR="$HOME/Library/Application Support/Backspace" USER_DATA_DIR="$HOME/Library/Application Support/@backspace/desktop"
INSTANCE_URL_FILE="$USER_DATA_DIR/instance-url.json" INSTANCE_URL_FILE="$USER_DATA_DIR/instance-url.json"
INSTANCE_URL_BACKUP="$INSTANCE_URL_FILE.smoketest-backup" INSTANCE_URL_BACKUP="$INSTANCE_URL_FILE.smoketest-backup"
TMP_DIR="$(mktemp -d -t backspace-smoke.XXXXXX)" TMP_DIR="$(mktemp -d -t backspace-smoke.XXXXXX)"
@@ -100,6 +104,17 @@ wait_for_log() {
done done
} }
# Returns 0 if the regex is NOT present in the log file (negative assertion),
# 1 if it is present.
assert_no_log() {
local logfile="$1"
local pattern="$2"
if grep -Eq "$pattern" "$logfile" 2>/dev/null; then
return 1
fi
return 0
}
kill_app() { kill_app() {
local pid="$1" local pid="$1"
kill "$pid" 2>/dev/null || true kill "$pid" 2>/dev/null || true
@@ -142,47 +157,50 @@ scenario_2_bad_url() {
} }
scenario_4_boot_stall() { scenario_4_boot_stall() {
log "Scenario 4: Boot stall (VITE_FORCE_BOOT_STALL build) → recovery (renderer-stalled)" log "Scenario 4: Boot stall → recovery (renderer-stalled)"
log "Building stall-variant web bundle..." log "Strategy: serve a tiny static HTML page locally that has no window.backspace"
log " integration → renderer loads but never pings → 20s → boot timer fires."
if (cd "$REPO_ROOT" && VITE_FORCE_BOOT_STALL=1 pnpm --filter @backspace/web build > "$TMP_DIR/web-build.log" 2>&1); then # The desktop loads remote URLs via mainWindow.loadURL. Setting BACKSPACE_URL
log " web build complete" # at launch overrides the saved instance URL. We serve a minimal HTML page
else # that doesn't import the web bundle at all — guaranteed to never call
bad "Scenario 4: VITE_FORCE_BOOT_STALL web build failed (see $TMP_DIR/web-build.log)" # rendererReady(). No web/desktop rebuilds needed.
return local stall_dir="$TMP_DIR/stall-page"
fi mkdir -p "$stall_dir"
cat > "$stall_dir/index.html" <<'HTML'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Boot Stall Test</title></head>
<body style="background:#13131a;color:#efefef;font-family:sans-serif;padding:2rem;">
<h1>Boot stall test page</h1>
<p>Renderer is alive but never calls window.backspace.rendererReady().
The main-process boot timer should fire after 20s and trigger recovery.</p>
</body>
</html>
HTML
log "Rebuilding desktop app with stalled bundle..." # Start a local HTTP server on a random high port to avoid collisions.
if (cd "$REPO_ROOT" && pnpm --filter @backspace/desktop build > "$TMP_DIR/desktop-build.log" 2>&1); then local port=8765
log " desktop build complete" python3 -m http.server "$port" --bind 127.0.0.1 -d "$stall_dir" > "$TMP_DIR/httpd.log" 2>&1 &
else local httpd_pid=$!
bad "Scenario 4: desktop rebuild failed (see $TMP_DIR/desktop-build.log)"
# Still rebuild the normal web bundle before returning
log "Rebuilding web bundle without VITE_FORCE_BOOT_STALL (cleanup)..."
(cd "$REPO_ROOT" && pnpm --filter @backspace/web build > "$TMP_DIR/web-rebuild-cleanup.log" 2>&1) || true
(cd "$REPO_ROOT" && pnpm --filter @backspace/desktop build > "$TMP_DIR/desktop-rebuild-cleanup.log" 2>&1) || true
return
fi
# Use the user's saved URL so we get past did-fail-load and into the # Poll for readiness (up to 5s).
# actual boot-stall path. If none is saved, the scenario can't run. local ready=0
backup_instance_url for _ in 1 2 3 4 5 6 7 8 9 10; do
if [[ -f "$INSTANCE_URL_BACKUP" ]]; then if curl -sf "http://127.0.0.1:$port/" > /dev/null 2>&1; then
cp "$INSTANCE_URL_BACKUP" "$INSTANCE_URL_FILE" ready=1; break
else fi
log " No saved instance URL found — Scenario 4 needs a working URL to exercise" sleep 0.5
log " the boot stall path. Run the app once, pick an instance, then re-run." done
bad "Scenario 4: no saved URL to test against" if [[ "$ready" != "1" ]]; then
# Rebuild normal bundle before returning bad "Scenario 4: local HTTP server failed to start on port $port"
log "Rebuilding web bundle without VITE_FORCE_BOOT_STALL (cleanup)..." kill "$httpd_pid" 2>/dev/null || true
(cd "$REPO_ROOT" && pnpm --filter @backspace/web build > "$TMP_DIR/web-rebuild-cleanup.log" 2>&1) || true
(cd "$REPO_ROOT" && pnpm --filter @backspace/desktop build > "$TMP_DIR/desktop-rebuild-cleanup.log" 2>&1) || true
return return
fi fi
local logfile="$TMP_DIR/scenario4.log" local logfile="$TMP_DIR/scenario4.log"
local pid BACKSPACE_URL="http://127.0.0.1:$port/" "$APP_BINARY" > "$logfile" 2>&1 &
pid=$(launch "$logfile") local pid=$!
log " Waiting up to 25s for renderer-stalled timer to fire..." log " Waiting up to 25s for renderer-stalled timer to fire..."
if wait_for_log "$logfile" '\[recovery\] entered: renderer-stalled' 25; then if wait_for_log "$logfile" '\[recovery\] entered: renderer-stalled' 25; then
@@ -194,27 +212,92 @@ scenario_4_boot_stall() {
fi fi
kill_app "$pid" kill_app "$pid"
kill "$httpd_pid" 2>/dev/null || true
# Wait for httpd to actually exit so the port is free for re-runs.
wait "$httpd_pid" 2>/dev/null || true
}
# Restore the user's instance-url.json scenario_13_ping_disarms_timer() {
if [[ -f "$INSTANCE_URL_BACKUP" ]]; then log "Scenario 13 (positive control): rendererReady ping disarms the boot timer"
mv "$INSTANCE_URL_BACKUP" "$INSTANCE_URL_FILE" log "Strategy: serve a page that DOES call window.backspace.rendererReady() →"
else log " verify NO recovery entry within 25s (timer correctly disarmed)."
rm -f "$INSTANCE_URL_FILE"
local ready_dir="$TMP_DIR/ready-page"
mkdir -p "$ready_dir"
cat > "$ready_dir/index.html" <<'HTML'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Boot Ready Test</title></head>
<body style="background:#13131a;color:#efefef;font-family:sans-serif;padding:2rem;">
<h1>Boot ready test page</h1>
<p>Renderer calls window.backspace.rendererReady() — the main-process
boot timer should be disarmed; recovery must NOT enter.</p>
<script>
// Marker fetches let the smoke harness verify each branch via the http.server log.
fetch('/_marker_script_ran').catch(function(){});
function tryPing() {
if (typeof window.backspace !== 'undefined' &&
typeof window.backspace.rendererReady === 'function') {
window.backspace.rendererReady();
fetch('/_marker_ping_sent').catch(function(){});
return true;
}
return false;
}
if (!tryPing()) {
// Preload may attach window.backspace asynchronously in some configs;
// retry briefly before declaring it absent.
var attempts = 0;
var iv = setInterval(function() {
attempts++;
if (tryPing() || attempts > 20) {
clearInterval(iv);
if (attempts > 20) fetch('/_marker_no_bridge').catch(function(){});
}
}, 100);
}
</script>
</body>
</html>
HTML
local port=8766
python3 -m http.server "$port" --bind 127.0.0.1 -d "$ready_dir" > "$TMP_DIR/httpd13.log" 2>&1 &
local httpd_pid=$!
local ready=0
for _ in 1 2 3 4 5 6 7 8 9 10; do
if curl -sf "http://127.0.0.1:$port/" > /dev/null 2>&1; then
ready=1; break
fi
sleep 0.5
done
if [[ "$ready" != "1" ]]; then
bad "Scenario 13: local HTTP server failed to start on port $port"
kill "$httpd_pid" 2>/dev/null || true
return
fi fi
# IMPORTANT: rebuild both bundles WITHOUT the env var so subsequent dev/test local logfile="$TMP_DIR/scenario13.log"
# runs are not left permanently stalled. BACKSPACE_URL="http://127.0.0.1:$port/" "$APP_BINARY" > "$logfile" 2>&1 &
log "Rebuilding web bundle without VITE_FORCE_BOOT_STALL (restoring normal build)..." local pid=$!
if (cd "$REPO_ROOT" && pnpm --filter @backspace/web build > "$TMP_DIR/web-rebuild.log" 2>&1); then
log " web bundle restored" log " Waiting 25s — boot timer must NOT fire (negative assertion)..."
sleep 25
if assert_no_log "$logfile" '\[recovery\] entered:'; then
ok "Scenario 13: boot timer correctly disarmed by rendererReady ping"
else else
log " WARNING: web rebuild failed — run 'pnpm --filter @backspace/web build' manually" bad "Scenario 13: recovery entered despite rendererReady ping (boot timer not disarmed)"
fi log "Diagnostic — what the page script did (from httpd.log):"
if (cd "$REPO_ROOT" && pnpm --filter @backspace/desktop build > "$TMP_DIR/desktop-rebuild.log" 2>&1); then grep -E 'GET /_marker_' "$TMP_DIR/httpd13.log" 2>/dev/null || log " (no markers — script did not run at all)"
log " desktop build restored" log "Last 20 lines of app log:"
else tail -n 20 "$logfile" || true
log " WARNING: desktop rebuild failed — run 'pnpm --filter @backspace/desktop build' manually"
fi fi
kill_app "$pid"
kill "$httpd_pid" 2>/dev/null || true
wait "$httpd_pid" 2>/dev/null || true
} }
# ─── Main ─────────────────────────────────────────────────────────────── # ─── Main ───────────────────────────────────────────────────────────────
@@ -239,6 +322,7 @@ main() {
scenario_2_bad_url scenario_2_bad_url
scenario_4_boot_stall scenario_4_boot_stall
scenario_13_ping_disarms_timer
log "─── Summary ─────────────────────────────────────────────────────" log "─── Summary ─────────────────────────────────────────────────────"
local r local r