test-helpers.sh 5.1 KB

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