Test

From Rune-kit/rune · View interactive version

Test is an AI agent skill from the GitHub repository Rune-kit/rune. TDD test writer. Use before implementing any feature or fix (write the failing test first), when the user asks to 'write tests', or when coverage is missing. Writes failing tests FIRST (red), then verifies they pass after implementation (green). Covers unit, integration, and e2e tests.

How to use this skill

Skill file

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.

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.

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.

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

  1. Read the implementation plan or task description carefully
  2. Use Glob to find existing test files: **/*.test.*, **/*.spec.*, **/test_*
  3. Use Read on 2-3 existing test files to understand:
  1. Use Glob to 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:

Verification gate: Framework identified before writing any test code.

Phase 3: Write Failing Tests

Use Write to create test files following the detected conventions:

  1. Mirror source file location: if source is src/auth/login.ts, test is src/auth/login.test.ts
  2. Structure tests with clear describe / it blocks (or language equivalent):
  1. Cover all three categories:
  1. Use proper assertions. Do NOT use implementation details — test behavior:
  1. For async code: use async/await or pytest @pytest.mark.asyncio
Python Async Tests (pytest-asyncio)

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Phase 3.5: Cycle Discipline (Vertical Slicing Gate)

Before running the new test in Phase 4, count what's about to be added to the working tree:

bulk_test_count = (test files staged + test files unstaged but new) since the last GREEN commit

Gate: bulk_test_count MUST be exactly 1.

| State | Action |

|-------|--------|

| bulk_test_count == 1 | Proceed to Phase 4 (run RED). |

| bulk_test_count >= 2 AND no prior GREEN this session | HORIZONTAL VIOLATION — pause, keep one test, defer the rest. |

| bulk_test_count >= 2 AND last GREEN exists in git log | HORIZONTAL VIOLATION — same. |

| bulk_test_count == 0 | No test to run; this phase is a no-op. |

Cycle audit log — append to TEST.md (or create) one line per cycle:

cycle 1 — RED: test_validates_email_rejects_empty | GREEN: validateEmail handles empty | commit: 4f3a1c
cycle 2 — RED: test_validates_email_requires_at_sign | GREEN: validateEmail checks @ | commit: a92e0d

The audit log is the receipt. completion-gate reads it.

When the violation fires, do NOT delete tests automatically — surface to the calling agent: "horizontal slicing detected (N tests before first GREEN). Recommend keeping test K, deferring N-1 to subsequent cycles."

Phase 4: Run Tests — Verify They FAIL (RED)

Use Bash to run ONLY the newly created test files (not full suite):

Hard gate: ALL new tests MUST fail at this point.

Phase 5: After Implementation — Verify Tests PASS (GREEN)

After rune:fix writes implementation code, run the same test command again:

  1. ALL tests in the new test files MUST pass
  2. Run the full test suite with Bash to check for regressions:
  1. If any test fails: report clearly which test, what was expected, what was received
  2. If an existing test now fails (regression): escalate to rune:debug

Verification gate: 100% of new tests pass AND 0 regressions in existing tests.

Phase 6: Coverage Check

After GREEN phase, call verification to check coverage threshold (80% minimum):

Phase 6.5: Diff-Aware Mode (optional)

When invoked with mode: "diff-aware" or by cook after implementation:

  1. Run git diff main --name-only to get changed files
  2. For each changed file, trace its blast radius: what imports it? what routes does it serve? what components render it?
  3. Map changed files → affected routes/endpoints/pages
  4. Prioritize tests: files with most downstream dependents get tested first
  5. Generate targeted test commands that cover ONLY affected paths — skip unchanged modules

This mode is valuable for large codebases where running the full suite is slow. It answers: "what could this diff have broken?"

Input:  git diff main --name-only
Output: Prioritized test plan targeting only affected paths

Test Types — 4-Layer Methodology

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

TEST.md — Test Plan + Results Document

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Skill Behavior Tests (Eval Scenarios)

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Error Recovery

