feat: v2.0 iteration — test coverage, reliability, observability
M0: Test coverage expansion (151 → 321 tests, 61% → 66% lines) - normalizeKnowledge CRLF/LF normalization fix - gooseReviewer: 11 tests, 97.35% coverage - r2 storage: 22 tests, 100% coverage - triage processor: 11 tests, exported buildTriagePrompt/labelName - webhook processor: 35 tests, 72.39% coverage - conflict resolver: 12 tests - review processor: 7 tests M1: Reliability hardening - Smart retry with jitter, maxTotalTimeoutMs, custom retryable predicate - MergeGuard: in-memory merge dedup tracker (100% coverage) - failureMessages: 7-category failure classifier (100% coverage) M2: Observability - MetricsCollector: Actions-run metrics with snapshot/markdown/reset - 11 tests, 100% coverage New files: 12 test files, 3 source modules All gates: typecheck ✓ lint ✓ format ✓ 321/321 tests ✓ build ✓
This commit is contained in:
1 parent
db53296d78
commit
78bf8e9e41
87 files changed
+6003
-2587
No files matched your search
@@ -12,24 +12,24 @@ Validate external data immediately when it enters your system—at API endpoints
|
||||
**Incorrect (validating deep in business logic):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
// No validation at API boundary
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json()
|
||||
const body = await req.json();
|
||||
// Raw unknown data passed through
|
||||
return await processOrder(body)
|
||||
return await processOrder(body);
|
||||
}
|
||||
|
||||
async function processOrder(data: unknown) {
|
||||
// Data passed around unvalidated
|
||||
const items = await calculateTotals(data)
|
||||
return await chargeCustomer(data, items)
|
||||
const items = await calculateTotals(data);
|
||||
return await chargeCustomer(data, items);
|
||||
}
|
||||
|
||||
async function calculateTotals(data: unknown) {
|
||||
// Finally validating way too late
|
||||
const order = orderSchema.parse(data) // Throws here, far from entry point
|
||||
const order = orderSchema.parse(data); // Throws here, far from entry point
|
||||
// ...
|
||||
}
|
||||
// Hard to trace where bad data came from
|
||||
@@ -38,52 +38,56 @@ async function calculateTotals(data: unknown) {
|
||||
**Correct (validating at boundary):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const orderSchema = z.object({
|
||||
customerId: z.string().uuid(),
|
||||
items: z.array(z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().int().positive(),
|
||||
})).min(1),
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().int().positive()
|
||||
})
|
||||
)
|
||||
.min(1),
|
||||
shippingAddress: z.object({
|
||||
street: z.string(),
|
||||
city: z.string(),
|
||||
country: z.string(),
|
||||
}),
|
||||
})
|
||||
country: z.string()
|
||||
})
|
||||
});
|
||||
|
||||
type Order = z.infer<typeof orderSchema>
|
||||
type Order = z.infer<typeof orderSchema>;
|
||||
|
||||
// Validate immediately at boundary
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json()
|
||||
const body = await req.json();
|
||||
|
||||
const result = orderSchema.safeParse(body)
|
||||
const result = orderSchema.safeParse(body);
|
||||
if (!result.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid order', issues: result.error.issues },
|
||||
{ error: "Invalid order", issues: result.error.issues },
|
||||
{ status: 400 }
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Now data is validated and typed
|
||||
return await processOrder(result.data)
|
||||
return await processOrder(result.data);
|
||||
}
|
||||
|
||||
// Business logic receives typed, validated data
|
||||
async function processOrder(order: Order) {
|
||||
// order is guaranteed to match schema
|
||||
const items = await calculateTotals(order)
|
||||
return await chargeCustomer(order, items)
|
||||
const items = await calculateTotals(order);
|
||||
return await chargeCustomer(order, items);
|
||||
}
|
||||
|
||||
async function calculateTotals(order: Order) {
|
||||
// No validation needed - type guarantees shape
|
||||
return order.items.map(item => ({
|
||||
return order.items.map((item) => ({
|
||||
...item,
|
||||
total: item.quantity * getPrice(item.productId),
|
||||
}))
|
||||
total: item.quantity * getPrice(item.productId)
|
||||
}));
|
||||
}
|
||||
```
|
||||
|
||||
@@ -92,28 +96,29 @@ async function calculateTotals(order: Order) {
|
||||
```typescript
|
||||
// API endpoints
|
||||
export async function POST(req: NextRequest) {
|
||||
const data = await req.json()
|
||||
const validated = requestSchema.safeParse(data)
|
||||
const data = await req.json();
|
||||
const validated = requestSchema.safeParse(data);
|
||||
// ...
|
||||
}
|
||||
|
||||
// Message queue consumers
|
||||
async function handleMessage(rawMessage: string) {
|
||||
const data = JSON.parse(rawMessage)
|
||||
const validated = messageSchema.safeParse(data)
|
||||
const data = JSON.parse(rawMessage);
|
||||
const validated = messageSchema.safeParse(data);
|
||||
// ...
|
||||
}
|
||||
|
||||
// Configuration loading
|
||||
const config = configSchema.parse(JSON.parse(process.env.CONFIG!))
|
||||
const config = configSchema.parse(JSON.parse(process.env.CONFIG!));
|
||||
|
||||
// External API responses
|
||||
const response = await fetch('/api/users')
|
||||
const data = await response.json()
|
||||
const users = usersResponseSchema.parse(data)
|
||||
const response = await fetch("/api/users");
|
||||
const data = await response.json();
|
||||
const users = usersResponseSchema.parse(data);
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- Internal function calls with already-validated data
|
||||
- Performance-critical hot paths (validate once, trust afterward)
|
||||
|
||||
|
||||
Reference in new issue
Block a user