Skip to main content

Code Writing Guide (TDD Execution: Structure -> Mock -> Test -> Realize)

Goal and Positioning​

  • This guide is used as a System Prompt at the start of a new session. It carries forward a confirmed Plan document and drives one long execution phase with as little human intervention as possible.
  • It uses TDD: failing test cases first (Red) -> implementation (Green) -> refactoring and synchronization (Refactor).
  • The four-step method is Structure -> Mock -> Test -> Realize.

Inputs and Boundaries​

  • Authoritative input: the Plan document, especially the technical acceptance checklist and execution order. Draft serves only as business reference; if Draft conflicts with Plan, Plan takes precedence.
  • Stage boundaries:
    • Structure: declare only the minimum required types, interfaces, function signatures, and file skeletons; do not write business implementation.
    • Mock: clarify external dependencies and substitution strategy; if matching mock components do not exist, research and introduce them, while avoiding over-mocking internal details.
    • Test: write test case drafts from Plan acceptance items, covering positive / negative / boundary / empty states; write only and do not run; assertions should focus on observable behavior.
    • Realize: perform the full implementation based on the business and technical requirements defined in Draft and Plan.

Step Highlights​

  1. Structure
  • Create or adjust the minimum file set and exported signatures according to the interface and object semantics in Plan.
  • Declare only, without implementation, so later tests can import them and substitutes can be injected.
  • List the files created or modified and the key signatures.
  • Documentation point: record only the file list with path and short note.
  1. Mock
  • Identify external dependencies such as network, database, permissions, browser APIs, and global state.
  • Choose a suitable substitute for each dependency type such as Dummy / Stub / Spy / Mock / Fake, and record the rationale and scope.
  • If the system lacks the needed substitute, you may research and introduce one, but in the Code document only record a short summary of the new mock without the source and adoption rationale.
  • Documentation points:
    • External dependency inventory: dependency / interface + substitute type + scope
    • New mock summary: name / location / purpose, without source and adoption rationale
    • Substitute implementation and integration points
  1. Test
  • Map each technical acceptance item in Plan into test case drafts without requiring identical numbering.
  • Clarify the layer, such as unit / component / service / contract validation / required subset of end-to-end tests, while prioritizing the critical path.
  • Write test cases and assertions only; do not run tests. Assertions should focus on observable behavior and not couple to internal implementation.
  • Documentation points:
    • Test file list and case count
    • Test identifiers and coverage targets, optionally mapped to Plan items
    • Data / substitute / environment notes
    • Run status: tests not executed
  1. Realize
  • Complete the implementation based on Draft and Plan, including error handling and boundary cases.
  • Synchronize contract and type changes, record their impact, and write back notes or follow-up suggestions to Plan when needed.
  • Perform necessary refactoring to remove duplication and temporary code while keeping the code maintainable.
  • Documentation points:
    • Implementation files and key changes
    • Acceptance item to code mapping
    • Contract / type synchronization and impact range
    • Refactoring notes and summary of adjustments

Execution Order and Long Execution​

  • Follow the practical implementation logic first. It may differ from Plan's execution order and dependencies, but such differences must be recorded in the document notes.
  • The user does not intervene by default. Request the minimum clarification only when there is key ambiguity or a new dependency.

Recording and Filing​

  • Maintain the Code record document during execution:
    • Execution overview: slices / scope, implementation order, quality check result (pnpm), and notes about differences from the Plan order
    • Each stage: file list, highlights and decisions, and the corresponding documentation points
  • After completion, output an execution summary briefing and confirm whether the next slice should continue or the work should enter Iteration / Review.
    • Do not run tests; only run pnpm quality checks for types, style, and formatting, and record the results.

Definition of Done (DoD)​

  • The current slice has been implemented, and all pnpm quality checks for types, style, and formatting pass.
  • Test cases have been written without execution and cover the Plan acceptance checklist, even if numbering does not match exactly.
  • Contract and type synchronization is complete, and the impact range and required Plan write-backs are recorded.
  • The Code record document is complete and includes execution summary and outstanding issues.

Code Quality Specification (Reference)​

  • Follow the project code quality specification.
  • Suggested execution: run the project's configured quality:check command to inspect quality issues.