Called By (inbound)

Calls (outbound)

Data Flow

Feeds Into →

Fed By ←

Feedback Loops ↻

Anti-Rationalization Table

| Excuse | Reality |

|---|---|

| "Too simple to need tests first" | Simple code breaks. Test takes 30 seconds. Write it first. |

| "I'll write tests after — same result" | Tests-after = "what does this do?" Tests-first = "what SHOULD this do?" Completely different. |

| "I already wrote the code, let me just add tests" | Iron Law: delete the code. Start over with tests. Sunk cost is not an argument. |

| "Tests after achieve the same goals" | They don't. Tests-after are biased by the implementation you just wrote. |

| "It's about spirit not ritual" | Violating the letter IS violating the spirit. Write the test first. |

| "I mentally tested it" | Mental testing is not testing. Run the command, show the output. |

| "This is different because..." | It's not. Write the test first. |

| "I'll batch the tests since they're related" | Batched tests = tests of imagination. Each cycle reacts to the prior. Write one, GREEN it, then the next. |

| "All five tests are already written, let me just review them with you" | Same fallacy as code-before-test. Keep the first one, defer the others to subsequent cycles. |

Advanced: Oracle-Injection E2E Testing

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Spec→Test Traceability

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Eval-Driven Development

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Red Flags — STOP and Start Over

If you catch yourself with ANY of these, delete implementation code and restart with tests:

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

Constraints

  1. MUST write tests BEFORE implementation code — if tests pass without implementation, they are wrong
  2. MUST cover happy path + edge cases + error cases — not just happy path
  3. MUST run tests to verify they FAIL before implementation exists (RED phase is mandatory)
  4. MUST NOT write tests that test mock behavior instead of real code behavior
  5. MUST achieve 80% coverage minimum — identify and fill gaps
  6. MUST use the project's existing test framework and conventions — don't introduce a new one
  7. MUST NOT say "tests pass" without showing actual test runner output
  8. MUST delete implementation code written before tests — Iron Law, no exceptions
  9. MUST show RED phase output (actual failure) — "I confirmed they fail" without output is REJECTED
  10. MUST NOT modify source/implementation files — test writes test files ONLY, hand off source changes to rune:fix
  11. MUST NOT write a 2nd test until the 1st test reaches GREEN — vertical slicing only, bulk_test_count <= 1 enforced
  12. MUST emit commit pair per cycle (test: then feat:) — git log is the audit trail for "I did TDD" claims

Mesh Gates

| Gate | Requires | If Missing |

|------|----------|------------|

| RED Gate | All new tests FAIL before implementation | If any pass, rewrite stricter tests |

| GREEN Gate | All tests PASS after implementation | Fix code, not tests |

| Coverage Gate | 80%+ coverage verified via verification | Write additional tests for gaps |

Output Format

## Test Report
- **Framework**: [detected]
- **Files Created**: [list of new test file paths]
- **Tests Written**: [count]
- **Status**: RED (failing as expected) | GREEN (all passing)

### Test Cases
| Test | Status | Description |
|------|--------|-------------|
| `test_name` | FAIL/PASS | [what it tests] |

### Coverage
- Lines: [X]% | Branches: [Y]%
- Gaps: `path/to/file.ts:42-58` — uncovered branch (error handling)

### Regressions (if any)
- [existing test that broke, with error details]

Testing Anti-Patterns (Gate Functions)

Full detail moved to CODE0 (progressive disclosure). Read that file before executing this section.

Returns

| Artifact | Format | Location |

|----------|--------|----------|

| Test files | Source files | Co-located or __tests__/ per project convention |

| Test plan + results | Markdown | TEST.md in test directory (non-trivial features only) |

| Eval scenarios | Markdown | skills/<name>/evals.md (for skill behavior testing) |

| Coverage report | Inline stdout | Shown in Test Report |

| Test Report | Markdown (inline) | Emitted to calling skill (cook, fix, review) |

