@chankov/agent-skills 0.2.0 → 0.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 (178) hide show
  1. package/.claude/commands/{doctor.md → doctor-agent-skills.md} +1 -1
  2. package/.pi/extensions/agent-skills-update-check/README.md +4 -4
  3. package/.pi/prompts/{doctor.md → doctor-agent-skills.md} +1 -1
  4. package/.versions/0.2.0/.claude/commands/{doctor.md → doctor-agent-skills.md} +1 -1
  5. package/.versions/0.2.0/.pi/extensions/agent-skills-update-check/README.md +4 -4
  6. package/.versions/0.2.0/.pi/prompts/{doctor.md → doctor-agent-skills.md} +1 -1
  7. package/.versions/0.2.0/skills/guided-workspace-setup/SKILL.md +40 -2
  8. package/.versions/0.3.0/.claude/commands/build.md +18 -0
  9. package/.versions/0.3.0/.claude/commands/code-simplify.md +22 -0
  10. package/.versions/0.3.0/.claude/commands/design-agent.md +14 -0
  11. package/.versions/0.3.0/.claude/commands/doctor-agent-skills.md +13 -0
  12. package/.versions/0.3.0/.claude/commands/plan.md +16 -0
  13. package/.versions/0.3.0/.claude/commands/prime.md +22 -0
  14. package/.versions/0.3.0/.claude/commands/review.md +16 -0
  15. package/.versions/0.3.0/.claude/commands/setup-agent-skills.md +19 -0
  16. package/.versions/0.3.0/.claude/commands/ship.md +17 -0
  17. package/.versions/0.3.0/.claude/commands/spec.md +15 -0
  18. package/.versions/0.3.0/.claude/commands/test.md +19 -0
  19. package/.versions/0.3.0/.opencode/commands/as-build.md +17 -0
  20. package/.versions/0.3.0/.opencode/commands/as-code-simplify.md +16 -0
  21. package/.versions/0.3.0/.opencode/commands/as-design-agent.md +15 -0
  22. package/.versions/0.3.0/.opencode/commands/as-doctor-agent-skills.md +11 -0
  23. package/.versions/0.3.0/.opencode/commands/as-plan.md +16 -0
  24. package/.versions/0.3.0/.opencode/commands/as-prime.md +22 -0
  25. package/.versions/0.3.0/.opencode/commands/as-review.md +15 -0
  26. package/.versions/0.3.0/.opencode/commands/as-setup-agent-skills.md +11 -0
  27. package/.versions/0.3.0/.opencode/commands/as-ship.md +16 -0
  28. package/.versions/0.3.0/.opencode/commands/as-spec.md +16 -0
  29. package/.versions/0.3.0/.opencode/commands/as-test.md +21 -0
  30. package/.versions/0.3.0/.pi/agents/agent-chain.yaml +49 -0
  31. package/.versions/0.3.0/.pi/agents/bowser.md +19 -0
  32. package/.versions/0.3.0/.pi/agents/pi-pi/agent-expert.md +98 -0
  33. package/.versions/0.3.0/.pi/agents/pi-pi/cli-expert.md +41 -0
  34. package/.versions/0.3.0/.pi/agents/pi-pi/config-expert.md +63 -0
  35. package/.versions/0.3.0/.pi/agents/pi-pi/ext-expert.md +43 -0
  36. package/.versions/0.3.0/.pi/agents/pi-pi/keybinding-expert.md +134 -0
  37. package/.versions/0.3.0/.pi/agents/pi-pi/pi-orchestrator.md +57 -0
  38. package/.versions/0.3.0/.pi/agents/pi-pi/prompt-expert.md +70 -0
  39. package/.versions/0.3.0/.pi/agents/pi-pi/skill-expert.md +42 -0
  40. package/.versions/0.3.0/.pi/agents/pi-pi/theme-expert.md +40 -0
  41. package/.versions/0.3.0/.pi/agents/pi-pi/tui-expert.md +85 -0
  42. package/.versions/0.3.0/.pi/agents/teams.yaml +31 -0
  43. package/.versions/0.3.0/.pi/damage-control-rules.yaml +278 -0
  44. package/.versions/0.3.0/.pi/extensions/agent-skills-update-check/README.md +58 -0
  45. package/.versions/0.3.0/.pi/extensions/agent-skills-update-check/index.ts +161 -0
  46. package/.versions/0.3.0/.pi/extensions/agent-skills-update-check/package.json +6 -0
  47. package/.versions/0.3.0/.pi/extensions/chrome-devtools-mcp/README.md +39 -0
  48. package/.versions/0.3.0/.pi/extensions/chrome-devtools-mcp/index.ts +61 -0
  49. package/.versions/0.3.0/.pi/extensions/chrome-devtools-mcp/package.json +6 -0
  50. package/.versions/0.3.0/.pi/extensions/compact-and-continue/README.md +42 -0
  51. package/.versions/0.3.0/.pi/extensions/compact-and-continue/index.ts +120 -0
  52. package/.versions/0.3.0/.pi/extensions/compact-and-continue/package.json +6 -0
  53. package/.versions/0.3.0/.pi/extensions/mcp-bridge/README.md +46 -0
  54. package/.versions/0.3.0/.pi/extensions/mcp-bridge/index.ts +206 -0
  55. package/.versions/0.3.0/.pi/extensions/mcp-bridge/package.json +6 -0
  56. package/.versions/0.3.0/.pi/extensions/package-lock.json +1143 -0
  57. package/.versions/0.3.0/.pi/extensions/package.json +9 -0
  58. package/.versions/0.3.0/.pi/harnesses/agent-chain/README.md +37 -0
  59. package/.versions/0.3.0/.pi/harnesses/agent-chain/index.ts +795 -0
  60. package/.versions/0.3.0/.pi/harnesses/agent-chain/package.json +6 -0
  61. package/.versions/0.3.0/.pi/harnesses/agent-team/README.md +38 -0
  62. package/.versions/0.3.0/.pi/harnesses/agent-team/index.ts +732 -0
  63. package/.versions/0.3.0/.pi/harnesses/agent-team/package.json +6 -0
  64. package/.versions/0.3.0/.pi/harnesses/coms/README.md +36 -0
  65. package/.versions/0.3.0/.pi/harnesses/coms/index.ts +1595 -0
  66. package/.versions/0.3.0/.pi/harnesses/coms/package.json +6 -0
  67. package/.versions/0.3.0/.pi/harnesses/coms-net/README.md +46 -0
  68. package/.versions/0.3.0/.pi/harnesses/coms-net/index.ts +1637 -0
  69. package/.versions/0.3.0/.pi/harnesses/coms-net/package.json +6 -0
  70. package/.versions/0.3.0/.pi/harnesses/damage-control/README.md +38 -0
  71. package/.versions/0.3.0/.pi/harnesses/damage-control/index.ts +207 -0
  72. package/.versions/0.3.0/.pi/harnesses/damage-control/package.json +6 -0
  73. package/.versions/0.3.0/.pi/harnesses/damage-control-continue/README.md +37 -0
  74. package/.versions/0.3.0/.pi/harnesses/damage-control-continue/index.ts +234 -0
  75. package/.versions/0.3.0/.pi/harnesses/damage-control-continue/package.json +6 -0
  76. package/.versions/0.3.0/.pi/harnesses/minimal/README.md +27 -0
  77. package/.versions/0.3.0/.pi/harnesses/minimal/index.ts +32 -0
  78. package/.versions/0.3.0/.pi/harnesses/minimal/package.json +6 -0
  79. package/.versions/0.3.0/.pi/harnesses/package-lock.json +35 -0
  80. package/.versions/0.3.0/.pi/harnesses/package.json +9 -0
  81. package/.versions/0.3.0/.pi/harnesses/pi-pi/README.md +39 -0
  82. package/.versions/0.3.0/.pi/harnesses/pi-pi/index.ts +631 -0
  83. package/.versions/0.3.0/.pi/harnesses/pi-pi/package.json +6 -0
  84. package/.versions/0.3.0/.pi/harnesses/purpose-gate/README.md +27 -0
  85. package/.versions/0.3.0/.pi/harnesses/purpose-gate/index.ts +82 -0
  86. package/.versions/0.3.0/.pi/harnesses/purpose-gate/package.json +6 -0
  87. package/.versions/0.3.0/.pi/harnesses/session-replay/README.md +28 -0
  88. package/.versions/0.3.0/.pi/harnesses/session-replay/index.ts +214 -0
  89. package/.versions/0.3.0/.pi/harnesses/session-replay/package.json +6 -0
  90. package/.versions/0.3.0/.pi/harnesses/subagent-widget/README.md +36 -0
  91. package/.versions/0.3.0/.pi/harnesses/subagent-widget/index.ts +479 -0
  92. package/.versions/0.3.0/.pi/harnesses/subagent-widget/package.json +6 -0
  93. package/.versions/0.3.0/.pi/harnesses/system-select/README.md +39 -0
  94. package/.versions/0.3.0/.pi/harnesses/system-select/index.ts +165 -0
  95. package/.versions/0.3.0/.pi/harnesses/system-select/package.json +6 -0
  96. package/.versions/0.3.0/.pi/harnesses/tilldone/README.md +35 -0
  97. package/.versions/0.3.0/.pi/harnesses/tilldone/index.ts +724 -0
  98. package/.versions/0.3.0/.pi/harnesses/tilldone/package.json +6 -0
  99. package/.versions/0.3.0/.pi/harnesses/tool-counter/README.md +31 -0
  100. package/.versions/0.3.0/.pi/harnesses/tool-counter/index.ts +100 -0
  101. package/.versions/0.3.0/.pi/harnesses/tool-counter/package.json +6 -0
  102. package/.versions/0.3.0/.pi/harnesses/tool-counter-widget/README.md +27 -0
  103. package/.versions/0.3.0/.pi/harnesses/tool-counter-widget/index.ts +66 -0
  104. package/.versions/0.3.0/.pi/harnesses/tool-counter-widget/package.json +6 -0
  105. package/.versions/0.3.0/.pi/prompts/build.md +24 -0
  106. package/.versions/0.3.0/.pi/prompts/code-simplify.md +22 -0
  107. package/.versions/0.3.0/.pi/prompts/doctor-agent-skills.md +13 -0
  108. package/.versions/0.3.0/.pi/prompts/plan.md +16 -0
  109. package/.versions/0.3.0/.pi/prompts/review.md +16 -0
  110. package/.versions/0.3.0/.pi/prompts/setup-agent-skills.md +19 -0
  111. package/.versions/0.3.0/.pi/prompts/ship.md +17 -0
  112. package/.versions/0.3.0/.pi/prompts/spec.md +15 -0
  113. package/.versions/0.3.0/.pi/prompts/test.md +19 -0
  114. package/.versions/0.3.0/.pi/skills/bowser/SKILL.md +114 -0
  115. package/.versions/0.3.0/.version +1 -0
  116. package/.versions/0.3.0/agents/builder.md +6 -0
  117. package/.versions/0.3.0/agents/code-reviewer.md +93 -0
  118. package/.versions/0.3.0/agents/documenter.md +6 -0
  119. package/.versions/0.3.0/agents/plan-reviewer.md +22 -0
  120. package/.versions/0.3.0/agents/planner.md +6 -0
  121. package/.versions/0.3.0/agents/scout.md +6 -0
  122. package/.versions/0.3.0/agents/security-auditor.md +97 -0
  123. package/.versions/0.3.0/agents/test-engineer.md +89 -0
  124. package/.versions/0.3.0/hooks/SIMPLIFY-IGNORE.md +90 -0
  125. package/.versions/0.3.0/hooks/hooks.json +14 -0
  126. package/.versions/0.3.0/hooks/session-start.sh +74 -0
  127. package/.versions/0.3.0/hooks/simplify-ignore-test.sh +247 -0
  128. package/.versions/0.3.0/hooks/simplify-ignore.sh +302 -0
  129. package/.versions/0.3.0/references/accessibility-checklist.md +159 -0
  130. package/.versions/0.3.0/references/performance-checklist.md +121 -0
  131. package/.versions/0.3.0/references/prompting-patterns.md +380 -0
  132. package/.versions/0.3.0/references/security-checklist.md +134 -0
  133. package/.versions/0.3.0/references/testing-patterns.md +236 -0
  134. package/.versions/0.3.0/skills/api-and-interface-design/SKILL.md +294 -0
  135. package/.versions/0.3.0/skills/browser-testing-with-devtools/SKILL.md +335 -0
  136. package/.versions/0.3.0/skills/ci-cd-and-automation/SKILL.md +390 -0
  137. package/.versions/0.3.0/skills/code-review-and-quality/SKILL.md +347 -0
  138. package/.versions/0.3.0/skills/code-simplification/SKILL.md +331 -0
  139. package/.versions/0.3.0/skills/context-engineering/SKILL.md +291 -0
  140. package/.versions/0.3.0/skills/debugging-and-error-recovery/SKILL.md +300 -0
  141. package/.versions/0.3.0/skills/deprecation-and-migration/SKILL.md +206 -0
  142. package/.versions/0.3.0/skills/designing-agents/SKILL.md +394 -0
  143. package/.versions/0.3.0/skills/designing-agents/pi-harness-authoring.md +213 -0
  144. package/.versions/0.3.0/skills/documentation-and-adrs/SKILL.md +278 -0
  145. package/.versions/0.3.0/skills/frontend-ui-engineering/SKILL.md +322 -0
  146. package/.versions/0.3.0/skills/git-workflow-and-versioning/SKILL.md +316 -0
  147. package/.versions/0.3.0/skills/guided-workspace-setup/SKILL.md +331 -0
  148. package/.versions/0.3.0/skills/idea-refine/SKILL.md +178 -0
  149. package/.versions/0.3.0/skills/idea-refine/examples.md +238 -0
  150. package/.versions/0.3.0/skills/idea-refine/frameworks.md +99 -0
  151. package/.versions/0.3.0/skills/idea-refine/refinement-criteria.md +113 -0
  152. package/.versions/0.3.0/skills/idea-refine/scripts/idea-refine.sh +15 -0
  153. package/.versions/0.3.0/skills/incremental-implementation/SKILL.md +279 -0
  154. package/.versions/0.3.0/skills/performance-optimization/SKILL.md +350 -0
  155. package/.versions/0.3.0/skills/planning-and-task-breakdown/SKILL.md +237 -0
  156. package/.versions/0.3.0/skills/security-and-hardening/SKILL.md +349 -0
  157. package/.versions/0.3.0/skills/shipping-and-launch/SKILL.md +309 -0
  158. package/.versions/0.3.0/skills/source-driven-development/SKILL.md +194 -0
  159. package/.versions/0.3.0/skills/spec-driven-development/SKILL.md +237 -0
  160. package/.versions/0.3.0/skills/test-driven-development/SKILL.md +379 -0
  161. package/.versions/0.3.0/skills/using-agent-skills/SKILL.md +176 -0
  162. package/CHANGELOG.md +72 -0
  163. package/README.md +5 -5
  164. package/bin/cli.js +100 -24
  165. package/bin/lib/bootstrap.js +254 -0
  166. package/bin/lib/doctor.js +1 -1
  167. package/docs/getting-started.md +2 -2
  168. package/docs/npm-install.md +34 -11
  169. package/package.json +1 -1
  170. package/skills/guided-workspace-setup/SKILL.md +40 -2
  171. /package/.claude/commands/{setup.md → setup-agent-skills.md} +0 -0
  172. /package/.opencode/commands/{as-doctor.md → as-doctor-agent-skills.md} +0 -0
  173. /package/.opencode/commands/{as-setup.md → as-setup-agent-skills.md} +0 -0
  174. /package/.pi/prompts/{setup.md → setup-agent-skills.md} +0 -0
  175. /package/.versions/0.2.0/.claude/commands/{setup.md → setup-agent-skills.md} +0 -0
  176. /package/.versions/0.2.0/.opencode/commands/{as-doctor.md → as-doctor-agent-skills.md} +0 -0
  177. /package/.versions/0.2.0/.opencode/commands/{as-setup.md → as-setup-agent-skills.md} +0 -0
  178. /package/.versions/0.2.0/.pi/prompts/{setup.md → setup-agent-skills.md} +0 -0
