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,23 +12,23 @@ tags: parse, safeParse, error-handling, validation
|
||||
**Incorrect (parse without error handling):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from "zod";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const createUserSchema = z.object({
|
||||
email: z.string().email(),
|
||||
name: z.string().min(1),
|
||||
})
|
||||
name: z.string().min(1)
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json()
|
||||
const body = await req.json();
|
||||
|
||||
// If validation fails, this throws and crashes the handler
|
||||
const user = createUserSchema.parse(body)
|
||||
const user = createUserSchema.parse(body);
|
||||
|
||||
// Never reached if parse throws
|
||||
await db.users.create({ data: user })
|
||||
return NextResponse.json({ success: true })
|
||||
await db.users.create({ data: user });
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
// Result: 500 Internal Server Error with stack trace
|
||||
```
|
||||
@@ -36,30 +36,30 @@ export async function POST(req: NextRequest) {
|
||||
**Correct (using safeParse):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from "zod";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const createUserSchema = z.object({
|
||||
email: z.string().email(),
|
||||
name: z.string().min(1),
|
||||
})
|
||||
name: z.string().min(1)
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json()
|
||||
const body = await req.json();
|
||||
|
||||
const result = createUserSchema.safeParse(body)
|
||||
const result = createUserSchema.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
// Return structured error response
|
||||
return NextResponse.json(
|
||||
{ error: 'Validation failed', issues: result.error.issues },
|
||||
{ error: "Validation failed", issues: result.error.issues },
|
||||
{ status: 400 }
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// result.data is typed correctly
|
||||
await db.users.create({ data: result.data })
|
||||
return NextResponse.json({ success: true })
|
||||
await db.users.create({ data: result.data });
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
```
|
||||
|
||||
@@ -85,16 +85,17 @@ if (result.success) {
|
||||
|
||||
```typescript
|
||||
// Internal data you control - parse is fine
|
||||
const config = configSchema.parse(JSON.parse(process.env.CONFIG))
|
||||
const config = configSchema.parse(JSON.parse(process.env.CONFIG));
|
||||
|
||||
// Test assertions - parse throws helpful errors
|
||||
expect(() => schema.parse(invalidData)).toThrow()
|
||||
expect(() => schema.parse(invalidData)).toThrow();
|
||||
|
||||
// Schema development - see errors immediately
|
||||
schema.parse(testData) // See what fails during development
|
||||
schema.parse(testData); // See what fails during development
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- Internal configuration parsing where invalid data should crash early
|
||||
- Tests where you want exceptions to fail the test
|
||||
- Scripts where you want to see the full error
|
||||
|
||||
Reference in new issue
Block a user