name: tdd
description: Comprehensive test-driven development: TDD workflow, test suggestions from diff,
test generation, and property-based testing. Use when: (1) implementing new features,
(2) fixing bugs, (3) refactoring code, (4) reviewing test coverage.
Enforces RED → GREEN → REFACTOR cycle.
category: principles
disable-model-invocation: false
user-invocable: true
allowed-tools: Read, Grep, Glob, Bash, Write, Edit
Test-Driven Development
Comprehensive testing workflow: TDD cycle, test suggestions, generation, and property-based testing.
TDD Cycle (RED → GREEN → REFACTOR)
You are not allowed to implement code until the full TDD cycle is followed.
Phase 1: RED (Write Failing Test)
BLOCKED: Cannot proceed until test failure is proven
- Write a test that captures the expected behavior
- Run the test - it MUST fail
- Document the failure output as proof
Phase 1 Checklist:
Example output:
PHASE 1 - RED ✗
Test: should return user by email
Result: FAILED
Failure: expected undefined to equal { id: 1, email: 'test@example.com' }
Proceeding to Phase 2...
Phase 2: GREEN (Make It Pass)
BLOCKED: Cannot proceed until test passes
- Write the MINIMUM code to make the test pass
- No refactoring, no extra features, no "while I'm here" changes
- Run the test - it MUST pass
- Document the pass output as proof
Phase 3: REFACTOR (Clean Up)
Only proceed after Phase 2 is complete
- Review code for improvements (naming, structure, duplication)
- Make changes while keeping tests green
- Run tests after each refactoring change
Blocking Conditions
| Phase |
Condition to Proceed |
| RED → GREEN |
Test failure output must be shown |
| GREEN → REFACTOR |
Test pass output must be shown |
| REFACTOR → Done |
Tests must still pass |
If Phase 1 is missing: "BLOCKED: PHASE 1 - RED REQUIRED"
Test Suggestions from Diff
Analyze code changes and recommend test additions.
Procedure
- Get all changes in this branch compared to default
- Identify changed functions, methods, classes
- Prioritize by risk level:
| Risk Level |
Triggers |
| HIGH |
New code, core logic, missing coverage |
| MEDIUM |
Modified parameters, return types, conditionals |
| LOW |
Trivial changes with existing coverage |
Output Format
## Risk Level High
**functionName**
- location: src/path/to/file.ts
- change type: modified
- reason: Why this needs tests
- suggested tests:
- Test case 1
- Test case 2
Test Generation
Generate comprehensive tests for functions.
Phases
- Detect: Identify language and testing framework
- Locate: Find existing tests, determine placement
- Analyze: Parse signatures, inputs, outputs, edge cases
- Design: Create minimal set for branch coverage
- Emit: Write tests consistent with framework style
- Validate: Self-check imports, assertions, compilation
- Run: Execute tests, verify they pass
Security-Focused Cases
Include tests for:
- Invalid/tainted inputs
- Injection payloads
- Path traversal
- Overflow/underflow
- Encoding pitfalls
Property-Based Testing
Use for stronger coverage than example-based tests.
When to Use
| Pattern |
Property |
Priority |
| encode/decode pair |
Roundtrip |
HIGH |
| Pure function |
Multiple |
HIGH |
| Validator |
Valid after normalize |
MEDIUM |
| Sorting/ordering |
Idempotence + ordering |
MEDIUM |
| Normalization |
Idempotence |
MEDIUM |
Property Catalog
| Property |
Formula |
When to Use |
| Roundtrip |
decode(encode(x)) == x |
Serialization pairs |
| Idempotence |
f(f(x)) == f(x) |
Normalization, formatting |
| Invariant |
Property holds before/after |
Any transformation |
| Commutativity |
f(a, b) == f(b, a) |
Binary/set operations |
Detection Patterns
Invoke when you detect:
- Serialization pairs: encode/decode, serialize/deserialize, toJSON/fromJSON
- Parsers: URL parsing, config parsing, protocol parsing
- Normalization: normalize, sanitize, clean, canonicalize
- Validators: is_valid, validate, check_*
For detailed patterns, see references/property-patterns.md.
Combined With no-workarounds
When both tdd AND no-workarounds are activated:
- You are BLOCKED from implementing ANY fix until Phase 1 (RED) is complete
- You are BLOCKED from working around the tool failure
- The ONLY valid path: RED → GREEN → REFACTOR → Verify tool works
Rationalizations (All Rejected)
| Excuse |
Why It's Wrong |
Required Action |
| "It's a simple change" |
Simple changes still need tests |
Write the test |
| "I'll add tests after" |
Tests after = not TDD |
BLOCKED |
| "Tests are slow" |
Speed doesn't override process |
Write the test |
| "I know this works" |
Confidence ≠ proof |
Write the test |
| "Just this once" |
That's what you said last time |
Write the test |
Reference Files
references/property-patterns.md - Property-based testing patterns
references/libraries.md - PBT libraries by language
1---2name: tdd-53description: Comprehensive testing workflow: TDD cycle, test suggestions, generation, and property-based testing.4---5
6---
7name: tdd
8description: Comprehensive test-driven development: TDD workflow, test suggestions from diff,
9test generation, and property-based testing. Use when: (1) implementing new features,
10(2) fixing bugs, (3) refactoring code, (4) reviewing test coverage.
11Enforces RED → GREEN → REFACTOR cycle.
12
13category: principles
14disable-model-invocation: false
15user-invocable: true
16allowed-tools: Read, Grep, Glob, Bash, Write, Edit
17---
18
19# Test-Driven Development
20
21Comprehensive testing workflow: TDD cycle, test suggestions, generation, and property-based testing.
22
23---
24
25## TDD Cycle (RED → GREEN → REFACTOR)
26
27You are not allowed to implement code until the full TDD cycle is followed.
28
29### Phase 1: RED (Write Failing Test)
30
31**BLOCKED: Cannot proceed until test failure is proven**
32
331. Write a test that captures the expected behavior
342. Run the test - it MUST fail
353. Document the failure output as proof
36
37**Phase 1 Checklist:**
38- [ ] Test file created/modified
39- [ ] Test run completed
40- [ ] Failure output captured
41- [ ] Failure is for the RIGHT reason (not syntax error)
42
43**Example output:**
44```
45PHASE 1 - RED ✗
46Test: should return user by email
47Result: FAILED
48Failure: expected undefined to equal { id: 1, email: 'test@example.com' }
49Proceeding to Phase 2...
50```
51
52### Phase 2: GREEN (Make It Pass)
53
54**BLOCKED: Cannot proceed until test passes**
55
561. Write the MINIMUM code to make the test pass
572. No refactoring, no extra features, no "while I'm here" changes
583. Run the test - it MUST pass
594. Document the pass output as proof
60
61### Phase 3: REFACTOR (Clean Up)
62
63**Only proceed after Phase 2 is complete**
64
651. Review code for improvements (naming, structure, duplication)
662. Make changes while keeping tests green
673. Run tests after each refactoring change
68
69### Blocking Conditions
70
71| Phase | Condition to Proceed |
72|-------|---------------------|
73| RED → GREEN | Test failure output must be shown |
74| GREEN → REFACTOR | Test pass output must be shown |
75| REFACTOR → Done | Tests must still pass |
76
77**If Phase 1 is missing:** **"BLOCKED: PHASE 1 - RED REQUIRED"**
78
79---
80
81## Test Suggestions from Diff
82
83Analyze code changes and recommend test additions.
84
85### Procedure
86
871. Get all changes in this branch compared to default
882. Identify changed functions, methods, classes
893. Prioritize by risk level:
90
91| Risk Level | Triggers |
92|------------|----------|
93| **HIGH** | New code, core logic, missing coverage |
94| **MEDIUM** | Modified parameters, return types, conditionals |
95| **LOW** | Trivial changes with existing coverage |
96
97### Output Format
98
99```markdown
100## Risk Level High
101**functionName**
102- location: src/path/to/file.ts
103- change type: modified
104- reason: Why this needs tests
105- suggested tests:
106 - Test case 1
107 - Test case 2
108```
109
110---
111
112## Test Generation
113
114Generate comprehensive tests for functions.
115
116### Phases
117
1181. **Detect**: Identify language and testing framework
1192. **Locate**: Find existing tests, determine placement
1203. **Analyze**: Parse signatures, inputs, outputs, edge cases
1214. **Design**: Create minimal set for branch coverage
1225. **Emit**: Write tests consistent with framework style
1236. **Validate**: Self-check imports, assertions, compilation
1247. **Run**: Execute tests, verify they pass
125
126### Security-Focused Cases
127
128Include tests for:
129- Invalid/tainted inputs
130- Injection payloads
131- Path traversal
132- Overflow/underflow
133- Encoding pitfalls
134
135---
136
137## Property-Based Testing
138
139Use for stronger coverage than example-based tests.
140
141### When to Use
142
143| Pattern | Property | Priority |
144|---------|----------|----------|
145| encode/decode pair | Roundtrip | HIGH |
146| Pure function | Multiple | HIGH |
147| Validator | Valid after normalize | MEDIUM |
148| Sorting/ordering | Idempotence + ordering | MEDIUM |
149| Normalization | Idempotence | MEDIUM |
150
151### Property Catalog
152
153| Property | Formula | When to Use |
154|----------|---------|-------------|
155| **Roundtrip** | `decode(encode(x)) == x` | Serialization pairs |
156| **Idempotence** | `f(f(x)) == f(x)` | Normalization, formatting |
157| **Invariant** | Property holds before/after | Any transformation |
158| **Commutativity** | `f(a, b) == f(b, a)` | Binary/set operations |
159
160### Detection Patterns
161
162Invoke when you detect:
163- **Serialization pairs**: encode/decode, serialize/deserialize, toJSON/fromJSON
164- **Parsers**: URL parsing, config parsing, protocol parsing
165- **Normalization**: normalize, sanitize, clean, canonicalize
166- **Validators**: is_valid, validate, check_*
167
168For detailed patterns, see `references/property-patterns.md`.
169
170---
171
172## Combined With no-workarounds
173
174**When both tdd AND no-workarounds are activated:**
1751. You are BLOCKED from implementing ANY fix until Phase 1 (RED) is complete
1762. You are BLOCKED from working around the tool failure
1773. The ONLY valid path: RED → GREEN → REFACTOR → Verify tool works
178
179---
180
181## Rationalizations (All Rejected)
182
183| Excuse | Why It's Wrong | Required Action |
184|--------|----------------|-----------------|
185| "It's a simple change" | Simple changes still need tests | Write the test |
186| "I'll add tests after" | Tests after = not TDD | BLOCKED |
187| "Tests are slow" | Speed doesn't override process | Write the test |
188| "I know this works" | Confidence ≠ proof | Write the test |
189| "Just this once" | That's what you said last time | Write the test |
190
191---
192
193## Reference Files
194
195- `references/property-patterns.md` - Property-based testing patterns
196- `references/libraries.md` - PBT libraries by language