#!/usr/bin/env bash
# Smoke tests for library-policy-nudge.sh
# Usage: bash .claude/hooks/library-policy-nudge.test.sh

set -euo pipefail

SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/library-policy-nudge.sh"
MARKER="/evaluate-library"
PASS=0
FAIL=0

assert_contains() {
  local name="$1"
  local input="$2"
  local output
  output=$(printf '%s' "$input" | bash "$SCRIPT" 2>/dev/null)
  if echo "$output" | grep -qF "$MARKER"; then
    echo "  PASS: $name"
    PASS=$((PASS + 1))
  else
    echo "  FAIL: $name"
    echo "    Expected stdout to contain: $MARKER"
    echo "    Got: ${output:-<empty>}"
    FAIL=$((FAIL + 1))
  fi
}

assert_no_output() {
  local name="$1"
  local input="$2"
  local output
  output=$(printf '%s' "$input" | bash "$SCRIPT" 2>/dev/null)
  if ! echo "$output" | grep -qF "$MARKER"; then
    echo "  PASS: $name"
    PASS=$((PASS + 1))
  else
    echo "  FAIL: $name"
    echo "    Expected no $MARKER in stdout, got: $output"
    FAIL=$((FAIL + 1))
  fi
}

echo "library-policy-nudge.sh smoke tests"
echo "------------------------------------"

# pnpm add <pkg> → reminder (runtime dep)
assert_contains \
  "pnpm add foo triggers reminder" \
  '{"tool_input":{"command":"pnpm add foo"}}'

# pnpm add -D <pkg> → no reminder (dev dep)
assert_no_output \
  "pnpm add -D foo produces no reminder" \
  '{"tool_input":{"command":"pnpm add -D foo"}}'

# pnpm add --save-dev <pkg> → no reminder (dev dep, long flag)
assert_no_output \
  "pnpm add --save-dev foo produces no reminder" \
  '{"tool_input":{"command":"pnpm add --save-dev foo"}}'

# Edit on non-package.json → no reminder
assert_no_output \
  "Edit on feature.manifest.ts produces no reminder" \
  '{"tool_input":{"file_path":"/workspace/packages/auth/src/feature.manifest.ts"}}'

# Edit on package.json → reminder
assert_contains \
  "Edit on package.json triggers reminder" \
  '{"tool_input":{"file_path":"/workspace/packages/auth/package.json"}}'

echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]]
