#!/usr/bin/env bash
# capture_golden.sh -- Regenerate golden test fixtures from owl.sh and live.sh
# Runs every deterministic subcommand in an isolated environment, captures
# raw stdout/stderr/exitcode to fixture files under tests/golden/.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
OWL="bash $SCRIPT_DIR/owl.sh"
LIVE="bash $SCRIPT_DIR/live.sh"
FIXTURES="$SCRIPT_DIR/tests/golden"

# Create isolated HOME so auto-setup and owlery don't touch real state
TMPDIR_ROOT=$(mktemp -d)
export HOME="$TMPDIR_ROOT"
# Phase 18.2: spacetime relocated out of ~/.claude; golden fixture setup now uses SPT_HOME.
mkdir -p "${SPT_HOME:-$HOME/.spt}/owlery"

# Pre-seed settings.json so _auto_setup does NOT fire and produce extra output
mkdir -p "$HOME/.claude"
cat > "$HOME/.claude/settings.json" <<SETTINGSEOF
{"env":{"OWL":"$OWL","LIVE":"$LIVE"},"hooks":{}}
SETTINGSEOF

# Export OWL and LIVE so live.sh can find owl.sh
export OWL
export LIVE

# Ensure output directories exist
mkdir -p "$FIXTURES/owl" "$FIXTURES/live"

# --- Helper: capture a subcommand's stdout, stderr, and exit code ---
# Usage: capture LABEL SUBDIR COMMAND [ARGS...]
capture() {
  local label="$1"
  local subdir="$2"
  shift 2
  local out="$FIXTURES/$subdir/$label.stdout"
  local err="$FIXTURES/$subdir/$label.stderr"
  local exitfile="$FIXTURES/$subdir/$label.exitcode"
  # Run in subshell with set +e so non-zero exits don't abort the script
  (
    set +e
    "$@" >"$out" 2>"$err"
    echo $? > "$exitfile"
  )
}

# ===================================================================
# OWL.SH SUBCOMMANDS
# ===================================================================

# -- Deterministic, no pre-state needed --
capture list_empty owl $OWL list
capture unknown_cmd owl $OWL foobar

# -- State-dependent: setup, list, stop sequence --
capture setup owl $OWL setup testid
capture list_one owl $OWL list
capture stop owl $OWL stop testid
capture stop_nonexistent owl $OWL stop nosuchid
capture stop_all owl $OWL stop --all

# -- State-dependent: message delivery --
$OWL setup recvr >/dev/null 2>/dev/null
capture deliver owl sh -c "echo 'hello world' | $OWL deliver recvr senderid"
capture reply owl sh -c "echo 'reply body' | $OWL reply recvr senderid"
$OWL stop recvr >/dev/null 2>/dev/null

# -- Cleanup commands --
capture cleanup_session_empty owl $OWL cleanup-session

# -- Session-resume (no stale perches) --
capture session_resume_empty owl $OWL session-resume

# -- Error cases --
capture deliver_no_target owl sh -c "echo 'msg' | $OWL deliver nosuchid fromid"

# ===================================================================
# LIVE.SH SUBCOMMANDS
# ===================================================================

# -- Deterministic, no pre-state --
capture list_empty live $LIVE list
capture list_psyches_empty live $LIVE list-psyches
capture unknown_cmd live $LIVE foobar

# -- Error cases --
capture pulse_wait_too_short live $LIVE pulse-wait 10

# -- State-dependent: psyche-download, clear-psyche --
# (context-save fixture removed by quick-260521-myj — the $LIVE context-save
# subcommand was deleted as a footgun; Psyche LLM is the sole writer of
# agents/{id}/live_context.md via the Write tool.)
capture psyche_download_none live $LIVE psyche-download testid
capture clear_psyche_none live $LIVE clear-psyche testid

# Create a context file for psyche-download test
mkdir -p "$HOME/.claude/psyche-contexts"
echo "test context" > "$HOME/.claude/psyche-contexts/testid.md"

capture psyche_download_exists live $LIVE psyche-download testid
capture clear_psyche live $LIVE clear-psyche testid

# ===================================================================
# CLEANUP
# ===================================================================
rm -rf "$TMPDIR_ROOT"

# Summary
OWL_COUNT=$(ls "$FIXTURES/owl/"*.stderr 2>/dev/null | wc -l)
LIVE_COUNT=$(ls "$FIXTURES/live/"*.stderr 2>/dev/null | wc -l)
TOTAL=$((OWL_COUNT + LIVE_COUNT))
echo "Golden fixtures captured to tests/golden/ ($OWL_COUNT owl + $LIVE_COUNT live = $TOTAL total)"
