class-ai-agent 1.2.3 → 1.3.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.
Files changed (116) hide show
  1. package/.agent/README.md +33 -0
  2. package/.agent/SESSION.md +54 -0
  3. package/.agent/SESSION.template.md +46 -0
  4. package/.claude/CLAUDE.md +21 -6
  5. package/.claude/commands/build.md +5 -4
  6. package/.claude/commands/debug.md +2 -1
  7. package/.claude/commands/handoff.md +94 -0
  8. package/.claude/commands/plan.md +1 -0
  9. package/.claude/commands/publish-npm.md +119 -0
  10. package/.claude/commands/resume.md +107 -0
  11. package/.claude/commands/spec.md +2 -1
  12. package/.claude/references/agent-continuity.md +42 -0
  13. package/.claude/references/codegraph.md +50 -0
  14. package/.claude/rules/agent-continuity.md +39 -0
  15. package/.claude/skills/agent-continuity/SKILL.md +70 -0
  16. package/.cursor/CURSOR.md +37 -5
  17. package/.cursor/commands/build.md +5 -4
  18. package/.cursor/commands/debug.md +2 -1
  19. package/.cursor/commands/handoff.md +94 -0
  20. package/.cursor/commands/plan.md +1 -0
  21. package/.cursor/commands/publish-npm.md +119 -0
  22. package/.cursor/commands/resume.md +107 -0
  23. package/.cursor/commands/spec.md +2 -1
  24. package/.cursor/mcp.json +15 -0
  25. package/.cursor/references/agent-continuity.md +42 -0
  26. package/.cursor/references/codegraph.md +87 -0
  27. package/.cursor/rules/agent-continuity.mdc +44 -0
  28. package/.cursor/rules/codegraph.mdc +47 -0
  29. package/.cursor/rules/cursor-overview.mdc +10 -3
  30. package/.cursor/skills/agent-continuity/SKILL.md +70 -0
  31. package/.kiro/KIRO.md +146 -0
  32. package/.kiro/agents/backend.md +395 -0
  33. package/.kiro/agents/code-reviewer.md +110 -0
  34. package/.kiro/agents/copywriter-seo.md +236 -0
  35. package/.kiro/agents/frontend.md +384 -0
  36. package/.kiro/agents/project-manager.md +201 -0
  37. package/.kiro/agents/qa.md +221 -0
  38. package/.kiro/agents/security-auditor.md +143 -0
  39. package/.kiro/agents/systems-architect.md +211 -0
  40. package/.kiro/agents/test-engineer.md +123 -0
  41. package/.kiro/agents/ui-ux-designer.md +210 -0
  42. package/.kiro/commands/build.md +133 -0
  43. package/.kiro/commands/debug.md +243 -0
  44. package/.kiro/commands/deploy.md +40 -0
  45. package/.kiro/commands/fix-issue.md +42 -0
  46. package/.kiro/commands/handoff.md +94 -0
  47. package/.kiro/commands/plan.md +126 -0
  48. package/.kiro/commands/publish-npm.md +119 -0
  49. package/.kiro/commands/resume.md +107 -0
  50. package/.kiro/commands/review.md +50 -0
  51. package/.kiro/commands/simplify.md +222 -0
  52. package/.kiro/commands/spec.md +96 -0
  53. package/.kiro/commands/test.md +214 -0
  54. package/.kiro/references/accessibility-checklist.md +174 -0
  55. package/.kiro/references/agent-continuity.md +42 -0
  56. package/.kiro/references/codegraph.md +86 -0
  57. package/.kiro/references/performance-checklist.md +150 -0
  58. package/.kiro/references/security-checklist.md +94 -0
  59. package/.kiro/references/testing-patterns.md +183 -0
  60. package/.kiro/settings/mcp.json +15 -0
  61. package/.kiro/settings.json +8 -0
  62. package/.kiro/skills/agent-continuity/SKILL.md +70 -0
  63. package/.kiro/skills/code-review/SKILL.md +208 -0
  64. package/.kiro/skills/deploy/SKILL.md +68 -0
  65. package/.kiro/skills/deploy/deploy.md +735 -0
  66. package/.kiro/skills/incremental-implementation/SKILL.md +210 -0
  67. package/.kiro/skills/security-review/SKILL.md +71 -0
  68. package/.kiro/skills/tdd/SKILL.md +217 -0
  69. package/.kiro/skills/ui-ux-pro-max/SKILL.md +288 -0
  70. package/.kiro/skills/ui-ux-pro-max/data/charts.csv +26 -0
  71. package/.kiro/skills/ui-ux-pro-max/data/colors.csv +97 -0
  72. package/.kiro/skills/ui-ux-pro-max/data/icons.csv +101 -0
  73. package/.kiro/skills/ui-ux-pro-max/data/landing.csv +31 -0
  74. package/.kiro/skills/ui-ux-pro-max/data/products.csv +97 -0
  75. package/.kiro/skills/ui-ux-pro-max/data/react-performance.csv +45 -0
  76. package/.kiro/skills/ui-ux-pro-max/data/stacks/astro.csv +54 -0
  77. package/.kiro/skills/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  78. package/.kiro/skills/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  79. package/.kiro/skills/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  80. package/.kiro/skills/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  81. package/.kiro/skills/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
  82. package/.kiro/skills/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  83. package/.kiro/skills/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  84. package/.kiro/skills/ui-ux-pro-max/data/stacks/react.csv +54 -0
  85. package/.kiro/skills/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  86. package/.kiro/skills/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  87. package/.kiro/skills/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  88. package/.kiro/skills/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  89. package/.kiro/skills/ui-ux-pro-max/data/styles.csv +68 -0
  90. package/.kiro/skills/ui-ux-pro-max/data/typography.csv +58 -0
  91. package/.kiro/skills/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
  92. package/.kiro/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  93. package/.kiro/skills/ui-ux-pro-max/data/web-interface.csv +31 -0
  94. package/.kiro/skills/ui-ux-pro-max/scripts/core.py +253 -0
  95. package/.kiro/skills/ui-ux-pro-max/scripts/design_system.py +1067 -0
  96. package/.kiro/skills/ui-ux-pro-max/scripts/search.py +114 -0
  97. package/.kiro/steering/agent-continuity.md +44 -0
  98. package/.kiro/steering/api-conventions.md +85 -0
  99. package/.kiro/steering/clean-code.md +211 -0
  100. package/.kiro/steering/code-style.md +92 -0
  101. package/.kiro/steering/codegraph.md +47 -0
  102. package/.kiro/steering/database.md +66 -0
  103. package/.kiro/steering/error-handling.md +98 -0
  104. package/.kiro/steering/git-workflow.md +83 -0
  105. package/.kiro/steering/kiro-overview.md +38 -0
  106. package/.kiro/steering/monitoring.md +317 -0
  107. package/.kiro/steering/naming-conventions.md +266 -0
  108. package/.kiro/steering/project-structure.md +71 -0
  109. package/.kiro/steering/security.md +95 -0
  110. package/.kiro/steering/system-design.md +168 -0
  111. package/.kiro/steering/tech-stack.md +462 -0
  112. package/.kiro/steering/testing.md +110 -0
  113. package/AGENTS.md +13 -7
  114. package/README.md +122 -18
  115. package/bin/class-ai-agent.cjs +165 -11
  116. package/package.json +10 -4
