@skilldeck/craft 0.0.0-stage → 1.0.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.
@@ -0,0 +1,112 @@
1
+ # Test seams by layer
2
+
3
+ A **seam** is where a test meets the code: what the test calls, what it controls, and what it observes. A good seam is stable (it survives refactors), fast and deterministic. It substitutes only the things that are truly external: the network, the clock, randomness, and slow or shared storage.
4
+
5
+ Examples use JavaScript and TypeScript with a generic `test` and `expect`. Translate them to the project's framework (pytest, Go `testing`, JUnit) and keep the shape.
6
+
7
+ ## 1. Pure function
8
+
9
+ **Seam:** the function's arguments and its return value. Nothing is substituted.
10
+
11
+ ```ts
12
+ // src/pricing.ts
13
+ export function priceFor(lines: Line[], rules: Rules): number
14
+
15
+ // test
16
+ test("applies the bulk discount at exactly 10 units", () => {
17
+ expect(priceFor([{ sku: "A", unit: 10, qty: 10 }], { bulkAt: 10, bulkPct: 10 })).toBe(90)
18
+ })
19
+ ```
20
+
21
+ - Make the hidden inputs explicit. If it calls `Date.now()` or `Math.random()`, pass `now` or `rng` in as a parameter.
22
+ - Edges to name: empty input, exactly at a boundary, one past it, invalid values.
23
+
24
+ ## 2. Module boundary (service with collaborators)
25
+
26
+ **Seam:** the module's public functions. Collaborators are injected through a constructor or a factory. Substitute a collaborator only when it is external or slow; prefer a small in-memory fake to a mock.
27
+
28
+ ```ts
29
+ // src/signup.ts
30
+ export function makeSignup({ users, mailer, clock }: Deps) {
31
+ return { async register(email: string) { /* ... */ } }
32
+ }
33
+
34
+ // test
35
+ test("sends exactly one welcome email per new user", async () => {
36
+ const sent: string[] = []
37
+ const signup = makeSignup({ users: memoryUsers(), mailer: { send: (to) => sent.push(to) }, clock: fixedClock("2026-01-01") })
38
+ await signup.register("a@x.io")
39
+ await signup.register("a@x.io")
40
+ expect(sent).toEqual(["a@x.io"])
41
+ })
42
+ ```
43
+
44
+ - Assert on outcomes (what was stored, what was sent), not on which private helper got called.
45
+ - A test that needs five fakes is telling you the module is too wide. Consider [[deepen-modules]].
46
+
47
+ ## 3. HTTP handler
48
+
49
+ **Seam:** an in-process request, sent with the framework's injector (Fastify's `inject`, supertest, Hono's `app.request`, Go's `httptest`). No real port and no real network. Storage is faked or uses a per-test database.
50
+
51
+ ```ts
52
+ test("POST /orders rejects a negative quantity with 422 and a field error", async () => {
53
+ const app = buildApp({ orders: memoryOrders() })
54
+ const res = await app.inject({ method: "POST", url: "/orders", payload: { sku: "A", qty: -1 } })
55
+ expect(res.statusCode).toBe(422)
56
+ expect(res.json()).toEqual({ error: "invalid", field: "qty" })
57
+ })
58
+ ```
59
+
60
+ - Test the contract: status, body shape, headers. Keep business rules in the module seam (layer 2), so handler tests stay few and thin.
61
+ - Write one test per auth outcome: no credentials, wrong tenant, allowed.
62
+
63
+ ## 4. CLI command
64
+
65
+ **Seam:** the command as a function that takes `argv`, `env` and streams, and returns an exit code. Spawning a real process is the integration test, and one per command is enough.
66
+
67
+ ```js
68
+ // src/cli.mjs
69
+ export async function main(argv, { stdout, stderr, env, cwd }) { /* returns exit code */ }
70
+
71
+ // test
72
+ test("exits 2 and prints usage when no file is given", async () => {
73
+ const out = capture(), err = capture()
74
+ const code = await main([], { stdout: out, stderr: err, env: {}, cwd: tmpdir() })
75
+ expect(code).toBe(2)
76
+ expect(err.text()).toMatch(/usage:/)
77
+ })
78
+ ```
79
+
80
+ - Use a fresh temp directory for each test that touches files, and assert on the files it writes.
81
+ - Keep `process.exit` out of `main`; only the bin shim calls it.
82
+
83
+ ## 5. DB repository
84
+
85
+ **Seam:** the repository's methods, run against a **real** database of the same engine. In-memory SQLite stands in only when production is SQLite. Use a transaction that rolls back after each test, or one schema per test.
86
+
87
+ ```ts
88
+ test("findActive excludes soft-deleted users", async () => {
89
+ await withTestDb(async (db) => {
90
+ const repo = makeUserRepo(db)
91
+ await repo.insert({ id: 1, email: "a@x.io" })
92
+ await repo.insert({ id: 2, email: "b@x.io", deletedAt: new Date() })
93
+ expect((await repo.findActive()).map((u) => u.id)).toEqual([1])
94
+ })
95
+ })
96
+ ```
97
+
98
+ - Never mock the SQL. Mocked queries test your guess about the database, not the database.
99
+ - Services above the repository use an in-memory fake that implements the same interface. Run that fake against the same repository tests (a contract test) so the two can't drift apart.
100
+
101
+ ## When TDD doesn't fit, and what to do instead
102
+
103
+ | Situation | Why TDD is a poor fit | Do this instead |
104
+ |---|---|---|
105
+ | Visual layout, styling, animation | The "right" result is judged by eye | Extract logic (formatting, state transitions) and TDD that; check the view with a screenshot or story |
106
+ | Thin glue between two libraries | No logic of your own to specify | One integration smoke test that runs the real path end to end |
107
+ | Config, environment, infrastructure | Behavior lives in another system | A smoke check with an expected output (`curl` the health route, run `--version`), recorded in the PR |
108
+ | Exploratory spike | You don't know the behavior yet | Timebox it, throw it away, then TDD the real version |
109
+ | Generated code | The generator owns the behavior | Test the generator, or snapshot-test the output contract once |
110
+ | Legacy code with no seams | You can't get a test in | Write a characterization test at the outermost seam you can reach, then introduce a seam under that test |
111
+
112
+ Each of these needs the user's OK as an exception under [[test-first]]. Say which row applies and what you will check instead.