#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
BIN="$ROOT_DIR/stack/nostr-id-tui/build-host/nostr-id-tui"
SERVICES_CONF="$ROOT_DIR/iso/config/includes.chroot/etc/n-os-tr/services.conf"

if [[ ! -x "$BIN" ]]; then
  echo "error: missing binary $BIN"
  echo "build first: cmake -S stack/nostr-id-tui -B stack/nostr-id-tui/build-host && cmake --build stack/nostr-id-tui/build-host -j"
  exit 1
fi

if [[ ! -f "$SERVICES_CONF" ]]; then
  echo "error: missing services config $SERVICES_CONF"
  exit 1
fi

OUT="$ROOT_DIR/stack/nostr-id-tui/build-host/test-services-sim.out"

# Simulated run (no real systemctl operations). We just verify the TUI launches and renders.
timeout 2s env TERM=xterm-256color \
  NOSTR_TUI_SIMULATE_SYSTEMCTL=1 \
  NOSTR_TUI_SERVICES_CONF="$SERVICES_CONF" \
  script -q -c "$BIN" /dev/null > "$OUT" 2>&1 || true

if grep -q "n-OS-tr Identity" "$OUT"; then
  echo "ok: TUI launched and rendered header"
else
  echo "error: expected TUI header not found in output"
  echo "see $OUT"
  exit 1
fi

echo "ok: simulated services smoke test passed"
