Explorar o código

feat: persist brainstorm mockups to .superpowers/ directory

start-server.sh now accepts --project-dir to store session files under
.superpowers/brainstorm/ instead of /tmp. stop-server.sh only deletes
ephemeral /tmp sessions, keeping persistent ones for later review.

Fix test race condition with polling-based server startup wait.
Jesse Vincent hai 7 meses
pai
achega
b10a3c0995

+ 29 - 6
lib/brainstorm-server/start-server.sh

@@ -1,16 +1,40 @@
 #!/bin/bash
 # Start the brainstorm server and output connection info
-# Usage: start-server.sh
+# Usage: start-server.sh [--project-dir <path>]
 #
-# Starts server on a random high port, outputs JSON with URL
-# Each session gets its own temp directory to avoid conflicts
-# Server runs in background, PID saved for cleanup
+# Starts server on a random high port, outputs JSON with URL.
+# Each session gets its own directory to avoid conflicts.
+#
+# Options:
+#   --project-dir <path>  Store session files under <path>/.superpowers/brainstorm/
+#                         instead of /tmp. Files persist after server stops.
 
 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
 
+# Parse arguments
+PROJECT_DIR=""
+while [[ $# -gt 0 ]]; do
+  case "$1" in
+    --project-dir)
+      PROJECT_DIR="$2"
+      shift 2
+      ;;
+    *)
+      echo "{\"error\": \"Unknown argument: $1\"}"
+      exit 1
+      ;;
+  esac
+done
+
 # Generate unique session directory
 SESSION_ID="$$-$(date +%s)"
-SCREEN_DIR="/tmp/brainstorm-${SESSION_ID}"
+
+if [[ -n "$PROJECT_DIR" ]]; then
+  SCREEN_DIR="${PROJECT_DIR}/.superpowers/brainstorm/${SESSION_ID}"
+else
+  SCREEN_DIR="/tmp/brainstorm-${SESSION_ID}"
+fi
+
 PID_FILE="${SCREEN_DIR}/.server.pid"
 LOG_FILE="${SCREEN_DIR}/.server.log"
 
@@ -33,7 +57,6 @@ echo "$SERVER_PID" > "$PID_FILE"
 # Wait for server-started message (check log file)
 for i in {1..50}; do
   if grep -q "server-started" "$LOG_FILE" 2>/dev/null; then
-    # Extract and output the server-started line
     grep "server-started" "$LOG_FILE" | head -1
     exit 0
   fi

+ 12 - 3
lib/brainstorm-server/stop-server.sh

@@ -1,6 +1,10 @@
 #!/bin/bash
-# Stop the brainstorm server and clean up session directory
+# Stop the brainstorm server and clean up
 # Usage: stop-server.sh <screen_dir>
+#
+# Kills the server process. Only deletes session directory if it's
+# under /tmp (ephemeral). Persistent directories (.superpowers/) are
+# kept so mockups can be reviewed later.
 
 SCREEN_DIR="$1"
 
@@ -14,8 +18,13 @@ PID_FILE="${SCREEN_DIR}/.server.pid"
 if [[ -f "$PID_FILE" ]]; then
   pid=$(cat "$PID_FILE")
   kill "$pid" 2>/dev/null
-  # Clean up session directory
-  rm -rf "$SCREEN_DIR"
+  rm -f "$PID_FILE" "${SCREEN_DIR}/.server.log"
+
+  # Only delete ephemeral /tmp directories
+  if [[ "$SCREEN_DIR" == /tmp/* ]]; then
+    rm -rf "$SCREEN_DIR"
+  fi
+
   echo '{"status": "stopped"}'
 else
   echo '{"status": "not_running"}'

+ 8 - 2
tests/brainstorm-server/server.test.js

@@ -42,10 +42,16 @@ async function runTests() {
   const server = startServer();
 
   let stdout = '';
+  let stderr = '';
   server.stdout.on('data', (data) => { stdout += data.toString(); });
-  server.stderr.on('data', (data) => { console.error('Server stderr:', data.toString()); });
+  server.stderr.on('data', (data) => { stderr += data.toString(); });
 
-  await sleep(1000);
+  // Wait for server to start (up to 3 seconds)
+  for (let i = 0; i < 30; i++) {
+    if (stdout.includes('server-started')) break;
+    await sleep(100);
+  }
+  if (stderr) console.error('Server stderr:', stderr);
 
   try {
     // Test 1: Server starts and outputs JSON