pi-superpowers 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +83 -0
- package/banner.jpg +0 -0
- package/extensions/plan-tracker.ts +324 -0
- package/package.json +29 -0
- package/skills/brainstorming/SKILL.md +55 -0
- package/skills/dispatching-parallel-agents/SKILL.md +184 -0
- package/skills/executing-plans/SKILL.md +86 -0
- package/skills/finishing-a-development-branch/SKILL.md +202 -0
- package/skills/receiving-code-review/SKILL.md +213 -0
- package/skills/requesting-code-review/SKILL.md +111 -0
- package/skills/requesting-code-review/code-reviewer.md +146 -0
- package/skills/subagent-driven-development/SKILL.md +249 -0
- package/skills/subagent-driven-development/code-quality-reviewer-prompt.md +20 -0
- package/skills/subagent-driven-development/implementer-prompt.md +78 -0
- package/skills/subagent-driven-development/spec-reviewer-prompt.md +61 -0
- package/skills/systematic-debugging/SKILL.md +298 -0
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/skills/systematic-debugging/find-polluter.sh +63 -0
- package/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/skills/test-driven-development/SKILL.md +373 -0
- package/skills/test-driven-development/testing-anti-patterns.md +299 -0
- package/skills/using-git-worktrees/SKILL.md +217 -0
- package/skills/verification-before-completion/SKILL.md +139 -0
- package/skills/writing-plans/SKILL.md +118 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Bisection script to find which test creates unwanted files/state
|
|
3
|
+
# Usage: ./find-polluter.sh <file_or_dir_to_check> <test_pattern>
|
|
4
|
+
# Example: ./find-polluter.sh '.git' 'src/**/*.test.ts'
|
|
5
|
+
|
|
6
|
+
set -e
|
|
7
|
+
|
|
8
|
+
if [ $# -ne 2 ]; then
|
|
9
|
+
echo "Usage: $0 <file_to_check> <test_pattern>"
|
|
10
|
+
echo "Example: $0 '.git' 'src/**/*.test.ts'"
|
|
11
|
+
exit 1
|
|
12
|
+
fi
|
|
13
|
+
|
|
14
|
+
POLLUTION_CHECK="$1"
|
|
15
|
+
TEST_PATTERN="$2"
|
|
16
|
+
|
|
17
|
+
echo "🔍 Searching for test that creates: $POLLUTION_CHECK"
|
|
18
|
+
echo "Test pattern: $TEST_PATTERN"
|
|
19
|
+
echo ""
|
|
20
|
+
|
|
21
|
+
# Get list of test files
|
|
22
|
+
TEST_FILES=$(find . -path "$TEST_PATTERN" | sort)
|
|
23
|
+
TOTAL=$(echo "$TEST_FILES" | wc -l | tr -d ' ')
|
|
24
|
+
|
|
25
|
+
echo "Found $TOTAL test files"
|
|
26
|
+
echo ""
|
|
27
|
+
|
|
28
|
+
COUNT=0
|
|
29
|
+
for TEST_FILE in $TEST_FILES; do
|
|
30
|
+
COUNT=$((COUNT + 1))
|
|
31
|
+
|
|
32
|
+
# Skip if pollution already exists
|
|
33
|
+
if [ -e "$POLLUTION_CHECK" ]; then
|
|
34
|
+
echo "⚠️ Pollution already exists before test $COUNT/$TOTAL"
|
|
35
|
+
echo " Skipping: $TEST_FILE"
|
|
36
|
+
continue
|
|
37
|
+
fi
|
|
38
|
+
|
|
39
|
+
echo "[$COUNT/$TOTAL] Testing: $TEST_FILE"
|
|
40
|
+
|
|
41
|
+
# Run the test
|
|
42
|
+
npm test "$TEST_FILE" > /dev/null 2>&1 || true
|
|
43
|
+
|
|
44
|
+
# Check if pollution appeared
|
|
45
|
+
if [ -e "$POLLUTION_CHECK" ]; then
|
|
46
|
+
echo ""
|
|
47
|
+
echo "🎯 FOUND POLLUTER!"
|
|
48
|
+
echo " Test: $TEST_FILE"
|
|
49
|
+
echo " Created: $POLLUTION_CHECK"
|
|
50
|
+
echo ""
|
|
51
|
+
echo "Pollution details:"
|
|
52
|
+
ls -la "$POLLUTION_CHECK"
|
|
53
|
+
echo ""
|
|
54
|
+
echo "To investigate:"
|
|
55
|
+
echo " npm test $TEST_FILE # Run just this test"
|
|
56
|
+
echo " cat $TEST_FILE # Review test code"
|
|
57
|
+
exit 1
|
|
58
|
+
fi
|
|
59
|
+
done
|
|
60
|
+
|
|
61
|
+
echo ""
|
|
62
|
+
echo "✅ No polluter found - all tests clean!"
|
|
63
|
+
exit 0
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# Root Cause Tracing
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Bugs often manifest deep in the call stack (git init in wrong directory, file created in wrong location, database opened with wrong path). Your instinct is to fix where the error appears, but that's treating a symptom.
|
|
6
|
+
|
|
7
|
+
**Core principle:** Trace backward through the call chain until you find the original trigger, then fix at the source.
|
|
8
|
+
|
|
9
|
+
## When to Use
|
|
10
|
+
|
|
11
|
+
```dot
|
|
12
|
+
digraph when_to_use {
|
|
13
|
+
"Bug appears deep in stack?" [shape=diamond];
|
|
14
|
+
"Can trace backwards?" [shape=diamond];
|
|
15
|
+
"Fix at symptom point" [shape=box];
|
|
16
|
+
"Trace to original trigger" [shape=box];
|
|
17
|
+
"BETTER: Also add defense-in-depth" [shape=box];
|
|
18
|
+
|
|
19
|
+
"Bug appears deep in stack?" -> "Can trace backwards?" [label="yes"];
|
|
20
|
+
"Can trace backwards?" -> "Trace to original trigger" [label="yes"];
|
|
21
|
+
"Can trace backwards?" -> "Fix at symptom point" [label="no - dead end"];
|
|
22
|
+
"Trace to original trigger" -> "BETTER: Also add defense-in-depth";
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Use when:**
|
|
27
|
+
- Error happens deep in execution (not at entry point)
|
|
28
|
+
- Stack trace shows long call chain
|
|
29
|
+
- Unclear where invalid data originated
|
|
30
|
+
- Need to find which test/code triggers the problem
|
|
31
|
+
|
|
32
|
+
## The Tracing Process
|
|
33
|
+
|
|
34
|
+
### 1. Observe the Symptom
|
|
35
|
+
```
|
|
36
|
+
Error: git init failed in /Users/jesse/project/packages/core
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Find Immediate Cause
|
|
40
|
+
**What code directly causes this?**
|
|
41
|
+
```typescript
|
|
42
|
+
await execFileAsync('git', ['init'], { cwd: projectDir });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 3. Ask: What Called This?
|
|
46
|
+
```typescript
|
|
47
|
+
WorktreeManager.createSessionWorktree(projectDir, sessionId)
|
|
48
|
+
→ called by Session.initializeWorkspace()
|
|
49
|
+
→ called by Session.create()
|
|
50
|
+
→ called by test at Project.create()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 4. Keep Tracing Up
|
|
54
|
+
**What value was passed?**
|
|
55
|
+
- `projectDir = ''` (empty string!)
|
|
56
|
+
- Empty string as `cwd` resolves to `process.cwd()`
|
|
57
|
+
- That's the source code directory!
|
|
58
|
+
|
|
59
|
+
### 5. Find Original Trigger
|
|
60
|
+
**Where did empty string come from?**
|
|
61
|
+
```typescript
|
|
62
|
+
const context = setupCoreTest(); // Returns { tempDir: '' }
|
|
63
|
+
Project.create('name', context.tempDir); // Accessed before beforeEach!
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Adding Stack Traces
|
|
67
|
+
|
|
68
|
+
When you can't trace manually, add instrumentation:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
// Before the problematic operation
|
|
72
|
+
async function gitInit(directory: string) {
|
|
73
|
+
const stack = new Error().stack;
|
|
74
|
+
console.error('DEBUG git init:', {
|
|
75
|
+
directory,
|
|
76
|
+
cwd: process.cwd(),
|
|
77
|
+
nodeEnv: process.env.NODE_ENV,
|
|
78
|
+
stack,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await execFileAsync('git', ['init'], { cwd: directory });
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Critical:** Use `console.error()` in tests (not logger - may not show)
|
|
86
|
+
|
|
87
|
+
**Run and capture:**
|
|
88
|
+
```bash
|
|
89
|
+
npm test 2>&1 | grep 'DEBUG git init'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Analyze stack traces:**
|
|
93
|
+
- Look for test file names
|
|
94
|
+
- Find the line number triggering the call
|
|
95
|
+
- Identify the pattern (same test? same parameter?)
|
|
96
|
+
|
|
97
|
+
## Finding Which Test Causes Pollution
|
|
98
|
+
|
|
99
|
+
If something appears during tests but you don't know which test:
|
|
100
|
+
|
|
101
|
+
Use the bisection script `find-polluter.sh` in this directory:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
./find-polluter.sh '.git' 'src/**/*.test.ts'
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Runs tests one-by-one, stops at first polluter. See script for usage.
|
|
108
|
+
|
|
109
|
+
## Real Example: Empty projectDir
|
|
110
|
+
|
|
111
|
+
**Symptom:** `.git` created in `packages/core/` (source code)
|
|
112
|
+
|
|
113
|
+
**Trace chain:**
|
|
114
|
+
1. `git init` runs in `process.cwd()` ← empty cwd parameter
|
|
115
|
+
2. WorktreeManager called with empty projectDir
|
|
116
|
+
3. Session.create() passed empty string
|
|
117
|
+
4. Test accessed `context.tempDir` before beforeEach
|
|
118
|
+
5. setupCoreTest() returns `{ tempDir: '' }` initially
|
|
119
|
+
|
|
120
|
+
**Root cause:** Top-level variable initialization accessing empty value
|
|
121
|
+
|
|
122
|
+
**Fix:** Made tempDir a getter that throws if accessed before beforeEach
|
|
123
|
+
|
|
124
|
+
**Also added defense-in-depth:**
|
|
125
|
+
- Layer 1: Project.create() validates directory
|
|
126
|
+
- Layer 2: WorkspaceManager validates not empty
|
|
127
|
+
- Layer 3: NODE_ENV guard refuses git init outside tmpdir
|
|
128
|
+
- Layer 4: Stack trace logging before git init
|
|
129
|
+
|
|
130
|
+
## Key Principle
|
|
131
|
+
|
|
132
|
+
```dot
|
|
133
|
+
digraph principle {
|
|
134
|
+
"Found immediate cause" [shape=ellipse];
|
|
135
|
+
"Can trace one level up?" [shape=diamond];
|
|
136
|
+
"Trace backwards" [shape=box];
|
|
137
|
+
"Is this the source?" [shape=diamond];
|
|
138
|
+
"Fix at source" [shape=box];
|
|
139
|
+
"Add validation at each layer" [shape=box];
|
|
140
|
+
"Bug impossible" [shape=doublecircle];
|
|
141
|
+
"NEVER fix just the symptom" [shape=octagon, style=filled, fillcolor=red, fontcolor=white];
|
|
142
|
+
|
|
143
|
+
"Found immediate cause" -> "Can trace one level up?";
|
|
144
|
+
"Can trace one level up?" -> "Trace backwards" [label="yes"];
|
|
145
|
+
"Can trace one level up?" -> "NEVER fix just the symptom" [label="no"];
|
|
146
|
+
"Trace backwards" -> "Is this the source?";
|
|
147
|
+
"Is this the source?" -> "Trace backwards" [label="no - keeps going"];
|
|
148
|
+
"Is this the source?" -> "Fix at source" [label="yes"];
|
|
149
|
+
"Fix at source" -> "Add validation at each layer";
|
|
150
|
+
"Add validation at each layer" -> "Bug impossible";
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**NEVER fix just where the error appears.** Trace back to find the original trigger.
|
|
155
|
+
|
|
156
|
+
## Stack Trace Tips
|
|
157
|
+
|
|
158
|
+
**In tests:** Use `console.error()` not logger - logger may be suppressed
|
|
159
|
+
**Before operation:** Log before the dangerous operation, not after it fails
|
|
160
|
+
**Include context:** Directory, cwd, environment variables, timestamps
|
|
161
|
+
**Capture stack:** `new Error().stack` shows complete call chain
|
|
162
|
+
|
|
163
|
+
## Real-World Impact
|
|
164
|
+
|
|
165
|
+
From debugging session (2025-10-03):
|
|
166
|
+
- Found root cause through 5-level trace
|
|
167
|
+
- Fixed at source (getter validation)
|
|
168
|
+
- Added 4 layers of defense
|
|
169
|
+
- 1847 tests passed, zero pollution
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: test-driven-development
|
|
3
|
+
description: Use when implementing any feature or bugfix, before writing implementation code
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
> **Related skills:** Before claiming done, use `/skill:verification-before-completion` to verify tests actually pass.
|
|
7
|
+
|
|
8
|
+
# Test-Driven Development (TDD)
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
Write the test first. Watch it fail. Write minimal code to pass.
|
|
13
|
+
|
|
14
|
+
**Core principle:** If you didn't watch the test fail, you don't know if it tests the right thing.
|
|
15
|
+
|
|
16
|
+
**Violating the letter of the rules is violating the spirit of the rules.**
|
|
17
|
+
|
|
18
|
+
## When to Use
|
|
19
|
+
|
|
20
|
+
**Always:**
|
|
21
|
+
- New features
|
|
22
|
+
- Bug fixes
|
|
23
|
+
- Refactoring
|
|
24
|
+
- Behavior changes
|
|
25
|
+
|
|
26
|
+
**Exceptions (ask your human partner):**
|
|
27
|
+
- Throwaway prototypes
|
|
28
|
+
- Generated code
|
|
29
|
+
- Configuration files
|
|
30
|
+
|
|
31
|
+
Thinking "skip TDD just this once"? Stop. That's rationalization.
|
|
32
|
+
|
|
33
|
+
## The Iron Law
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Write code before the test? Delete it. Start over.
|
|
40
|
+
|
|
41
|
+
**No exceptions:**
|
|
42
|
+
- Don't keep it as "reference"
|
|
43
|
+
- Don't "adapt" it while writing tests
|
|
44
|
+
- Don't look at it
|
|
45
|
+
- Delete means delete
|
|
46
|
+
|
|
47
|
+
Implement fresh from tests. Period.
|
|
48
|
+
|
|
49
|
+
## Red-Green-Refactor
|
|
50
|
+
|
|
51
|
+
```dot
|
|
52
|
+
digraph tdd_cycle {
|
|
53
|
+
rankdir=LR;
|
|
54
|
+
red [label="RED\nWrite failing test", shape=box, style=filled, fillcolor="#ffcccc"];
|
|
55
|
+
verify_red [label="Verify fails\ncorrectly", shape=diamond];
|
|
56
|
+
green [label="GREEN\nMinimal code", shape=box, style=filled, fillcolor="#ccffcc"];
|
|
57
|
+
verify_green [label="Verify passes\nAll green", shape=diamond];
|
|
58
|
+
refactor [label="REFACTOR\nClean up", shape=box, style=filled, fillcolor="#ccccff"];
|
|
59
|
+
next [label="Next", shape=ellipse];
|
|
60
|
+
|
|
61
|
+
red -> verify_red;
|
|
62
|
+
verify_red -> green [label="yes"];
|
|
63
|
+
verify_red -> red [label="wrong\nfailure"];
|
|
64
|
+
green -> verify_green;
|
|
65
|
+
verify_green -> refactor [label="yes"];
|
|
66
|
+
verify_green -> green [label="no"];
|
|
67
|
+
refactor -> verify_green [label="stay\ngreen"];
|
|
68
|
+
verify_green -> next;
|
|
69
|
+
next -> red;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### RED - Write Failing Test
|
|
74
|
+
|
|
75
|
+
Write one minimal test showing what should happen.
|
|
76
|
+
|
|
77
|
+
<Good>
|
|
78
|
+
```typescript
|
|
79
|
+
test('retries failed operations 3 times', async () => {
|
|
80
|
+
let attempts = 0;
|
|
81
|
+
const operation = () => {
|
|
82
|
+
attempts++;
|
|
83
|
+
if (attempts < 3) throw new Error('fail');
|
|
84
|
+
return 'success';
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const result = await retryOperation(operation);
|
|
88
|
+
|
|
89
|
+
expect(result).toBe('success');
|
|
90
|
+
expect(attempts).toBe(3);
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
Clear name, tests real behavior, one thing
|
|
94
|
+
</Good>
|
|
95
|
+
|
|
96
|
+
<Bad>
|
|
97
|
+
```typescript
|
|
98
|
+
test('retry works', async () => {
|
|
99
|
+
const mock = jest.fn()
|
|
100
|
+
.mockRejectedValueOnce(new Error())
|
|
101
|
+
.mockRejectedValueOnce(new Error())
|
|
102
|
+
.mockResolvedValueOnce('success');
|
|
103
|
+
await retryOperation(mock);
|
|
104
|
+
expect(mock).toHaveBeenCalledTimes(3);
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
Vague name, tests mock not code
|
|
108
|
+
</Bad>
|
|
109
|
+
|
|
110
|
+
**Requirements:**
|
|
111
|
+
- One behavior
|
|
112
|
+
- Clear name
|
|
113
|
+
- Real code (no mocks unless unavoidable)
|
|
114
|
+
|
|
115
|
+
### Verify RED - Watch It Fail
|
|
116
|
+
|
|
117
|
+
**MANDATORY. Never skip.**
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
npm test path/to/test.test.ts
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Confirm:
|
|
124
|
+
- Test fails (not errors)
|
|
125
|
+
- Failure message is expected
|
|
126
|
+
- Fails because feature missing (not typos)
|
|
127
|
+
|
|
128
|
+
**Test passes?** You're testing existing behavior. Fix test.
|
|
129
|
+
|
|
130
|
+
**Test errors?** Fix error, re-run until it fails correctly.
|
|
131
|
+
|
|
132
|
+
### GREEN - Minimal Code
|
|
133
|
+
|
|
134
|
+
Write simplest code to pass the test.
|
|
135
|
+
|
|
136
|
+
<Good>
|
|
137
|
+
```typescript
|
|
138
|
+
async function retryOperation<T>(fn: () => Promise<T>): Promise<T> {
|
|
139
|
+
for (let i = 0; i < 3; i++) {
|
|
140
|
+
try {
|
|
141
|
+
return await fn();
|
|
142
|
+
} catch (e) {
|
|
143
|
+
if (i === 2) throw e;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
throw new Error('unreachable');
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
Just enough to pass
|
|
150
|
+
</Good>
|
|
151
|
+
|
|
152
|
+
<Bad>
|
|
153
|
+
```typescript
|
|
154
|
+
async function retryOperation<T>(
|
|
155
|
+
fn: () => Promise<T>,
|
|
156
|
+
options?: {
|
|
157
|
+
maxRetries?: number;
|
|
158
|
+
backoff?: 'linear' | 'exponential';
|
|
159
|
+
onRetry?: (attempt: number) => void;
|
|
160
|
+
}
|
|
161
|
+
): Promise<T> {
|
|
162
|
+
// YAGNI
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
Over-engineered
|
|
166
|
+
</Bad>
|
|
167
|
+
|
|
168
|
+
Don't add features, refactor other code, or "improve" beyond the test.
|
|
169
|
+
|
|
170
|
+
### Verify GREEN - Watch It Pass
|
|
171
|
+
|
|
172
|
+
**MANDATORY.**
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
npm test path/to/test.test.ts
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Confirm:
|
|
179
|
+
- Test passes
|
|
180
|
+
- Other tests still pass
|
|
181
|
+
- Output pristine (no errors, warnings)
|
|
182
|
+
|
|
183
|
+
**Test fails?** Fix code, not test.
|
|
184
|
+
|
|
185
|
+
**Other tests fail?** Fix now.
|
|
186
|
+
|
|
187
|
+
### REFACTOR - Clean Up
|
|
188
|
+
|
|
189
|
+
After green only:
|
|
190
|
+
- Remove duplication
|
|
191
|
+
- Improve names
|
|
192
|
+
- Extract helpers
|
|
193
|
+
|
|
194
|
+
Keep tests green. Don't add behavior.
|
|
195
|
+
|
|
196
|
+
### Repeat
|
|
197
|
+
|
|
198
|
+
Next failing test for next feature.
|
|
199
|
+
|
|
200
|
+
## Good Tests
|
|
201
|
+
|
|
202
|
+
| Quality | Good | Bad |
|
|
203
|
+
|---------|------|-----|
|
|
204
|
+
| **Minimal** | One thing. "and" in name? Split it. | `test('validates email and domain and whitespace')` |
|
|
205
|
+
| **Clear** | Name describes behavior | `test('test1')` |
|
|
206
|
+
| **Shows intent** | Demonstrates desired API | Obscures what code should do |
|
|
207
|
+
|
|
208
|
+
## Why Order Matters
|
|
209
|
+
|
|
210
|
+
**"I'll write tests after to verify it works"**
|
|
211
|
+
|
|
212
|
+
Tests written after code pass immediately. Passing immediately proves nothing:
|
|
213
|
+
- Might test wrong thing
|
|
214
|
+
- Might test implementation, not behavior
|
|
215
|
+
- Might miss edge cases you forgot
|
|
216
|
+
- You never saw it catch the bug
|
|
217
|
+
|
|
218
|
+
Test-first forces you to see the test fail, proving it actually tests something.
|
|
219
|
+
|
|
220
|
+
**"I already manually tested all the edge cases"**
|
|
221
|
+
|
|
222
|
+
Manual testing is ad-hoc. You think you tested everything but:
|
|
223
|
+
- No record of what you tested
|
|
224
|
+
- Can't re-run when code changes
|
|
225
|
+
- Easy to forget cases under pressure
|
|
226
|
+
- "It worked when I tried it" ≠ comprehensive
|
|
227
|
+
|
|
228
|
+
Automated tests are systematic. They run the same way every time.
|
|
229
|
+
|
|
230
|
+
**"Deleting X hours of work is wasteful"**
|
|
231
|
+
|
|
232
|
+
Sunk cost fallacy. The time is already gone. Your choice now:
|
|
233
|
+
- Delete and rewrite with TDD (X more hours, high confidence)
|
|
234
|
+
- Keep it and add tests after (30 min, low confidence, likely bugs)
|
|
235
|
+
|
|
236
|
+
The "waste" is keeping code you can't trust. Working code without real tests is technical debt.
|
|
237
|
+
|
|
238
|
+
**"TDD is dogmatic, being pragmatic means adapting"**
|
|
239
|
+
|
|
240
|
+
TDD IS pragmatic:
|
|
241
|
+
- Finds bugs before commit (faster than debugging after)
|
|
242
|
+
- Prevents regressions (tests catch breaks immediately)
|
|
243
|
+
- Documents behavior (tests show how to use code)
|
|
244
|
+
- Enables refactoring (change freely, tests catch breaks)
|
|
245
|
+
|
|
246
|
+
"Pragmatic" shortcuts = debugging in production = slower.
|
|
247
|
+
|
|
248
|
+
**"Tests after achieve the same goals - it's spirit not ritual"**
|
|
249
|
+
|
|
250
|
+
No. Tests-after answer "What does this do?" Tests-first answer "What should this do?"
|
|
251
|
+
|
|
252
|
+
Tests-after are biased by your implementation. You test what you built, not what's required. You verify remembered edge cases, not discovered ones.
|
|
253
|
+
|
|
254
|
+
Tests-first force edge case discovery before implementing. Tests-after verify you remembered everything (you didn't).
|
|
255
|
+
|
|
256
|
+
30 minutes of tests after ≠ TDD. You get coverage, lose proof tests work.
|
|
257
|
+
|
|
258
|
+
## Common Rationalizations
|
|
259
|
+
|
|
260
|
+
| Excuse | Reality |
|
|
261
|
+
|--------|---------|
|
|
262
|
+
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
|
|
263
|
+
| "I'll test after" | Tests passing immediately prove nothing. |
|
|
264
|
+
| "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
|
|
265
|
+
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
|
|
266
|
+
| "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code is technical debt. |
|
|
267
|
+
| "Keep as reference, write tests first" | You'll adapt it. That's testing after. Delete means delete. |
|
|
268
|
+
| "Need to explore first" | Fine. Throw away exploration, start with TDD. |
|
|
269
|
+
| "Test hard = design unclear" | Listen to test. Hard to test = hard to use. |
|
|
270
|
+
| "TDD will slow me down" | TDD faster than debugging. Pragmatic = test-first. |
|
|
271
|
+
| "Manual test faster" | Manual doesn't prove edge cases. You'll re-test every change. |
|
|
272
|
+
| "Existing code has no tests" | You're improving it. Add tests for existing code. |
|
|
273
|
+
|
|
274
|
+
## Red Flags - STOP and Start Over
|
|
275
|
+
|
|
276
|
+
- Code before test
|
|
277
|
+
- Test after implementation
|
|
278
|
+
- Test passes immediately
|
|
279
|
+
- Can't explain why test failed
|
|
280
|
+
- Tests added "later"
|
|
281
|
+
- Rationalizing "just this once"
|
|
282
|
+
- "I already manually tested it"
|
|
283
|
+
- "Tests after achieve the same purpose"
|
|
284
|
+
- "It's about spirit not ritual"
|
|
285
|
+
- "Keep as reference" or "adapt existing code"
|
|
286
|
+
- "Already spent X hours, deleting is wasteful"
|
|
287
|
+
- "TDD is dogmatic, I'm being pragmatic"
|
|
288
|
+
- "This is different because..."
|
|
289
|
+
|
|
290
|
+
**All of these mean: Delete code. Start over with TDD.**
|
|
291
|
+
|
|
292
|
+
## Example: Bug Fix
|
|
293
|
+
|
|
294
|
+
**Bug:** Empty email accepted
|
|
295
|
+
|
|
296
|
+
**RED**
|
|
297
|
+
```typescript
|
|
298
|
+
test('rejects empty email', async () => {
|
|
299
|
+
const result = await submitForm({ email: '' });
|
|
300
|
+
expect(result.error).toBe('Email required');
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
**Verify RED**
|
|
305
|
+
```bash
|
|
306
|
+
$ npm test
|
|
307
|
+
FAIL: expected 'Email required', got undefined
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**GREEN**
|
|
311
|
+
```typescript
|
|
312
|
+
function submitForm(data: FormData) {
|
|
313
|
+
if (!data.email?.trim()) {
|
|
314
|
+
return { error: 'Email required' };
|
|
315
|
+
}
|
|
316
|
+
// ...
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
**Verify GREEN**
|
|
321
|
+
```bash
|
|
322
|
+
$ npm test
|
|
323
|
+
PASS
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**REFACTOR**
|
|
327
|
+
Extract validation for multiple fields if needed.
|
|
328
|
+
|
|
329
|
+
## Verification Checklist
|
|
330
|
+
|
|
331
|
+
Before marking work complete:
|
|
332
|
+
|
|
333
|
+
- [ ] Every new function/method has a test
|
|
334
|
+
- [ ] Watched each test fail before implementing
|
|
335
|
+
- [ ] Each test failed for expected reason (feature missing, not typo)
|
|
336
|
+
- [ ] Wrote minimal code to pass each test
|
|
337
|
+
- [ ] All tests pass
|
|
338
|
+
- [ ] Output pristine (no errors, warnings)
|
|
339
|
+
- [ ] Tests use real code (mocks only if unavoidable)
|
|
340
|
+
- [ ] Edge cases and errors covered
|
|
341
|
+
|
|
342
|
+
Can't check all boxes? You skipped TDD. Start over.
|
|
343
|
+
|
|
344
|
+
## When Stuck
|
|
345
|
+
|
|
346
|
+
| Problem | Solution |
|
|
347
|
+
|---------|----------|
|
|
348
|
+
| Don't know how to test | Write wished-for API. Write assertion first. Ask your human partner. |
|
|
349
|
+
| Test too complicated | Design too complicated. Simplify interface. |
|
|
350
|
+
| Must mock everything | Code too coupled. Use dependency injection. |
|
|
351
|
+
| Test setup huge | Extract helpers. Still complex? Simplify design. |
|
|
352
|
+
|
|
353
|
+
## Debugging Integration
|
|
354
|
+
|
|
355
|
+
Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression.
|
|
356
|
+
|
|
357
|
+
Never fix bugs without a test.
|
|
358
|
+
|
|
359
|
+
## Testing Anti-Patterns
|
|
360
|
+
|
|
361
|
+
When adding mocks or test utilities, read `testing-anti-patterns.md` in this skill directory to avoid common pitfalls:
|
|
362
|
+
- Testing mock behavior instead of real behavior
|
|
363
|
+
- Adding test-only methods to production classes
|
|
364
|
+
- Mocking without understanding dependencies
|
|
365
|
+
|
|
366
|
+
## Final Rule
|
|
367
|
+
|
|
368
|
+
```
|
|
369
|
+
Production code → test exists and failed first
|
|
370
|
+
Otherwise → not TDD
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
No exceptions without your human partner's permission.
|