#!/usr/bin/env bash
# Runs the Kanboard contract test against the live local store.
# `bun test` alone cannot run it: cpu-guard routes bun through systemd-run,
# which drops the environment the test needs, so the suite silently skips.
set -euo pipefail

env_file="${OVERDECK_KANBOARD_ENV_FILE:-$HOME/.config/overdeck/kanboard.env}"
url="${OVERDECK_KANBOARD_LIVE_URL:-http://127.0.0.1:31339/jsonrpc.php}"

if [[ ! -r "$env_file" ]]; then
  echo "kanboard-live: no readable env file at $env_file" >&2
  exit 1
fi

token="$(sed -n 's/^KANBOARD_API_TOKEN=//p' "$env_file")"
if [[ -z "$token" ]]; then
  echo "kanboard-live: KANBOARD_API_TOKEN missing from $env_file" >&2
  exit 1
fi

if ! curl -fsS -m 10 -o /dev/null "${url%/jsonrpc.php}/healthcheck.php"; then
  echo "kanboard-live: store unreachable at $url" >&2
  exit 1
fi

cd "$(dirname "${BASH_SOURCE[0]}")"
OVERDECK_KANBOARD_LIVE_URL="$url" \
OVERDECK_KANBOARD_LIVE_TOKEN="$token" \
  exec /usr/bin/bun test src/incidents/kanboard-live.integration.test.ts "$@"
