test
<HARD-GATE>
Tests define the EXPECTED BEHAVIOR. They MUST be written BEFORE implementation code.
If tests pass without implementation → the tests are wrong. Rewrite them.
The only exception: when retrofitting tests for existing untested code.
THE IRON LAW: Write code before test? DELETE IT. Start over.
- Do NOT keep it as "reference"
- Do NOT "adapt" it while writing tests
- Do NOT look at it to "inform" test design
- Delete means delete.
git checkout -- <file>or remove the changes entirely.
This is not negotiable. This is not optional. "But I already wrote it" is a sunk cost fallacy.
ROLE BOUNDARY: Test writes TEST FILES only. NEVER modify source/implementation files.
- Do NOT "quickly fix" a broken import in source to make tests run
- Do NOT refactor source code to be "more testable"
- Do NOT add missing exports to source files
- If source needs changes → hand off to
rune:fix. Test's job ends at the test file.
This separation ensures test never writes code biased toward passing its own tests.
VERTICAL SLICING (Iron Law extension): one test → GREEN → one test → GREEN. Never bulk.
- bulk_test_count MUST stay <= 1 before the first GREEN in a session
- After each GREEN, bulk_test_count resets to 0; writing 2+ tests before the next GREEN = HORIZONTAL VIOLATION
- Each cycle MUST emit a commit pair:
test(scope): <behavior>+feat(scope): <behavior> - Claim "I did TDD" is verified by
completion-gateagainstgit log --oneline— no paired commits = REJECTED - Horizontal slicing produces tests-of-imagination, not tests-of-behavior. See references/vertical-tdd.md.
- Emit signal
tdd.horizontal.violationwhen triggered; preflight blocks merge until cycles are unwound.
Exceptions (narrow, must be documented in test header): retrofitting characterization tests for legacy untested code; spec-driven scaffolding where the contract is external (OpenAPI, wire protocol).
</HARD-GATE>
Instructions
Phase 1: Understand What to Test
- Read the implementation plan or task description carefully
- Use
Globto find existing test files:**/*.test.*,**/*.spec.*,**/test_* - Use
Readon 2-3 existing test files to understand:
- Test framework in use
- File naming convention (e.g.,
foo.test.tsmirrorsfoo.ts) - Test directory structure (co-located vs
__tests__/vstests/) - Assertion style and patterns
- Use
Globto find the source file(s) being tested
TodoWrite: [
{ content: "Understand scope and find existing test patterns", status: "in_progress" },
{ content: "Detect test framework and conventions", status: "pending" },
{ content: "Write failing tests (RED phase)", status: "pending" },
{ content: "Run tests — verify they FAIL", status: "pending" },
{ content: "After implementation: verify tests PASS (GREEN phase)", status: "pending" }
]
Phase 2: Detect Test Framework
Use Glob to find config files and identify the framework:
jest.config.*or"jest"key inpackage.json→ Jestvitest.config.*or"vitest"key inpackage.json→ Vitestpytest.ini,[tool.pytest.ini_options]inpyproject.toml→ pytest- Async check: If pytest detected AND source files contain
async def: - Check if
pytest-asynciois in dependencies (pyproject.toml [project.dependencies]or[project.optional-dependencies]) - Check if
asyncio_modeis set in[tool.pytest.ini_options](values:auto,strict, or absent) - If async code exists but no
asyncio_modeconfigured → WARN: "pytest-asyncio not configured. Async tests may silently pass without executing async code. Recommend addingasyncio_mode = \"auto\"to[tool.pytest.ini_options]in pyproject.toml." Cargo.tomlwith#[cfg(test)]pattern → built-incargo test*_test.gofiles present → built-ingo testcypress.config.*→ Cypress (E2E)playwright.config.*→ Playwright (E2E)
Verification gate: Framework identified before writing any test code.
Phase 3: Write Failing Tests
Use Write to create test files following the detected conventions:
- Mirror source file location: if source is
src/auth/login.ts, test issrc/auth/login.test.ts - Structure tests with clear
describe/itblocks (or language equivalent):
describe('Feature name')it('should [expected behavior] when [condition]')
- Cover all three categories:
- Happy path: valid inputs, expected success output
- Edge cases: empty input, boundary values, large input
- Error cases: invalid input, missing data, network failure simulation
- Use proper assertions. Do NOT use implementation details — test behavior:
- Jest/Vitest:
expect(result).toBe(expected) - pytest:
assert result == expected - Rust:
assert_eq!(result, expected) - Go:
if result != expected { t.Errorf(...) }
- For async code: use
async/awaitor pytest@pytest.mark.asyncio