@patricio0312rev/skillset 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/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +176 -0
- package/bin/cli.js +37 -0
- package/package.json +55 -0
- package/src/commands/init.js +301 -0
- package/src/index.js +168 -0
- package/src/lib/config.js +200 -0
- package/src/lib/generator.js +166 -0
- package/src/utils/display.js +95 -0
- package/src/utils/readme.js +196 -0
- package/src/utils/tool-specific.js +233 -0
- package/templates/ai-engineering/agent-orchestration-planner/ SKILL.md +266 -0
- package/templates/ai-engineering/cost-latency-optimizer/ SKILL.md +270 -0
- package/templates/ai-engineering/doc-to-vector-dataset-generator/ SKILL.md +239 -0
- package/templates/ai-engineering/evaluation-harness/ SKILL.md +219 -0
- package/templates/ai-engineering/guardrails-safety-filter-builder/ SKILL.md +226 -0
- package/templates/ai-engineering/llm-debugger/ SKILL.md +283 -0
- package/templates/ai-engineering/prompt-regression-tester/ SKILL.md +216 -0
- package/templates/ai-engineering/prompt-template-builder/ SKILL.md +393 -0
- package/templates/ai-engineering/rag-pipeline-builder/ SKILL.md +244 -0
- package/templates/ai-engineering/tool-function-schema-designer/ SKILL.md +219 -0
- package/templates/architecture/adr-writer/ SKILL.md +250 -0
- package/templates/architecture/api-versioning-deprecation-planner/ SKILL.md +331 -0
- package/templates/architecture/domain-model-boundaries-mapper/ SKILL.md +300 -0
- package/templates/architecture/migration-planner/ SKILL.md +376 -0
- package/templates/architecture/performance-budget-setter/ SKILL.md +318 -0
- package/templates/architecture/reliability-strategy-builder/ SKILL.md +286 -0
- package/templates/architecture/rfc-generator/ SKILL.md +362 -0
- package/templates/architecture/scalability-playbook/ SKILL.md +279 -0
- package/templates/architecture/system-design-generator/ SKILL.md +339 -0
- package/templates/architecture/tech-debt-prioritizer/ SKILL.md +329 -0
- package/templates/backend/api-contract-normalizer/ SKILL.md +487 -0
- package/templates/backend/api-endpoint-generator/ SKILL.md +415 -0
- package/templates/backend/auth-module-builder/ SKILL.md +99 -0
- package/templates/backend/background-jobs-designer/ SKILL.md +166 -0
- package/templates/backend/caching-strategist/ SKILL.md +190 -0
- package/templates/backend/error-handling-standardizer/ SKILL.md +174 -0
- package/templates/backend/rate-limiting-abuse-protection/ SKILL.md +147 -0
- package/templates/backend/rbac-permissions-builder/ SKILL.md +158 -0
- package/templates/backend/service-layer-extractor/ SKILL.md +269 -0
- package/templates/backend/webhook-receiver-hardener/ SKILL.md +211 -0
- package/templates/ci-cd/artifact-sbom-publisher/ SKILL.md +236 -0
- package/templates/ci-cd/caching-strategy-optimizer/ SKILL.md +195 -0
- package/templates/ci-cd/deployment-checklist-generator/ SKILL.md +381 -0
- package/templates/ci-cd/github-actions-pipeline-creator/ SKILL.md +348 -0
- package/templates/ci-cd/monorepo-ci-optimizer/ SKILL.md +298 -0
- package/templates/ci-cd/preview-environments-builder/ SKILL.md +187 -0
- package/templates/ci-cd/quality-gates-enforcer/ SKILL.md +342 -0
- package/templates/ci-cd/release-automation-builder/ SKILL.md +281 -0
- package/templates/ci-cd/rollback-workflow-builder/ SKILL.md +372 -0
- package/templates/ci-cd/secrets-env-manager/ SKILL.md +242 -0
- package/templates/db-management/backup-restore-runbook-generator/ SKILL.md +505 -0
- package/templates/db-management/data-integrity-auditor/ SKILL.md +505 -0
- package/templates/db-management/data-retention-archiving-planner/ SKILL.md +430 -0
- package/templates/db-management/data-seeding-fixtures-builder/ SKILL.md +375 -0
- package/templates/db-management/db-performance-watchlist/ SKILL.md +425 -0
- package/templates/db-management/etl-sync-job-builder/ SKILL.md +457 -0
- package/templates/db-management/multi-tenant-safety-checker/ SKILL.md +398 -0
- package/templates/db-management/prisma-migration-assistant/ SKILL.md +379 -0
- package/templates/db-management/schema-consistency-checker/ SKILL.md +440 -0
- package/templates/db-management/sql-query-optimizer/ SKILL.md +324 -0
- package/templates/foundation/changelog-writer/ SKILL.md +431 -0
- package/templates/foundation/code-formatter-installer/ SKILL.md +320 -0
- package/templates/foundation/codebase-summarizer/ SKILL.md +360 -0
- package/templates/foundation/dependency-doctor/ SKILL.md +163 -0
- package/templates/foundation/dev-environment-bootstrapper/ SKILL.md +259 -0
- package/templates/foundation/dev-onboarding-builder/ SKILL.md +556 -0
- package/templates/foundation/docs-starter-kit/ SKILL.md +574 -0
- package/templates/foundation/explaining-code/SKILL.md +13 -0
- package/templates/foundation/git-hygiene-enforcer/ SKILL.md +455 -0
- package/templates/foundation/project-scaffolder/ SKILL.md +65 -0
- package/templates/foundation/project-scaffolder/references/templates.md +126 -0
- package/templates/foundation/repo-structure-linter/ SKILL.md +0 -0
- package/templates/foundation/repo-structure-linter/references/conventions.md +98 -0
- package/templates/frontend/animation-micro-interaction-pack/ SKILL.md +41 -0
- package/templates/frontend/component-scaffold-generator/ SKILL.md +562 -0
- package/templates/frontend/design-to-component-translator/ SKILL.md +547 -0
- package/templates/frontend/form-wizard-builder/ SKILL.md +553 -0
- package/templates/frontend/frontend-refactor-planner/ SKILL.md +37 -0
- package/templates/frontend/i18n-frontend-implementer/ SKILL.md +44 -0
- package/templates/frontend/modal-drawer-system/ SKILL.md +377 -0
- package/templates/frontend/page-layout-builder/ SKILL.md +630 -0
- package/templates/frontend/state-ux-flow-builder/ SKILL.md +23 -0
- package/templates/frontend/table-builder/ SKILL.md +350 -0
- package/templates/performance/alerting-dashboard-builder/ SKILL.md +162 -0
- package/templates/performance/backend-latency-profiler-helper/ SKILL.md +108 -0
- package/templates/performance/caching-cdn-strategy-planner/ SKILL.md +150 -0
- package/templates/performance/capacity-planning-helper/ SKILL.md +242 -0
- package/templates/performance/core-web-vitals-tuner/ SKILL.md +126 -0
- package/templates/performance/incident-runbook-generator/ SKILL.md +162 -0
- package/templates/performance/load-test-scenario-builder/ SKILL.md +256 -0
- package/templates/performance/observability-setup/ SKILL.md +232 -0
- package/templates/performance/postmortem-writer/ SKILL.md +203 -0
- package/templates/performance/structured-logging-standardizer/ SKILL.md +122 -0
- package/templates/security/auth-security-reviewer/ SKILL.md +428 -0
- package/templates/security/dependency-vulnerability-triage/ SKILL.md +495 -0
- package/templates/security/input-validation-sanitization-auditor/ SKILL.md +76 -0
- package/templates/security/pii-redaction-logging-policy-builder/ SKILL.md +65 -0
- package/templates/security/rbac-policy-tester/ SKILL.md +80 -0
- package/templates/security/secrets-scanner/ SKILL.md +462 -0
- package/templates/security/secure-headers-csp-builder/ SKILL.md +404 -0
- package/templates/security/security-incident-playbook-generator/ SKILL.md +76 -0
- package/templates/security/security-pr-checklist-skill/ SKILL.md +62 -0
- package/templates/security/threat-model-generator/ SKILL.md +394 -0
- package/templates/testing/contract-testing-builder/ SKILL.md +492 -0
- package/templates/testing/coverage-strategist/ SKILL.md +436 -0
- package/templates/testing/e2e-test-builder/ SKILL.md +382 -0
- package/templates/testing/flaky-test-detective/ SKILL.md +416 -0
- package/templates/testing/integration-test-builder/ SKILL.md +525 -0
- package/templates/testing/mocking-assistant/ SKILL.md +383 -0
- package/templates/testing/snapshot-test-refactorer/ SKILL.md +375 -0
- package/templates/testing/test-data-factory-builder/ SKILL.md +449 -0
- package/templates/testing/test-reporting-triage-skill/ SKILL.md +469 -0
- package/templates/testing/unit-test-generator/ SKILL.md +548 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: test-data-factory-builder
|
|
3
|
+
description: Creates factories and builders for generating consistent, composable test data with realistic values and relationship handling. Use for "test factories", "test data builders", "fixture factories", or "test data generation".
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Test Data Factory Builder
|
|
7
|
+
|
|
8
|
+
Create composable factories for consistent test data.
|
|
9
|
+
|
|
10
|
+
## Factory Pattern
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
// factories/UserFactory.ts
|
|
14
|
+
import { faker } from "@faker-js/faker";
|
|
15
|
+
|
|
16
|
+
export class UserFactory {
|
|
17
|
+
private data: Partial<User> = {};
|
|
18
|
+
|
|
19
|
+
static create(overrides?: Partial<User>): User {
|
|
20
|
+
return new UserFactory().with(overrides).build();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
with(overrides: Partial<User>): this {
|
|
24
|
+
this.data = { ...this.data, ...overrides };
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
withEmail(email: string): this {
|
|
29
|
+
this.data.email = email;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
withRole(role: UserRole): this {
|
|
34
|
+
this.data.role = role;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
asAdmin(): this {
|
|
39
|
+
this.data.role = "ADMIN";
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
build(): User {
|
|
44
|
+
return {
|
|
45
|
+
id: this.data.id || faker.string.uuid(),
|
|
46
|
+
email: this.data.email || faker.internet.email(),
|
|
47
|
+
name: this.data.name || faker.person.fullName(),
|
|
48
|
+
role: this.data.role || "USER",
|
|
49
|
+
createdAt: this.data.createdAt || faker.date.past(),
|
|
50
|
+
...this.data,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Usage
|
|
56
|
+
const user = UserFactory.create();
|
|
57
|
+
const admin = UserFactory.create().asAdmin().build();
|
|
58
|
+
const specific = UserFactory.create({ email: "test@example.com" });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Builder Pattern
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// builders/OrderBuilder.ts
|
|
65
|
+
export class OrderBuilder {
|
|
66
|
+
private user?: User;
|
|
67
|
+
private items: OrderItem[] = [];
|
|
68
|
+
private status: OrderStatus = "PENDING";
|
|
69
|
+
|
|
70
|
+
forUser(user: User): this {
|
|
71
|
+
this.user = user;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
withItem(product: Product, quantity: number = 1): this {
|
|
76
|
+
this.items.push({
|
|
77
|
+
id: faker.string.uuid(),
|
|
78
|
+
productId: product.id,
|
|
79
|
+
quantity,
|
|
80
|
+
price: product.price,
|
|
81
|
+
});
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
withStatus(status: OrderStatus): this {
|
|
86
|
+
this.status = status;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
asPaid(): this {
|
|
91
|
+
this.status = "PAID";
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async build(): Promise<Order> {
|
|
96
|
+
if (!this.user) {
|
|
97
|
+
throw new Error("User is required");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const total = this.items.reduce(
|
|
101
|
+
(sum, item) => sum + item.price * item.quantity,
|
|
102
|
+
0
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
id: faker.string.uuid(),
|
|
107
|
+
userId: this.user.id,
|
|
108
|
+
items: this.items,
|
|
109
|
+
total,
|
|
110
|
+
status: this.status,
|
|
111
|
+
createdAt: new Date(),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Usage
|
|
117
|
+
const order = await new OrderBuilder()
|
|
118
|
+
.forUser(user)
|
|
119
|
+
.withItem(laptop, 2)
|
|
120
|
+
.withItem(phone, 1)
|
|
121
|
+
.asPaid()
|
|
122
|
+
.build();
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Relationship Handling
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// factories/OrderFactory.ts
|
|
129
|
+
export class OrderFactory {
|
|
130
|
+
static async createWithUser(overrides?: Partial<Order>): Promise<Order> {
|
|
131
|
+
// Create user if not provided
|
|
132
|
+
const user = UserFactory.create();
|
|
133
|
+
|
|
134
|
+
// Create products
|
|
135
|
+
const products = [
|
|
136
|
+
ProductFactory.create({ price: 99.99 }),
|
|
137
|
+
ProductFactory.create({ price: 199.99 }),
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
// Create order with relationships
|
|
141
|
+
return {
|
|
142
|
+
id: faker.string.uuid(),
|
|
143
|
+
userId: user.id,
|
|
144
|
+
user,
|
|
145
|
+
items: products.map((product) => ({
|
|
146
|
+
id: faker.string.uuid(),
|
|
147
|
+
productId: product.id,
|
|
148
|
+
product,
|
|
149
|
+
quantity: 1,
|
|
150
|
+
price: product.price,
|
|
151
|
+
})),
|
|
152
|
+
total: products.reduce((sum, p) => sum + p.price, 0),
|
|
153
|
+
status: "PENDING",
|
|
154
|
+
createdAt: new Date(),
|
|
155
|
+
...overrides,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Database Persistence
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
// factories/UserFactory.ts with persistence
|
|
165
|
+
export class UserFactory {
|
|
166
|
+
private prisma: PrismaClient;
|
|
167
|
+
|
|
168
|
+
constructor(prisma: PrismaClient) {
|
|
169
|
+
this.prisma = prisma;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async create(overrides?: Partial<User>): Promise<User> {
|
|
173
|
+
const data = {
|
|
174
|
+
email: faker.internet.email(),
|
|
175
|
+
name: faker.person.fullName(),
|
|
176
|
+
role: "USER",
|
|
177
|
+
...overrides,
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
return this.prisma.user.create({ data });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async createMany(count: number): Promise<User[]> {
|
|
184
|
+
return Promise.all(Array.from({ length: count }, () => this.create()));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async createAdmin(): Promise<User> {
|
|
188
|
+
return this.create({ role: "ADMIN" });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Usage in tests
|
|
193
|
+
test("should list users", async () => {
|
|
194
|
+
const userFactory = new UserFactory(prisma);
|
|
195
|
+
await userFactory.createMany(5);
|
|
196
|
+
|
|
197
|
+
const users = await userService.list();
|
|
198
|
+
expect(users).toHaveLength(5);
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Traits Pattern
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
// factories/UserFactory.ts with traits
|
|
206
|
+
export class UserFactory {
|
|
207
|
+
private traits: string[] = [];
|
|
208
|
+
|
|
209
|
+
withTrait(trait: string): this {
|
|
210
|
+
this.traits.push(trait);
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
build(): User {
|
|
215
|
+
let user: User = {
|
|
216
|
+
id: faker.string.uuid(),
|
|
217
|
+
email: faker.internet.email(),
|
|
218
|
+
name: faker.person.fullName(),
|
|
219
|
+
role: "USER",
|
|
220
|
+
createdAt: new Date(),
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// Apply traits
|
|
224
|
+
if (this.traits.includes("verified")) {
|
|
225
|
+
user.emailVerified = true;
|
|
226
|
+
user.verifiedAt = new Date();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (this.traits.includes("suspended")) {
|
|
230
|
+
user.status = "SUSPENDED";
|
|
231
|
+
user.suspendedAt = new Date();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (this.traits.includes("premium")) {
|
|
235
|
+
user.subscription = "PREMIUM";
|
|
236
|
+
user.subscriptionExpiresAt = faker.date.future();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return user;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Usage
|
|
244
|
+
const verifiedUser = new UserFactory().withTrait("verified").build();
|
|
245
|
+
|
|
246
|
+
const suspendedPremiumUser = new UserFactory()
|
|
247
|
+
.withTrait("suspended")
|
|
248
|
+
.withTrait("premium")
|
|
249
|
+
.build();
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Sequence Generation
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
// factories/sequence.ts
|
|
256
|
+
class Sequence {
|
|
257
|
+
private counters = new Map<string, number>();
|
|
258
|
+
|
|
259
|
+
next(key: string): number {
|
|
260
|
+
const current = this.counters.get(key) || 0;
|
|
261
|
+
const next = current + 1;
|
|
262
|
+
this.counters.set(key, next);
|
|
263
|
+
return next;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
reset(key?: string): void {
|
|
267
|
+
if (key) {
|
|
268
|
+
this.counters.delete(key);
|
|
269
|
+
} else {
|
|
270
|
+
this.counters.clear();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const sequence = new Sequence();
|
|
276
|
+
|
|
277
|
+
// Usage in factory
|
|
278
|
+
export class UserFactory {
|
|
279
|
+
build(): User {
|
|
280
|
+
return {
|
|
281
|
+
id: faker.string.uuid(),
|
|
282
|
+
email: `user${sequence.next("user")}@example.com`,
|
|
283
|
+
name: `Test User ${sequence.next("user")}`,
|
|
284
|
+
// ...
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Creates: user1@example.com, user2@example.com, etc.
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Composable Factories
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
// factories/index.ts
|
|
296
|
+
export const TestDataBuilder = {
|
|
297
|
+
user: (overrides?: Partial<User>) => new UserFactory().with(overrides),
|
|
298
|
+
product: (overrides?: Partial<Product>) =>
|
|
299
|
+
new ProductFactory().with(overrides),
|
|
300
|
+
order: () => new OrderBuilder(),
|
|
301
|
+
|
|
302
|
+
// Composite builders
|
|
303
|
+
checkoutScenario: async () => {
|
|
304
|
+
const user = TestDataBuilder.user().build();
|
|
305
|
+
const products = [
|
|
306
|
+
TestDataBuilder.product({ price: 99.99 }).build(),
|
|
307
|
+
TestDataBuilder.product({ price: 199.99 }).build(),
|
|
308
|
+
];
|
|
309
|
+
const order = await TestDataBuilder.order()
|
|
310
|
+
.forUser(user)
|
|
311
|
+
.withItem(products[0], 2)
|
|
312
|
+
.withItem(products[1], 1)
|
|
313
|
+
.build();
|
|
314
|
+
|
|
315
|
+
return { user, products, order };
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// Usage
|
|
320
|
+
test("should process checkout", async () => {
|
|
321
|
+
const { user, order } = await TestDataBuilder.checkoutScenario();
|
|
322
|
+
|
|
323
|
+
const result = await checkoutService.process(order);
|
|
324
|
+
expect(result.status).toBe("SUCCESS");
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
## Realistic Data Generators
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
// generators/realistic.ts
|
|
332
|
+
import { faker } from "@faker-js/faker";
|
|
333
|
+
|
|
334
|
+
export const RealisticData = {
|
|
335
|
+
creditCard: () => ({
|
|
336
|
+
number: "4242424242424242", // Test card
|
|
337
|
+
expiry: faker.date.future().toISOString().slice(0, 7), // YYYY-MM
|
|
338
|
+
cvc: "123",
|
|
339
|
+
name: faker.person.fullName(),
|
|
340
|
+
}),
|
|
341
|
+
|
|
342
|
+
address: () => ({
|
|
343
|
+
street: faker.location.streetAddress(),
|
|
344
|
+
city: faker.location.city(),
|
|
345
|
+
state: faker.location.state(),
|
|
346
|
+
zip: faker.location.zipCode(),
|
|
347
|
+
country: "US",
|
|
348
|
+
}),
|
|
349
|
+
|
|
350
|
+
product: () => ({
|
|
351
|
+
name: faker.commerce.productName(),
|
|
352
|
+
description: faker.commerce.productDescription(),
|
|
353
|
+
price: parseFloat(faker.commerce.price()),
|
|
354
|
+
category: faker.commerce.department(),
|
|
355
|
+
sku: faker.string.alphanumeric(10).toUpperCase(),
|
|
356
|
+
}),
|
|
357
|
+
|
|
358
|
+
email: {
|
|
359
|
+
valid: () => faker.internet.email(),
|
|
360
|
+
invalid: () => "invalid-email",
|
|
361
|
+
disposable: () => `${faker.string.alphanumeric(8)}@tempmail.com`,
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Factory Registry
|
|
367
|
+
|
|
368
|
+
```typescript
|
|
369
|
+
// factories/registry.ts
|
|
370
|
+
class FactoryRegistry {
|
|
371
|
+
private factories = new Map();
|
|
372
|
+
|
|
373
|
+
register<T>(name: string, factory: () => T): void {
|
|
374
|
+
this.factories.set(name, factory);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
create<T>(name: string, overrides?: Partial<T>): T {
|
|
378
|
+
const factory = this.factories.get(name);
|
|
379
|
+
if (!factory) {
|
|
380
|
+
throw new Error(`Factory not found: ${name}`);
|
|
381
|
+
}
|
|
382
|
+
const instance = factory();
|
|
383
|
+
return { ...instance, ...overrides };
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const registry = new FactoryRegistry();
|
|
388
|
+
|
|
389
|
+
// Register factories
|
|
390
|
+
registry.register("user", () => UserFactory.create());
|
|
391
|
+
registry.register("product", () => ProductFactory.create());
|
|
392
|
+
|
|
393
|
+
// Usage
|
|
394
|
+
const user = registry.create("user", { role: "ADMIN" });
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## Test Helpers
|
|
398
|
+
|
|
399
|
+
```typescript
|
|
400
|
+
// helpers/test-data.ts
|
|
401
|
+
export async function seedTestDatabase(prisma: PrismaClient) {
|
|
402
|
+
const userFactory = new UserFactory(prisma);
|
|
403
|
+
const productFactory = new ProductFactory(prisma);
|
|
404
|
+
|
|
405
|
+
// Create base data
|
|
406
|
+
const users = await userFactory.createMany(10);
|
|
407
|
+
const products = await productFactory.createMany(20);
|
|
408
|
+
|
|
409
|
+
// Create relationships
|
|
410
|
+
for (const user of users.slice(0, 5)) {
|
|
411
|
+
await new OrderBuilder()
|
|
412
|
+
.forUser(user)
|
|
413
|
+
.withItem(products[0], 2)
|
|
414
|
+
.withItem(products[1], 1)
|
|
415
|
+
.asPaid()
|
|
416
|
+
.build();
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return { users, products };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Usage
|
|
423
|
+
beforeEach(async () => {
|
|
424
|
+
await seedTestDatabase(prisma);
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
## Best Practices
|
|
429
|
+
|
|
430
|
+
1. **Deterministic by default**: Use seeded faker
|
|
431
|
+
2. **Minimal data**: Only create what's needed
|
|
432
|
+
3. **Composable**: Combine factories
|
|
433
|
+
4. **Type-safe**: Full TypeScript support
|
|
434
|
+
5. **Relationships**: Easy to create related data
|
|
435
|
+
6. **Database-agnostic**: Works with or without DB
|
|
436
|
+
7. **Clear naming**: Descriptive factory methods
|
|
437
|
+
|
|
438
|
+
## Output Checklist
|
|
439
|
+
|
|
440
|
+
- [ ] Factory classes created
|
|
441
|
+
- [ ] Builder pattern implemented
|
|
442
|
+
- [ ] Relationship handling
|
|
443
|
+
- [ ] Database persistence option
|
|
444
|
+
- [ ] Traits for variations
|
|
445
|
+
- [ ] Sequence generation
|
|
446
|
+
- [ ] Composable builders
|
|
447
|
+
- [ ] Realistic data generators
|
|
448
|
+
- [ ] Factory registry (optional)
|
|
449
|
+
- [ ] Test helpers created
|