@ftrdotdev/pi-qrspi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -0
- package/extensions/qrspi-artifacts.ts +331 -0
- package/package.json +53 -0
- package/prompts/qrspi-design.md +68 -0
- package/prompts/qrspi-iterate.md +65 -0
- package/prompts/qrspi-plan.md +105 -0
- package/prompts/qrspi-research.md +65 -0
- package/prompts/qrspi-structure.md +62 -0
- package/prompts/qrspi.md +54 -0
- package/skills/planning-workflow/SKILL.md +77 -0
- package/skills/planning-workflow/templates/design-concept.md +100 -0
- package/skills/planning-workflow/templates/iterate.md +42 -0
- package/skills/planning-workflow/templates/outline.md +79 -0
- package/skills/planning-workflow/templates/plan.md +194 -0
- package/skills/planning-workflow/templates/research.md +88 -0
- package/skills/planning-workflow/templates/session-summary.md +21 -0
- package/skills/tdd/SKILL.md +178 -0
- package/skills/tdd/assets/test-template.md +199 -0
- package/skills/tdd/references/dependency-injection.md +183 -0
- package/skills/tdd/references/test-doubles.md +172 -0
- package/skills/tdd/references/test-patterns.md +215 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Implementation Plan Template
|
|
2
|
+
|
|
3
|
+
**Usage**: Produced during the QRSPI Plan (P) phase. Load `skill:tdd` before authoring any "Tests First" section. Each phase is a vertical slice — one complete, observable behavior end-to-end. Build Agent executes phases in order; each Verification Gate must pass before the next phase begins.
|
|
4
|
+
|
|
5
|
+
**Constraints**:
|
|
6
|
+
|
|
7
|
+
- Usually ~1,000 lines in total (prose plus code samples) — guidance, not a hard cap; the real constraints are self-containment and per-slice verification gates
|
|
8
|
+
- Code samples are illustrative (locate insertion point, show pattern) — not complete implementations
|
|
9
|
+
- Each phase must deliver something demonstrable at its verification gate
|
|
10
|
+
- No phase may be a technical layer (e.g., "implement all models") — each phase is a behavior
|
|
11
|
+
- In verification gates, use tools over bash commands where available
|
|
12
|
+
|
|
13
|
+
## How to Produce This Document
|
|
14
|
+
|
|
15
|
+
1. Read the design concept and structure/outline silently.
|
|
16
|
+
2. If more exploration is needed, read the research document and look for answers before exploring further.
|
|
17
|
+
3. Write the document using the schema below.
|
|
18
|
+
4. Write to `<worktree-root>/.qrspi/plans/YYYYMMDD-{slug}.md`.
|
|
19
|
+
5. Return the artifact path and a one-paragraph summary of the plan.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
````markdown
|
|
24
|
+
# {Feature/Task Name} — Implementation Plan
|
|
25
|
+
|
|
26
|
+
**Date**: {ISO date}
|
|
27
|
+
**Status**: ready
|
|
28
|
+
**Design concept**: {path to design concept artifact}
|
|
29
|
+
**Research artifact**: {path to research artifact}
|
|
30
|
+
**Issue**: {link — omit if no issue is referenced}
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Overview
|
|
35
|
+
|
|
36
|
+
{What we are implementing and why — 2–4 sentences. Reference the design concept by name, not by repeating its content.}
|
|
37
|
+
|
|
38
|
+
## Skills Consulted
|
|
39
|
+
|
|
40
|
+
- `{skill-name}` — {what it contributed}
|
|
41
|
+
- _(none relevant)_ — if no skills were loaded
|
|
42
|
+
|
|
43
|
+
## Research Summary
|
|
44
|
+
|
|
45
|
+
{Findings from the Research Gate: libraries, APIs, patterns verified. One source per claim. If nothing needed research, state "No external research required."}
|
|
46
|
+
|
|
47
|
+
## Current State Analysis
|
|
48
|
+
|
|
49
|
+
{What exists now, what is missing, key constraints. File:line references for every relevant finding.}
|
|
50
|
+
|
|
51
|
+
## Desired End State
|
|
52
|
+
|
|
53
|
+
{Specification of the completed feature and how to verify it is correct from the outside — what a caller or user observes.}
|
|
54
|
+
|
|
55
|
+
## Slice Order Rationale
|
|
56
|
+
|
|
57
|
+
{Why the slices are ordered as they are. Identify the highest-risk or highest-uncertainty slice and confirm it appears early. Identify any hard dependency ordering between slices (Slice 2 depends on the interface established in Slice 1, etc.).}
|
|
58
|
+
|
|
59
|
+
## Out of Scope
|
|
60
|
+
|
|
61
|
+
{Explicitly list what we are NOT doing. Taken from the design concept's exclusions. Prevents scope creep during implementation.}
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Slice 1 — {Behavior Name, not Layer Name}
|
|
66
|
+
|
|
67
|
+
### What this slice delivers
|
|
68
|
+
|
|
69
|
+
{One paragraph: what Build Agent will be able to demonstrate at the end of this slice. Written as an observable outcome — what a caller sees, not what code was written.}
|
|
70
|
+
|
|
71
|
+
### Tests First (Red)
|
|
72
|
+
|
|
73
|
+
{Before writing any implementation, Build Agent writes these tests. They must fail at the start of this slice for the right behavioral reason — not a compilation error.}
|
|
74
|
+
|
|
75
|
+
Load `skill:tdd` → `references/test-doubles.md` and `assets/test-template.md` for double selection and scaffold.
|
|
76
|
+
|
|
77
|
+
- **{Test name following "subject does what when condition" convention}**
|
|
78
|
+
- Arrange: {what to set up — subject, doubles, inputs}
|
|
79
|
+
- Act: {what to invoke}
|
|
80
|
+
- Assert: {what observable outcome to verify}
|
|
81
|
+
|
|
82
|
+
- **{Test name}**
|
|
83
|
+
- Arrange: {…}
|
|
84
|
+
- Act: {…}
|
|
85
|
+
- Assert: {…}
|
|
86
|
+
|
|
87
|
+
- **{Edge case or error path test}**
|
|
88
|
+
- Arrange: {…}
|
|
89
|
+
- Act: {…}
|
|
90
|
+
- Assert: {expected error or boundary behavior}
|
|
91
|
+
|
|
92
|
+
### Implementation (Green)
|
|
93
|
+
|
|
94
|
+
#### {Component or file}
|
|
95
|
+
|
|
96
|
+
**File**: `path/to/file.ext`
|
|
97
|
+
**Action**: create / modify / delete
|
|
98
|
+
**Reason**: {why this change is needed to make the tests above pass}
|
|
99
|
+
|
|
100
|
+
```language
|
|
101
|
+
// Enough context to locate the insertion point:
|
|
102
|
+
// include enclosing function signature or surrounding block.
|
|
103
|
+
// Do not write the full implementation.
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### {Next component or file}
|
|
107
|
+
|
|
108
|
+
**File**: `path/to/file.ext`
|
|
109
|
+
**Action**: create / modify / delete
|
|
110
|
+
**Reason**: {why}
|
|
111
|
+
|
|
112
|
+
```language
|
|
113
|
+
// Insertion point context only
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Patterns to Follow
|
|
117
|
+
|
|
118
|
+
- {Pattern name}: `file:line` — {description and why Build Agent must match it}
|
|
119
|
+
|
|
120
|
+
### Refactor (if needed)
|
|
121
|
+
|
|
122
|
+
{Identify any structural improvements that are safe once tests are green. No new behavior. If none, state "No refactor needed for this slice."}
|
|
123
|
+
|
|
124
|
+
### Verification Gate — Slice 1
|
|
125
|
+
|
|
126
|
+
**Build Agent must complete every item before starting Slice 2.**
|
|
127
|
+
|
|
128
|
+
Automated:
|
|
129
|
+
|
|
130
|
+
- [ ] All tests written in "Tests First" above pass
|
|
131
|
+
- [ ] `{additional command}` — {expected outcome}
|
|
132
|
+
- [ ] `{lint or type-check command}` — zero errors
|
|
133
|
+
|
|
134
|
+
Manual:
|
|
135
|
+
|
|
136
|
+
- [ ] {Observable behavior to confirm from the outside — what a caller or user sees}
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## Slice 2 — {Behavior Name}
|
|
141
|
+
|
|
142
|
+
### What this slice delivers
|
|
143
|
+
|
|
144
|
+
{…}
|
|
145
|
+
|
|
146
|
+
### Tests First (Red)
|
|
147
|
+
|
|
148
|
+
{…}
|
|
149
|
+
|
|
150
|
+
### Implementation (Green)
|
|
151
|
+
|
|
152
|
+
{…}
|
|
153
|
+
|
|
154
|
+
### Patterns to Follow
|
|
155
|
+
|
|
156
|
+
{…}
|
|
157
|
+
|
|
158
|
+
### Refactor (if needed)
|
|
159
|
+
|
|
160
|
+
{…}
|
|
161
|
+
|
|
162
|
+
### Verification Gate — Slice 2
|
|
163
|
+
|
|
164
|
+
**Build Agent must complete every item before starting Slice 3.**
|
|
165
|
+
|
|
166
|
+
Automated:
|
|
167
|
+
|
|
168
|
+
- [ ] All tests written in "Tests First" above pass
|
|
169
|
+
- [ ] `{command}` — {expected outcome}
|
|
170
|
+
|
|
171
|
+
Manual:
|
|
172
|
+
|
|
173
|
+
- [ ] {Observable behavior}
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## Risk Register
|
|
178
|
+
|
|
179
|
+
| Risk | Likelihood | Impact | Mitigation |
|
|
180
|
+
| ------ | ---------------- | ---------------- | ------------ |
|
|
181
|
+
| {risk} | low / med / high | low / med / high | {mitigation} |
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Final Verification
|
|
186
|
+
|
|
187
|
+
After all slices and their Verification Gates pass:
|
|
188
|
+
|
|
189
|
+
- [ ] All plan requirements implemented
|
|
190
|
+
- [ ] All Verification Gates passed
|
|
191
|
+
- [ ] No regressions in adjacent functionality
|
|
192
|
+
- [ ] Each slice's tests pass in isolation (no cross-slice test pollution)
|
|
193
|
+
- [ ] {Any deployment or migration step}
|
|
194
|
+
````
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Research Document Template
|
|
2
|
+
|
|
3
|
+
**Usage**: Produced during the QRSPI Research (R) phase by the Research Agent.
|
|
4
|
+
Plan Agent reads this before entering the Design phase.
|
|
5
|
+
Do not suggest improvements, critique, or plan — document only what exists.
|
|
6
|
+
|
|
7
|
+
**Constraints**:
|
|
8
|
+
|
|
9
|
+
- Document prose: ~100 words per subsection as a guide (not a hard cap), excluding the **Research Question** and **Summary** sections
|
|
10
|
+
- Detailed Findings:
|
|
11
|
+
- succinct prose, sacrifice grammar for concision
|
|
12
|
+
- one line per sentence
|
|
13
|
+
- Code References:
|
|
14
|
+
- capped at 3 entries per finding
|
|
15
|
+
- order entries based on importance High -> Low
|
|
16
|
+
- Inline summary: 5–10 lines, no duplication of document body
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Document
|
|
21
|
+
|
|
22
|
+
**Path**: `<worktree-root>/.qrspi/research/YYYYMMDD-{topic-slug}.md`
|
|
23
|
+
|
|
24
|
+
```markdown
|
|
25
|
+
---
|
|
26
|
+
date: { ISO datetime }
|
|
27
|
+
git_commit: { hash }
|
|
28
|
+
branch: { branch }
|
|
29
|
+
repository: { repo name }
|
|
30
|
+
topic: "{topic}"
|
|
31
|
+
subagents_used: [{ roles invoked: locator | analyzer | pattern-finder }]
|
|
32
|
+
skills_consulted: [{ skill names loaded, or "none" }]
|
|
33
|
+
status: complete
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
# Research: {topic}
|
|
37
|
+
|
|
38
|
+
## Research Question
|
|
39
|
+
|
|
40
|
+
{Original topic as stated — verbatim, not reframed.}
|
|
41
|
+
|
|
42
|
+
## Summary
|
|
43
|
+
|
|
44
|
+
{3–5 sentences: key components, their connections, and what matters for planning. Written for Plan Agent — what does a planner need to know about this area before designing a solution?}
|
|
45
|
+
|
|
46
|
+
## Detailed Findings
|
|
47
|
+
|
|
48
|
+
### {Component or Subsystem}
|
|
49
|
+
|
|
50
|
+
{Responsibilities, interfaces, data structures, control flow, and integration points with file:line references. One subsection per distinct component.}
|
|
51
|
+
|
|
52
|
+
## Key Patterns
|
|
53
|
+
|
|
54
|
+
{Conventions with file:line examples. Omit this section entirely if Pattern Finder was not invoked.}
|
|
55
|
+
|
|
56
|
+
## Code References
|
|
57
|
+
|
|
58
|
+
- `path/to/file:line` — {description}
|
|
59
|
+
- {…}
|
|
60
|
+
|
|
61
|
+
## Open Questions
|
|
62
|
+
|
|
63
|
+
{Gaps and ambiguities that could not be resolved from the codebase. Each item is either a plan-time assumption for Plan Agent to make explicit, or a trigger for further research.}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Inline Summary
|
|
69
|
+
|
|
70
|
+
Output this inline immediately after writing the document:
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
Research complete: .qrspi/research/{filename}.md
|
|
74
|
+
|
|
75
|
+
Key files:
|
|
76
|
+
- {path} — {one line}
|
|
77
|
+
|
|
78
|
+
Key patterns (if applicable):
|
|
79
|
+
- {pattern with file:line}
|
|
80
|
+
|
|
81
|
+
Skills consulted:
|
|
82
|
+
- {skill name} — {what it contributed, or "none relevant found"}
|
|
83
|
+
|
|
84
|
+
Open questions:
|
|
85
|
+
- {question}
|
|
86
|
+
|
|
87
|
+
Plan Agent: read the full document before planning.
|
|
88
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Session Summary Template
|
|
2
|
+
|
|
3
|
+
**Usage**: Output by the QRSPI orchestration command after all phases are approved. Provides a single reference point for all artifact paths produced in the session.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
QRSPI session complete.
|
|
9
|
+
|
|
10
|
+
Slug: {slug}
|
|
11
|
+
Ticket: {ticket reference, or "none"}
|
|
12
|
+
Date: {YYYY-MM-DD}
|
|
13
|
+
|
|
14
|
+
Artifacts:
|
|
15
|
+
Research: <worktree-root>/.qrspi/research/YYYYMMDD-{slug}.md
|
|
16
|
+
Design: <worktree-root>/.qrspi/designs/YYYYMMDD-{slug}.md
|
|
17
|
+
Outline: <worktree-root>/.qrspi/outlines/YYYYMMDD-{slug}.md
|
|
18
|
+
Plan: <worktree-root>/.qrspi/plans/YYYYMMDD-{slug}.md
|
|
19
|
+
|
|
20
|
+
Next step: pass the plan path to Build Agent.
|
|
21
|
+
```
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: tdd
|
|
3
|
+
description: Guides test-driven development of software components using the red-green-refactor loop. Covers test doubles, dependency injection, test structure, and naming. Use when writing new features test-first, adding tests to existing code, designing a component's interface through tests, or when the user mentions TDD, unit tests, mocks, stubs, or test structure.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Test-Driven Development
|
|
7
|
+
|
|
8
|
+
TDD is a design practice as much as a testing practice. Tests written before implementation force the interface to be designed from the caller's perspective. Painful test setup signals a design problem — too many dependencies, responsibilities, or the wrong abstraction boundary. Let the tests tell you.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Schools
|
|
13
|
+
|
|
14
|
+
Two schools differ on what constitutes a "unit" and where to draw the mock boundary.
|
|
15
|
+
|
|
16
|
+
**London school (default here):** A unit is a single class or function. Double all collaborators at module boundaries. Preferred for new code — drives interface design and keeps feedback fast.
|
|
17
|
+
|
|
18
|
+
**Chicago school:** A unit is a cluster of related objects. Wire real collaborators; only double external infrastructure (databases, HTTP, clocks). Preferred when retrofitting tests onto existing code with stable internal structure.
|
|
19
|
+
|
|
20
|
+
Both are valid. The choice determines where doubles live, not whether you test.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Red-Green-Refactor
|
|
25
|
+
|
|
26
|
+
### Red — write a failing test
|
|
27
|
+
|
|
28
|
+
Write a test that specifies one behavior that does not yet exist. Run it. Confirm it fails **for the right reason**: the behavior is absent, not a compilation error or misconfigured test harness. A test that fails for the wrong reason gives false confidence when it later passes.
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
test "order total includes 20% tax when region is EU":
|
|
32
|
+
order = Order(region: Region.EU, pricing: StubPricingService(basePrice: 100.00))
|
|
33
|
+
assert order.calculate() == 120.00
|
|
34
|
+
// Fails: Order.calculate() does not exist yet — correct red
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Green — write the minimum passing implementation
|
|
38
|
+
|
|
39
|
+
Write the simplest generalisation that satisfies all current tests. "Minimum" means the simplest correct implementation — not a hardcoded return value, unless you are deliberately triangulating (see below). Do not add behavior not yet specified by a test.
|
|
40
|
+
|
|
41
|
+
**Triangulation** is a valid incremental technique: return a hardcode on the first test, write a second test that contradicts it, then generalise. Use it consciously, not reflexively.
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
// First test: order with one item at 100.00 in EU → 120.00
|
|
45
|
+
calculate() -> decimal:
|
|
46
|
+
return 120.00 // hardcode to pass first test only
|
|
47
|
+
|
|
48
|
+
// Second test: order with one item at 200.00 in EU → 240.00
|
|
49
|
+
// Hardcode no longer works — now generalise:
|
|
50
|
+
calculate() -> decimal:
|
|
51
|
+
return this.pricing.getPrice(this.item) * (1 + this.region.taxRate())
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Checkpoint
|
|
55
|
+
|
|
56
|
+
Before refactoring: commit or checkpoint the moment all tests are green. If a refactor breaks tests, revert to the checkpoint rather than debugging a mixed change. This preserves the diagnostic value of the loop.
|
|
57
|
+
|
|
58
|
+
### Refactor — improve structure, not behavior
|
|
59
|
+
|
|
60
|
+
In the refactor phase: no new behavior enters. Rename, extract, consolidate, simplify. If you discover a missing case during refactor, stop — write a test for it (red), pass it (green), then resume the refactor.
|
|
61
|
+
|
|
62
|
+
Watch for design signals during refactor:
|
|
63
|
+
|
|
64
|
+
- Arrange section requires more than five or six lines → component has too many responsibilities
|
|
65
|
+
- Hard to find a meaningful assertion → interface does not expose the right behavior
|
|
66
|
+
- Must mock many collaborators → dependency structure needs redesign
|
|
67
|
+
|
|
68
|
+
When the arrange section is painful, read [references/dependency-injection.md](references/dependency-injection.md).
|
|
69
|
+
When choosing the right double is unclear, read [references/test-doubles.md](references/test-doubles.md).
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Test Structure
|
|
74
|
+
|
|
75
|
+
Use Arrange / Act / Assert. Keep the sections visually distinct.
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
test "payment is declined when card has insufficient funds":
|
|
79
|
+
// Arrange
|
|
80
|
+
gateway = StubPaymentGateway(response: DeclinedResult(code: "INSUFFICIENT_FUNDS"))
|
|
81
|
+
order = Order(gateway: gateway)
|
|
82
|
+
|
|
83
|
+
// Act / Assert
|
|
84
|
+
assertThrows(PaymentDeclined, code: "INSUFFICIENT_FUNDS"):
|
|
85
|
+
order.confirm()
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
One behavior per test. Multiple assertions are acceptable when they describe facets of the same outcome — not when they cover separate behaviors. If the first assertion failing would hide whether a second behavior is correct, split the test.
|
|
89
|
+
|
|
90
|
+
For naming, fixture setup, assertion quality, Object Mother, Test Data Builder, and test pyramid guidance: read [references/test-patterns.md](references/test-patterns.md).
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Dependency Injection
|
|
95
|
+
|
|
96
|
+
The subject under test must not instantiate its own collaborators that cross a boundary. If it does, doubles cannot be substituted.
|
|
97
|
+
|
|
98
|
+
**Constructor injection** — default for object-oriented code. Declare dependencies as constructor parameters.
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
// Untestable — creates its own dependency
|
|
102
|
+
class OrderService:
|
|
103
|
+
confirm(orderId: string) -> void:
|
|
104
|
+
SmtpClient.send(...) // hard-coded
|
|
105
|
+
|
|
106
|
+
// Testable — dependency is substitutable
|
|
107
|
+
class OrderService:
|
|
108
|
+
emailSender: EmailSender
|
|
109
|
+
|
|
110
|
+
constructor(emailSender: EmailSender):
|
|
111
|
+
this.emailSender = emailSender
|
|
112
|
+
|
|
113
|
+
confirm(orderId: string) -> void:
|
|
114
|
+
this.emailSender.send(...)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Argument injection** — default for functional or stateless code. Pass the dependency as a function parameter.
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
function confirmOrder(
|
|
121
|
+
orderId: string,
|
|
122
|
+
sendEmail: (to: string, body: string) -> void
|
|
123
|
+
) -> void:
|
|
124
|
+
sendEmail(lookupEmail(orderId), buildBody(orderId))
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Inject one level deep. The test wires the subject's direct dependencies only. Those dependencies' own dependencies are their internal concern.
|
|
128
|
+
|
|
129
|
+
For factory injection, DI containers, and when to prefer each pattern: read [references/dependency-injection.md](references/dependency-injection.md).
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Mock Boundary Rule
|
|
134
|
+
|
|
135
|
+
Mock at the edge of what you own. A boundary is any dependency your code calls but does not define:
|
|
136
|
+
|
|
137
|
+
- External services: HTTP APIs, payment gateways, identity providers
|
|
138
|
+
- Infrastructure: databases, file systems, message queues
|
|
139
|
+
- Non-determinism: system clock, random number generation
|
|
140
|
+
|
|
141
|
+
Do not mock:
|
|
142
|
+
|
|
143
|
+
- Classes and functions within the same module as the code under test
|
|
144
|
+
- Value objects and domain entities
|
|
145
|
+
- Pure functions with no I/O
|
|
146
|
+
|
|
147
|
+
The test: _if I refactor the internal structure without changing observable behavior, would this double break?_ If yes, it is coupled to an implementation detail, not a boundary.
|
|
148
|
+
|
|
149
|
+
For the full test doubles taxonomy (stub, spy, fake, mock, dummy) and a decision guide: read [references/test-doubles.md](references/test-doubles.md).
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Workflow Checklist
|
|
154
|
+
|
|
155
|
+
Copy and track progress through each cycle:
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
TDD cycle:
|
|
159
|
+
- [ ] Write one failing test — confirm it fails for the right reason (Red)
|
|
160
|
+
- [ ] Write minimum generalisation to pass all tests (Green)
|
|
161
|
+
- [ ] Checkpoint: commit or stage before touching structure
|
|
162
|
+
- [ ] Refactor structure only — no new behavior
|
|
163
|
+
- [ ] If a missing case is found during refactor: stop, write a test, then resume
|
|
164
|
+
- [ ] Repeat for the next behavior
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Reference Files
|
|
170
|
+
|
|
171
|
+
Read these on demand — do not load all at once.
|
|
172
|
+
|
|
173
|
+
| File | Read when |
|
|
174
|
+
| ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
175
|
+
| [references/test-doubles.md](references/test-doubles.md) | Choosing between stub, spy, fake, mock, or dummy; deciding where to draw the mock boundary |
|
|
176
|
+
| [references/dependency-injection.md](references/dependency-injection.md) | Arrange section is complex; too many collaborators; choosing constructor vs argument vs factory injection |
|
|
177
|
+
| [references/test-patterns.md](references/test-patterns.md) | Naming a test; structuring fixture data; writing assertions that diagnose failures; understanding the test pyramid |
|
|
178
|
+
| [assets/test-template.md](assets/test-template.md) | Generating a test suite from scratch; need a concrete scaffold showing London vs Chicago school wiring, builder setup, and Arrange/Act/Assert structure |
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Test Template
|
|
2
|
+
|
|
3
|
+
Language-agnostic pseudo-code scaffold for a well-formed test suite. Two variants are shown: London school (doubles at boundaries) and Chicago school (real collaborators, double only external infrastructure). Choose based on the guidance in SKILL.md.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## London School — Double at Module Boundaries
|
|
8
|
+
|
|
9
|
+
Preferred for new code. Each collaborator that crosses a module or infrastructure boundary is replaced with a double. Only the subject under test runs real code.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
// --- Doubles ---
|
|
13
|
+
|
|
14
|
+
class FakeOrderRepository implements OrderRepository:
|
|
15
|
+
store: Map<string, Order> = {}
|
|
16
|
+
|
|
17
|
+
save(order: Order) -> void:
|
|
18
|
+
store[order.id] = order
|
|
19
|
+
|
|
20
|
+
findById(id: string) -> Order | null:
|
|
21
|
+
return store[id] ?? null
|
|
22
|
+
|
|
23
|
+
class SpyEmailSender implements EmailSender:
|
|
24
|
+
calls: List<{to, subject}> = []
|
|
25
|
+
|
|
26
|
+
send(to: string, subject: string, body: string) -> void:
|
|
27
|
+
calls.append({to, subject})
|
|
28
|
+
|
|
29
|
+
class StubPricingService implements PricingService:
|
|
30
|
+
constructor(private basePrice: decimal) {}
|
|
31
|
+
|
|
32
|
+
getPrice(productId: string) -> decimal:
|
|
33
|
+
return basePrice
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// --- Subject wiring helper ---
|
|
37
|
+
|
|
38
|
+
function makeOrderService(overrides = {}) -> {service, repo, emailSender}:
|
|
39
|
+
repo = overrides.repo ?? FakeOrderRepository()
|
|
40
|
+
emailSender = overrides.emailSender ?? SpyEmailSender()
|
|
41
|
+
pricing = overrides.pricing ?? StubPricingService(basePrice: 10.00)
|
|
42
|
+
|
|
43
|
+
service = OrderService(
|
|
44
|
+
repo: repo,
|
|
45
|
+
emailSender: emailSender,
|
|
46
|
+
pricing: pricing
|
|
47
|
+
)
|
|
48
|
+
return {service, repo, emailSender}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
// --- Tests ---
|
|
52
|
+
|
|
53
|
+
test "confirmed order is persisted":
|
|
54
|
+
// Arrange
|
|
55
|
+
{service, repo} = makeOrderService()
|
|
56
|
+
|
|
57
|
+
// Act
|
|
58
|
+
service.confirm(orderId: "order-1")
|
|
59
|
+
|
|
60
|
+
// Assert
|
|
61
|
+
saved = repo.findById("order-1")
|
|
62
|
+
assert saved != null
|
|
63
|
+
assert saved.status == Status.CONFIRMED
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
test "confirmation email is sent to customer on confirm":
|
|
67
|
+
// Arrange
|
|
68
|
+
{service, emailSender} = makeOrderService()
|
|
69
|
+
|
|
70
|
+
// Act
|
|
71
|
+
service.confirm(orderId: "order-1")
|
|
72
|
+
|
|
73
|
+
// Assert
|
|
74
|
+
assert emailSender.calls.length == 1
|
|
75
|
+
assert emailSender.calls[0].to == "customer@example.com"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
test "confirm throws when order is already cancelled":
|
|
79
|
+
// Arrange
|
|
80
|
+
cancelledOrder = OrderBuilder().withStatus(Status.CANCELLED).build()
|
|
81
|
+
repo = FakeOrderRepository()
|
|
82
|
+
repo.save(cancelledOrder)
|
|
83
|
+
{service} = makeOrderService(overrides: {repo})
|
|
84
|
+
|
|
85
|
+
// Act / Assert
|
|
86
|
+
assertThrows(InvalidStateError):
|
|
87
|
+
service.confirm(orderId: cancelledOrder.id)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**What to observe:**
|
|
91
|
+
|
|
92
|
+
- Each double replaces exactly one boundary dependency
|
|
93
|
+
- `makeOrderService` keeps arrange sections short; only the field relevant to the test is overridden
|
|
94
|
+
- Each test exercises one behavior; assertions are scoped to that behavior
|
|
95
|
+
- `SpyEmailSender` records calls for after-the-fact assertion; no upfront expectation needed
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Chicago School — Real Collaborators, Double Only Infrastructure
|
|
100
|
+
|
|
101
|
+
Preferred when retrofitting tests onto existing code with stable internal structure, or when the interactions between real collaborators are themselves the thing being verified.
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
// --- Only external infrastructure is doubled ---
|
|
105
|
+
|
|
106
|
+
class InMemoryDatabase implements Database:
|
|
107
|
+
tables: Map<string, List<Row>> = {}
|
|
108
|
+
|
|
109
|
+
query(sql: string, params: List) -> List<Row>:
|
|
110
|
+
// simplified in-memory SQL execution
|
|
111
|
+
...
|
|
112
|
+
|
|
113
|
+
execute(sql: string, params: List) -> void:
|
|
114
|
+
...
|
|
115
|
+
|
|
116
|
+
class FakeEmailProvider implements EmailProvider:
|
|
117
|
+
sent: List<EmailMessage> = []
|
|
118
|
+
|
|
119
|
+
deliver(message: EmailMessage) -> void:
|
|
120
|
+
sent.append(message)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
// --- Real internal collaborators are wired ---
|
|
124
|
+
|
|
125
|
+
function makeSystem(overrides = {}) -> {orderService, emailService, db, emailProvider}:
|
|
126
|
+
db = overrides.db ?? InMemoryDatabase()
|
|
127
|
+
emailProvider = overrides.emailProvider ?? FakeEmailProvider()
|
|
128
|
+
|
|
129
|
+
// Real internal components — not doubled
|
|
130
|
+
orderRepo = SqlOrderRepository(db: db)
|
|
131
|
+
emailService = EmailService(provider: emailProvider)
|
|
132
|
+
pricing = PricingService(db: db)
|
|
133
|
+
|
|
134
|
+
orderService = OrderService(
|
|
135
|
+
repo: orderRepo,
|
|
136
|
+
emailService: emailService,
|
|
137
|
+
pricing: pricing
|
|
138
|
+
)
|
|
139
|
+
return {orderService, emailService, db, emailProvider}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
// --- Tests verify observable state, not interaction details ---
|
|
143
|
+
|
|
144
|
+
test "confirmed order is persisted and customer notified":
|
|
145
|
+
// Arrange
|
|
146
|
+
{orderService, db, emailProvider} = makeSystem()
|
|
147
|
+
db.seed("orders", [Order(id: "order-1", status: Status.PENDING, customerEmail: "a@b.com")])
|
|
148
|
+
|
|
149
|
+
// Act
|
|
150
|
+
orderService.confirm(orderId: "order-1")
|
|
151
|
+
|
|
152
|
+
// Assert — state-based, not interaction-based
|
|
153
|
+
row = db.query("SELECT status FROM orders WHERE id = ?", ["order-1"])[0]
|
|
154
|
+
assert row.status == "CONFIRMED"
|
|
155
|
+
assert emailProvider.sent.any(m -> m.to == "a@b.com")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
test "total includes tax for EU orders":
|
|
159
|
+
// Arrange
|
|
160
|
+
{orderService, db} = makeSystem()
|
|
161
|
+
db.seed("products", [Product(id: "p-1", basePrice: 100.00)])
|
|
162
|
+
db.seed("orders", [Order(id: "order-1", productId: "p-1", region: "EU")])
|
|
163
|
+
|
|
164
|
+
// Act
|
|
165
|
+
total = orderService.calculateTotal(orderId: "order-1")
|
|
166
|
+
|
|
167
|
+
// Assert
|
|
168
|
+
assert total == 120.00
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**What to observe:**
|
|
172
|
+
|
|
173
|
+
- Only `Database` and `EmailProvider` — the external infrastructure — are doubled
|
|
174
|
+
- `OrderRepository`, `EmailService`, `PricingService` are real; their interactions are part of what is being tested
|
|
175
|
+
- Assertions are on persisted state and delivered messages, not on which internal methods were called
|
|
176
|
+
- Arrange seeds data into the fake database rather than constructing object graphs directly
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Builder Reference
|
|
181
|
+
|
|
182
|
+
Both variants benefit from a builder for constructing domain objects with varying attributes.
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
class OrderBuilder:
|
|
186
|
+
_id: string = "order-1"
|
|
187
|
+
_status: Status = Status.PENDING
|
|
188
|
+
_region: Region = Region.DOMESTIC
|
|
189
|
+
_items: List = [Item(productId: "p-1", qty: 1)]
|
|
190
|
+
|
|
191
|
+
withId(id: string) -> OrderBuilder: _id = id; return this
|
|
192
|
+
withStatus(s: Status) -> OrderBuilder: _status = s; return this
|
|
193
|
+
withRegion(r: Region) -> OrderBuilder: _region = r; return this
|
|
194
|
+
withItems(items: List) -> OrderBuilder: _items = items; return this
|
|
195
|
+
build() -> Order:
|
|
196
|
+
return Order(id: _id, status: _status, region: _region, items: _items)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Always provide defaults for every field. A test sets only the fields that are meaningful to its behavior — all others are noise suppressed by the builder's defaults.
|