AgentSkillsCN

kimchi:tdd

此命令用于验证 bead YAML 文件是否具备独立执行的能力。它会依次运行四项验证规则(上下文完整性、交付成果清晰度、测试规范性、隔离性),并对存在缺陷的 bead 进行深度优化。这是 Kimchi 规划流程的第八阶段。

SKILL.md
--- frontmatter
name: kimchi:tdd
description: Use when implementing any feature, bugfix, or behavior change — before writing implementation code. Enforces RED-GREEN-REFACTOR cycle.

Test-Driven Development

Overview

Write the test first. Watch it fail. Write minimal code to pass.

Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.

Violating the letter of the rules is violating the spirit of the rules.

When This Applies

Always when writing code that has behavior:

  • New features
  • Bug fixes
  • Refactoring
  • Behavior changes

Exceptions (ask your human partner):

  • Pure configuration files
  • Database migrations (tested via schema verification)
  • Static content
  • Throwaway prototypes

Thinking "skip TDD just this once"? Stop. That's rationalization.

The Iron Law

code
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST

Write code before the test? Delete it. Start over.

No exceptions:

  • Don't keep it as "reference"
  • Don't "adapt" it while writing tests
  • Don't look at it
  • Delete means delete

Implement fresh from tests. Period.

Red-Green-Refactor

RED — Write Failing Test

Write one minimal test showing what should happen.

<Good> ```ruby it "rejects file over 5MB with clear error" do large_file = fixture_file("6mb.jpg")

result = AvatarUploadService.call(large_file, user_id: user.id)

expect(result).to be_failure expect(result.error).to eq("File too large (max 5MB)") end

code
Clear name, tests real behavior, one thing
</Good>

<Bad>
```ruby
it "works" do
  mock = double("s3", upload: true)
  allow(S3Client).to receive(:new).and_return(mock)
  result = AvatarUploadService.call(file, user_id: 1)
  expect(mock).to have_received(:upload)
end

Vague name, tests mock not code </Bad>

Requirements:

  • One behavior per test
  • Clear name describing the behavior
  • Real code (no mocks unless unavoidable)

Verify RED — Watch It Fail

MANDATORY. Never skip.

bash
# Run the test, expect failure
bundle exec rspec spec/services/avatars/upload_service_spec.rb

Confirm:

  • Test fails (not errors)
  • Failure message is expected
  • Fails because feature missing (not typos)

Test passes? You're testing existing behavior. Fix test.

Test errors? Fix error, re-run until it fails correctly.

GREEN — Minimal Code

Write simplest code to pass the test.

<Good> ```ruby def call(file, user_id:) return failure("File too large (max 5MB)") if file.size > 5.megabytes

url = s3_client.upload(file, key: "avatars/#{user_id}/#{SecureRandom.uuid}") success(url: url) end

code
Just enough to pass
</Good>

<Bad>
```ruby
def call(file, user_id:, options: {})
  strategy = options.fetch(:resize_strategy, :vips)
  validator = ValidatorFactory.for(file.content_type)
  pipeline = ProcessingPipeline.new(strategy: strategy)
  # YAGNI
end

Over-engineered </Bad>

Don't add features, refactor other code, or "improve" beyond the test.

Verify GREEN — Watch It Pass

MANDATORY.

bash
bundle exec rspec spec/services/avatars/upload_service_spec.rb

Confirm:

  • Test passes
  • Other tests still pass
  • Output pristine (no errors, warnings)

Test fails? Fix code, not test.

Other tests fail? Fix now.

REFACTOR — Clean Up

After green only:

  • Remove duplication
  • Improve names
  • Extract helpers

Keep tests green. Don't add behavior.

REPEAT

Next failing test for next feature.

Common Rationalizations

ExcuseReality
"Too simple to test"Simple code breaks. Test takes 30 seconds.
"I'll test after"Tests passing immediately prove nothing.
"Tests after achieve same goals"Tests-after = "what does this do?" Tests-first = "what should this do?"
"Already manually tested"Ad-hoc ≠ systematic. No record, can't re-run.
"Deleting X hours is wasteful"Sunk cost fallacy. Keeping unverified code is technical debt.
"Keep as reference, write tests first"You'll adapt it. That's testing after. Delete means delete.
"Need to explore first"Fine. Throw away exploration, start with TDD.
"Test hard = design unclear"Listen to test. Hard to test = hard to use.
"TDD will slow me down"TDD faster than debugging. Pragmatic = test-first.
"Existing code has no tests"You're improving it. Add tests for the code you touch.

Red Flags — STOP and Start Over

  • Code before test
  • Test after implementation
  • Test passes immediately
  • Can't explain why test failed
  • Tests added "later"
  • Rationalizing "just this once"
  • "I already manually tested it"
  • "Keep as reference" or "adapt existing code"
  • "Already spent X hours, deleting is wasteful"
  • "This is different because..."

All of these mean: Delete code. Start over with TDD.

Verification Checklist

Before marking work complete:

  • Every new function/method has a test
  • Watched each test fail before implementing
  • Each test failed for expected reason (feature missing, not typo)
  • Wrote minimal code to pass each test
  • All tests pass
  • Output pristine (no errors, warnings)
  • Tests use real code (mocks only if unavoidable)
  • Edge cases and errors covered

Can't check all boxes? You skipped TDD. Start over.

Debugging Integration

Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression.

Never fix bugs without a test.

When Stuck

ProblemSolution
Don't know how to testWrite wished-for API. Write assertion first. Ask your human partner.
Test too complicatedDesign too complicated. Simplify interface.
Must mock everythingCode too coupled. Use dependency injection.
Test setup hugeExtract helpers. Still complex? Simplify design.

Final Rule

code
Production code → test exists and failed first
Otherwise → not TDD

No exceptions without your human partner's permission.