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
324 lines
15 KiB
TypeScript
324 lines
15 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { tEn } from "@/test/i18nCatalog";
|
|
import {
|
|
addOption,
|
|
emptySku,
|
|
emptySkuVariant,
|
|
generateVariants,
|
|
parseOptionValues,
|
|
prefillFromQianyi,
|
|
removeOption,
|
|
setHasVariants,
|
|
skuFromView,
|
|
skuPart,
|
|
suggestVariantCode,
|
|
validateSku,
|
|
withOptionLabels,
|
|
type SkuDraft,
|
|
type SkuView,
|
|
} from "./catalogSetup";
|
|
|
|
// Up to three option axes per SKU (add-sku-option-axes).
|
|
|
|
function tee(overrides: Partial<SkuDraft> = {}): SkuDraft {
|
|
return { ...emptySku(), code: "TEE-1", name: "Tee", ...overrides };
|
|
}
|
|
|
|
describe("a new SKU", () => {
|
|
it("starts without options and with one default variant", () => {
|
|
const sku = emptySku();
|
|
expect(sku.optionNames).toEqual([]);
|
|
expect(sku.optionValues).toEqual([]);
|
|
expect(sku.variants).toHaveLength(1);
|
|
expect(sku.variants[0].options).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("setHasVariants", () => {
|
|
it("turning on adds one empty option and keeps the default variant's weight", () => {
|
|
const sku = tee({ variants: [{ ...emptySkuVariant(), weight: "0.3" }] });
|
|
const next = setHasVariants(sku, true);
|
|
expect(next.optionNames).toEqual([""]);
|
|
expect(next.optionValues).toEqual([""]);
|
|
expect(next.variants).toHaveLength(1);
|
|
expect(next.variants[0]).toMatchObject({ options: [""], weight: "0.3" });
|
|
});
|
|
|
|
it("turning off clears the options when one variant is left", () => {
|
|
const sku = tee({ optionNames: ["Colour"], optionValues: ["Red"], variants: [{ ...emptySkuVariant(), options: ["Red"], variant: "Colour", variantName: "Red" }] });
|
|
const next = setHasVariants(sku, false);
|
|
expect(next.optionNames).toEqual([]);
|
|
expect(next.variants[0]).toMatchObject({ options: [], variant: "", variantName: "" });
|
|
});
|
|
|
|
it("turning off with several variants changes nothing", () => {
|
|
const sku = tee({
|
|
optionNames: ["Colour"],
|
|
optionValues: ["Red, Blue"],
|
|
variants: [{ ...emptySkuVariant(), options: ["Red"] }, { ...emptySkuVariant(), options: ["Blue"] }],
|
|
});
|
|
expect(setHasVariants(sku, false)).toBe(sku);
|
|
});
|
|
});
|
|
|
|
describe("addOption / removeOption", () => {
|
|
it("adds up to three options, each with an empty value on every variant", () => {
|
|
let sku = setHasVariants(tee(), true);
|
|
sku = addOption(addOption(sku));
|
|
expect(sku.optionNames).toEqual(["", "", ""]);
|
|
expect(sku.optionValues).toEqual(["", "", ""]);
|
|
expect(sku.variants[0].options).toEqual(["", "", ""]);
|
|
expect(addOption(sku)).toBe(sku);
|
|
});
|
|
|
|
it("removes one option from the names, the values and every variant", () => {
|
|
const sku = tee({
|
|
optionNames: ["Colour", "Size"],
|
|
optionValues: ["Red", "S, M"],
|
|
variants: [{ ...emptySkuVariant(), options: ["Red", "S"] }, { ...emptySkuVariant(), options: ["Red", "M"] }],
|
|
});
|
|
const next = removeOption(sku, 0);
|
|
expect(next.optionNames).toEqual(["Size"]);
|
|
expect(next.optionValues).toEqual(["S, M"]);
|
|
expect(next.variants.map((v) => v.options)).toEqual([["S"], ["M"]]);
|
|
expect(next.variants.map((v) => [v.variant, v.variantName])).toEqual([["Size", "S"], ["Size", "M"]]);
|
|
});
|
|
|
|
it("never removes the last option (the switch does that)", () => {
|
|
const sku = tee({ optionNames: ["Colour"], optionValues: [""], variants: [{ ...emptySkuVariant(), options: [""] }] });
|
|
expect(removeOption(sku, 0)).toBe(sku);
|
|
});
|
|
});
|
|
|
|
describe("parseOptionValues", () => {
|
|
it("splits on commas, trims, drops blanks and repeats ignoring case", () => {
|
|
expect(parseOptionValues(" Red, Blue ,, red,Sky Blue ")).toEqual(["Red", "Blue", "Sky Blue"]);
|
|
});
|
|
});
|
|
|
|
describe("generateVariants", () => {
|
|
it("builds every combination with suggested codes and composed labels", () => {
|
|
const sku = tee({ optionNames: ["Colour", "Size"], optionValues: ["Red, Blue", "S, M"], variants: [{ ...emptySkuVariant(), options: ["", ""] }] });
|
|
const next = generateVariants(sku);
|
|
expect(next.variants.map((v) => v.options)).toEqual([["Red", "S"], ["Red", "M"], ["Blue", "S"], ["Blue", "M"]]);
|
|
expect(next.variants.map((v) => v.code)).toEqual(["TEE-1-RED-S", "TEE-1-RED-M", "TEE-1-BLUE-S", "TEE-1-BLUE-M"]);
|
|
expect(next.variants[0]).toMatchObject({ variant: "Colour / Size", variantName: "Red / S" });
|
|
});
|
|
|
|
it("gives the first combination to a row with no values, keeping its weight", () => {
|
|
const sku = tee({ optionNames: ["Size"], optionValues: ["S, M"], variants: [{ ...emptySkuVariant(), options: [""], weight: "0.3" }] });
|
|
const next = generateVariants(sku);
|
|
expect(next.variants).toHaveLength(2);
|
|
expect(next.variants[0]).toMatchObject({ options: ["S"], weight: "0.3", code: "TEE-1-S" });
|
|
expect(next.variants[1]).toMatchObject({ options: ["M"], weight: "" });
|
|
});
|
|
|
|
it("appends only missing combinations, matching existing rows ignoring case, and never removes a row", () => {
|
|
const red = { ...emptySkuVariant(), id: 5, code: "OLD-RED", options: ["red"] };
|
|
const pink = { ...emptySkuVariant(), id: 6, code: "OLD-PINK", options: ["Pink"] };
|
|
const sku = tee({ optionNames: ["Colour"], optionValues: ["Red, Blue"], variants: [red, pink] });
|
|
const next = generateVariants(sku);
|
|
expect(next.variants.map((v) => v.options)).toEqual([["red"], ["Pink"], ["Blue"]]);
|
|
expect(next.variants[0].code).toBe("OLD-RED");
|
|
});
|
|
|
|
it("refreshes a row's code that is still the earlier suggestion", () => {
|
|
const stale = { ...emptySkuVariant(), options: [""], variant: "Colour", variantName: "", code: "TEE-1" };
|
|
const next = generateVariants(tee({ optionNames: ["Colour"], optionValues: ["Red, Blue"], variants: [stale] }));
|
|
expect(next.variants.map((v) => v.code)).toEqual(["TEE-1-RED", "TEE-1-BLUE"]);
|
|
});
|
|
|
|
it("does nothing while an option has no values", () => {
|
|
const sku = tee({ optionNames: ["Colour", "Size"], optionValues: ["Red", " "], variants: [{ ...emptySkuVariant(), options: ["", ""] }] });
|
|
expect(generateVariants(sku)).toBe(sku);
|
|
});
|
|
});
|
|
|
|
describe("withOptionLabels", () => {
|
|
it("composes the variant label from the names and the row's values", () => {
|
|
const sku = tee({ optionNames: [" Colour", "Size "], variants: [{ ...emptySkuVariant(), options: ["Red", " L"] }] });
|
|
expect(withOptionLabels(sku).variants[0]).toMatchObject({ variant: "Colour / Size", variantName: "Red / L" });
|
|
});
|
|
|
|
it("leaves the default variant unlabelled", () => {
|
|
expect(withOptionLabels(tee()).variants[0]).toMatchObject({ variant: "", variantName: "" });
|
|
});
|
|
});
|
|
|
|
describe("suggestVariantCode", () => {
|
|
it("suggests nothing for a variant with options but no value yet (never the bare SKU code)", () => {
|
|
expect(suggestVariantCode("TEE-1", "Colour", "")).toBe("");
|
|
expect(suggestVariantCode("TEE-1", "Colour / Size", " / ")).toBe("");
|
|
});
|
|
|
|
it("joins the values with dashes, keeping letters and digits", () => {
|
|
expect(suggestVariantCode("TEE-1", "Colour / Size", "Red / L")).toBe("TEE-1-RED-L");
|
|
expect(suggestVariantCode("TEE-1", "Size", "0-3 months")).toBe("TEE-1-0-3-MONTHS");
|
|
expect(suggestVariantCode("TEE-1", "Colour", "红色")).toBe("TEE-1-红色");
|
|
});
|
|
});
|
|
|
|
describe("skuFromView / skuPart", () => {
|
|
const view: SkuView = {
|
|
id: 9,
|
|
code: "TEE-1",
|
|
name: "Tee",
|
|
status: "Y",
|
|
optionNames: ["Colour", "Size"],
|
|
variations: [
|
|
{ id: 70, code: "TEE-1-RED-S", variant: "Colour / Size", variantName: "Red / S", options: ["Red", "S"], weight: 0.2, usedBy: [], onHand: 0 },
|
|
{ id: 71, code: "TEE-1-RED-M", variant: "Colour / Size", variantName: "Red / M", options: ["Red", "M"], weight: 0.2, usedBy: [], onHand: 0 },
|
|
],
|
|
};
|
|
|
|
it("loads the names, each variant's values and the distinct values per option", () => {
|
|
const draft = skuFromView(view);
|
|
expect(draft.optionNames).toEqual(["Colour", "Size"]);
|
|
expect(draft.optionValues).toEqual(["Red", "S, M"]);
|
|
expect(draft.variants.map((v) => v.options)).toEqual([["Red", "S"], ["Red", "M"]]);
|
|
});
|
|
|
|
it("loads a SKU without options", () => {
|
|
const draft = skuFromView({ ...view, optionNames: undefined, variations: [{ ...view.variations[0], variant: "DEFAULT", variantName: "DEFAULT", options: undefined }] });
|
|
expect(draft.optionNames).toEqual([]);
|
|
expect(draft.optionValues).toEqual([]);
|
|
expect(draft.variants[0].options).toEqual([]);
|
|
});
|
|
|
|
it("sends the trimmed names and values", () => {
|
|
const draft = tee({ optionNames: [" Colour "], variants: [{ ...emptySkuVariant(), code: "TEE-1-RED", options: [" Red "], weight: "0.2" }] });
|
|
const part = skuPart(draft);
|
|
expect(part.optionNames).toEqual(["Colour"]);
|
|
expect(part.variations[0].options).toEqual(["Red"]);
|
|
});
|
|
|
|
it("sends no values for the default variant", () => {
|
|
const part = skuPart(tee({ variants: [{ ...emptySkuVariant(), weight: "0.2" }] }));
|
|
expect(part.optionNames).toEqual([]);
|
|
expect(part.variations[0].options).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("validateSku options", () => {
|
|
const twoRows = (a: string[], b: string[]): SkuDraft =>
|
|
tee({
|
|
optionNames: ["Colour", "Size"],
|
|
variants: [
|
|
{ ...emptySkuVariant(), code: "A", options: a, weight: "0.2", lengthCm: "20", widthCm: "10", heightCm: "5" },
|
|
{ ...emptySkuVariant(), code: "B", options: b, weight: "0.2", lengthCm: "20", widthCm: "10", heightCm: "5" },
|
|
],
|
|
});
|
|
|
|
it("needs every option name, once", () => {
|
|
expect(validateSku(tee({ optionNames: ["Colour", " "], variants: [{ ...emptySkuVariant(), code: "A", options: ["Red", "S"], weight: "1" }] }), tEn)["sku.optionNames[1]"]).toBe("Option name is required");
|
|
expect(validateSku(tee({ optionNames: ["Colour", "colour"], variants: [{ ...emptySkuVariant(), code: "A", options: ["Red", "S"], weight: "1" }] }), tEn)["sku.optionNames[1]"]).toBe("Option name is repeated");
|
|
});
|
|
|
|
it("needs every value and a unique combination ignoring case", () => {
|
|
expect(validateSku(twoRows(["Red", "S"], ["Blue", ""]), tEn)["sku.variations[1].options"]).toBe("Every option needs a value");
|
|
expect(validateSku(twoRows(["Red", "S"], ["red", "s"]), tEn)["sku.variations[1].options"]).toBe("This combination is repeated");
|
|
expect(validateSku(twoRows(["Red", "S"], ["Red", "M"]), tEn)).toEqual({});
|
|
});
|
|
|
|
it("needs a code on a variant with options, but the default variant takes the SKU code", () => {
|
|
expect(validateSku(tee({ optionNames: ["Colour"], variants: [{ ...emptySkuVariant(), options: ["Red"], weight: "1", lengthCm: "20", widthCm: "10", heightCm: "5" }] }), tEn)["sku.variations[0].code"]).toBe("Variant code is required");
|
|
expect(validateSku(tee({ variants: [{ ...emptySkuVariant(), weight: "1", lengthCm: "20", widthCm: "10", heightCm: "5" }] }), tEn)).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("prefillFromQianyi by option name", () => {
|
|
const option = { sku: "0626400016351", title: "HEY STRIPE", weightKg: 0.245, color: "#BLU010", size: "35", linkedTo: null };
|
|
|
|
it("fills the Colour and Size values, the code and the weight of an unsaved row", () => {
|
|
const sku = tee({ optionNames: ["Colour", "Size"], variants: [{ ...emptySkuVariant(), options: ["", ""] }] });
|
|
const next = prefillFromQianyi(sku, 0, option);
|
|
expect(next.variants[0]).toMatchObject({
|
|
code: "0626400016351",
|
|
weight: "0.245",
|
|
options: ["#BLU010", "35"],
|
|
variant: "Colour / Size",
|
|
variantName: "#BLU010 / 35",
|
|
qySku: "0626400016351",
|
|
});
|
|
expect(Object.keys(next.variants[0].qyFilled ?? {})).toEqual(["code", "weight", "variant"]);
|
|
});
|
|
|
|
it("keeps typed values, fills only options QianYi knows, and leaves Material alone", () => {
|
|
const sku = tee({ optionNames: ["Color", "Material"], variants: [{ ...emptySkuVariant(), options: ["Pink", ""] }] });
|
|
const next = prefillFromQianyi(sku, 0, option);
|
|
expect(next.variants[0].options).toEqual(["Pink", ""]);
|
|
});
|
|
|
|
it("refills a value an earlier QianYi pick put there", () => {
|
|
const sku = tee({ optionNames: ["Colour"], variants: [{ ...emptySkuVariant(), options: [""] }] });
|
|
const first = prefillFromQianyi(sku, 0, option);
|
|
const second = prefillFromQianyi(first, 0, { ...option, sku: "B", color: "#RED001" });
|
|
expect(second.variants[0].options).toEqual(["#RED001"]);
|
|
});
|
|
|
|
it.each(["!!!", "\u2605"])("refuses a QianYi colour %s with no letter or number", (color) => {
|
|
const sku = tee({ optionNames: ["Colour"], variants: [{ ...emptySkuVariant(), options: [""] }] });
|
|
const next = prefillFromQianyi(sku, 0, { ...option, color });
|
|
expect(next.variants[0].options).toEqual([""]);
|
|
expect(next.variants[0].qyFilled?.variant).toBeUndefined();
|
|
});
|
|
|
|
it("still fills a QianYi colour that has a letter", () => {
|
|
const sku = tee({ optionNames: ["Colour"], variants: [{ ...emptySkuVariant(), options: [""] }] });
|
|
const next = prefillFromQianyi(sku, 0, { ...option, color: "Red" });
|
|
expect(next.variants[0].options).toEqual(["Red"]);
|
|
expect(next.variants[0].qyFilled?.variant).toBe("Red");
|
|
});
|
|
|
|
describe("a re-pick with a QianYi colour that counts as no value", () => {
|
|
const colourOnly = () => tee({ optionNames: ["Colour"], variants: [{ ...emptySkuVariant(), options: [""] }] });
|
|
|
|
it("clears a value an earlier QianYi pick put there when the colour has no letter or number", () => {
|
|
const first = prefillFromQianyi(colourOnly(), 0, { ...option, color: "Red" });
|
|
expect(first.variants[0].options).toEqual(["Red"]);
|
|
const second = prefillFromQianyi(first, 0, { ...option, sku: "B", color: "★" });
|
|
expect(second.variants[0].options).toEqual([""]);
|
|
});
|
|
|
|
it("clears a value an earlier QianYi pick put there when the colour is empty", () => {
|
|
const first = prefillFromQianyi(colourOnly(), 0, { ...option, color: "Red" });
|
|
const second = prefillFromQianyi(first, 0, { ...option, sku: "B", color: "" });
|
|
expect(second.variants[0].options).toEqual([""]);
|
|
});
|
|
|
|
it("clears a value an earlier QianYi pick put there when the colour is null", () => {
|
|
const first = prefillFromQianyi(colourOnly(), 0, { ...option, color: "Red" });
|
|
const second = prefillFromQianyi(first, 0, { ...option, sku: "B", color: null });
|
|
expect(second.variants[0].options).toEqual([""]);
|
|
});
|
|
|
|
it("never clears a value the admin typed", () => {
|
|
const typed = tee({ optionNames: ["Colour"], variants: [{ ...emptySkuVariant(), options: ["Blue"] }] });
|
|
const next = prefillFromQianyi(typed, 0, { ...option, color: "★" });
|
|
expect(next.variants[0].options).toEqual(["Blue"]);
|
|
});
|
|
});
|
|
|
|
it("refuses only the symbol-only axis and fills the other", () => {
|
|
const sku = tee({ optionNames: ["Colour", "Size"], variants: [{ ...emptySkuVariant(), options: ["", ""] }] });
|
|
const next = prefillFromQianyi(sku, 0, { ...option, color: "\u2605", size: "M" });
|
|
expect(next.variants[0].options).toEqual(["", "M"]);
|
|
});
|
|
|
|
it("never gives a SKU without options any, and keeps the default variant's code blank", () => {
|
|
const next = prefillFromQianyi(tee(), 0, option);
|
|
expect(next.optionNames).toEqual([]);
|
|
expect(next.variants[0]).toMatchObject({ options: [], code: "", weight: "0.245", variant: "", variantName: "" });
|
|
});
|
|
|
|
it("gives a new SKU without options and without a code the QianYi code as its SKU code", () => {
|
|
const next = prefillFromQianyi(tee({ code: "" }), 0, option);
|
|
expect(next.code).toBe("0626400016351");
|
|
expect(next.variants[0].code).toBe("");
|
|
expect(Object.keys(next.variants[0].qyFilled ?? {})).toEqual(["code", "weight"]);
|
|
// a typed SKU code and a saved SKU's code are kept
|
|
expect(prefillFromQianyi(tee(), 0, option).code).toBe("TEE-1");
|
|
expect(prefillFromQianyi(tee({ id: 9, code: "" }), 0, option).code).toBe("");
|
|
});
|
|
});
|