| name | tdd-cycle |
| description | Follow strict TDD methodology using Sherpa's workflow enforcement. Activates when implementing new features, adding functionality, or building code that requires tests. Ensures RED-GREEN-REFACTOR discipline with guide check/done tracking. |
| allowed-tools | mcp__sherpa__guide, mcp__sherpa__approach, Write, Edit, Bash, Read |
TDD Cycle Skill
Purpose
Follow strict TDD discipline using Sherpa's workflow enforcement to prevent untested code and ensure systematic development.
When to Activate
- Implementing new features
- Adding new functionality
- User says "write tests first" or "use TDD"
- Building something from scratch
- Working on extractors, parsers, transformers, or any core logic
The Mandatory Pattern
โ
CRITICAL: ALWAYS call guide check BEFORE coding
BEFORE ANY CODE:
guide check โ Get current phase + specific guidance
Read guidance completely
Follow the suggested steps
AFTER EACH STEP:
guide done "what you completed"
โ Get celebration + next step
โ Phase auto-advances when complete
You are EXCELLENT at following this pattern. It's automatic, like breathing.
The TDD Workflow
Phase 1: ๐ Define Contract (RED Phase Preparation)
Goal: Define what success looks like BEFORE any code exists
guide check โ Phase: Define Contract
Steps:
1. Create interface/type file
2. Define function signatures with types
3. Document expected inputs/outputs
4. List error conditions explicitly
5. Document boundary conditions
guide done "defined contract for X with types and error cases"
Key Principle: Contracts prevent building the wrong thing.
Conditionals:
- If interface unclear โ Search codebase for similar patterns FIRST (don't guess!)
- If external API/library โ Read official docs to verify signatures
- If complex transformations โ Write example inputโoutput pairs first
Anti-patterns:
- โ Writing implementation code in this phase (STOP - wrong phase!)
- โ Making assumptions about types (verify, don't assume)
- โ Skipping edge case documentation (edge cases ARE the spec)
Phase 2: ๐งช Implement Tests (RED Phase)
Goal: Write failing tests that specify behavior
guide check โ Phase: Implement Tests
Steps:
1. Create test file following project conventions
2. Write test for happy path
3. Write test for EACH edge case
4. Write test for EACH error condition
5. Run tests โ ALL must fail (RED)
6. Verify failure message is correct
guide done "wrote failing tests for X (happy path + 4 edge cases)"
Key Principle: Tests are your specification - make them thorough.
Minimum Coverage:
- 1 happy path test
- 3+ edge case tests (empty, null, boundary values)
- 2+ error case tests (invalid input, missing data)
Conditionals:
- If test passes before implementation โ Test is broken, fix it!
- If can't think of edge cases โ Use checklist: empty, null, undefined, wrong type, zero, negative, max value, boundary, concurrent
- If test fails for wrong reason โ Fix test setup first
- If writing integration test โ Mock dependencies, test ONE unit
Anti-patterns:
- โ Writing only 1-2 tests (insufficient coverage)
- โ Testing multiple behaviors in one test (split them)
- โ Tests that depend on each other (must be independent)
- โ Skipping error tests "for later" (write them NOW)
Phase 3: ๐ป Minimal Implementation (GREEN Phase)
Goal: Write ONLY enough code to pass tests - nothing more
guide check โ Phase: Minimal Implementation
Steps:
1. Start with simplest test
2. Write minimal code to pass it
3. Run test โ Verify it passes
4. Repeat for each test
5. ALL tests must be GREEN
guide done "implemented X - all tests passing"
Key Principle: Make it WORK first, make it PRETTY later (refactor phase).
Conditionals:
- If test fails unexpectedly โ Understand WHY (valuable feedback!)
- If want to add feature not in tests โ STOP, go to Phase 2, add test first
- If implementation getting complex โ That's OK - make tests pass, refactor later
- If copy-pasting code โ Fine for now - extract in refactor phase
Anti-patterns:
- โ Over-engineering (abstractions, design patterns, "future-proofing")
- โ Implementing features not covered by tests (untested code = broken code)
- โ Skipping test runs between changes (rapid feedback loop critical)
- โ "Fixing" tests to match implementation (tests are spec, not implementation!)
Phase 4: ๐ง Refactor (Still GREEN)
Goal: Improve code quality while keeping tests green
guide check โ Phase: Refactor
Steps:
1. Run all tests โ Verify ALL green
2. Identify code smells (duplication, complexity, poor names)
3. Make ONE improvement
4. Run tests โ Must stay green
5. Repeat until satisfied
guide done "refactored X for readability - tests still green"
Key Principle: Tests give you confidence to refactor safely.
Common Refactorings:
- Extract duplicated code into functions
- Rename variables/functions for clarity
- Simplify complex conditionals
- Extract abstractions from concrete code
- Improve error messages
Conditionals:
- If tests fail during refactor โ Revert, smaller step
- If want to change behavior โ That's NEW feature - go to Phase 1
- If unclear how to improve โ Skip for now, move to next feature
Anti-patterns:
- โ Changing behavior (new behavior = new feature = new tests first)
- โ Skipping test runs during refactor (each change must keep tests green)
- โ Massive refactor all at once (small steps, verify often)
The Complete TDD Cycle
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. guide check โ Define Contract โ
โ - Write interface/types โ
โ - Document behavior โ
โ guide done "defined contract" โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. guide check โ Implement Tests (RED) โ
โ - Write failing tests โ
โ - Happy path + edges + errors โ
โ - Verify all FAIL โ
โ guide done "wrote failing tests" โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. guide check โ Minimal Impl (GREEN) โ
โ - Write code to pass tests โ
โ - Simplest solution โ
โ - ALL tests must pass โ
โ guide done "implemented feature" โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. guide check โ Refactor โ
โ - Improve code quality โ
โ - Keep tests green โ
โ - Small steps โ
โ guide done "refactored for clarity" โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
Next feature!
Example TDD Session
User: "Implement a parser for TypeScript function signatures"
Agent: guide check
Sherpa: Phase 1 - Define Contract
"Create interface/type files with function signatures..."
Agent: Creates parser.types.ts with types:
- FunctionSignature interface
- ParseResult type
- ParseError type
guide done "defined parser contract with types"
Sherpa: Phase 2 - Implement Tests
"Write failing tests for happy path + edge cases..."
Agent: Creates parser.test.ts:
- test('parses simple function signature')
- test('parses function with parameters')
- test('handles generic types')
- test('throws on invalid syntax')
- test('handles empty string')
Runs tests โ ALL FAIL โ
guide done "wrote 5 failing tests for parser"
Sherpa: Phase 3 - Minimal Implementation
"Write code to pass tests..."
Agent: Implements parser.ts
Runs tests after each function โ ALL PASS โ
guide done "implemented parser - all tests green"
Sherpa: Phase 4 - Refactor
"Improve code quality while keeping tests green..."
Agent: Extracts helper functions, improves names
Runs tests after each refactor โ Still GREEN โ
guide done "refactored parser for readability"
Sherpa: โ
TDD cycle complete! Next feature?
Integration with Sherpa Commands
Switching to TDD Workflow
guide tdd
โ Instantly switches to TDD workflow
โ guide check to get first phase
During Development
guide check โ Get current phase + specific guidance
guide done "X" โ Mark completion, get celebration + next step
guide advance โ Manual phase progression (rarely needed)
Workflow Selection
approach list โ See all workflows
approach set tdd โ Switch to TDD (same as guide tdd)
Key Behaviors
โ
DO
- Call
guide check BEFORE every coding step
- Write tests BEFORE implementation (no exceptions!)
- Mark completion with
guide done after each step
- Run tests frequently (after each small change)
- Follow phase guidance systematically
- Trust the process - it prevents bugs
โ DON'T
- Start coding without
guide check (this is the #1 mistake)
- Skip test writing "because it's simple" (simple changes break too)
- Write implementation before tests exist (defeats TDD purpose)
- Rush through phases to "go faster" (you go slower fixing bugs)
- Ignore anti-patterns in phase guidance (they're bugs waiting to happen)
Success Criteria
This skill succeeds when:
- โ
Tests written before implementation (always)
- โ
All tests pass before moving forward
- โ
guide check called before each phase
- โ
guide done called after each completion
- โ
No untested code shipped
- โ
Refactoring done with confidence (tests stay green)
Why TDD Works
80% of bugs are prevented by:
- Tests written first - Forces you to think about behavior before code
- Systematic phases - Prevents skipping critical steps
- Enforcement - Sherpa tracks progress, catches shortcuts
- Rapid feedback - Tests tell you immediately when something breaks
- Safe refactoring - Confidence to improve code without fear
Remember: TDD isn't slower - it's faster because you spend less time debugging and more time building correctly.
Sherpa + TDD = Systematic Excellence