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

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