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,27 +12,27 @@ When a field's type depends on another field's value (e.g., `type: 'success'` me
|
||||
**Incorrect (regular union - no automatic narrowing):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const successSchema = z.object({
|
||||
type: z.literal('success'),
|
||||
data: z.object({ id: z.string() }),
|
||||
})
|
||||
type: z.literal("success"),
|
||||
data: z.object({ id: z.string() })
|
||||
});
|
||||
|
||||
const errorSchema = z.object({
|
||||
type: z.literal('error'),
|
||||
message: z.string(),
|
||||
})
|
||||
type: z.literal("error"),
|
||||
message: z.string()
|
||||
});
|
||||
|
||||
// Regular union - Zod tries each option in order
|
||||
const responseSchema = z.union([successSchema, errorSchema])
|
||||
const responseSchema = z.union([successSchema, errorSchema]);
|
||||
|
||||
type Response = z.infer<typeof responseSchema>
|
||||
type Response = z.infer<typeof responseSchema>;
|
||||
|
||||
function handleResponse(response: Response) {
|
||||
// TypeScript doesn't narrow automatically
|
||||
if (response.type === 'success') {
|
||||
response.data // Error: Property 'data' does not exist on type 'Response'
|
||||
if (response.type === "success") {
|
||||
response.data; // Error: Property 'data' does not exist on type 'Response'
|
||||
// Must cast or use type guards
|
||||
}
|
||||
}
|
||||
@@ -41,32 +41,29 @@ function handleResponse(response: Response) {
|
||||
**Correct (discriminated union):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const successSchema = z.object({
|
||||
type: z.literal('success'),
|
||||
data: z.object({ id: z.string() }),
|
||||
})
|
||||
type: z.literal("success"),
|
||||
data: z.object({ id: z.string() })
|
||||
});
|
||||
|
||||
const errorSchema = z.object({
|
||||
type: z.literal('error'),
|
||||
message: z.string(),
|
||||
})
|
||||
type: z.literal("error"),
|
||||
message: z.string()
|
||||
});
|
||||
|
||||
// Discriminated union - Zod uses 'type' field for O(1) dispatch
|
||||
const responseSchema = z.discriminatedUnion('type', [
|
||||
successSchema,
|
||||
errorSchema,
|
||||
])
|
||||
const responseSchema = z.discriminatedUnion("type", [successSchema, errorSchema]);
|
||||
|
||||
type Response = z.infer<typeof responseSchema>
|
||||
type Response = z.infer<typeof responseSchema>;
|
||||
|
||||
function handleResponse(response: Response) {
|
||||
// TypeScript narrows automatically!
|
||||
if (response.type === 'success') {
|
||||
response.data.id // Works - TypeScript knows data exists
|
||||
if (response.type === "success") {
|
||||
response.data.id; // Works - TypeScript knows data exists
|
||||
} else {
|
||||
response.message // Works - TypeScript knows message exists
|
||||
response.message; // Works - TypeScript knows message exists
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -75,63 +72,64 @@ function handleResponse(response: Response) {
|
||||
|
||||
```typescript
|
||||
// API responses
|
||||
const apiResponse = z.discriminatedUnion('status', [
|
||||
z.object({ status: z.literal('success'), data: z.unknown() }),
|
||||
z.object({ status: z.literal('error'), error: z.string(), code: z.number() }),
|
||||
z.object({ status: z.literal('loading') }),
|
||||
])
|
||||
const apiResponse = z.discriminatedUnion("status", [
|
||||
z.object({ status: z.literal("success"), data: z.unknown() }),
|
||||
z.object({ status: z.literal("error"), error: z.string(), code: z.number() }),
|
||||
z.object({ status: z.literal("loading") })
|
||||
]);
|
||||
|
||||
// Event types
|
||||
const event = z.discriminatedUnion('type', [
|
||||
z.object({ type: z.literal('click'), x: z.number(), y: z.number() }),
|
||||
z.object({ type: z.literal('keypress'), key: z.string() }),
|
||||
z.object({ type: z.literal('scroll'), delta: z.number() }),
|
||||
])
|
||||
const event = z.discriminatedUnion("type", [
|
||||
z.object({ type: z.literal("click"), x: z.number(), y: z.number() }),
|
||||
z.object({ type: z.literal("keypress"), key: z.string() }),
|
||||
z.object({ type: z.literal("scroll"), delta: z.number() })
|
||||
]);
|
||||
|
||||
// Database records with polymorphic types
|
||||
const notification = z.discriminatedUnion('channel', [
|
||||
z.object({ channel: z.literal('email'), address: z.string().email() }),
|
||||
z.object({ channel: z.literal('sms'), phoneNumber: z.string() }),
|
||||
z.object({ channel: z.literal('push'), deviceToken: z.string() }),
|
||||
])
|
||||
const notification = z.discriminatedUnion("channel", [
|
||||
z.object({ channel: z.literal("email"), address: z.string().email() }),
|
||||
z.object({ channel: z.literal("sms"), phoneNumber: z.string() }),
|
||||
z.object({ channel: z.literal("push"), deviceToken: z.string() })
|
||||
]);
|
||||
```
|
||||
|
||||
**Type-safe handling:**
|
||||
|
||||
```typescript
|
||||
const paymentSchema = z.discriminatedUnion('method', [
|
||||
const paymentSchema = z.discriminatedUnion("method", [
|
||||
z.object({
|
||||
method: z.literal('card'),
|
||||
method: z.literal("card"),
|
||||
cardNumber: z.string(),
|
||||
expiryDate: z.string(),
|
||||
expiryDate: z.string()
|
||||
}),
|
||||
z.object({
|
||||
method: z.literal('bank'),
|
||||
method: z.literal("bank"),
|
||||
accountNumber: z.string(),
|
||||
routingNumber: z.string(),
|
||||
routingNumber: z.string()
|
||||
}),
|
||||
z.object({
|
||||
method: z.literal('crypto'),
|
||||
walletAddress: z.string(),
|
||||
}),
|
||||
])
|
||||
method: z.literal("crypto"),
|
||||
walletAddress: z.string()
|
||||
})
|
||||
]);
|
||||
|
||||
type Payment = z.infer<typeof paymentSchema>
|
||||
type Payment = z.infer<typeof paymentSchema>;
|
||||
|
||||
function processPayment(payment: Payment) {
|
||||
switch (payment.method) {
|
||||
case 'card':
|
||||
return chargeCard(payment.cardNumber, payment.expiryDate)
|
||||
case 'bank':
|
||||
return initiateBankTransfer(payment.accountNumber, payment.routingNumber)
|
||||
case 'crypto':
|
||||
return sendCrypto(payment.walletAddress)
|
||||
case "card":
|
||||
return chargeCard(payment.cardNumber, payment.expiryDate);
|
||||
case "bank":
|
||||
return initiateBankTransfer(payment.accountNumber, payment.routingNumber);
|
||||
case "crypto":
|
||||
return sendCrypto(payment.walletAddress);
|
||||
// TypeScript exhaustiveness check - no default needed
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- When variants don't share a common discriminator field
|
||||
- When the discriminator isn't a literal type (use regular union)
|
||||
|
||||
|
||||
Reference in new issue
Block a user