#!/bin/bash
# install_test.sh — tests for scripts/install.sh
#
# Tests install.sh against the real public Gitea releases (REQ-045 made
# the repo + releases publicly accessible). Uses real existing release
# tags (v0.4.1, v0.4.2) so no mock infrastructure is needed.
#
# Tests:
#   1. user-level install (binary at ~/.local/bin/orca)
#   2. in-place update (v0.4.1 -> v0.4.2) preserves namespace state
#   3. idempotent re-install (v0.4.2 -> v0.4.2)
#   4. --system install (requires root; /usr/local/bin/orca)
#   5. --system without root fails with error
#
# Usage: bash scripts/install_test.sh
#        sudo bash scripts/install_test.sh  (to include --system tests)
#
# Each test is wrapped in `timeout 30` to prevent hangs. The whole
# suite is wrapped in `timeout 120`.

set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_SH="$SCRIPT_DIR/install.sh"

PASS=0
FAIL=0

ok()   { echo "  PASS: $1"; PASS=$((PASS+1)); }
fail() { echo "  FAIL: $1"; FAIL=$((FAIL+1)); }

# Kill any background processes on exit (defensive — no background procs
# expected in this version, but keeps the harness safe).
trap 'kill 0 2>/dev/null || true' EXIT

run_install() {
  timeout 30 bash "$INSTALL_SH" "$@" 2>&1
}

echo "=== Test 1: user-level install (v0.4.1) ==="
FAKE_HOME="$(mktemp -d)"
export HOME="$FAKE_HOME"
if run_install --version v0.4.1 > /tmp/it1.log 2>&1; then
  if [ -x "$FAKE_HOME/.local/bin/orca" ]; then
    ok "binary at ~/.local/bin/orca"
  else
    fail "binary not at ~/.local/bin/orca"
  fi
  INSTALLED_VER="$(timeout 5 "$FAKE_HOME/.local/bin/orca" version --json 2>/dev/null | sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')"
  if [ "$INSTALLED_VER" = "v0.4.1" ]; then
    ok "installed version is v0.4.1"
  else
    fail "installed version is '${INSTALLED_VER}', expected v0.4.1"
  fi
else
  fail "user install exited non-zero"; cat /tmp/it1.log
fi

echo "=== Test 2: in-place update (v0.4.1 -> v0.4.2) preserves namespace ==="
mkdir -p "$FAKE_HOME/.orca"
echo "preserve-me" > "$FAKE_HOME/.orca/orca.db"
if run_install --version v0.4.2 > /tmp/it2.log 2>&1; then
  if grep -q "updated orca from v0.4.1 to v0.4.2" /tmp/it2.log; then
    ok "update message printed"
  else
    fail "update message not printed"; cat /tmp/it2.log
  fi
  if [ "$(cat "$FAKE_HOME/.orca/orca.db" 2>/dev/null)" = "preserve-me" ]; then
    ok "namespace state preserved during update"
  else
    fail "namespace state was modified or removed during update"
  fi
  INSTALLED_VER="$(timeout 5 "$FAKE_HOME/.local/bin/orca" version --json 2>/dev/null | sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')"
  if [ "$INSTALLED_VER" = "v0.4.2" ]; then
    ok "binary updated to v0.4.2"
  else
    fail "binary version is '${INSTALLED_VER}', expected v0.4.2"
  fi
else
  fail "update exited non-zero"; cat /tmp/it2.log
fi

echo "=== Test 3: idempotent re-install (v0.4.2 -> v0.4.2) ==="
if run_install --version v0.4.2 > /tmp/it3.log 2>&1; then
  if grep -q "reinstalled orca v0.4.2" /tmp/it3.log; then
    ok "reinstall message printed"
  else
    fail "reinstall message not printed"; cat /tmp/it3.log
  fi
else
  fail "reinstall exited non-zero"; cat /tmp/it3.log
fi

echo "=== Test 4: --system install (requires root) ==="
if [ "$(id -u)" -eq 0 ]; then
  if run_install --system --version v0.4.1 > /tmp/it4.log 2>&1; then
    if [ -x /usr/local/bin/orca ]; then
      ok "binary at /usr/local/bin/orca"
    else
      fail "binary not at /usr/local/bin/orca"
    fi
    if grep -q "namespace root: /root/.orca" /tmp/it4.log; then
      ok "--system reports /root/.orca namespace"
    else
      fail "--system did not report /root/.orca namespace"; cat /tmp/it4.log
    fi
    rm -f /usr/local/bin/orca
  else
    fail "--system install exited non-zero"; cat /tmp/it4.log
  fi
else
  echo "  SKIP: --system test (not running as root)"
fi

echo "=== Test 5: --system without root fails ==="
if [ "$(id -u)" -ne 0 ]; then
  if run_install --system --version v0.4.1 2>&1 | grep -q "requires root"; then
    ok "--system without root correctly errors"
  else
    fail "--system without root did not error"
  fi
else
  echo "  SKIP: --system-without-root test (running as root)"
fi

echo ""
echo "=== Results: $PASS passed, $FAIL failed ==="
rm -rf "$FAKE_HOME" /tmp/it1.log /tmp/it2.log /tmp/it3.log /tmp/it4.log 2>/dev/null
exit $FAIL