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,41 +12,41 @@ tags: schema, unknown, any, type-safety
|
||||
**Incorrect (using z.any):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const eventSchema = z.object({
|
||||
type: z.string(),
|
||||
payload: z.any(), // Infers to 'any'
|
||||
})
|
||||
payload: z.any() // Infers to 'any'
|
||||
});
|
||||
|
||||
type Event = z.infer<typeof eventSchema>
|
||||
type Event = z.infer<typeof eventSchema>;
|
||||
// { type: string; payload: any }
|
||||
|
||||
function handleEvent(event: Event) {
|
||||
// No type error - TypeScript allows anything
|
||||
console.log(event.payload.foo.bar.baz) // Runtime crash if structure is wrong
|
||||
console.log(event.payload.foo.bar.baz); // Runtime crash if structure is wrong
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (using z.unknown):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const eventSchema = z.object({
|
||||
type: z.string(),
|
||||
payload: z.unknown(), // Infers to 'unknown'
|
||||
})
|
||||
payload: z.unknown() // Infers to 'unknown'
|
||||
});
|
||||
|
||||
type Event = z.infer<typeof eventSchema>
|
||||
type Event = z.infer<typeof eventSchema>;
|
||||
// { type: string; payload: unknown }
|
||||
|
||||
function handleEvent(event: Event) {
|
||||
// TypeScript error: Object is of type 'unknown'
|
||||
console.log(event.payload.foo) // Won't compile
|
||||
console.log(event.payload.foo); // Won't compile
|
||||
|
||||
// Must narrow type first
|
||||
if (typeof event.payload === 'object' && event.payload !== null) {
|
||||
if (typeof event.payload === "object" && event.payload !== null) {
|
||||
// Now TypeScript knows it's an object
|
||||
}
|
||||
}
|
||||
@@ -55,33 +55,31 @@ function handleEvent(event: Event) {
|
||||
**Better approach with discriminated unions:**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const userCreatedSchema = z.object({
|
||||
type: z.literal('user.created'),
|
||||
type: z.literal("user.created"),
|
||||
payload: z.object({
|
||||
userId: z.string(),
|
||||
email: z.string().email(),
|
||||
}),
|
||||
})
|
||||
email: z.string().email()
|
||||
})
|
||||
});
|
||||
|
||||
const orderPlacedSchema = z.object({
|
||||
type: z.literal('order.placed'),
|
||||
type: z.literal("order.placed"),
|
||||
payload: z.object({
|
||||
orderId: z.string(),
|
||||
amount: z.number(),
|
||||
}),
|
||||
})
|
||||
amount: z.number()
|
||||
})
|
||||
});
|
||||
|
||||
const eventSchema = z.discriminatedUnion('type', [
|
||||
userCreatedSchema,
|
||||
orderPlacedSchema,
|
||||
])
|
||||
const eventSchema = z.discriminatedUnion("type", [userCreatedSchema, orderPlacedSchema]);
|
||||
|
||||
// Full type safety for each event type
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- When you're consuming a third-party API where you truly don't know the shape
|
||||
- When prototyping and will add proper types later
|
||||
|
||||
|
||||
Reference in new issue
Block a user