---
name: tdd-lean
description: Use for Gate 3 fast mode — Batch Red-Green-Refactor. Write all tests first, run once, implement all code, run once. Framework-agnostic.
---

# TDD Lean — Gate 3 Fast Mode

## When to Use

**Fast mode only.** This skill replaces `superpowers:test-driven-development` in fast mode.
Full mode continues to use `superpowers:test-driven-development` unchanged.

## The Batch Red-Green-Refactor Pattern

Instead of per-test Red-Green-Refactor, batch all tests first, then implement all production code.

### Phase 1 — RED BATCH

Write ALL test methods for the feature in test file(s).
Run test runner ONCE → confirm all FAIL.

If any test PASSes immediately → that test is wrong. Fix it before proceeding.

```
✅ Accept: Tests: 0 passed, 8 failed (1.2s)
❌ Stop if: any test passes immediately (test is testing wrong behavior)
```

### Phase 2 — GREEN BATCH

Implement ALL production code.
Run test runner ONCE → confirm all PASS.

If tests FAIL:
- Fix inline. DO NOT spawn a subagent.
- Maximum 2 retry runs.
- If still failing after 2 retries: stop and report exact error to developer.

```
✅ Accept: Tests: 8 passed (2.1s)
❌ Stop if: tests still fail after 2 retries → report to developer
```

### Phase 3 — REFACTOR

Clean up duplication or naming if needed.
Run ONCE to confirm still PASS.
Do NOT add behavior. Do NOT fix unrelated tests.

## Test Output Discipline — CRITICAL

After EVERY test run, keep ONLY the summary line in context. Discard everything else.

**Keep:**
```
✅ Tests: 12 passed (3.2s)
❌ Tests: 10 passed, 2 failed — UserService:45 expected "X" got "Y"
```

**Discard immediately:**
- Full stack traces
- Framework startup logs (Spring Boot "Started in 3.2s", Jest runner header, etc.)
- Warning and deprecation messages
- Individual test output blocks

**Why this matters:** A single Spring Boot test run produces ~300 lines (~860 tokens). Running 16–24 times per feature = 13,000–20,000 tokens from test output alone. Batch approach = 2–3 runs total.

## Checkpoint Calls

At each phase milestone, call the checkpoint command with your token estimate:

```bash
aiflow checkpoint --gate 3 --step "tests-written" --tokens [estimate]
aiflow checkpoint --gate 3 --step "code-done" --tokens [estimate]
aiflow checkpoint --gate 3 --step "tests-pass" --tokens [estimate]
```

**Token estimate:** Count approximate characters you have generated in this gate so far ÷ 3.5

## Summary.md Update

After Phase 2 (GREEN BATCH), append task results to `plan/[ticket-id]/summary.md`:

```markdown
### Gate 3 — Code
- ✅ [Task name] + tests
- ✅ [Task name] + tests
- ❌ [Task name] (error → fixed inline)
```

Also append token data:
```markdown
## Token Usage
| Gate | Tokens (est.) | Time |
|------|---------------|------|
| G3   | ~[value]      | [Xm] |
```
(Update existing row if Gate 3 row already exists; add if not.)

## Gate Transition Reminder

After all tests pass and Gate 3 is complete, display:

```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ GATE 3: CODE GENERATION COMPLETE

All tests pass. Ready for Gate 4 (AI Self-Review).

💡 Token tip: Continue in this session OR start fresh for Gate 4:
   1. Run: aiflow task next --ticket [ticket-id]
   2. Open a new terminal / chat session
   3. Run: aiflow task resume [ticket-id]
   Gate 4 will begin with a clean context window.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```

## Mandatory Rules

- ❌ DO NOT run test suite after each individual test method
- ❌ DO NOT use `superpowers:test-driven-development` in fast mode
- ❌ DO NOT spawn subagents to fix failing tests
- ❌ DO NOT keep full test output in context
- ✅ MUST run test suite exactly: once after RED BATCH, once after GREEN BATCH
- ✅ MUST discard test output and keep only summary line
- ✅ MUST call `aiflow checkpoint` at each milestone
- ✅ MUST update summary.md with task results (✅/❌) and token data
