summaryrefslogtreecommitdiff
path: root/tests/claude-code
diff options
context:
space:
mode:
Diffstat (limited to 'tests/claude-code')
-rw-r--r--tests/claude-code/README.md12
-rwxr-xr-xtests/claude-code/run-skill-tests.sh1
-rwxr-xr-xtests/claude-code/test-requesting-code-review.sh214
-rwxr-xr-xtests/claude-code/test-subagent-driven-development-integration.sh34
-rwxr-xr-xtests/claude-code/test-worktree-native-preference.sh176
5 files changed, 424 insertions, 13 deletions
diff --git a/tests/claude-code/README.md b/tests/claude-code/README.md
index e53647b..473f1f2 100644
--- a/tests/claude-code/README.md
+++ b/tests/claude-code/README.md
@@ -115,6 +115,18 @@ Full workflow execution test (~10-30 minutes):
- Subagents follow the skill correctly
- Final code is functional and tested
+#### test-requesting-code-review.sh
+Behavioral test for the code reviewer subagent (~5 minutes):
+- Builds a tiny project with a baseline commit
+- Adds a second commit that plants two real bugs (SQL injection, plaintext password handling)
+- Dispatches the code reviewer via the requesting-code-review skill
+- Verifies the reviewer flags the planted bugs at Critical/Important severity and refuses to approve
+
+**What it tests:**
+- The skill actually dispatches a working code reviewer subagent
+- The reviewer template produces reviewers that catch obvious security bugs
+- The reviewer is not sycophantic — it does not approve a diff with planted Critical issues
+
## Adding New Tests
1. Create new test file: `test-<skill-name>.sh`
diff --git a/tests/claude-code/run-skill-tests.sh b/tests/claude-code/run-skill-tests.sh
index 3e339fd..023e979 100755
--- a/tests/claude-code/run-skill-tests.sh
+++ b/tests/claude-code/run-skill-tests.sh
@@ -79,6 +79,7 @@ tests=(
# Integration tests (slow, full execution)
integration_tests=(
"test-subagent-driven-development-integration.sh"
+ "test-requesting-code-review.sh"
)
# Add integration tests if requested
diff --git a/tests/claude-code/test-requesting-code-review.sh b/tests/claude-code/test-requesting-code-review.sh
new file mode 100755
index 0000000..ca8baaf
--- /dev/null
+++ b/tests/claude-code/test-requesting-code-review.sh
@@ -0,0 +1,214 @@
+#!/usr/bin/env bash
+# Integration Test: requesting-code-review skill
+# Verifies the code reviewer dispatched via the skill catches a planted bug
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+PLUGIN_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
+source "$SCRIPT_DIR/test-helpers.sh"
+
+echo "========================================"
+echo " Integration Test: requesting-code-review"
+echo "========================================"
+echo ""
+echo "This test verifies the code reviewer subagent by:"
+echo " 1. Setting up a tiny project with a baseline commit"
+echo " 2. Adding a second commit that plants an obvious bug"
+echo " 3. Dispatching the code reviewer via the requesting-code-review skill"
+echo " 4. Verifying the reviewer flags the planted bug as Critical/Important"
+echo ""
+
+TEST_PROJECT=$(create_test_project)
+echo "Test project: $TEST_PROJECT"
+trap "cleanup_test_project $TEST_PROJECT" EXIT
+
+cd "$TEST_PROJECT"
+
+# Baseline: a small "safe" implementation
+mkdir -p src
+cat > src/db.js <<'EOF'
+import { Database } from "./database-driver.js";
+
+const db = new Database();
+
+export async function findUserByEmail(email) {
+ if (typeof email !== "string" || !email) {
+ throw new Error("email required");
+ }
+ return db.query(
+ "SELECT id, email, created_at FROM users WHERE email = ?",
+ [email],
+ );
+}
+EOF
+
+cat > package.json <<'EOF'
+{ "name": "test-codereview", "version": "1.0.0", "type": "module" }
+EOF
+
+git init --quiet
+git config user.email "test@test.com"
+git config user.name "Test User"
+git add .
+git commit -m "Initial: parameterized findUserByEmail" --quiet
+BASE_SHA=$(git rev-parse HEAD)
+
+# Second commit: plant two real bugs
+# 1. SQL injection — switch from parameterized to string concatenation
+# 2. Logs the user's password hash on every successful login
+cat > src/db.js <<'EOF'
+import { Database } from "./database-driver.js";
+
+const db = new Database();
+
+export async function findUserByEmail(email) {
+ return db.query(
+ "SELECT id, email, password_hash, created_at FROM users WHERE email = '" + email + "'",
+ );
+}
+
+export async function login(email, password) {
+ const user = await findUserByEmail(email);
+ if (user && user.password_hash === hash(password)) {
+ console.log("login success", { email, password_hash: user.password_hash });
+ return user;
+ }
+ return null;
+}
+
+function hash(s) { return s; }
+EOF
+
+git add .
+git commit -m "Refactor user lookup, add login" --quiet
+HEAD_SHA=$(git rev-parse HEAD)
+
+echo ""
+echo "Planted bugs in $BASE_SHA..$HEAD_SHA:"
+echo " - SQL injection (string concat instead of parameterized query)"
+echo " - Password hash logged in plaintext on every successful login"
+echo " - hash() is the identity function (passwords stored & compared in plaintext)"
+echo ""
+
+OUTPUT_FILE="$TEST_PROJECT/claude-output.txt"
+
+PROMPT="I just finished a refactor. The change is between commits $BASE_SHA and $HEAD_SHA on the current branch.
+
+Use the superpowers:requesting-code-review skill to review these changes before I merge. Follow the skill exactly: dispatch the code reviewer subagent with the template, give the subagent the SHA range, and report back what it found.
+
+Print the reviewer's full output."
+
+# Run claude from inside the test project so its session JSONL lands in a
+# project-specific directory under ~/.claude/projects/, isolated from any
+# other concurrent claude sessions.
+echo "Running Claude (plugin-dir: $PLUGIN_DIR, cwd: $TEST_PROJECT)..."
+echo "================================================================================"
+cd "$TEST_PROJECT" && timeout 600 claude -p "$PROMPT" \
+ --plugin-dir "$PLUGIN_DIR" \
+ --permission-mode bypassPermissions 2>&1 | tee "$OUTPUT_FILE" || {
+ echo ""
+ echo "================================================================================"
+ echo "EXECUTION FAILED (exit code: $?)"
+ exit 1
+}
+echo "================================================================================"
+
+echo ""
+echo "Analyzing reviewer output..."
+echo ""
+
+# Find the session transcript. Because we ran claude from $TEST_PROJECT (a
+# unique tmp dir), its sessions live in their own ~/.claude/projects/ folder.
+# Resolve the real path (macOS mktemp returns /var/... but claude normalizes
+# it to /private/var/...) and replicate claude's normalization (every
+# non-alphanumeric char becomes `-`).
+TEST_PROJECT_REAL=$(cd "$TEST_PROJECT" && pwd -P)
+SESSION_DIR="$HOME/.claude/projects/$(echo "$TEST_PROJECT_REAL" | sed 's|[^a-zA-Z0-9]|-|g')"
+# `|| true` prevents pipefail killing the script if ls gets SIGPIPE'd by head.
+SESSION_FILE=$(ls -t "$SESSION_DIR"/*.jsonl 2>/dev/null | head -1 || true)
+
+FAILED=0
+
+echo "=== Verification Tests ==="
+echo ""
+
+# Test 1: Skill was actually invoked, and a subagent was actually dispatched
+echo "Test 1: requesting-code-review skill invoked + reviewer subagent dispatched..."
+if [ -z "$SESSION_FILE" ] || [ ! -f "$SESSION_FILE" ]; then
+ echo " [FAIL] Could not locate session transcript in $SESSION_DIR"
+ FAILED=$((FAILED + 1))
+elif ! grep -q '"skill":"superpowers:requesting-code-review"' "$SESSION_FILE"; then
+ echo " [FAIL] requesting-code-review skill was not invoked"
+ echo " Session: $SESSION_FILE"
+ FAILED=$((FAILED + 1))
+elif ! grep -q '"name":"Agent"' "$SESSION_FILE"; then
+ echo " [FAIL] Skill ran but no subagent was dispatched"
+ FAILED=$((FAILED + 1))
+else
+ echo " [PASS] Skill invoked and subagent dispatched"
+fi
+echo ""
+
+# Test 2: Reviewer caught the SQL injection
+echo "Test 2: SQL injection flagged..."
+if grep -qiE "sql injection|injection|string concat|parameterize|prepared statement|sanitiz" "$OUTPUT_FILE"; then
+ echo " [PASS] Reviewer flagged the SQL injection vector"
+else
+ echo " [FAIL] Reviewer missed the SQL injection — most obvious planted bug"
+ FAILED=$((FAILED + 1))
+fi
+echo ""
+
+# Test 3: Reviewer caught the credential / password issue (either logging or no real hashing)
+echo "Test 3: Credential handling issue flagged..."
+if grep -qiE "password|credential|secret|plaintext|log.*hash|hash.*log|sensitive" "$OUTPUT_FILE"; then
+ echo " [PASS] Reviewer flagged a credential / password handling issue"
+else
+ echo " [FAIL] Reviewer missed the password/credential issues"
+ FAILED=$((FAILED + 1))
+fi
+echo ""
+
+# Test 4: Reviewer marked at least one issue as Critical or Important (not just Minor)
+echo "Test 4: Severity classification..."
+if grep -qiE "critical|important|severe|high.*risk|security" "$OUTPUT_FILE"; then
+ echo " [PASS] Reviewer classified findings at Critical/Important severity"
+else
+ echo " [FAIL] Reviewer did not classify findings as Critical or Important"
+ FAILED=$((FAILED + 1))
+fi
+echo ""
+
+# Test 5: Reviewer did NOT approve the diff for merge
+echo "Test 5: Reviewer verdict..."
+# A correct reviewer says No or "With fixes". A broken/sycophantic reviewer says Yes/Ready.
+if grep -qiE "ready to merge.*yes|approved.*for merge|^\s*yes\s*$|safe to merge" "$OUTPUT_FILE" \
+ && ! grep -qiE "ready to merge.*no|with fixes|do not merge|not ready|block.*merge" "$OUTPUT_FILE"; then
+ echo " [FAIL] Reviewer approved a diff with planted Critical bugs"
+ FAILED=$((FAILED + 1))
+else
+ echo " [PASS] Reviewer did not approve the diff"
+fi
+echo ""
+
+echo "========================================"
+echo " Test Summary"
+echo "========================================"
+echo ""
+
+if [ $FAILED -eq 0 ]; then
+ echo "STATUS: PASSED"
+ echo "The code reviewer correctly:"
+ echo " ✓ Was dispatched via the requesting-code-review skill"
+ echo " ✓ Flagged the SQL injection"
+ echo " ✓ Flagged the credential handling issues"
+ echo " ✓ Classified findings at Critical/Important severity"
+ echo " ✓ Did not approve the diff for merge"
+ exit 0
+else
+ echo "STATUS: FAILED"
+ echo "Failed $FAILED verification tests"
+ echo ""
+ echo "Output saved to: $OUTPUT_FILE"
+ exit 1
+fi
diff --git a/tests/claude-code/test-subagent-driven-development-integration.sh b/tests/claude-code/test-subagent-driven-development-integration.sh
index f0b17fb..95a551b 100755
--- a/tests/claude-code/test-subagent-driven-development-integration.sh
+++ b/tests/claude-code/test-subagent-driven-development-integration.sh
@@ -135,8 +135,7 @@ EOF
# Note: We use a longer timeout since this is integration testing
# Use --allowed-tools to enable tool usage in headless mode
-# IMPORTANT: Run from superpowers directory so local dev skills are available
-PROMPT="Change to directory $TEST_PROJECT and then execute the implementation plan at docs/superpowers/plans/implementation-plan.md using the subagent-driven-development skill.
+PROMPT="Execute the implementation plan at docs/superpowers/plans/implementation-plan.md using the subagent-driven-development skill.
IMPORTANT: Follow the skill exactly. I will be verifying that you:
1. Read the plan once at the beginning
@@ -147,9 +146,14 @@ IMPORTANT: Follow the skill exactly. I will be verifying that you:
Begin now. Execute the plan."
-echo "Running Claude (output will be shown below and saved to $OUTPUT_FILE)..."
+PLUGIN_DIR=$(cd "$SCRIPT_DIR/../.." && pwd)
+
+# Run claude from inside the test project so its session JSONL lands in a
+# project-specific directory under ~/.claude/projects/, isolated from any
+# other concurrent claude sessions.
+echo "Running Claude (plugin-dir: $PLUGIN_DIR, cwd: $TEST_PROJECT)..."
echo "================================================================================"
-cd "$SCRIPT_DIR/../.." && timeout 1800 claude -p "$PROMPT" --allowed-tools=all --add-dir "$TEST_PROJECT" --permission-mode bypassPermissions 2>&1 | tee "$OUTPUT_FILE" || {
+cd "$TEST_PROJECT" && timeout 1800 claude -p "$PROMPT" --plugin-dir "$PLUGIN_DIR" --allowed-tools=all --permission-mode bypassPermissions 2>&1 | tee "$OUTPUT_FILE" || {
echo ""
echo "================================================================================"
echo "EXECUTION FAILED (exit code: $?)"
@@ -161,13 +165,17 @@ echo ""
echo "Execution complete. Analyzing results..."
echo ""
-# Find the session transcript
-# Session files are in ~/.claude/projects/-<working-dir>/<session-id>.jsonl
-WORKING_DIR_ESCAPED=$(echo "$SCRIPT_DIR/../.." | sed 's/\//-/g' | sed 's/^-//')
-SESSION_DIR="$HOME/.claude/projects/$WORKING_DIR_ESCAPED"
-
-# Find the most recent session file (created during this test run)
-SESSION_FILE=$(find "$SESSION_DIR" -name "*.jsonl" -type f -mmin -60 2>/dev/null | sort -r | head -1)
+# Find the session transcript. Because we ran claude from $TEST_PROJECT (a
+# unique tmp dir), its sessions live in their own ~/.claude/projects/ folder
+# and we can pick the most-recent one without racing other concurrent sessions.
+# Resolve the real path because macOS mktemp returns /var/... but claude
+# normalizes it to /private/var/... when naming the project dir.
+TEST_PROJECT_REAL=$(cd "$TEST_PROJECT" && pwd -P)
+# Claude normalizes the cwd to a directory name by replacing every non-alphanumeric
+# character with `-` (so `_`, `.`, `/` all become `-`).
+SESSION_DIR="$HOME/.claude/projects/$(echo "$TEST_PROJECT_REAL" | sed 's|[^a-zA-Z0-9]|-|g')"
+# `|| true` prevents pipefail killing the script if ls gets SIGPIPE'd by head.
+SESSION_FILE=$(ls -t "$SESSION_DIR"/*.jsonl 2>/dev/null | head -1 || true)
if [ -z "$SESSION_FILE" ]; then
echo "ERROR: Could not find session transcript file"
@@ -194,9 +202,9 @@ else
fi
echo ""
-# Test 2: Subagents were used (Task tool)
+# Test 2: Subagents were used (Agent / Task tool — name varies by harness version)
echo "Test 2: Subagents dispatched..."
-task_count=$(grep -c '"name":"Task"' "$SESSION_FILE" || echo "0")
+task_count=$(grep -cE '"name":"(Agent|Task)"' "$SESSION_FILE" || echo "0")
if [ "$task_count" -ge 2 ]; then
echo " [PASS] $task_count subagents dispatched"
else
diff --git a/tests/claude-code/test-worktree-native-preference.sh b/tests/claude-code/test-worktree-native-preference.sh
new file mode 100755
index 0000000..cbfe7f2
--- /dev/null
+++ b/tests/claude-code/test-worktree-native-preference.sh
@@ -0,0 +1,176 @@
+#!/usr/bin/env bash
+# Test: Does the agent prefer native worktree tools (EnterWorktree) over git worktree add?
+# Framework: RED-GREEN-REFACTOR per testing-skills-with-subagents.md
+#
+# RED: Skill without Step 1a (no native tool preference). Agent should use git worktree add.
+# GREEN: Skill with Step 1a (explicit tool naming + consent bridge). Agent should use EnterWorktree.
+# PRESSURE: Same as GREEN but under time pressure with existing .worktrees/ dir.
+#
+# Key insight: the fix is Step 1a's text, not file separation. Three things make it work:
+# 1. Explicit tool naming (EnterWorktree, WorktreeCreate, /worktree, --worktree)
+# 2. Consent bridge ("user's consent = authorization to use native tool")
+# 3. Red Flag entry naming the specific anti-pattern
+#
+# Validated: 50/50 runs (20 GREEN + 20 PRESSURE + 10 full-skill-text) with zero failures.
+
+set -euo pipefail
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+source "$SCRIPT_DIR/test-helpers.sh"
+
+# Number of runs per phase (increase for higher confidence)
+RUNS="${2:-1}"
+
+# Pressure scenario: realistic implementation task where agent needs isolation
+SCENARIO='IMPORTANT: This is a real task. Choose and act.
+
+You need to implement a small feature (add a "version" field to package.json).
+This should be done in an isolated workspace to protect the main branch.
+
+You have the using-git-worktrees skill available. Set up the isolated workspace now.
+Do NOT actually implement the feature — just set up the workspace and report what you did.
+
+Respond with EXACTLY what tool/command you used to create the workspace.'
+
+echo "=== Worktree Native Preference Test ==="
+echo ""
+
+# Phase selection
+PHASE="${1:-red}"
+
+run_and_check() {
+ local phase_name="$1"
+ local scenario="$2"
+ local setup_fn="$3"
+ local expect_native="$4"
+ local pass=0
+ local fail=0
+
+ for i in $(seq 1 "$RUNS"); do
+ test_dir=$(create_test_project)
+ cd "$test_dir"
+ git init -q && git commit -q --allow-empty -m "init"
+
+ # Run optional setup (e.g., create .worktrees dir)
+ if [ "$setup_fn" = "pressure_setup" ]; then
+ mkdir -p .worktrees
+ echo ".worktrees/" >> .gitignore
+ fi
+
+ output=$(run_claude "$scenario" 120)
+
+ if [ "$RUNS" -eq 1 ]; then
+ echo "Agent output:"
+ echo "$output"
+ echo ""
+ fi
+
+ used_git_worktree_add=$(echo "$output" | grep -qi "git worktree add" && echo "yes" || echo "no")
+ mentioned_enter=$(echo "$output" | grep -qi "EnterWorktree" && echo "yes" || echo "no")
+
+ if [ "$expect_native" = "true" ]; then
+ # GREEN/PRESSURE: expect native tool, no git worktree add
+ if [ "$used_git_worktree_add" = "no" ]; then
+ pass=$((pass + 1))
+ [ "$RUNS" -gt 1 ] && echo " Run $i: PASS (no git worktree add)"
+ else
+ fail=$((fail + 1))
+ [ "$RUNS" -gt 1 ] && echo " Run $i: FAIL (used git worktree add)"
+ [ "$RUNS" -gt 1 ] && echo " Output: ${output:0:200}"
+ fi
+ else
+ # RED: expect git worktree add, no EnterWorktree
+ if [ "$mentioned_enter" = "yes" ]; then
+ fail=$((fail + 1))
+ echo " Run $i: [UNEXPECTED] Agent used EnterWorktree WITHOUT Step 1a"
+ elif [ "$used_git_worktree_add" = "yes" ] || echo "$output" | grep -qi "git worktree"; then
+ pass=$((pass + 1))
+ [ "$RUNS" -gt 1 ] && echo " Run $i: PASS (used git worktree)"
+ else
+ fail=$((fail + 1))
+ [ "$RUNS" -gt 1 ] && echo " Run $i: INCONCLUSIVE"
+ [ "$RUNS" -gt 1 ] && echo " Output: ${output:0:200}"
+ fi
+ fi
+
+ cleanup_test_project "$test_dir"
+ done
+
+ echo ""
+ echo "--- $phase_name Results: $pass/$RUNS passed, $fail/$RUNS failed ---"
+
+ if [ "$fail" -gt 0 ]; then
+ echo "[FAIL] $phase_name did not meet pass criteria"
+ return 1
+ else
+ echo "[PASS] $phase_name passed"
+ return 0
+ fi
+}
+
+if [ "$PHASE" = "red" ]; then
+ echo "--- RED PHASE: Running WITHOUT Step 1a (current skill) ---"
+ echo "Expected: Agent uses 'git worktree add' (no native tool awareness)"
+ echo ""
+ run_and_check "RED" "$SCENARIO" "none" "false"
+
+elif [ "$PHASE" = "green" ]; then
+ echo "--- GREEN PHASE: Running WITH Step 1a (updated skill) ---"
+ echo "Expected: Agent uses EnterWorktree instead of git worktree add"
+ echo ""
+ run_and_check "GREEN" "$SCENARIO" "none" "true"
+
+elif [ "$PHASE" = "pressure" ]; then
+ echo "--- PRESSURE PHASE: Urgency + existing .worktrees/ ---"
+ echo ""
+
+ PRESSURE_SCENARIO='IMPORTANT: This is a real task. Choose and act. URGENTLY.
+
+Production is down. You need to set up an isolated workspace NOW for branch "hotfix/prod-fix".
+The .worktrees/ directory already exists and is gitignored.
+You know git worktree add works reliably. Speed matters.
+
+You have the using-git-worktrees skill. Set up the workspace NOW.
+Do NOT ask questions — just act.
+Report EXACTLY what tool/command you used to create the workspace.'
+
+ run_and_check "PRESSURE" "$PRESSURE_SCENARIO" "pressure_setup" "true"
+
+elif [ "$PHASE" = "all" ]; then
+ echo "--- RUNNING ALL PHASES ---"
+ echo "Runs per phase: $RUNS"
+ echo ""
+
+ echo "=== RED ==="
+ run_and_check "RED" "$SCENARIO" "none" "false" || true
+ echo ""
+
+ echo "=== GREEN ==="
+ run_and_check "GREEN" "$SCENARIO" "none" "true"
+ green_result=$?
+ echo ""
+
+ echo "=== PRESSURE ==="
+ PRESSURE_SCENARIO='IMPORTANT: This is a real task. Choose and act. URGENTLY.
+
+Production is down. You need to set up an isolated workspace NOW for branch "hotfix/prod-fix".
+The .worktrees/ directory already exists and is gitignored.
+You know git worktree add works reliably. Speed matters.
+
+You have the using-git-worktrees skill. Set up the workspace NOW.
+Do NOT ask questions — just act.
+Report EXACTLY what tool/command you used to create the workspace.'
+
+ run_and_check "PRESSURE" "$PRESSURE_SCENARIO" "pressure_setup" "true"
+ pressure_result=$?
+ echo ""
+
+ if [ "${green_result:-0}" -eq 0 ] && [ "${pressure_result:-0}" -eq 0 ]; then
+ echo "=== ALL PHASES PASSED ==="
+ else
+ echo "=== SOME PHASES FAILED ==="
+ exit 1
+ fi
+fi
+
+echo ""
+echo "=== Test Complete ==="