#!/usr/bin/env bash
# Tests for agents-md-bootstrap.sh (Task 486).
#
# Validates the doctrine: every core specialist .md under
# <account_dir>/specialists/agents/ has exactly one matching line in
# <account_dir>/agents/admin/AGENTS.md after the bootstrap runs. Operator
# edits survive re-seed; premium-plugin agents (`--` in filename) are
# skipped.

set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BOOTSTRAP="$SCRIPT_DIR/../lib/agents-md-bootstrap.sh"
PASS=0
FAIL=0
FAILED_TESTS=()

assert_eq() {
  if [ "$1" = "$2" ]; then PASS=$((PASS+1));
  else FAIL=$((FAIL+1)); FAILED_TESTS+=("$3: expected [$2] got [$1]"); fi
}
assert_contains() {
  if grep -qF -- "$2" "$1"; then PASS=$((PASS+1));
  else FAIL=$((FAIL+1)); FAILED_TESTS+=("$3: file does not contain [$2]"); fi
}
assert_not_contains() {
  if grep -qF -- "$2" "$1"; then
    FAIL=$((FAIL+1)); FAILED_TESTS+=("$3: file unexpectedly contains [$2]")
  else PASS=$((PASS+1)); fi
}
assert_matches() {
  if echo "$1" | grep -qE "$2"; then PASS=$((PASS+1));
  else FAIL=$((FAIL+1)); FAILED_TESTS+=("$3: output [$1] does not match [$2]"); fi
}

make_account() {
  local tmp; tmp=$(mktemp -d)
  mkdir -p "$tmp/specialists/agents" "$tmp/agents/admin"
  for n in alpha-specialist beta-specialist gamma-specialist; do
    cat > "$tmp/specialists/agents/${n}.md" <<EOF
---
name: ${n}
description: "Owns ${n} work. Delegate when the operator asks for ${n}-flavoured deliverables."
tools: Read
---
# ${n}
EOF
  done
  echo "$tmp"
}

# Case 1 — cold install: empty state, every specialist appended.
TMP=$(make_account)
OUT=$(bash "$BOOTSTRAP" "$TMP")
assert_contains "$TMP/agents/admin/AGENTS.md" "- **specialists:alpha-specialist**: " "cold:alpha-present"
assert_contains "$TMP/agents/admin/AGENTS.md" "- **specialists:beta-specialist**: " "cold:beta-present"
assert_contains "$TMP/agents/admin/AGENTS.md" "- **specialists:gamma-specialist**: " "cold:gamma-present"
assert_matches "$OUT" "specialists=3 appended=3 existing=0" "cold:log-counts"
rm -rf "$TMP"

# Case 2 — idempotent re-install: second run leaves file byte-identical.
TMP=$(make_account)
bash "$BOOTSTRAP" "$TMP" >/dev/null
FIRST_SHA=$(shasum "$TMP/agents/admin/AGENTS.md" | awk '{print $1}')
OUT=$(bash "$BOOTSTRAP" "$TMP")
SECOND_SHA=$(shasum "$TMP/agents/admin/AGENTS.md" | awk '{print $1}')
assert_eq "$SECOND_SHA" "$FIRST_SHA" "idempotent:bytes-identical"
assert_matches "$OUT" "specialists=3 appended=0 existing=3" "idempotent:log-counts"
rm -rf "$TMP"

# Case 3 — partial heal: pre-populated AGENTS.md with one operator-tuned
# line for alpha-specialist. Re-run must append beta + gamma, leave alpha
# verbatim (operator wording survives, default description never written).
TMP=$(make_account)
cat > "$TMP/agents/admin/AGENTS.md" <<'EOF'
# Installed Roles

- **specialists:alpha-specialist**: Operator-tuned alpha rule that must survive.
EOF
OUT=$(bash "$BOOTSTRAP" "$TMP")
assert_contains "$TMP/agents/admin/AGENTS.md" "Operator-tuned alpha rule that must survive." "partial:operator-edit-preserved"
assert_contains "$TMP/agents/admin/AGENTS.md" "- **specialists:beta-specialist**: " "partial:beta-appended"
assert_contains "$TMP/agents/admin/AGENTS.md" "- **specialists:gamma-specialist**: " "partial:gamma-appended"
assert_not_contains "$TMP/agents/admin/AGENTS.md" "Owns alpha-specialist work." "partial:default-alpha-not-overwriting"
assert_matches "$OUT" "specialists=3 appended=2 existing=1" "partial:log-counts"
rm -rf "$TMP"

# Case 4 — doctrine observable: count of `- **specialists:<name>**:` lines
# in AGENTS.md equals count of core specialist .md files. Premium plugin
# agents (`--` in filename) are skipped.
TMP=$(make_account)
cat > "$TMP/specialists/agents/premium--special.md" <<'EOF'
---
name: premium--special
description: "Premium plugin agent skipped by the bootstrap."
---
EOF
bash "$BOOTSTRAP" "$TMP" >/dev/null
CORE_FILES=$(find "$TMP/specialists/agents" -maxdepth 1 -name '*.md' -not -name '*--*' -type f | wc -l | tr -d ' ')
AGENTS_LINES=$(grep -cE '^- \*\*specialists:' "$TMP/agents/admin/AGENTS.md" || true)
assert_eq "$AGENTS_LINES" "$CORE_FILES" "doctrine:agents-md-line-count-equals-core-count"
assert_not_contains "$TMP/agents/admin/AGENTS.md" "premium--special" "doctrine:premium-plugin-agent-skipped"
rm -rf "$TMP"

echo
echo "PASS: $PASS, FAIL: $FAIL"
if [ "$FAIL" -gt 0 ]; then
  for t in "${FAILED_TESTS[@]}"; do echo "FAIL: $t"; done
  exit 1
fi
exit 0
