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,13 +12,13 @@ Hardcoded error messages in English exclude users who speak other languages. Use
|
||||
**Incorrect (hardcoded English messages):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const userSchema = z.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
email: z.string().email('Invalid email address'),
|
||||
age: z.number().min(18, 'You must be at least 18 years old'),
|
||||
})
|
||||
name: z.string().min(1, "Name is required"),
|
||||
email: z.string().email("Invalid email address"),
|
||||
age: z.number().min(18, "You must be at least 18 years old")
|
||||
});
|
||||
|
||||
// French users see English errors - poor UX
|
||||
```
|
||||
@@ -26,77 +26,74 @@ const userSchema = z.object({
|
||||
**Correct (localized error messages):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
// Translation dictionaries
|
||||
const translations = {
|
||||
en: {
|
||||
required: 'This field is required',
|
||||
invalidEmail: 'Please enter a valid email address',
|
||||
required: "This field is required",
|
||||
invalidEmail: "Please enter a valid email address",
|
||||
tooShort: (min: number) => `Must be at least ${min} characters`,
|
||||
tooYoung: (min: number) => `You must be at least ${min} years old`,
|
||||
tooYoung: (min: number) => `You must be at least ${min} years old`
|
||||
},
|
||||
fr: {
|
||||
required: 'Ce champ est obligatoire',
|
||||
invalidEmail: 'Veuillez entrer une adresse email valide',
|
||||
required: "Ce champ est obligatoire",
|
||||
invalidEmail: "Veuillez entrer une adresse email valide",
|
||||
tooShort: (min: number) => `Doit contenir au moins ${min} caractères`,
|
||||
tooYoung: (min: number) => `Vous devez avoir au moins ${min} ans`,
|
||||
tooYoung: (min: number) => `Vous devez avoir au moins ${min} ans`
|
||||
},
|
||||
es: {
|
||||
required: 'Este campo es requerido',
|
||||
invalidEmail: 'Por favor ingrese un correo electrónico válido',
|
||||
required: "Este campo es requerido",
|
||||
invalidEmail: "Por favor ingrese un correo electrónico válido",
|
||||
tooShort: (min: number) => `Debe tener al menos ${min} caracteres`,
|
||||
tooYoung: (min: number) => `Debes tener al menos ${min} años`,
|
||||
},
|
||||
} as const
|
||||
tooYoung: (min: number) => `Debes tener al menos ${min} años`
|
||||
}
|
||||
} as const;
|
||||
|
||||
type Locale = keyof typeof translations
|
||||
type Locale = keyof typeof translations;
|
||||
|
||||
function createErrorMap(locale: Locale): z.ZodErrorMap {
|
||||
const t = translations[locale]
|
||||
const t = translations[locale];
|
||||
|
||||
return (issue, ctx) => {
|
||||
switch (issue.code) {
|
||||
case z.ZodIssueCode.invalid_type:
|
||||
if (issue.received === 'undefined') {
|
||||
return { message: t.required }
|
||||
if (issue.received === "undefined") {
|
||||
return { message: t.required };
|
||||
}
|
||||
break
|
||||
break;
|
||||
|
||||
case z.ZodIssueCode.invalid_string:
|
||||
if (issue.validation === 'email') {
|
||||
return { message: t.invalidEmail }
|
||||
if (issue.validation === "email") {
|
||||
return { message: t.invalidEmail };
|
||||
}
|
||||
break
|
||||
break;
|
||||
|
||||
case z.ZodIssueCode.too_small:
|
||||
if (issue.type === 'string') {
|
||||
return { message: t.tooShort(issue.minimum as number) }
|
||||
if (issue.type === "string") {
|
||||
return { message: t.tooShort(issue.minimum as number) };
|
||||
}
|
||||
if (issue.type === 'number') {
|
||||
return { message: t.tooYoung(issue.minimum as number) }
|
||||
if (issue.type === "number") {
|
||||
return { message: t.tooYoung(issue.minimum as number) };
|
||||
}
|
||||
break
|
||||
break;
|
||||
}
|
||||
|
||||
return { message: ctx.defaultError }
|
||||
}
|
||||
return { message: ctx.defaultError };
|
||||
};
|
||||
}
|
||||
|
||||
// Usage with user's locale
|
||||
const userLocale: Locale = 'fr'
|
||||
const errorMap = createErrorMap(userLocale)
|
||||
const userLocale: Locale = "fr";
|
||||
const errorMap = createErrorMap(userLocale);
|
||||
|
||||
const userSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
email: z.string().email(),
|
||||
age: z.number().min(18),
|
||||
})
|
||||
age: z.number().min(18)
|
||||
});
|
||||
|
||||
const result = userSchema.safeParse(
|
||||
{ name: '', email: 'bad', age: 15 },
|
||||
{ errorMap }
|
||||
)
|
||||
const result = userSchema.safeParse({ name: "", email: "bad", age: 15 }, { errorMap });
|
||||
|
||||
// French error messages:
|
||||
// - "Ce champ est obligatoire"
|
||||
@@ -108,8 +105,8 @@ const result = userSchema.safeParse(
|
||||
|
||||
```typescript
|
||||
// At application startup
|
||||
const userLocale = getUserLocale() // From cookie, header, etc.
|
||||
z.setErrorMap(createErrorMap(userLocale))
|
||||
const userLocale = getUserLocale(); // From cookie, header, etc.
|
||||
z.setErrorMap(createErrorMap(userLocale));
|
||||
|
||||
// All schemas now use localized messages
|
||||
```
|
||||
@@ -117,28 +114,26 @@ z.setErrorMap(createErrorMap(userLocale))
|
||||
**With i18n libraries (react-intl, i18next):**
|
||||
|
||||
```typescript
|
||||
import { useIntl } from 'react-intl'
|
||||
import { useIntl } from "react-intl";
|
||||
|
||||
function useZodErrorMap() {
|
||||
const intl = useIntl()
|
||||
const intl = useIntl();
|
||||
|
||||
return (issue: z.ZodIssue, ctx: z.ErrorMapCtx) => {
|
||||
switch (issue.code) {
|
||||
case z.ZodIssueCode.too_small:
|
||||
return {
|
||||
message: intl.formatMessage(
|
||||
{ id: 'validation.tooShort' },
|
||||
{ min: issue.minimum }
|
||||
)
|
||||
}
|
||||
message: intl.formatMessage({ id: "validation.tooShort" }, { min: issue.minimum })
|
||||
};
|
||||
// ...
|
||||
}
|
||||
return { message: ctx.defaultError }
|
||||
}
|
||||
return { message: ctx.defaultError };
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- Internal tools used only by your team
|
||||
- Single-language applications
|
||||
|
||||
|
||||
Reference in new issue
Block a user