mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
Six touch-disjoint waves landed on develop before this release: - W1-KNIP — exports/types tiers flipped to enforcing-mode on main; 35 unused exports + 52 unused types triaged to 0/0; shadcn surface + zod-infer contract types scoped via knip.json ignore-block. - W2-ZOD-MULTI-ISSUE — `returnAllZodIssues` shared helper + `/api/dashboard/widgets` 422 now returns every Zod issue + worker audit-ledger breadcrumb. 41 sibling routes documented for v1.4.43. - W3-QUERYKEY-LONGTAIL — 40 settings/medications/admin/hooks files migrated to the queryKeys factory; 9 new factory entries; ESLint rule + test-guard scope extended in lockstep. - W4-ARCH-HYGIENE — BERLIN_DAY_FORMATTER consolidated across nine sites (seven status helpers + bucket-series + bp-in-target); Suspense double-comment consolidated; doctor-report sanitiser regex rewritten to escape-form so diffs are reviewable; pr-detection- worker soft-delete filter; offhost-backup DR-intent comment. - W5-IOS-WORKOUTS — `dedupeWorkoutBatch` write-time cross-source dedup helper for the v1.5 iOS HealthKit observer ingest; dashboard tile-strip placeholder parity with the live TrendCard chrome. - W6-WITHINGS-OFF + ENV-CHECK — Withings off-response classifier (transient/reauth/persistent) + WithingsApiError typed throw + the `pnpm check-env` CLI. APNs `allOrNone: true` closes the AP-2 silent-disable gap the wave was conceived to close. No migration, no env-var change, no API contract break for iOS v0.5.4. The new `/api/dashboard/widgets` 422 envelope is additively extended. pnpm test --run green at 4815 passing / 1 skipped. pnpm typecheck, pnpm lint, pnpm knip (no --include flag) green.
127 lines
4.5 KiB
TypeScript
127 lines
4.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { z } from "zod/v4";
|
|
import {
|
|
returnAllZodIssues,
|
|
sanitiseZodIssues,
|
|
} from "../api-response";
|
|
|
|
// v1.4.42 W2 — every Zod-validated route historically returned
|
|
// `parsed.error.issues[0].message`, dropping every issue past the first.
|
|
// `returnAllZodIssues` is the additive replacement: clients that only
|
|
// read `error` keep working, new callers branch on `details.issues`.
|
|
|
|
const schema = z.object({
|
|
version: z.literal(1),
|
|
widgets: z.array(z.object({ id: z.string() })).min(1),
|
|
comparisonBaseline: z.enum(["none", "lastMonth", "lastYear"]).optional(),
|
|
});
|
|
|
|
describe("returnAllZodIssues", () => {
|
|
it("returns 422 by default with a sanitised issue list", async () => {
|
|
const parsed = schema.safeParse({});
|
|
expect(parsed.success).toBe(false);
|
|
if (parsed.success) return;
|
|
|
|
const response = returnAllZodIssues(parsed.error);
|
|
expect(response.status).toBe(422);
|
|
|
|
const body = (await response.json()) as {
|
|
data: null;
|
|
error: string;
|
|
details: { issues: Array<{ path: string; code: string; message: string }> };
|
|
};
|
|
expect(body.data).toBeNull();
|
|
expect(body.error).toBe("Validation failed");
|
|
expect(body.details.issues.length).toBeGreaterThanOrEqual(2);
|
|
expect(body.details.issues[0]).toMatchObject({
|
|
code: expect.any(String),
|
|
message: expect.any(String),
|
|
path: expect.any(String),
|
|
});
|
|
});
|
|
|
|
it("surfaces TWO simultaneous validation errors", async () => {
|
|
const parsed = schema.safeParse({ version: 2, widgets: [] });
|
|
expect(parsed.success).toBe(false);
|
|
if (parsed.success) return;
|
|
|
|
const response = returnAllZodIssues(parsed.error);
|
|
const body = (await response.json()) as {
|
|
details: { issues: Array<{ path: string; code: string }> };
|
|
};
|
|
expect(body.details.issues.length).toBe(2);
|
|
const paths = body.details.issues.map((i) => i.path).sort();
|
|
expect(paths).toEqual(["version", "widgets"]);
|
|
});
|
|
|
|
it("surfaces THREE simultaneous validation errors", async () => {
|
|
const parsed = schema.safeParse({
|
|
version: 2,
|
|
widgets: [],
|
|
comparisonBaseline: "yesterday",
|
|
});
|
|
expect(parsed.success).toBe(false);
|
|
if (parsed.success) return;
|
|
|
|
const response = returnAllZodIssues(parsed.error);
|
|
const body = (await response.json()) as {
|
|
details: { issues: Array<{ path: string; code: string }> };
|
|
};
|
|
expect(body.details.issues.length).toBe(3);
|
|
const paths = body.details.issues.map((i) => i.path).sort();
|
|
expect(paths).toEqual(["comparisonBaseline", "version", "widgets"]);
|
|
});
|
|
|
|
it("flattens nested paths with dots", async () => {
|
|
const nested = z.object({ outer: z.object({ inner: z.string() }) });
|
|
const parsed = nested.safeParse({ outer: { inner: 5 } });
|
|
if (parsed.success) throw new Error("expected failure");
|
|
|
|
const sanitised = sanitiseZodIssues(parsed.error.issues);
|
|
expect(sanitised[0].path).toBe("outer.inner");
|
|
});
|
|
|
|
it("does NOT echo issue.params (privacy: may contain user input)", async () => {
|
|
const parsed = schema.safeParse({
|
|
version: 99,
|
|
widgets: [{ id: "x" }],
|
|
});
|
|
if (parsed.success) throw new Error("expected failure");
|
|
|
|
const response = returnAllZodIssues(parsed.error);
|
|
const body = (await response.json()) as {
|
|
details: { issues: Array<Record<string, unknown>> };
|
|
};
|
|
for (const issue of body.details.issues) {
|
|
expect(issue).not.toHaveProperty("params");
|
|
// Only path / code / message survive the sanitiser.
|
|
expect(Object.keys(issue).sort()).toEqual(["code", "message", "path"]);
|
|
}
|
|
});
|
|
|
|
it("respects a custom status code", async () => {
|
|
const parsed = schema.safeParse({});
|
|
if (parsed.success) throw new Error("expected failure");
|
|
const response = returnAllZodIssues(parsed.error, 400);
|
|
expect(response.status).toBe(400);
|
|
});
|
|
|
|
it("forwards meta.errorCode and headers like apiError", async () => {
|
|
const parsed = schema.safeParse({});
|
|
if (parsed.success) throw new Error("expected failure");
|
|
|
|
const response = returnAllZodIssues(parsed.error, 422, {
|
|
errorCode: "dashboard.widgets.invalid",
|
|
headers: { "X-Test": "yes" },
|
|
});
|
|
expect(response.headers.get("X-Test")).toBe("yes");
|
|
const body = (await response.json()) as {
|
|
meta?: { errorCode?: string; headers?: unknown };
|
|
};
|
|
expect(body.meta?.errorCode).toBe("dashboard.widgets.invalid");
|
|
// Headers are consumed by the NextResponse constructor and stripped
|
|
// from the JSON envelope, matching apiError's contract.
|
|
expect(body.meta?.headers).toBeUndefined();
|
|
});
|
|
});
|