#!/usr/bin/env bash
# Test for admin-skills-bootstrap.sh (Task 698). Builds a fixture plugins tree
# and asserts the admin-usable derivation, the fail-safe no-declaration handling,
# the owned-set guard, robust block-scalar description extraction, and the log.
set -u

GEN="$(cd "$(dirname "$0")/.." && pwd)/admin-skills-bootstrap.sh"
[ -x "$GEN" ] || { echo "FAIL: $GEN not executable" >&2; exit 1; }

TMP=$(mktemp -d); ACC="$TMP/account"; PLUG="$TMP/plugins"
mkdir -p "$ACC/agents/admin"

# mk <plugin> <slug> <description-line> <declaration-paragraph>
mk() {
  mkdir -p "$PLUG/$1/skills/$2"
  printf -- '---\nname: %s\ndescription: %s\n---\n\n# %s\n\n%s\n' \
    "$2" "$3" "$2" "$4" > "$PLUG/$1/skills/$2/SKILL.md"
}

# admin-usable, single-line quoted description
mk admin session-management '"reset and list sessions"' "Invoked by the admin agent directly."
# admin-usable, folded block-scalar description (robust extraction must join lines)
printf -- '---\nname: datetime\ndescription: >\n  resolve the current\n  date and time\n---\n\n# datetime\n\nInvoked by the admin agent directly.\n' \
  > "$PLUG/admin/skills/datetime/SKILL.md" 2>/dev/null || { mkdir -p "$PLUG/admin/skills/datetime"; printf -- '---\nname: datetime\ndescription: >\n  resolve the current\n  date and time\n---\n\n# datetime\n\nInvoked by the admin agent directly.\n' > "$PLUG/admin/skills/datetime/SKILL.md"; }
# specialist-owned — declaration names a specialist
mk deep-research deep-research '"multi-source research"' 'Invoked from `specialists:research-assistant`.'
# content-producer authoring, declaration TAMPERED to admin — owned-set must still exclude
mk admin professional-document '"author a document"' "Invoked by the admin agent directly."
# admin-usable whose declaration MENTIONS downstream specialists after a period —
# must stay admin-usable (the specialists: are routing targets, not the owner).
mk admin unzip-attachment '"safely extract an attachment"' \
  '**Invoked by the admin agent directly.** Admin forwards the tree to `specialists:content-producer` or `specialists:librarian` downstream.'
# no declaration — fail-safe: excluded and reported
mk admin mystery-skill '"does a mystery thing"' "No declaration here at all."

OWNED="$TMP/owned.ts"
cat > "$OWNED" <<'EOF'
export const CONTENT_PRODUCER_AUTHORING_SKILL_SLUGS = [
  "professional-document",
  "a4-print-documents",
  "publish-site",
] as const;
EOF

OUT=$(bash "$GEN" "$ACC" "$PLUG" "$OWNED" 2>&1)
MD="$ACC/agents/admin/ADMIN-SKILLS.md"

PASS=0; FAIL=0
chk() { if eval "$2"; then echo "PASS: $1"; PASS=$((PASS+1)); else echo "FAIL: $1 (out=$OUT)" >&2; FAIL=$((FAIL+1)); fi; }

chk "admin skill included with desc"  'grep -qE "^- session-management: reset and list sessions$" "$MD"'
chk "block-scalar desc joined"        'grep -qE "^- datetime: resolve the current date and time$" "$MD"'
chk "downstream-mention stays admin"  'grep -qE "^- unzip-attachment: " "$MD"'
chk "specialist skill excluded"       '! grep -q "deep-research" "$MD"'
chk "owned-set skill excluded"        '! grep -q "professional-document" "$MD"'
chk "no-decl skill excluded"          '! grep -q "mystery-skill" "$MD"'
chk "no-decl reported"                'echo "$OUT" | grep -q "skill=mystery-skill missing-declaration"'
chk "scan log shape"                  'echo "$OUT" | grep -qE "\[admin-skills\] scanned=6 admin-usable=3 no-declaration=1"'
chk "list sorted by slug"             '[ "$(grep "^- " "$MD" | head -1)" = "- datetime: resolve the current date and time" ]'

rm -rf "$TMP"
echo "----"
echo "PASS=$PASS FAIL=$FAIL"
[ "$FAIL" -eq 0 ]
