test-subagent-driven-development-integration.sh 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #!/usr/bin/env bash
  2. # Integration Test: subagent-driven-development workflow
  3. # Actually executes a plan and verifies the new workflow behaviors
  4. set -euo pipefail
  5. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  6. source "$SCRIPT_DIR/test-helpers.sh"
  7. echo "========================================"
  8. echo " Integration Test: subagent-driven-development"
  9. echo "========================================"
  10. echo ""
  11. echo "This test executes a real plan using the skill and verifies:"
  12. echo " 1. Plan is read once (not per task)"
  13. echo " 2. Full task text provided to subagents"
  14. echo " 3. Subagents perform self-review"
  15. echo " 4. Spec compliance review before code quality"
  16. echo " 5. Review loops when issues found"
  17. echo " 6. Spec reviewer reads code independently"
  18. echo ""
  19. echo "WARNING: This test may take 10-30 minutes to complete."
  20. echo ""
  21. # Create test project
  22. TEST_PROJECT=$(create_test_project)
  23. echo "Test project: $TEST_PROJECT"
  24. # Trap to cleanup
  25. trap "cleanup_test_project $TEST_PROJECT" EXIT
  26. # Set up minimal Node.js project
  27. cd "$TEST_PROJECT"
  28. cat > package.json <<'EOF'
  29. {
  30. "name": "test-project",
  31. "version": "1.0.0",
  32. "type": "module",
  33. "scripts": {
  34. "test": "node --test"
  35. }
  36. }
  37. EOF
  38. mkdir -p src test docs/superpowers/plans
  39. # Create a simple implementation plan
  40. cat > docs/superpowers/plans/implementation-plan.md <<'EOF'
  41. # Test Implementation Plan
  42. This is a minimal plan to test the subagent-driven-development workflow.
  43. ## Task 1: Create Add Function
  44. Create a function that adds two numbers.
  45. **File:** `src/math.js`
  46. **Requirements:**
  47. - Function named `add`
  48. - Takes two parameters: `a` and `b`
  49. - Returns the sum of `a` and `b`
  50. - Export the function
  51. **Implementation:**
  52. ```javascript
  53. export function add(a, b) {
  54. return a + b;
  55. }
  56. ```
  57. **Tests:** Create `test/math.test.js` that verifies:
  58. - `add(2, 3)` returns `5`
  59. - `add(0, 0)` returns `0`
  60. - `add(-1, 1)` returns `0`
  61. **Verification:** `npm test`
  62. ## Task 2: Create Multiply Function
  63. Create a function that multiplies two numbers.
  64. **File:** `src/math.js` (add to existing file)
  65. **Requirements:**
  66. - Function named `multiply`
  67. - Takes two parameters: `a` and `b`
  68. - Returns the product of `a` and `b`
  69. - Export the function
  70. - DO NOT add any extra features (like power, divide, etc.)
  71. **Implementation:**
  72. ```javascript
  73. export function multiply(a, b) {
  74. return a * b;
  75. }
  76. ```
  77. **Tests:** Add to `test/math.test.js`:
  78. - `multiply(2, 3)` returns `6`
  79. - `multiply(0, 5)` returns `0`
  80. - `multiply(-2, 3)` returns `-6`
  81. **Verification:** `npm test`
  82. EOF
  83. # Initialize git repo
  84. git init --quiet
  85. git config user.email "test@test.com"
  86. git config user.name "Test User"
  87. git add .
  88. git commit -m "Initial commit" --quiet
  89. echo ""
  90. echo "Project setup complete. Starting execution..."
  91. echo ""
  92. # Run Claude with subagent-driven-development
  93. # Capture full output to analyze
  94. OUTPUT_FILE="$TEST_PROJECT/claude-output.txt"
  95. # Create prompt file
  96. cat > "$TEST_PROJECT/prompt.txt" <<'EOF'
  97. I want you to execute the implementation plan at docs/superpowers/plans/implementation-plan.md using the subagent-driven-development skill.
  98. IMPORTANT: Follow the skill exactly. I will be verifying that you:
  99. 1. Read the plan once at the beginning
  100. 2. Provide full task text to subagents (don't make them read files)
  101. 3. Ensure subagents do self-review before reporting
  102. 4. Run spec compliance review before code quality review
  103. 5. Use review loops when issues are found
  104. Begin now. Execute the plan.
  105. EOF
  106. # Note: We use a longer timeout since this is integration testing
  107. # Use --allowed-tools to enable tool usage in headless mode
  108. PROMPT="Execute the implementation plan at docs/superpowers/plans/implementation-plan.md using the subagent-driven-development skill.
  109. IMPORTANT: Follow the skill exactly. I will be verifying that you:
  110. 1. Read the plan once at the beginning
  111. 2. Provide full task text to subagents (don't make them read files)
  112. 3. Ensure subagents do self-review before reporting
  113. 4. Run spec compliance review before code quality review
  114. 5. Use review loops when issues are found
  115. Begin now. Execute the plan."
  116. PLUGIN_DIR=$(cd "$SCRIPT_DIR/../.." && pwd)
  117. # Run claude from inside the test project so its session JSONL lands in a
  118. # project-specific directory under ~/.claude/projects/, isolated from any
  119. # other concurrent claude sessions.
  120. echo "Running Claude (plugin-dir: $PLUGIN_DIR, cwd: $TEST_PROJECT)..."
  121. echo "================================================================================"
  122. cd "$TEST_PROJECT" && timeout 1800 claude -p "$PROMPT" --plugin-dir "$PLUGIN_DIR" --allowed-tools=all --permission-mode bypassPermissions 2>&1 | tee "$OUTPUT_FILE" || {
  123. echo ""
  124. echo "================================================================================"
  125. echo "EXECUTION FAILED (exit code: $?)"
  126. exit 1
  127. }
  128. echo "================================================================================"
  129. echo ""
  130. echo "Execution complete. Analyzing results..."
  131. echo ""
  132. # Find the session transcript. Because we ran claude from $TEST_PROJECT (a
  133. # unique tmp dir), its sessions live in their own ~/.claude/projects/ folder
  134. # and we can pick the most-recent one without racing other concurrent sessions.
  135. # Resolve the real path because macOS mktemp returns /var/... but claude
  136. # normalizes it to /private/var/... when naming the project dir.
  137. TEST_PROJECT_REAL=$(cd "$TEST_PROJECT" && pwd -P)
  138. # Claude normalizes the cwd to a directory name by replacing every non-alphanumeric
  139. # character with `-` (so `_`, `.`, `/` all become `-`).
  140. SESSION_DIR="$HOME/.claude/projects/$(echo "$TEST_PROJECT_REAL" | sed 's|[^a-zA-Z0-9]|-|g')"
  141. # `|| true` prevents pipefail killing the script if ls gets SIGPIPE'd by head.
  142. SESSION_FILE=$(ls -t "$SESSION_DIR"/*.jsonl 2>/dev/null | head -1 || true)
  143. if [ -z "$SESSION_FILE" ]; then
  144. echo "ERROR: Could not find session transcript file"
  145. echo "Looked in: $SESSION_DIR"
  146. exit 1
  147. fi
  148. echo "Analyzing session transcript: $(basename "$SESSION_FILE")"
  149. echo ""
  150. # Verification tests
  151. FAILED=0
  152. echo "=== Verification Tests ==="
  153. echo ""
  154. # Test 1: Skill was invoked
  155. echo "Test 1: Skill tool invoked..."
  156. if grep -q '"name":"Skill".*"skill":"superpowers:subagent-driven-development"' "$SESSION_FILE"; then
  157. echo " [PASS] subagent-driven-development skill was invoked"
  158. else
  159. echo " [FAIL] Skill was not invoked"
  160. FAILED=$((FAILED + 1))
  161. fi
  162. echo ""
  163. # Test 2: Subagents were used (Agent / Task tool — name varies by harness version)
  164. echo "Test 2: Subagents dispatched..."
  165. task_count=$(grep -cE '"name":"(Agent|Task)"' "$SESSION_FILE" || echo "0")
  166. if [ "$task_count" -ge 2 ]; then
  167. echo " [PASS] $task_count subagents dispatched"
  168. else
  169. echo " [FAIL] Only $task_count subagent(s) dispatched (expected >= 2)"
  170. FAILED=$((FAILED + 1))
  171. fi
  172. echo ""
  173. # Test 3: TodoWrite was used for tracking
  174. echo "Test 3: Task tracking..."
  175. todo_count=$(grep -c '"name":"TodoWrite"' "$SESSION_FILE" || echo "0")
  176. if [ "$todo_count" -ge 1 ]; then
  177. echo " [PASS] TodoWrite used $todo_count time(s) for task tracking"
  178. else
  179. echo " [FAIL] TodoWrite not used"
  180. FAILED=$((FAILED + 1))
  181. fi
  182. echo ""
  183. # Test 6: Implementation actually works
  184. echo "Test 6: Implementation verification..."
  185. if [ -f "$TEST_PROJECT/src/math.js" ]; then
  186. echo " [PASS] src/math.js created"
  187. if grep -q "export function add" "$TEST_PROJECT/src/math.js"; then
  188. echo " [PASS] add function exists"
  189. else
  190. echo " [FAIL] add function missing"
  191. FAILED=$((FAILED + 1))
  192. fi
  193. if grep -q "export function multiply" "$TEST_PROJECT/src/math.js"; then
  194. echo " [PASS] multiply function exists"
  195. else
  196. echo " [FAIL] multiply function missing"
  197. FAILED=$((FAILED + 1))
  198. fi
  199. else
  200. echo " [FAIL] src/math.js not created"
  201. FAILED=$((FAILED + 1))
  202. fi
  203. if [ -f "$TEST_PROJECT/test/math.test.js" ]; then
  204. echo " [PASS] test/math.test.js created"
  205. else
  206. echo " [FAIL] test/math.test.js not created"
  207. FAILED=$((FAILED + 1))
  208. fi
  209. # Try running tests
  210. if cd "$TEST_PROJECT" && npm test > test-output.txt 2>&1; then
  211. echo " [PASS] Tests pass"
  212. else
  213. echo " [FAIL] Tests failed"
  214. cat test-output.txt
  215. FAILED=$((FAILED + 1))
  216. fi
  217. echo ""
  218. # Test 7: Git commits show proper workflow
  219. echo "Test 7: Git commit history..."
  220. commit_count=$(git -C "$TEST_PROJECT" log --oneline | wc -l)
  221. if [ "$commit_count" -gt 2 ]; then # Initial + at least 2 task commits
  222. echo " [PASS] Multiple commits created ($commit_count total)"
  223. else
  224. echo " [FAIL] Too few commits ($commit_count, expected >2)"
  225. FAILED=$((FAILED + 1))
  226. fi
  227. echo ""
  228. # Test 8: Check for extra features (spec compliance should catch)
  229. echo "Test 8: No extra features added (spec compliance)..."
  230. if grep -q "export function divide\|export function power\|export function subtract" "$TEST_PROJECT/src/math.js" 2>/dev/null; then
  231. echo " [WARN] Extra features found (spec review should have caught this)"
  232. # Not failing on this as it tests reviewer effectiveness
  233. else
  234. echo " [PASS] No extra features added"
  235. fi
  236. echo ""
  237. # Token Usage Analysis
  238. echo "========================================="
  239. echo " Token Usage Analysis"
  240. echo "========================================="
  241. echo ""
  242. python3 "$SCRIPT_DIR/analyze-token-usage.py" "$SESSION_FILE"
  243. echo ""
  244. # Summary
  245. echo "========================================"
  246. echo " Test Summary"
  247. echo "========================================"
  248. echo ""
  249. if [ $FAILED -eq 0 ]; then
  250. echo "STATUS: PASSED"
  251. echo "All verification tests passed!"
  252. echo ""
  253. echo "The subagent-driven-development skill correctly:"
  254. echo " ✓ Reads plan once at start"
  255. echo " ✓ Provides full task text to subagents"
  256. echo " ✓ Enforces self-review"
  257. echo " ✓ Runs spec compliance before code quality"
  258. echo " ✓ Spec reviewer verifies independently"
  259. echo " ✓ Produces working implementation"
  260. exit 0
  261. else
  262. echo "STATUS: FAILED"
  263. echo "Failed $FAILED verification tests"
  264. echo ""
  265. echo "Output saved to: $OUTPUT_FILE"
  266. echo ""
  267. echo "Review the output to see what went wrong."
  268. exit 1
  269. fi