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
82 lines
3.9 KiB
TypeScript
82 lines
3.9 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import { resolveMessage } from "@/app/lib/i18n/t";
|
|
import { insufficientStockMessage, type InsufficientStockLine } from "./insufficientStock";
|
|
|
|
// Resolves through the built-in English defaults, as useT() does outside a provider.
|
|
const t = (code: string, params?: Record<string, string | number>) => resolveMessage({}, code, params);
|
|
|
|
const LINES: InsufficientStockLine[] = [
|
|
{
|
|
skuVariationId: "7",
|
|
productLabel: "Baby Milk (P001)",
|
|
variations: [{ skuVariationId: "7", variationId: "V-7", label: "100g" }],
|
|
},
|
|
{
|
|
skuVariationId: "50",
|
|
productLabel: "Momcozy Washing Block",
|
|
variations: [
|
|
{ skuVariationId: "49", variationId: "V-49", label: "LARGE" },
|
|
{ skuVariationId: "50", variationId: "V-50", label: "DEFAULT" },
|
|
],
|
|
},
|
|
];
|
|
|
|
function rejection(details?: unknown, code = "INSUFFICIENT_STOCK") {
|
|
return new ApiError("Insufficient stock for sku_variation 50: available 0, requested 10, shortfall 10", 400, {
|
|
error: "Insufficient stock for sku_variation 50: available 0, requested 10, shortfall 10",
|
|
code,
|
|
...(details === undefined ? {} : { details }),
|
|
});
|
|
}
|
|
|
|
describe("insufficientStockMessage", () => {
|
|
it("names the matching line's product and variation with the numbers", () => {
|
|
const msg = insufficientStockMessage(rejection({ skuVariationId: 50, available: 0, requested: 10 }), LINES, t);
|
|
expect(msg).toBe("Not enough stock for Momcozy Washing Block — DEFAULT (0 available, 10 requested).");
|
|
expect(msg).not.toMatch(/sku_variation/);
|
|
});
|
|
|
|
it("formats numbers without locale grouping", () => {
|
|
const msg = insufficientStockMessage(rejection({ skuVariationId: 7, available: 1200, requested: 15000 }), LINES, t);
|
|
expect(msg).toBe("Not enough stock for Baby Milk (P001) — 100g (1200 available, 15000 requested).");
|
|
});
|
|
|
|
it("keeps a variation label that contains a colon intact", () => {
|
|
const lines: InsufficientStockLine[] = [
|
|
{
|
|
skuVariationId: "50",
|
|
productLabel: "Momcozy Washing Block",
|
|
variations: [{ skuVariationId: "50", variationId: "V-50", label: "Color: Black" }],
|
|
},
|
|
];
|
|
const msg = insufficientStockMessage(rejection({ skuVariationId: 50, available: 0, requested: 10 }), lines, t);
|
|
expect(msg).toBe("Not enough stock for Momcozy Washing Block — Color: Black (0 available, 10 requested).");
|
|
});
|
|
|
|
it("uses the product label alone when the line has no variation label", () => {
|
|
const lines: InsufficientStockLine[] = [{ skuVariationId: "50", productLabel: "Momcozy Washing Block", variations: [] }];
|
|
const msg = insufficientStockMessage(rejection({ skuVariationId: 50, available: 0, requested: 10 }), lines, t);
|
|
expect(msg).toBe("Not enough stock for Momcozy Washing Block (0 available, 10 requested).");
|
|
});
|
|
|
|
it("falls back to the generic text when no submitted line matches", () => {
|
|
const msg = insufficientStockMessage(rejection({ skuVariationId: 999, available: 0, requested: 10 }), LINES, t);
|
|
expect(msg).toBe("Not enough stock for one of the lines.");
|
|
});
|
|
|
|
it("falls back to the generic text when the details are missing or incomplete", () => {
|
|
expect(insufficientStockMessage(rejection(), LINES, t)).toBe("Not enough stock for one of the lines.");
|
|
expect(insufficientStockMessage(rejection({ skuVariationId: 50 }), LINES, t)).toBe(
|
|
"Not enough stock for one of the lines.",
|
|
);
|
|
});
|
|
|
|
it("returns null for an error without the INSUFFICIENT_STOCK code", () => {
|
|
expect(insufficientStockMessage(rejection({ skuVariationId: 50, available: 0, requested: 10 }, "OTHER"), LINES, t)).toBeNull();
|
|
expect(insufficientStockMessage(new ApiError("boom", 400, { error: "boom" }), LINES, t)).toBeNull();
|
|
expect(insufficientStockMessage(new Error("plain"), LINES, t)).toBeNull();
|
|
expect(insufficientStockMessage("nope", LINES, t)).toBeNull();
|
|
});
|
|
});
|