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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SCRIPT="$ROOT/run-tests.sh"

PASS=0
FAIL=0

ok() { PASS=$((PASS+1)); printf '  ok   %s\n' "$1"; }
bad() { FAIL=$((FAIL+1)); printf '  FAIL %s\n     %s\n' "$1" "$2"; }

echo "run-tests.sh contract:"

if [[ -x "$SCRIPT" ]]; then
  ok "entrypoint exists and is executable"
else
  bad "entrypoint exists and is executable" "missing or non-executable: $SCRIPT"
fi

if out="$("$SCRIPT" --dry-run)"; then
  ok "dry-run exits zero"
else
  bad "dry-run exits zero" "command failed"
  out=""
fi

for expected in \
  "bash \"$ROOT/lib/test-gates.sh\"" \
  "bash \"$ROOT/lib/test-journal.sh\"" \
  "bash \"$ROOT/lib/test-resolve-seat.sh\"" \
  "node \"$ROOT/lib/test/normalize-events.test.js\"" \
  "bash \"$ROOT/test/runner-integration.sh\"" \
  "bash \"$ROOT/test/dynwf-drift-guard.sh\""
do
  if grep -Fxq -- "$expected" <<<"$out"; then
    ok "dry-run lists $expected"
  else
    bad "dry-run lists $expected" "$out"
  fi
done

echo
echo "PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]]