@@ -0,0 +1,236 @@
1
+ # Testing Patterns Reference
2
+
3
+ Quick reference for common testing patterns across the stack. Use alongside the `test-driven-development` skill.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Test Structure (Arrange-Act-Assert)](#test-structure-arrange-act-assert)
8
+ - [Test Naming Conventions](#test-naming-conventions)
9
+ - [Common Assertions](#common-assertions)
10
+ - [Mocking Patterns](#mocking-patterns)
11
+ - [React/Component Testing](#reactcomponent-testing)
12
+ - [API / Integration Testing](#api--integration-testing)
13
+ - [E2E Testing (Playwright)](#e2e-testing-playwright)
14
+ - [Test Anti-Patterns](#test-anti-patterns)
15
+
16
+ ## Test Structure (Arrange-Act-Assert)
17
+
18
+ ```typescript
19
+ it('describes expected behavior', () => {
20
+ // Arrange: Set up test data and preconditions
21
+ const input = { title: 'Test Task', priority: 'high' };
22
+
23
+ // Act: Perform the action being tested
24
+ const result = createTask(input);
25
+
26
+ // Assert: Verify the outcome
27
+ expect(result.title).toBe('Test Task');
28
+ expect(result.priority).toBe('high');
29
+ expect(result.status).toBe('pending');
30
+ });
31
+ ```
32
+
33
+ ## Test Naming Conventions
34
+
35
+ ```typescript
36
+ // Pattern: [unit] [expected behavior] [condition]
37
+ describe('TaskService.createTask', () => {
38
+ it('creates a task with default pending status', () => {});
39
+ it('throws ValidationError when title is empty', () => {});
40
+ it('trims whitespace from title', () => {});
41
+ it('generates a unique ID for each task', () => {});
42
+ });
43
+ ```
44
+
45
+ ## Common Assertions
46
+
47
+ ```typescript
48
+ // Equality
49
+ expect(result).toBe(expected); // Strict equality (===)
50
+ expect(result).toEqual(expected); // Deep equality (objects/arrays)
51
+ expect(result).toStrictEqual(expected); // Deep equality + type matching
52
+
53
+ // Truthiness
54
+ expect(result).toBeTruthy();
55
+ expect(result).toBeFalsy();
56
+ expect(result).toBeNull();
57
+ expect(result).toBeDefined();
58
+ expect(result).toBeUndefined();
59
+
60
+ // Numbers
61
+ expect(result).toBeGreaterThan(5);
62
+ expect(result).toBeLessThanOrEqual(10);
63
+ expect(result).toBeCloseTo(0.3, 5); // Floating point
64
+
65
+ // Strings
66
+ expect(result).toMatch(/pattern/);
67
+ expect(result).toContain('substring');
68
+
69
+ // Arrays / Objects
70
+ expect(array).toContain(item);
71
+ expect(array).toHaveLength(3);
72
+ expect(object).toHaveProperty('key', 'value');
73
+
74
+ // Errors
75
+ expect(() => fn()).toThrow();
76
+ expect(() => fn()).toThrow(ValidationError);
77
+ expect(() => fn()).toThrow('specific message');
78
+
79
+ // Async
80
+ await expect(asyncFn()).resolves.toBe(value);
81
+ await expect(asyncFn()).rejects.toThrow(Error);
82
+ ```
83
+
84
+ ## Mocking Patterns
85
+
86
+ ### Mock Functions
87
+
88
+ ```typescript
89
+ const mockFn = jest.fn();
90
+ mockFn.mockReturnValue(42);
91
+ mockFn.mockResolvedValue({ data: 'test' });
92
+ mockFn.mockImplementation((x) => x * 2);
93
+
94
+ expect(mockFn).toHaveBeenCalled();
95
+ expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2');
96
+ expect(mockFn).toHaveBeenCalledTimes(3);
97
+ ```
98
+
99
+ ### Mock Modules
100
+
101
+ ```typescript
102
+ // Mock an entire module
103
+ jest.mock('./database', () => ({
104
+ query: jest.fn().mockResolvedValue([{ id: 1, title: 'Test' }]),
105
+ }));
106
+
107
+ // Mock specific exports
108
+ jest.mock('./utils', () => ({
109
+ ...jest.requireActual('./utils'),
110
+ generateId: jest.fn().mockReturnValue('test-id'),
111
+ }));
112
+ ```
113
+
114
+ ### Mock at Boundaries Only
115
+
116
+ ```
117
+ Mock these: Don't mock these:
118
+ ├── Database calls ├── Internal utility functions
119
+ ├── HTTP requests ├── Business logic
120
+ ├── File system operations ├── Data transformations
121
+ ├── External API calls ├── Validation functions
122
+ └── Time/Date (when needed) └── Pure functions
123
+ ```
124
+
125
+ ## React/Component Testing
126
+
127
+ ```tsx
128
+ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
129
+
130
+ describe('TaskForm', () => {
131
+ it('submits the form with entered data', async () => {
132
+ const onSubmit = jest.fn();
133
+ render(<TaskForm onSubmit={onSubmit} />);
134
+
135
+ // Find elements by accessible role/label (not test IDs)
136
+ await screen.findByRole('textbox', { name: /title/i });
137
+ fireEvent.change(screen.getByRole('textbox', { name: /title/i }), {
138
+ target: { value: 'New Task' },
139
+ });
140
+ fireEvent.click(screen.getByRole('button', { name: /create/i }));
141
+
142
+ await waitFor(() => {
143
+ expect(onSubmit).toHaveBeenCalledWith({ title: 'New Task' });
144
+ });
145
+ });
146
+
147
+ it('shows validation error for empty title', async () => {
148
+ render(<TaskForm onSubmit={jest.fn()} />);
149
+
150
+ fireEvent.click(screen.getByRole('button', { name: /create/i }));
151
+
152
+ expect(await screen.findByText(/title is required/i)).toBeInTheDocument();
153
+ });
154
+ });
155
+ ```
156
+
157
+ ## API / Integration Testing
158
+
159
+ ```typescript
160
+ import request from 'supertest';
161
+ import { app } from '../src/app';
162
+
163
+ describe('POST /api/tasks', () => {
164
+ it('creates a task and returns 201', async () => {
165
+ const response = await request(app)
166
+ .post('/api/tasks')
167
+ .send({ title: 'Test Task' })
168
+ .set('Authorization', `Bearer ${testToken}`)
169
+ .expect(201);
170
+
171
+ expect(response.body).toMatchObject({
172
+ id: expect.any(String),
173
+ title: 'Test Task',
174
+ status: 'pending',
175
+ });
176
+ });
177
+
178
+ it('returns 422 for invalid input', async () => {
179
+ const response = await request(app)
180
+ .post('/api/tasks')
181
+ .send({ title: '' })
182
+ .set('Authorization', `Bearer ${testToken}`)
183
+ .expect(422);
184
+
185
+ expect(response.body.error.code).toBe('VALIDATION_ERROR');
186
+ });
187
+
188
+ it('returns 401 without authentication', async () => {
189
+ await request(app)
190
+ .post('/api/tasks')
191
+ .send({ title: 'Test' })
192
+ .expect(401);
193
+ });
194
+ });
195
+ ```
196
+
197
+ ## E2E Testing (Playwright)
198
+
199
+ ```typescript
200
+ import { test, expect } from '@playwright/test';
201
+
202
+ test('user can create and complete a task', async ({ page }) => {
203
+ // Navigate and authenticate
204
+ await page.goto('/');
205
+ await page.fill('[name="email"]', 'test@example.com');
206
+ await page.fill('[name="password"]', 'testpass123');
207
+ await page.click('button:has-text("Log in")');
208
+
209
+ // Create a task
210
+ await page.click('button:has-text("New Task")');
211
+ await page.fill('[name="title"]', 'Buy groceries');
212
+ await page.click('button:has-text("Create")');
213
+
214
+ // Verify task appears
215
+ await expect(page.locator('text=Buy groceries')).toBeVisible();
216
+
217
+ // Complete the task
218
+ await page.click('[aria-label="Complete Buy groceries"]');
219
+ await expect(page.locator('text=Buy groceries')).toHaveCSS(
220
+ 'text-decoration-line', 'line-through'
221
+ );
222
+ });
223
+ ```
224
+
225
+ ## Test Anti-Patterns
226
+
227
+ | Anti-Pattern | Problem | Better Approach |
228
+ |---|---|---|
229
+ | Testing implementation details | Breaks on refactor | Test inputs/outputs |
230
+ | Snapshot everything | No one reviews snapshot diffs | Assert specific values |
231
+ | Shared mutable state | Tests pollute each other | Setup/teardown per test |
232
+ | Testing third-party code | Wastes time, not your bug | Mock the boundary |
233
+ | Skipping tests to pass CI | Hides real bugs | Fix or delete the test |
234
+ | Using `test.skip` permanently | Dead code | Remove or fix it |
235
+ | Overly broad assertions | Doesn't catch regressions | Be specific |
236
+ | No async error handling | Swallowed errors, false passes | Always `await` async tests |
@@ -0,0 +1,294 @@
1
+ ---
2
+ name: api-and-interface-design
3
+ description: Guides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.
4
+ ---
5
+
6
+ # API and Interface Design
7
+
8
+ ## Overview
9
+
10
+ Design stable, well-documented interfaces that are hard to misuse. Good interfaces make the right thing easy and the wrong thing hard. This applies to REST APIs, GraphQL schemas, module boundaries, component props, and any surface where one piece of code talks to another.
11
+
12
+ ## When to Use
13
+
14
+ - Designing new API endpoints
15
+ - Defining module boundaries or contracts between teams
16
+ - Creating component prop interfaces
17
+ - Establishing database schema that informs API shape
18
+ - Changing existing public interfaces
19
+
20
+ ## Core Principles
21
+
22
+ ### Hyrum's Law
23
+
24
+ > With a sufficient number of users of an API, all observable behaviors of your system will be depended on by somebody, regardless of what you promise in the contract.
25
+
26
+ This means: every public behavior — including undocumented quirks, error message text, timing, and ordering — becomes a de facto contract once users depend on it. Design implications:
27
+
28
+ - **Be intentional about what you expose.** Every observable behavior is a potential commitment.
29
+ - **Don't leak implementation details.** If users can observe it, they will depend on it.
30
+ - **Plan for deprecation at design time.** See `deprecation-and-migration` for how to safely remove things users depend on.
31
+ - **Tests are not enough.** Even with perfect contract tests, Hyrum's Law means "safe" changes can break real users who depend on undocumented behavior.
32
+
33
+ ### The One-Version Rule
34
+
35
+ Avoid forcing consumers to choose between multiple versions of the same dependency or API. Diamond dependency problems arise when different consumers need different versions of the same thing. Design for a world where only one version exists at a time — extend rather than fork.
36
+
37
+ ### 1. Contract First
38
+
39
+ Define the interface before implementing it. The contract is the spec — implementation follows.
40
+
41
+ ```typescript
42
+ // Define the contract first
43
+ interface TaskAPI {
44
+ // Creates a task and returns the created task with server-generated fields
45
+ createTask(input: CreateTaskInput): Promise<Task>;
46
+
47
+ // Returns paginated tasks matching filters
48
+ listTasks(params: ListTasksParams): Promise<PaginatedResult<Task>>;
49
+
50
+ // Returns a single task or throws NotFoundError
51
+ getTask(id: string): Promise<Task>;
52
+
53
+ // Partial update — only provided fields change
54
+ updateTask(id: string, input: UpdateTaskInput): Promise<Task>;
55
+
56
+ // Idempotent delete — succeeds even if already deleted
57
+ deleteTask(id: string): Promise<void>;
58
+ }
59
+ ```
60
+
61
+ ### 2. Consistent Error Semantics
62
+
63
+ Pick one error strategy and use it everywhere:
64
+
65
+ ```typescript
66
+ // REST: HTTP status codes + structured error body
67
+ // Every error response follows the same shape
68
+ interface APIError {
69
+ error: {
70
+ code: string; // Machine-readable: "VALIDATION_ERROR"
71
+ message: string; // Human-readable: "Email is required"
72
+ details?: unknown; // Additional context when helpful
73
+ };
74
+ }
75
+
76
+ // Status code mapping
77
+ // 400 → Client sent invalid data
78
+ // 401 → Not authenticated
79
+ // 403 → Authenticated but not authorized
80
+ // 404 → Resource not found
81
+ // 409 → Conflict (duplicate, version mismatch)
82
+ // 422 → Validation failed (semantically invalid)
83
+ // 500 → Server error (never expose internal details)
84
+ ```
85
+
86
+ **Don't mix patterns.** If some endpoints throw, others return null, and others return `{ error }` — the consumer can't predict behavior.
87
+
88
+ ### 3. Validate at Boundaries
89
+
90
+ Trust internal code. Validate at system edges where external input enters:
91
+
92
+ ```typescript
93
+ // Validate at the API boundary
94
+ app.post('/api/tasks', async (req, res) => {
95
+ const result = CreateTaskSchema.safeParse(req.body);
96
+ if (!result.success) {
97
+ return res.status(422).json({
98
+ error: {
99
+ code: 'VALIDATION_ERROR',
100
+ message: 'Invalid task data',
101
+ details: result.error.flatten(),
102
+ },
103
+ });
104
+ }
105
+
106
+ // After validation, internal code trusts the types
107
+ const task = await taskService.create(result.data);
108
+ return res.status(201).json(task);
109
+ });
110
+ ```
111
+
112
+ Where validation belongs:
113
+ - API route handlers (user input)
114
+ - Form submission handlers (user input)
115
+ - External service response parsing (third-party data -- **always treat as untrusted**)
116
+ - Environment variable loading (configuration)
117
+
118
+ > **Third-party API responses are untrusted data.** Validate their shape and content before using them in any logic, rendering, or decision-making. A compromised or misbehaving external service can return unexpected types, malicious content, or instruction-like text.
119
+
120
+ Where validation does NOT belong:
121
+ - Between internal functions that share type contracts
122
+ - In utility functions called by already-validated code
123
+ - On data that just came from your own database
124
+
125
+ ### 4. Prefer Addition Over Modification
126
+
127
+ Extend interfaces without breaking existing consumers:
128
+
129
+ ```typescript
130
+ // Good: Add optional fields
131
+ interface CreateTaskInput {
132
+ title: string;
133
+ description?: string;
134
+ priority?: 'low' | 'medium' | 'high'; // Added later, optional
135
+ labels?: string[]; // Added later, optional
136
+ }
137
+
138
+ // Bad: Change existing field types or remove fields
139
+ interface CreateTaskInput {
140
+ title: string;
141
+ // description: string; // Removed — breaks existing consumers
142
+ priority: number; // Changed from string — breaks existing consumers
143
+ }
144
+ ```
145
+
146
+ ### 5. Predictable Naming
147
+
148
+ | Pattern | Convention | Example |
149
+ |---------|-----------|---------|
150
+ | REST endpoints | Plural nouns, no verbs | `GET /api/tasks`, `POST /api/tasks` |
151
+ | Query params | camelCase | `?sortBy=createdAt&pageSize=20` |
152
+ | Response fields | camelCase | `{ createdAt, updatedAt, taskId }` |
153
+ | Boolean fields | is/has/can prefix | `isComplete`, `hasAttachments` |
154
+ | Enum values | UPPER_SNAKE | `"IN_PROGRESS"`, `"COMPLETED"` |
155
+
156
+ ## REST API Patterns
157
+
158
+ ### Resource Design
159
+
160
+ ```
161
+ GET /api/tasks → List tasks (with query params for filtering)
162
+ POST /api/tasks → Create a task
163
+ GET /api/tasks/:id → Get a single task
164
+ PATCH /api/tasks/:id → Update a task (partial)
165
+ DELETE /api/tasks/:id → Delete a task
166
+
167
+ GET /api/tasks/:id/comments → List comments for a task (sub-resource)
168
+ POST /api/tasks/:id/comments → Add a comment to a task
169
+ ```
170
+
171
+ ### Pagination
172
+
173
+ Paginate list endpoints:
174
+
175
+ ```typescript
176
+ // Request
177
+ GET /api/tasks?page=1&pageSize=20&sortBy=createdAt&sortOrder=desc
178
+
179
+ // Response
180
+ {
181
+ "data": [...],
182
+ "pagination": {
183
+ "page": 1,
184
+ "pageSize": 20,
185
+ "totalItems": 142,
186
+ "totalPages": 8
187
+ }
188
+ }
189
+ ```
190
+
191
+ ### Filtering
192
+
193
+ Use query parameters for filters:
194
+
195
+ ```
196
+ GET /api/tasks?status=in_progress&assignee=user123&createdAfter=2025-01-01
197
+ ```
198
+
199
+ ### Partial Updates (PATCH)
200
+
201
+ Accept partial objects — only update what's provided:
202
+
203
+ ```typescript
204
+ // Only title changes, everything else preserved
205
+ PATCH /api/tasks/123
206
+ { "title": "Updated title" }
207
+ ```
208
+
209
+ ## TypeScript Interface Patterns
210
+
211
+ ### Use Discriminated Unions for Variants
212
+
213
+ ```typescript
214
+ // Good: Each variant is explicit
215
+ type TaskStatus =
216
+ | { type: 'pending' }
217
+ | { type: 'in_progress'; assignee: string; startedAt: Date }
218
+ | { type: 'completed'; completedAt: Date; completedBy: string }
219
+ | { type: 'cancelled'; reason: string; cancelledAt: Date };
220
+
221
+ // Consumer gets type narrowing
222
+ function getStatusLabel(status: TaskStatus): string {
223
+ switch (status.type) {
224
+ case 'pending': return 'Pending';
225
+ case 'in_progress': return `In progress (${status.assignee})`;
226
+ case 'completed': return `Done on ${status.completedAt}`;
227
+ case 'cancelled': return `Cancelled: ${status.reason}`;
228
+ }
229
+ }
230
+ ```
231
+
232
+ ### Input/Output Separation
233
+
234
+ ```typescript
235
+ // Input: what the caller provides
236
+ interface CreateTaskInput {
237
+ title: string;
238
+ description?: string;
239
+ }
240
+
241
+ // Output: what the system returns (includes server-generated fields)
242
+ interface Task {
243
+ id: string;
244
+ title: string;
245
+ description: string | null;
246
+ createdAt: Date;
247
+ updatedAt: Date;
248
+ createdBy: string;
249
+ }
250
+ ```
251
+
252
+ ### Use Branded Types for IDs
253
+
254
+ ```typescript
255
+ type TaskId = string & { readonly __brand: 'TaskId' };
256
+ type UserId = string & { readonly __brand: 'UserId' };
257
+
258
+ // Prevents accidentally passing a UserId where a TaskId is expected
259
+ function getTask(id: TaskId): Promise<Task> { ... }
260
+ ```
261
+
262
+ ## Common Rationalizations
263
+
264
+ | Rationalization | Reality |
265
+ |---|---|
266
+ | "We'll document the API later" | The types ARE the documentation. Define them first. |
267
+ | "We don't need pagination for now" | You will the moment someone has 100+ items. Add it from the start. |
268
+ | "PATCH is complicated, let's just use PUT" | PUT requires the full object every time. PATCH is what clients actually want. |
269
+ | "We'll version the API when we need to" | Breaking changes without versioning break consumers. Design for extension from the start. |
270
+ | "Nobody uses that undocumented behavior" | Hyrum's Law: if it's observable, somebody depends on it. Treat every public behavior as a commitment. |
271
+ | "We can just maintain two versions" | Multiple versions multiply maintenance cost and create diamond dependency problems. Prefer the One-Version Rule. |
272
+ | "Internal APIs don't need contracts" | Internal consumers are still consumers. Contracts prevent coupling and enable parallel work. |
273
+
274
+ ## Red Flags
275
+
276
+ - Endpoints that return different shapes depending on conditions
277
+ - Inconsistent error formats across endpoints
278
+ - Validation scattered throughout internal code instead of at boundaries
279
+ - Breaking changes to existing fields (type changes, removals)
280
+ - List endpoints without pagination
281
+ - Verbs in REST URLs (`/api/createTask`, `/api/getUsers`)
282
+ - Third-party API responses used without validation or sanitization
283
+
284
+ ## Verification
285
+
286
+ After designing an API:
287
+
288
+ - [ ] Every endpoint has typed input and output schemas
289
+ - [ ] Error responses follow a single consistent format
290
+ - [ ] Validation happens at system boundaries only
291
+ - [ ] List endpoints support pagination
292
+ - [ ] New fields are additive and optional (backward compatible)
293
+ - [ ] Naming follows consistent conventions across all endpoints
294
+ - [ ] API documentation or types are committed alongside the implementation