Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
80 lines
2.8 KiB
TypeScript
80 lines
2.8 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { errorResponse } from "./apiRoute";
|
|
import type { ApiResult } from "@/app/lib/types";
|
|
|
|
function failure(overrides: Partial<Extract<ApiResult<unknown>, { ok: false }>>): Extract<
|
|
ApiResult<unknown>,
|
|
{ ok: false }
|
|
> {
|
|
return { ok: false, error: "boom", ...overrides };
|
|
}
|
|
|
|
describe("errorResponse", () => {
|
|
it("returns 401 when the result is marked expired, regardless of status", async () => {
|
|
const res = errorResponse(failure({ expired: true, status: 404 }));
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("forwards a genuine 404 upstream status", async () => {
|
|
const res = errorResponse(failure({ status: 404 }));
|
|
expect(res.status).toBe(404);
|
|
expect((await res.json()).error).toBe("boom");
|
|
});
|
|
|
|
it("forwards a genuine 403 upstream status", async () => {
|
|
const res = errorResponse(failure({ status: 403 }));
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("forwards an upstream machine-readable code", async () => {
|
|
const res = errorResponse(failure({ status: 403, code: "PASSWORD_RESET_REQUIRED" }));
|
|
expect(await res.json()).toEqual({ error: "boom", code: "PASSWORD_RESET_REQUIRED" });
|
|
});
|
|
|
|
it("forwards a genuine 409 upstream status", async () => {
|
|
const res = errorResponse(failure({ status: 409 }));
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it("falls back to 400 when the upstream status is a 5xx", async () => {
|
|
const res = errorResponse(failure({ status: 500 }));
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("falls back to 400 when no upstream status is present", async () => {
|
|
const res = errorResponse(failure({}));
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
describe("errorResponse — error list (add-shipping-rates)", () => {
|
|
it("includes the error list when the result carries one", async () => {
|
|
const res = errorResponse({ ok: false, status: 400, error: "Rates not saved", errors: ["Row 3: gap after 1 kg"] });
|
|
expect(res.status).toBe(400);
|
|
expect(await res.json()).toEqual({ error: "Rates not saved", errors: ["Row 3: gap after 1 kg"] });
|
|
});
|
|
});
|
|
|
|
describe("errorResponse — coded details (BABYBOO-39)", () => {
|
|
it("includes the details with the code when the result carries them", async () => {
|
|
const res = errorResponse({
|
|
ok: false,
|
|
status: 400,
|
|
error: "Insufficient stock",
|
|
code: "INSUFFICIENT_STOCK",
|
|
details: { skuVariationId: 50, available: 0, requested: 10 },
|
|
});
|
|
expect(res.status).toBe(400);
|
|
expect(await res.json()).toEqual({
|
|
error: "Insufficient stock",
|
|
code: "INSUFFICIENT_STOCK",
|
|
details: { skuVariationId: 50, available: 0, requested: 10 },
|
|
});
|
|
});
|
|
|
|
it("adds no details key when the result carries none", async () => {
|
|
const res = errorResponse({ ok: false, status: 400, error: "boom" });
|
|
expect(await res.json()).not.toHaveProperty("details");
|
|
});
|
|
});
|