Chain Metadata

Append to Test Report when invoked standalone. Suppress when called as sub-skill inside an L1 orchestrator (cook, team, etc.) — the orchestrator emits a consolidated block. See docs/references/chain-metadata.md.

chain_metadata:
  skill: "rune:test"
  version: "1.2.0"
  status: "[DONE]"
  domain: "[area tested]"
  files_changed:
    - "[test files created/modified]"
  exports:
    test_results: { passed: [N], failed: [N], coverage: [N] }
    test_files: ["[paths to test files]"]
    status: "[RED | GREEN]"  # RED = TDD failing (expected), GREEN = all pass
  suggested_next:  # status-aware — pick based on RED or GREEN
    # When GREEN:
    - skill: "rune:preflight"
      reason: "[grounded in results — e.g., 'All 15 tests GREEN, check edge case completeness']"
      consumes: ["test_results", "test_files"]
    # When RED (TDD expected):
    - skill: "rune:fix"
      reason: "[grounded in failures — e.g., '3 tests RED as expected, implement to make them pass']"
      consumes: ["test_results", "test_files"]

Sharp Edges

Known failure modes for this skill. Check these before declaring done.

| Failure Mode | Severity | Mitigation |

|---|---|---|

| Tests passing before implementation exists | CRITICAL | RED Gate: rewrite stricter tests — passing without code = not testing real behavior |

| Skipping the RED phase (not confirming FAIL) | HIGH | Run tests, confirm FAIL output before calling cook/fix to implement |

| Testing mock behavior instead of real code | HIGH | Anti-Pattern 1 gate: "Am I testing real behavior or mock existence?" |

| Mocking without understanding side effects | HIGH | Anti-Pattern 3 gate: run with real impl first, observe side effects, THEN mock minimally |

| Incomplete mocks missing downstream fields | HIGH | Anti-Pattern 4 iron rule: mock COMPLETE data structure, not just fields your test checks |

| Coverage below 80% without filling gaps | MEDIUM | Coverage Gate: identify uncovered lines and write additional tests |

| Introducing a new test framework instead of using existing one | MEDIUM | Constraint 6: detect framework first, use project's existing one always |

| Modifying source files to make tests work | HIGH | Role boundary: test writes test files ONLY — source changes go to rune:fix |

| Test-only methods leaking into production code | MEDIUM | Anti-Pattern 2 gate: if method only called by tests → move to test utilities |

| Bulk test writing before first GREEN (horizontal slicing) | CRITICAL | Phase 3.5 gate — bulk_test_count <= 1, defer additional tests to subsequent cycles |

| Test names describe shape (returns boolean, has property) instead of behavior | MEDIUM | Scan names for shape-words; rewrite to behavior verbs (accepts/rejects/produces). See references/test-quality.md |

| Mocking internal collaborators in same module | HIGH | System-boundary rule from references/mocking-policy.md — mock only what you don't own |

| Layering old shallow-module tests on top of new deepened-interface tests | MEDIUM | After improve-architecture deepens a module, REPLACE tests don't ADD them; one interface = one test surface |

Self-Validation

SELF-VALIDATION (run before emitting Test Report):
- [ ] Every test file has at least one assertion — no empty test bodies
- [ ] RED phase output shows actual failures (not "0 tests") — tests were real, not stubs
- [ ] No test modifies source code — test files only, source changes belong to fix
- [ ] Test names describe behavior, not implementation ("should reject expired token" not "test function X")
- [ ] No mocks of the thing being tested — only mock external dependencies
- [ ] If BA requirements exist (REQ-xxx), every requirement has at least one test — check plan's Traceability Matrix

Done When

Cost Profile

~$0.03-0.08 per invocation. Sonnet for writing tests, Bash for running them. Frequent invocation in TDD workflow.

Scope guardrail: Do not modify source or implementation files to make tests pass unless explicitly delegated by the parent agent.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.