@@ -0,0 +1,107 @@
1
+ ---
2
+ name: resume
3
+ description: Start-of-session — load .agent/SESSION.md and continue prior work
4
+ ---
5
+
6
+ # /resume — Continue prior work
7
+
8
+ > "Read the map before you move."
9
+
10
+ ## Purpose
11
+
12
+ Load cross-tool handoff state and continue work from a previous agent session without re-discovering context.
13
+
14
+ ## When to use
15
+
16
+ - Starting a new chat on the same feature
17
+ - Switching tools (Cursor ↔ Claude Code ↔ Kiro)
18
+ - User says "continue", "pick up where we left off", or "resume"
19
+ - After pulling a branch that includes an updated `.agent/SESSION.md`
20
+
21
+ ## Prerequisites
22
+
23
+ - **`.agent/SESSION.md`** exists in the project root
24
+ - If missing: run `npx class-ai-agent` or copy `.agent/SESSION.template.md` → `.agent/SESSION.md`
25
+
26
+ ## Workflow
27
+
28
+ ### Phase 1: Load handoff (read-only first)
29
+
30
+ Read in this order:
31
+
32
+ 1. **`.agent/SESSION.md`** — goal, done, in progress, next, decisions, gotchas, pointers
33
+ 2. **`tasks/todo.md`** — if referenced in Pointers
34
+ 3. **`SPEC.md`** or path from Pointers — if in spec/plan/build phase
35
+
36
+ > Do **not** edit code until Phase 3 plan is stated to the user.
37
+
38
+ ### Phase 2: Sanity check
39
+
40
+ From SESSION **Gotchas** and **Pointers**, run quick checks when noted:
41
+
42
+ - `git status` — uncommitted work matches SESSION?
43
+ - Build/test commands listed in Gotchas — run if stale or uncertain
44
+ - Branch matches Pointers
45
+
46
+ If SESSION reports **blockers** or broken build, surface them before implementing.
47
+
48
+ ### Phase 3: State plan to user
49
+
50
+ Reply with a short structured summary:
51
+
52
+ ```markdown
53
+ ## Resuming
54
+
55
+ **Goal:** [from SESSION]
56
+ **Phase:** [spec | plan | build | test | review | debug]
57
+ **Last updated:** [Meta date] via [tool/persona]
58
+
59
+ ### Already done
60
+ - ...
61
+
62
+ ### In progress
63
+ - ...
64
+
65
+ ### Next (this session)
66
+ 1. ...
67
+
68
+ ### Risks / blockers
69
+ - ...
70
+ ```
71
+
72
+ Ask for confirmation only if SESSION is ambiguous or blockers need a decision.
73
+
74
+ ### Phase 4: Continue workflow
75
+
76
+ | Phase in SESSION | Command to follow |
77
+ |------------------|-------------------|
78
+ | spec | `/spec` (refine) or `/plan` if spec is done |
79
+ | plan | `/plan` or `/build` if plan exists |
80
+ | build | `/build` |
81
+ | test | `/test` |
82
+ | review | `/review` |
83
+ | debug | `/debug` |
84
+
85
+ Update **Meta** in `.agent/SESSION.md` when you change phase or tool.
86
+
87
+ ### Phase 5: During work
88
+
89
+ - After meaningful progress, update SESSION **Done** / **In progress** / **Next**
90
+ - End session with **`/handoff`**
91
+
92
+ ## If SESSION is empty or stale
93
+
94
+ 1. Survey repo: `git log`, `tasks/todo.md`, open PRs
95
+ 2. Rebuild SESSION from evidence
96
+ 3. Ask user to confirm goal and next steps
97
+ 4. Run `/handoff` when aligned
98
+
99
+ ## Output
100
+
101
+ - Resumption summary (Phase 3)
102
+ - Explicit next action (first item from **Next**)
103
+ - No code changes until plan is stated (unless user asked for a specific fix)
104
+
105
+ ## Next step
106
+
107
+ Execute the first **Next** item using the appropriate workflow command (`/build`, etc.).
@@ -0,0 +1,50 @@
1
+ # Review Command
2
+
3
+ ## Description
4
+ Perform a thorough code review of specified files or a pull request.
5
+
6
+ ## Usage
7
+ Tell Claude: "Review [file/PR/feature]" or "Do a code review of [changes]"
8
+
9
+ ## Review Checklist
10
+
11
+ ### Code Quality
12
+ - [ ] Code follows style guide (`.claude/rules/code-style.md`)
13
+ - [ ] No unnecessary complexity or duplication
14
+ - [ ] Functions are small and focused (single responsibility)
15
+ - [ ] Variable and function names are descriptive
16
+
17
+ ### Security
18
+ - [ ] No hardcoded secrets or credentials
19
+ - [ ] Input validation is present
20
+ - [ ] Authentication/authorization checks in place
21
+ - [ ] See `.claude/rules/security.md` for full checklist
22
+
23
+ ### Error Handling
24
+ - [ ] Errors are properly caught and handled
25
+ - [ ] Meaningful error messages
26
+ - [ ] No swallowed exceptions
27
+ - [ ] See `.claude/rules/error-handling.md`
28
+
29
+ ### Testing
30
+ - [ ] Unit tests cover new logic
31
+ - [ ] Edge cases are tested
32
+ - [ ] Tests are readable and maintainable
33
+ - [ ] See `.claude/rules/testing.md`
34
+
35
+ ### Database
36
+ - [ ] Queries are optimized (no N+1)
37
+ - [ ] Transactions used where appropriate
38
+ - [ ] See `.claude/rules/database.md`
39
+
40
+ ### API
41
+ - [ ] Endpoints follow REST conventions
42
+ - [ ] Request/response schemas are documented
43
+ - [ ] See `.claude/rules/api-conventions.md`
44
+
45
+ ## Output Format
46
+ Provide feedback as:
47
+ - 🔴 **Critical** — Must fix before merge
48
+ - 🟡 **Warning** — Should fix, potential issue
49
+ - 🟢 **Suggestion** — Nice to have improvement
50
+ - ✅ **Good** — Highlight what's done well
@@ -0,0 +1,222 @@
1
+ ---
2
+ name: simplify
3
+ description: Reduce complexity without changing behavior — code simplification
4
+ ---
5
+
6
+ # /simplify — Code Simplification
7
+
8
+ > "Complexity is the enemy of execution."
9
+
10
+ ## Purpose
11
+
12
+ Simplify code for clarity and maintainability. Reduce complexity **without changing behavior**.
13
+
14
+ ## When to Use
15
+
16
+ - After `/review` identifies complexity issues
17
+ - When code is hard to understand
18
+ - Before adding new features to tangled code
19
+ - During tech debt cleanup sprints
20
+
21
+ ## Principles
22
+
23
+ ### Chesterton's Fence
24
+
25
+ > Before removing something, understand why it exists.
26
+
27
+ Don't delete code just because it looks unnecessary. Investigate:
28
+ - Git history: `git log -p -- path/to/file`
29
+ - Related tests
30
+ - Comments or documentation
31
+ - Ask team members if unsure
32
+
33
+ ### Rule of 500
34
+
35
+ If a function, file, or class exceeds ~500 lines, it likely needs splitting.
36
+
37
+ ---
38
+
39
+ ## Workflow
40
+
41
+ ### Step 1: Identify Target
42
+
43
+ ```bash
44
+ # Recently modified complex code
45
+ git diff --stat HEAD~10
46
+
47
+ # Or specify scope
48
+ # "Simplify the OrderService class"
49
+ ```
50
+
51
+ ### Step 2: Understand Before Changing
52
+
53
+ 1. **Read the code** — What does it do?
54
+ 2. **Check tests** — What behaviors are verified?
55
+ 3. **Trace callers** — Who uses this code?
56
+ 4. **Note edge cases** — Any special handling?
57
+
58
+ ### Step 3: Identify Opportunities
59
+
60
+ | Pattern | Simplification |
61
+ |---------|---------------|
62
+ | Deep nesting (> 3 levels) | Guard clauses, extract helpers |
63
+ | Long functions (> 30 lines) | Split by responsibility |
64
+ | Nested ternaries | `if/else` or `switch` |
65
+ | Unclear names | Rename for clarity |
66
+ | Duplicated code | Extract shared function |
67
+ | Dead code | Remove entirely |
68
+ | Complex conditionals | Extract to named function |
69
+ | Magic numbers | Named constants |
70
+
71
+ ### Step 4: Apply Incrementally
72
+
73
+ **One change at a time:**
74
+
75
+ ```javascript
76
+ // Before: Deep nesting
77
+ function processOrder(order) {
78
+ if (order) {
79
+ if (order.items.length > 0) {
80
+ if (order.status === 'pending') {
81
+ // ... actual logic buried here
82
+ }
83
+ }
84
+ }
85
+ }
86
+
87
+ // After: Guard clauses
88
+ function processOrder(order) {
89
+ if (!order) return;
90
+ if (order.items.length === 0) return;
91
+ if (order.status !== 'pending') return;
92
+
93
+ // ... actual logic at top level
94
+ }
95
+ ```
96
+
97
+ **Run tests after each change.**
98
+
99
+ ### Step 5: Validate
100
+
101
+ ```bash
102
+ # All tests pass
103
+ npm test
104
+
105
+ # Build succeeds
106
+ npm run build
107
+
108
+ # Behavior unchanged (manual check if needed)
109
+ ```
110
+
111
+ ### Step 6: If Tests Fail
112
+
113
+ **Revert immediately.** Don't debug while mid-simplification.
114
+
115
+ ```bash
116
+ git checkout -- .
117
+ ```
118
+
119
+ Then:
120
+ 1. Reassess the change
121
+ 2. Make a smaller change
122
+ 3. Or add missing tests first
123
+
124
+ ---
125
+
126
+ ## Common Simplifications
127
+
128
+ ### Extract Guard Clauses
129
+
130
+ ```javascript
131
+ // Before
132
+ function getDiscount(user) {
133
+ if (user) {
134
+ if (user.membership === 'premium') {
135
+ return 0.2;
136
+ } else {
137
+ return 0.1;
138
+ }
139
+ }
140
+ return 0;
141
+ }
142
+
143
+ // After
144
+ function getDiscount(user) {
145
+ if (!user) return 0;
146
+ if (user.membership === 'premium') return 0.2;
147
+ return 0.1;
148
+ }
149
+ ```
150
+
151
+ ### Extract Named Functions
152
+
153
+ ```javascript
154
+ // Before
155
+ const eligibleUsers = users.filter(u =>
156
+ u.age >= 18 && u.verified && !u.banned && u.subscription !== 'free'
157
+ );
158
+
159
+ // After
160
+ const isEligible = (user) =>
161
+ user.age >= 18 &&
162
+ user.verified &&
163
+ !user.banned &&
164
+ user.subscription !== 'free';
165
+
166
+ const eligibleUsers = users.filter(isEligible);
167
+ ```
168
+
169
+ ### Replace Nested Ternary
170
+
171
+ ```javascript
172
+ // Before
173
+ const status = isPaid ? (isShipped ? 'complete' : 'processing') : 'pending';
174
+
175
+ // After
176
+ function getOrderStatus(isPaid, isShipped) {
177
+ if (!isPaid) return 'pending';
178
+ if (!isShipped) return 'processing';
179
+ return 'complete';
180
+ }
181
+ ```
182
+
183
+ ### Remove Dead Code
184
+
185
+ ```javascript
186
+ // Before
187
+ function calculate(a, b) {
188
+ // const oldResult = legacyCalculate(a, b); // Commented out
189
+ const result = a + b;
190
+ // console.log('Debug:', result); // Debug log
191
+ return result;
192
+ }
193
+
194
+ // After
195
+ function calculate(a, b) {
196
+ return a + b;
197
+ }
198
+ ```
199
+
200
+ ---
201
+
202
+ ## Red Flags
203
+
204
+ Stop if you find yourself:
205
+
206
+ - Changing behavior while "simplifying"
207
+ - Unable to explain why code exists
208
+ - Simplifying without tests
209
+ - Making changes across unrelated files
210
+ - Creating new abstractions
211
+
212
+ ---
213
+
214
+ ## Output
215
+
216
+ - Simpler, clearer code
217
+ - All tests still passing
218
+ - Atomic commits with clear messages
219
+
220
+ ## Next Step
221
+
222
+ Run `/review` to verify improvements.
@@ -0,0 +1,96 @@
1
+ ---
2
+ name: spec
3
+ description: Spec before code — structured PRD creation for new features
4
+ ---
5
+
6
+ # /spec — Specification-Driven Development
7
+
8
+ > "Plan the work, then work the plan."
9
+
10
+ ## Purpose
11
+
12
+ Create a comprehensive specification document **before** writing any code. This ensures alignment on requirements, constraints, and acceptance criteria.
13
+
14
+ ## Workflow
15
+
16
+ ### Phase 1: Discovery (Ask Questions)
17
+
18
+ Before generating a spec, gather requirements by asking:
19
+
20
+ **Scope**
21
+ - What is the objective of this feature?
22
+ - Who are the target users?
23
+ - What problem does this solve?
24
+
25
+ **Features**
26
+ - What are the core features (MVP)?
27
+ - What are the acceptance criteria for each?
28
+ - What is explicitly out of scope?
29
+
30
+ **Technical**
31
+ - Any tech stack preferences or constraints?
32
+ - Integration points with existing systems?
33
+ - Performance requirements?
34
+
35
+ ### Phase 2: Generate Specification
36
+
37
+ After discovery, produce `SPEC.md` with these sections:
38
+
39
+ ```markdown
40
+ # Feature: [Name]
41
+
42
+ ## Objective
43
+ [1-2 sentences describing the goal]
44
+
45
+ ## Target Users
46
+ [Who will use this and why]
47
+
48
+ ## Core Features
49
+ 1. [Feature A] — [Acceptance criteria]
50
+ 2. [Feature B] — [Acceptance criteria]
51
+ 3. [Feature C] — [Acceptance criteria]
52
+
53
+ ## Out of Scope
54
+ - [What we're NOT building in this iteration]
55
+
56
+ ## Technical Approach
57
+ - Tech stack decisions
58
+ - Data models
59
+ - API contracts (if applicable)
60
+ - Integration points
61
+
62
+ ## Code Style
63
+ - Follow rules in `.claude/rules/`
64
+ - [Any feature-specific conventions]
65
+
66
+ ## Testing Strategy
67
+ - Unit tests for: [areas]
68
+ - Integration tests for: [areas]
69
+ - E2E tests for: [critical paths]
70
+
71
+ ## Boundaries
72
+ ### Always Do
73
+ - [Non-negotiables]
74
+
75
+ ### Ask First
76
+ - [Decisions requiring approval]
77
+
78
+ ### Never Do
79
+ - [Hard constraints]
80
+ ```
81
+
82
+ ### Phase 3: Review & Confirm
83
+
84
+ - Present the spec to the user
85
+ - Confirm before proceeding to `/plan`
86
+ - Save as `SPEC.md` in project root or `docs/specs/[feature].md`
87
+
88
+ ## Output
89
+
90
+ - `SPEC.md` — The specification document
91
+ - Clear alignment on what to build
92
+ - **`.agent/SESSION.md`** — Initialize or update: set Meta `phase` to `plan`, **Goal**, **Pointers** → spec path, **Next** → run `/plan`
93
+
94
+ ## Next Step
95
+
96
+ After spec is approved, run `/plan` to decompose into tasks. Run `/handoff` if ending the session before planning.
@@ -0,0 +1,214 @@
1
+ ---
2
+ name: test
3
+ description: Write tests using TDD patterns — tests are proof of correctness
4
+ ---
5
+
6
+ # /test — Test-Driven Development
7
+
8
+ > "Tests are proof, not afterthought."
9
+
10
+ ## Purpose
11
+
12
+ Write tests that prove code works correctly. Use the RED-GREEN-REFACTOR cycle for new features and the Prove-It pattern for bug fixes.
13
+
14
+ ## For New Features: RED-GREEN-REFACTOR
15
+
16
+ ### RED: Write Failing Test First
17
+
18
+ ```javascript
19
+ describe('UserService.findById', () => {
20
+ it('should return user when found', async () => {
21
+ // Arrange
22
+ const userId = 'user-123';
23
+
24
+ // Act
25
+ const user = await userService.findById(userId);
26
+
27
+ // Assert
28
+ expect(user).toBeDefined();
29
+ expect(user.id).toBe(userId);
30
+ });
31
+
32
+ it('should throw NotFoundError when user does not exist', async () => {
33
+ await expect(userService.findById('non-existent'))
34
+ .rejects.toThrow(NotFoundError);
35
+ });
36
+ });
37
+ ```
38
+
39
+ Run tests — **they should fail** (proves nothing is implemented yet).
40
+
41
+ ### GREEN: Write Minimal Code to Pass
42
+
43
+ ```javascript
44
+ async findById(id: string): Promise<User> {
45
+ const user = await this.db.user.findUnique({ where: { id } });
46
+ if (!user) throw new NotFoundError('User not found');
47
+ return user;
48
+ }
49
+ ```
50
+
51
+ Run tests — **they should pass**.
52
+
53
+ ### REFACTOR: Improve While Green
54
+
55
+ Clean up code while keeping tests passing:
56
+ - Better naming
57
+ - Extract helpers
58
+ - Remove duplication
59
+
60
+ ---
61
+
62
+ ## For Bug Fixes: Prove-It Pattern
63
+
64
+ ### Step 1: Write Test That Reproduces the Bug
65
+
66
+ ```javascript
67
+ it('should not duplicate items when adding to cart twice', async () => {
68
+ // This test should FAIL with the current buggy code
69
+ await cart.addItem('product-1', 1);
70
+ await cart.addItem('product-1', 1);
71
+
72
+ expect(cart.items).toHaveLength(1);
73
+ expect(cart.items[0].quantity).toBe(2);
74
+ });
75
+ ```
76
+
77
+ ### Step 2: Verify Test Fails
78
+
79
+ Run the test — confirm it **fails** (proving the bug exists).
80
+
81
+ ### Step 3: Fix the Bug
82
+
83
+ ```javascript
84
+ async addItem(productId: string, quantity: number) {
85
+ const existing = this.items.find(i => i.productId === productId);
86
+ if (existing) {
87
+ existing.quantity += quantity; // Fix: increment instead of duplicate
88
+ } else {
89
+ this.items.push({ productId, quantity });
90
+ }
91
+ }
92
+ ```
93
+
94
+ ### Step 4: Verify Test Passes
95
+
96
+ Run the test — confirm it **passes** (proving the fix works).
97
+
98
+ ### Step 5: Run Full Suite
99
+
100
+ ```bash
101
+ npm test # Ensure no regressions
102
+ ```
103
+
104
+ ---
105
+
106
+ ## Test Pyramid
107
+
108
+ | Level | Percentage | Speed | Scope |
109
+ |-------|------------|-------|-------|
110
+ | **Unit** | 80% | ms | Single function, no I/O |
111
+ | **Integration** | 15% | seconds | API + DB, component interactions |
112
+ | **E2E** | 5% | minutes | Full user flows |
113
+
114
+ ---
115
+
116
+ ## Writing Good Tests
117
+
118
+ ### Arrange-Act-Assert Pattern
119
+
120
+ ```javascript
121
+ it('should calculate total with discount', () => {
122
+ // Arrange — setup
123
+ const cart = new Cart();
124
+ cart.addItem({ price: 100 });
125
+ cart.applyDiscount(10);
126
+
127
+ // Act — execute
128
+ const total = cart.calculateTotal();
129
+
130
+ // Assert — verify
131
+ expect(total).toBe(90);
132
+ });
133
+ ```
134
+
135
+ ### DAMP Over DRY
136
+
137
+ Tests should be **Descriptive And Meaningful Phrases**. Each test reads independently:
138
+
139
+ ```javascript
140
+ // ✅ DAMP — clear and independent
141
+ it('should reject password shorter than 8 characters', () => {
142
+ const result = validatePassword('short');
143
+ expect(result.valid).toBe(false);
144
+ expect(result.error).toBe('Password must be at least 8 characters');
145
+ });
146
+
147
+ // ❌ Too DRY — requires reading shared setup
148
+ it('should reject short password', () => {
149
+ expect(validate(shortPassword)).toBe(false);
150
+ });
151
+ ```
152
+
153
+ ### Descriptive Test Names
154
+
155
+ ```javascript
156
+ // ✅ Good — describes behavior
157
+ 'should return 404 when user is not found'
158
+ 'should increment quantity when adding existing product'
159
+ 'should send welcome email after registration'
160
+
161
+ // ❌ Bad — vague
162
+ 'works correctly'
163
+ 'handles error'
164
+ 'test user'
165
+ ```
166
+
167
+ ### One Behavior Per Test
168
+
169
+ ```javascript
170
+ // ✅ Good — focused
171
+ it('should validate email format', () => { ... });
172
+ it('should require email field', () => { ... });
173
+
174
+ // ❌ Bad — testing multiple things
175
+ it('should validate email', () => {
176
+ expect(validate('')).toBe(false); // required
177
+ expect(validate('bad')).toBe(false); // format
178
+ expect(validate('a@b.c')).toBe(true); // valid
179
+ });
180
+ ```
181
+
182
+ ---
183
+
184
+ ## Test Doubles Preference
185
+
186
+ ```
187
+ 1. Real implementations (best)
188
+ 2. In-memory fakes (test DB, fake filesystem)
189
+ 3. Stubs (canned responses)
190
+ 4. Mocks (verify interactions — use sparingly)
191
+ ```
192
+
193
+ ---
194
+
195
+ ## Anti-Patterns to Avoid
196
+
197
+ | Anti-Pattern | Problem | Fix |
198
+ |--------------|---------|-----|
199
+ | Testing internals | Breaks on refactor | Test inputs/outputs only |
200
+ | Flaky tests | Erodes trust | Use deterministic data, isolate state |
201
+ | Over-mocking | False confidence | Prefer real implementations |
202
+ | Snapshot abuse | Large diffs ignored | Use for rare cases only |
203
+ | Shared state | Tests affect each other | Reset state in beforeEach |
204
+
205
+ ---
206
+
207
+ ## Verification Checklist
208
+
209
+ - [ ] All new behaviors have tests
210
+ - [ ] Bug fixes have reproduction tests
211
+ - [ ] Test names describe behavior
212
+ - [ ] No skipped or disabled tests
213
+ - [ ] Coverage maintained or improved
214
+ - [ ] Full test suite passes