1
0

test-helpers.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env bash
  2. # Helper functions for Claude Code skill tests
  3. # Run Claude Code with a prompt and capture output
  4. # Usage: run_claude "prompt text" [timeout_seconds] [allowed_tools]
  5. run_claude() {
  6. local prompt="$1"
  7. local timeout="${2:-60}"
  8. local allowed_tools="${3:-}"
  9. local output_file=$(mktemp)
  10. # Build command as an argv array so timeout wraps claude directly.
  11. local cmd=(claude -p "$prompt")
  12. if [ -n "$allowed_tools" ]; then
  13. cmd+=(--allowed-tools="$allowed_tools")
  14. fi
  15. # Run Claude in headless mode with timeout
  16. if timeout "$timeout" "${cmd[@]}" > "$output_file" 2>&1; then
  17. cat "$output_file"
  18. rm -f "$output_file"
  19. return 0
  20. else
  21. local exit_code=$?
  22. cat "$output_file" >&2
  23. rm -f "$output_file"
  24. return $exit_code
  25. fi
  26. }
  27. # Check if output contains a pattern
  28. # Usage: assert_contains "output" "pattern" "test name"
  29. # Matching is case-insensitive: patterns are prose keywords, and models
  30. # freely capitalize skill terms ("Do Not Trust", "Spec Compliance").
  31. assert_contains() {
  32. local output="$1"
  33. local pattern="$2"
  34. local test_name="${3:-test}"
  35. if echo "$output" | grep -qi "$pattern"; then
  36. echo " [PASS] $test_name"
  37. return 0
  38. else
  39. echo " [FAIL] $test_name"
  40. echo " Expected to find: $pattern"
  41. echo " In output:"
  42. echo "$output" | sed 's/^/ /'
  43. return 1
  44. fi
  45. }
  46. # Check if output does NOT contain a pattern
  47. # Usage: assert_not_contains "output" "pattern" "test name"
  48. assert_not_contains() {
  49. local output="$1"
  50. local pattern="$2"
  51. local test_name="${3:-test}"
  52. if echo "$output" | grep -qi "$pattern"; then
  53. echo " [FAIL] $test_name"
  54. echo " Did not expect to find: $pattern"
  55. echo " In output:"
  56. echo "$output" | sed 's/^/ /'
  57. return 1
  58. else
  59. echo " [PASS] $test_name"
  60. return 0
  61. fi
  62. }
  63. # Check if output matches a count
  64. # Usage: assert_count "output" "pattern" expected_count "test name"
  65. assert_count() {
  66. local output="$1"
  67. local pattern="$2"
  68. local expected="$3"
  69. local test_name="${4:-test}"
  70. local actual=$(echo "$output" | grep -ci "$pattern" || echo "0")
  71. if [ "$actual" -eq "$expected" ]; then
  72. echo " [PASS] $test_name (found $actual instances)"
  73. return 0
  74. else
  75. echo " [FAIL] $test_name"
  76. echo " Expected $expected instances of: $pattern"
  77. echo " Found $actual instances"
  78. echo " In output:"
  79. echo "$output" | sed 's/^/ /'
  80. return 1
  81. fi
  82. }
  83. # Check if pattern A appears before pattern B
  84. # Usage: assert_order "output" "pattern_a" "pattern_b" "test name"
  85. assert_order() {
  86. local output="$1"
  87. local pattern_a="$2"
  88. local pattern_b="$3"
  89. local test_name="${4:-test}"
  90. # Get line numbers where patterns appear
  91. local line_a=$(echo "$output" | grep -ni "$pattern_a" | head -1 | cut -d: -f1)
  92. local line_b=$(echo "$output" | grep -ni "$pattern_b" | head -1 | cut -d: -f1)
  93. if [ -z "$line_a" ]; then
  94. echo " [FAIL] $test_name: pattern A not found: $pattern_a"
  95. echo " In output:"
  96. echo "$output" | sed 's/^/ /'
  97. return 1
  98. fi
  99. if [ -z "$line_b" ]; then
  100. echo " [FAIL] $test_name: pattern B not found: $pattern_b"
  101. echo " In output:"
  102. echo "$output" | sed 's/^/ /'
  103. return 1
  104. fi
  105. if [ "$line_a" -lt "$line_b" ]; then
  106. echo " [PASS] $test_name (A at line $line_a, B at line $line_b)"
  107. return 0
  108. else
  109. echo " [FAIL] $test_name"
  110. echo " Expected '$pattern_a' before '$pattern_b'"
  111. echo " But found A at line $line_a, B at line $line_b"
  112. return 1
  113. fi
  114. }
  115. # Create a temporary test project directory
  116. # Usage: test_project=$(create_test_project)
  117. create_test_project() {
  118. local test_dir=$(mktemp -d)
  119. echo "$test_dir"
  120. }
  121. # Cleanup test project
  122. # Usage: cleanup_test_project "$test_dir"
  123. cleanup_test_project() {
  124. local test_dir="$1"
  125. if [ -d "$test_dir" ]; then
  126. rm -rf "$test_dir"
  127. fi
  128. }
  129. # Create a simple plan file for testing
  130. # Usage: create_test_plan "$project_dir" "$plan_name"
  131. create_test_plan() {
  132. local project_dir="$1"
  133. local plan_name="${2:-test-plan}"
  134. local plan_file="$project_dir/docs/superpowers/plans/$plan_name.md"
  135. mkdir -p "$(dirname "$plan_file")"
  136. cat > "$plan_file" <<'EOF'
  137. # Test Implementation Plan
  138. ## Task 1: Create Hello Function
  139. Create a simple hello function that returns "Hello, World!".
  140. **File:** `src/hello.js`
  141. **Implementation:**
  142. ```javascript
  143. export function hello() {
  144. return "Hello, World!";
  145. }
  146. ```
  147. **Tests:** Write a test that verifies the function returns the expected string.
  148. **Verification:** `npm test`
  149. ## Task 2: Create Goodbye Function
  150. Create a goodbye function that takes a name and returns a goodbye message.
  151. **File:** `src/goodbye.js`
  152. **Implementation:**
  153. ```javascript
  154. export function goodbye(name) {
  155. return `Goodbye, ${name}!`;
  156. }
  157. ```
  158. **Tests:** Write tests for:
  159. - Default name
  160. - Custom name
  161. - Edge cases (empty string, null)
  162. **Verification:** `npm test`
  163. EOF
  164. echo "$plan_file"
  165. }
  166. # Export functions for use in tests
  167. export -f run_claude
  168. export -f assert_contains
  169. export -f assert_not_contains
  170. export -f assert_count
  171. export -f assert_order
  172. export -f create_test_project
  173. export -f cleanup_test_project
  174. export -f create_test_plan