test-helpers.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. assert_contains() {
  30. local output="$1"
  31. local pattern="$2"
  32. local test_name="${3:-test}"
  33. if echo "$output" | grep -q "$pattern"; then
  34. echo " [PASS] $test_name"
  35. return 0
  36. else
  37. echo " [FAIL] $test_name"
  38. echo " Expected to find: $pattern"
  39. echo " In output:"
  40. echo "$output" | sed 's/^/ /'
  41. return 1
  42. fi
  43. }
  44. # Check if output does NOT contain a pattern
  45. # Usage: assert_not_contains "output" "pattern" "test name"
  46. assert_not_contains() {
  47. local output="$1"
  48. local pattern="$2"
  49. local test_name="${3:-test}"
  50. if echo "$output" | grep -q "$pattern"; then
  51. echo " [FAIL] $test_name"
  52. echo " Did not expect to find: $pattern"
  53. echo " In output:"
  54. echo "$output" | sed 's/^/ /'
  55. return 1
  56. else
  57. echo " [PASS] $test_name"
  58. return 0
  59. fi
  60. }
  61. # Check if output matches a count
  62. # Usage: assert_count "output" "pattern" expected_count "test name"
  63. assert_count() {
  64. local output="$1"
  65. local pattern="$2"
  66. local expected="$3"
  67. local test_name="${4:-test}"
  68. local actual=$(echo "$output" | grep -c "$pattern" || echo "0")
  69. if [ "$actual" -eq "$expected" ]; then
  70. echo " [PASS] $test_name (found $actual instances)"
  71. return 0
  72. else
  73. echo " [FAIL] $test_name"
  74. echo " Expected $expected instances of: $pattern"
  75. echo " Found $actual instances"
  76. echo " In output:"
  77. echo "$output" | sed 's/^/ /'
  78. return 1
  79. fi
  80. }
  81. # Check if pattern A appears before pattern B
  82. # Usage: assert_order "output" "pattern_a" "pattern_b" "test name"
  83. assert_order() {
  84. local output="$1"
  85. local pattern_a="$2"
  86. local pattern_b="$3"
  87. local test_name="${4:-test}"
  88. # Get line numbers where patterns appear
  89. local line_a=$(echo "$output" | grep -n "$pattern_a" | head -1 | cut -d: -f1)
  90. local line_b=$(echo "$output" | grep -n "$pattern_b" | head -1 | cut -d: -f1)
  91. if [ -z "$line_a" ]; then
  92. echo " [FAIL] $test_name: pattern A not found: $pattern_a"
  93. return 1
  94. fi
  95. if [ -z "$line_b" ]; then
  96. echo " [FAIL] $test_name: pattern B not found: $pattern_b"
  97. return 1
  98. fi
  99. if [ "$line_a" -lt "$line_b" ]; then
  100. echo " [PASS] $test_name (A at line $line_a, B at line $line_b)"
  101. return 0
  102. else
  103. echo " [FAIL] $test_name"
  104. echo " Expected '$pattern_a' before '$pattern_b'"
  105. echo " But found A at line $line_a, B at line $line_b"
  106. return 1
  107. fi
  108. }
  109. # Create a temporary test project directory
  110. # Usage: test_project=$(create_test_project)
  111. create_test_project() {
  112. local test_dir=$(mktemp -d)
  113. echo "$test_dir"
  114. }
  115. # Cleanup test project
  116. # Usage: cleanup_test_project "$test_dir"
  117. cleanup_test_project() {
  118. local test_dir="$1"
  119. if [ -d "$test_dir" ]; then
  120. rm -rf "$test_dir"
  121. fi
  122. }
  123. # Create a simple plan file for testing
  124. # Usage: create_test_plan "$project_dir" "$plan_name"
  125. create_test_plan() {
  126. local project_dir="$1"
  127. local plan_name="${2:-test-plan}"
  128. local plan_file="$project_dir/docs/superpowers/plans/$plan_name.md"
  129. mkdir -p "$(dirname "$plan_file")"
  130. cat > "$plan_file" <<'EOF'
  131. # Test Implementation Plan
  132. ## Task 1: Create Hello Function
  133. Create a simple hello function that returns "Hello, World!".
  134. **File:** `src/hello.js`
  135. **Implementation:**
  136. ```javascript
  137. export function hello() {
  138. return "Hello, World!";
  139. }
  140. ```
  141. **Tests:** Write a test that verifies the function returns the expected string.
  142. **Verification:** `npm test`
  143. ## Task 2: Create Goodbye Function
  144. Create a goodbye function that takes a name and returns a goodbye message.
  145. **File:** `src/goodbye.js`
  146. **Implementation:**
  147. ```javascript
  148. export function goodbye(name) {
  149. return `Goodbye, ${name}!`;
  150. }
  151. ```
  152. **Tests:** Write tests for:
  153. - Default name
  154. - Custom name
  155. - Edge cases (empty string, null)
  156. **Verification:** `npm test`
  157. EOF
  158. echo "$plan_file"
  159. }
  160. # Export functions for use in tests
  161. export -f run_claude
  162. export -f assert_contains
  163. export -f assert_not_contains
  164. export -f assert_count
  165. export -f assert_order
  166. export -f create_test_project
  167. export -f cleanup_test_project
  168. export -f create_test_plan