import { describe, expect, it } from "vitest"; import { tEn } from "@/test/i18nCatalog"; import { emptyPrice, emptySalesItem, emptySku, emptySkuVariant, errorsUnder, findPriceOverlaps, isDefaultVariant, parseServerError, pricingPart, salesItemFromView, salesItemPart, savedVariationKey, skuFromView, skuPart, suggestVariantCode, prefillFromQianyi, validatePrices, validateSalesItem, validateSku, type PriceDraft, type SalesItemDraft, type SalesItemView, type SkuDraft, type SkuVariantView, } from "./catalogSetup"; function sku(overrides: Partial = {}): SkuDraft { return { ...emptySku(), code: "BA-010", name: "Romper", optionNames: ["Size"], optionValues: ["S"], variants: [{ ...emptySkuVariant(), variant: "Size", variantName: "S", options: ["S"], code: "BA-010-S", weight: "0.2" }], ...overrides, }; } function salesItem(overrides: Partial = {}): SalesItemDraft { return { ...emptySalesItem(), code: "P-010", name: "Romper single", brand: "BABYLON", category: "APPAREL", images: [{ url: "https://cdn/a.jpg", sortOrder: 0 }], variations: [ { key: "k1", variant: "", variantName: "", components: [{ skuVariationCode: "BA-010-S", label: "BA-010-S", qty: "1", qtyFoc: "0", qyTouched: false }], }, ], ...overrides, }; } const TODAY = "2026-10-21"; function price(overrides: Partial = {}): PriceDraft { return { ...emptyPrice("k1"), priceType: "MEMBER", price: "59.90", ...overrides }; } describe("variant codes", () => { it("suggests SKU code plus the upper-cased name", () => { expect(suggestVariantCode("BA-002", "Colour", "Jet black")).toBe("BA-002-JET-BLACK"); }); it("gives a default variant the SKU code", () => { expect(isDefaultVariant("", " ")).toBe(true); expect(isDefaultVariant("DEFAULT", "DEFAULT")).toBe(true); expect(suggestVariantCode("BA-001", "", "")).toBe("BA-001"); }); }); describe("request parts", () => { it("sends a new SKU with numbers and no QianYi field until the admin touches it", () => { const part = skuPart(sku()); expect(part).toEqual({ code: "BA-010", name: "Romper", status: "Y", supplierId: null, // always sent: null clears the supplier (add-sku-supplier) optionNames: ["Size"], variations: [{ code: "BA-010-S", variant: "Size", variantName: "S", options: ["S"], weight: 0.2, lengthCm: null, widthCm: null, heightCm: null }], }); }); it("sends a cleared QianYi link as an empty string and keeps saved codes out", () => { const part = skuPart( sku({ id: 7, variants: [{ ...emptySkuVariant(), id: 71, code: "BA-010-S", variant: "Size", variantName: "S", options: ["S"], weight: "0.2", qyTouched: true }], }), ); expect(part.id).toBe(7); expect(part.code).toBeUndefined(); expect(part.variations[0]).toEqual({ id: 71, variant: "Size", variantName: "S", options: ["S"], weight: 0.2, lengthCm: null, widthCm: null, heightCm: null, qySku: "" }); }); it("sends Sales Item variations with their keys and numeric quantities", () => { const part = salesItemPart(salesItem({ compareAtPrice: "79.90" })); expect(part.productType).toBe("STANDARD"); expect(part.compareAtPrice).toBe(79.9); expect(part.variations).toEqual([ { key: "k1", variant: "", variantName: "", components: [{ skuVariationCode: "BA-010-S", qty: 1, qtyFoc: 0 }] }, ]); }); it("sends a group with its component ids and no variations", () => { const part = salesItemPart( salesItem({ isGroup: true, groupPickCount: 2, groupComponents: [{ componentProductId: "5" }, { componentProductId: "6" }] }), ); expect(part.productType).toBe("GROUP"); expect(part.groupComponentProductIds).toEqual([5, 6]); expect(part.variations).toEqual([]); }); it("names a saved variation by id and a new one by key", () => { const item = salesItem({ id: 26, variations: [ { id: 31, key: savedVariationKey(31), variant: "Colour", variantName: "red", components: [] }, { key: "k2", variant: "Colour", variantName: "blue", components: [] }, ], }); const part = pricingPart( [price({ id: 51, variationKey: "v31" }), price({ variationKey: "k2", country: "", effectiveFrom: "2026-12-01" })], item, ); expect(part.salesItemId).toBe(26); expect(part.rows[0]).toEqual({ id: 51, variationId: 31, currency: "MYR", priceType: "MEMBER", price: 59.9, status: "Y" }); expect(part.rows[1]).toEqual({ variationRef: "k2", currency: "MYR", priceType: "MEMBER", price: 59.9, status: "Y", effectiveFrom: "2026-12-01T00:00:00", }); }); it("sends a picked end date as the end of that day", () => { const part = pricingPart([price({ effectiveTo: "2026-12-31" })], salesItem()); expect(part.rows[0].effectiveTo).toBe("2026-12-31T23:59:59"); }); }); describe("views to drafts", () => { it("keeps on-hand unknown when the view does not carry it", () => { const draft = skuFromView({ id: 14, code: "BA-001", name: "Bodysuit", status: "Y", variations: [{ id: 14, code: "BA-001", variant: "DEFAULT", variantName: "DEFAULT", weight: 0.3, usedBy: [] } as unknown as SkuVariantView], }); expect(draft.variants[0].onHand).toBeNull(); }); it("loads a SKU, blanking DEFAULT markers", () => { const draft = skuFromView({ id: 14, code: "BA-001", name: "Bodysuit", status: "Y", variations: [{ id: 14, code: "BA-001", variant: "DEFAULT", variantName: "DEFAULT", weight: 0.3, usedBy: ["P-001"], onHand: 7, qySku: "QY-1", qyStatus: "OK" }], }); expect(draft.variants[0]).toMatchObject({ id: 14, code: "BA-001", variant: "", variantName: "", weight: "0.3", usedBy: ["P-001"], onHand: 7, qySku: "QY-1", qyTouched: false }); }); it("loads a Sales Item with each variation's components and prices", () => { const view: SalesItemView = { id: 26, code: "TEST-001", name: "Test", brand: "B", category: "C", status: "Y", productType: "BUNDLE", groupComponentProductIds: [], images: [{ url: "https://cdn/a.jpg", sortOrder: 0, primary: true }], yellowLabel: false, allowOversell: true, compareAtPrice: 199, variations: [ { id: 31, variant: "Colour", variantName: "red", components: [ { skuVariationId: 28, skuVariationCode: "TEST-001-RED", skuId: 23, skuCode: "TEST-001", skuName: "Test SKU", variant: "Colour", variantName: "red", qty: 1, qtyFoc: 0 }, ], pricing: [{ id: 51, variationId: 31, currency: "MYR", priceType: "MEMBER", price: 140, status: "Y" }], }, ], }; const { salesItem: item, prices } = salesItemFromView(view); expect(item).toMatchObject({ id: 26, isGroup: false, allowOversell: true, compareAtPrice: "199" }); expect(item.variations[0]).toMatchObject({ id: 31, key: "v31", variant: "Colour", variantName: "red" }); expect(item.variations[0].components[0]).toMatchObject({ skuVariationCode: "TEST-001-RED", qty: "1", qtyFoc: "0" }); expect(prices).toEqual([ { id: 51, variationKey: "v31", currency: "MYR", priceType: "MEMBER", country: "", region: "", price: "140", status: "Y", effectiveFrom: "", effectiveTo: "" }, ]); }); it("labels a loaded group's components by name and code, in order", () => { const view: SalesItemView = { id: 66, code: "TEST-GROUP-001", name: "Set", brand: "B", category: "C", status: "Y", productType: "GROUP", groupPickCount: 1, groupComponentProductIds: [61, 60, 7], groupComponents: [ { id: 61, code: "TEST-003-BLUE", name: "TESTING PRODUCT BLUE" }, { id: 60, code: "TEST-002", name: "TESTING PRODUCT" }, { id: 7, code: null, name: "LEGACY NO CODE" }, ], images: [], yellowLabel: false, allowOversell: false, variations: [], }; expect(salesItemFromView(view).salesItem.groupComponents).toEqual([ { componentProductId: "61", label: "TESTING PRODUCT BLUE (TEST-003-BLUE)" }, { componentProductId: "60", label: "TESTING PRODUCT (TEST-002)" }, { componentProductId: "7", label: "LEGACY NO CODE" }, ]); }); it("re-sends a loaded price's own times until the admin picks another date", () => { const view: SalesItemView = { id: 5, code: "LEGACY-1", name: "Legacy", brand: "B", category: "C", status: "Y", productType: "STANDARD", groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false, variations: [ { id: 1, variant: "DEFAULT", variantName: "DEFAULT", components: [], pricing: [{ id: 1, variationId: 1, currency: "MYR", priceType: "MEMBER", price: 10, status: "Y", effectiveFrom: "2024-11-25T08:03", effectiveTo: "2025-11-25T08:03:59" }], }, ], }; const { salesItem: item, prices } = salesItemFromView(view); expect(prices[0]).toMatchObject({ effectiveFrom: "2024-11-25", effectiveTo: "2025-11-25" }); expect(pricingPart(prices, item).rows[0]).toMatchObject({ effectiveFrom: "2024-11-25T08:03", effectiveTo: "2025-11-25T08:03:59", }); const edited = [{ ...prices[0], effectiveTo: "2025-12-31" }]; expect(pricingPart(edited, item).rows[0]).toMatchObject({ effectiveFrom: "2024-11-25T08:03", effectiveTo: "2025-12-31T23:59:59", }); }); it("carries removable and each component's SKU from the load", () => { const comp = (skuVariationId: number, skuId: number) => ({ skuVariationId, skuVariationCode: `V${skuVariationId}`, skuId, skuCode: "S", skuName: "S", variant: "DEFAULT", variantName: "DEFAULT", qty: 1, qtyFoc: 0, }); const base: SalesItemView = { id: 7, code: "P-7", name: "P", brand: "B", category: "C", status: "N", productType: "STANDARD", groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false, variations: [{ id: 71, variant: "Colour", variantName: "Red", removable: false, components: [comp(1, 101)], pricing: [] }], }; const single = salesItemFromView(base).salesItem; expect(single.variations[0].removable).toBe(false); expect(single.variations[0].components[0].skuId).toBe(101); }); it("a variation missing removable from an older load counts as removable", () => { const view = { id: 7, code: "P-7", name: "P", brand: "B", category: "C", status: "N", productType: "STANDARD", groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false, variations: [{ id: 71, variant: "DEFAULT", variantName: "DEFAULT", components: [], pricing: [] }], } as unknown as SalesItemView; expect(salesItemFromView(view).salesItem.variations[0].removable).toBe(true); }); it("carries the QianYi link source of each SKU variant", () => { const draft = skuFromView({ id: 14, code: "BA-001", name: "Bodysuit", status: "Y", variations: [{ id: 14, code: "BA-001", variant: "DEFAULT", variantName: "DEFAULT", weight: 0.3, usedBy: [], onHand: 0, qySku: "QY-1", qyStatus: "OK", qyLinkSource: "AUTO" }], }); expect(draft.variants[0].qyLinkSource).toBe("AUTO"); }); }); describe("client validation", () => { it("reports SKU fields at the backend's paths", () => { const errors = validateSku( sku({ name: " ", variants: [ { ...emptySkuVariant(), variant: "Size", variantName: "", options: [""], code: "X", weight: "0", lengthCm: "20", widthCm: "10", heightCm: "5" }, { ...emptySkuVariant(), variant: "Size", variantName: "M", options: ["M"], code: "X", weight: "1", lengthCm: "20", widthCm: "10", heightCm: "5" }, ], }), tEn, ); expect(Object.keys(errors).sort()).toEqual([ "sku.name", "sku.variations[0].options", "sku.variations[0].weight", "sku.variations[1].code", ]); }); it("reports Sales Item components at the backend's paths", () => { const errors = validateSalesItem( salesItem({ brand: "", status: "Y", // only activation needs an image images: [], variations: [ { key: "k1", variant: "", variantName: "", components: [ { skuVariationCode: "BA-010-S", label: "", qty: "0", qtyFoc: "0", qyTouched: false }, { skuVariationCode: "BA-010-S", label: "", qty: "1", qtyFoc: "0", qyTouched: false }, ], }, ], }), tEn, ); expect(errors["salesItem.brand"]).toBeDefined(); expect(errors["salesItem.images"]).toBeDefined(); expect(errors["salesItem.variations[0].components[0]"]).toBeDefined(); expect(errors["salesItem.variations[0].components[1].skuVariationCode"]).toBeDefined(); }); it("fulfilment defaults to ANY, round-trips from the view, and a Group always saves ANY", () => { expect(emptySalesItem().fulfilment).toBe("ANY"); expect(salesItemPart(emptySalesItem()).fulfilment).toBe("ANY"); const view = { id: 7, code: "P-7", name: "P", brand: "B", category: "C", status: "N", productType: "STANDARD", groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false, fulfilment: "PICKUP_ONLY", variations: [], } as SalesItemView; const draft = salesItemFromView(view).salesItem; expect(draft.fulfilment).toBe("PICKUP_ONLY"); expect(salesItemPart(draft).fulfilment).toBe("PICKUP_ONLY"); expect(salesItemFromView({ ...view, fulfilment: undefined }).salesItem.fulfilment).toBe("ANY"); expect(salesItemPart({ ...draft, isGroup: true }).fulfilment).toBe("ANY"); }); it("always sends the oversell policy, off by default", () => { expect(salesItemPart(emptySalesItem()).allowOversell).toBe(false); }); it("rejects a fractional quantity (the backend's int would silently truncate it)", () => { const errors = validateSalesItem( salesItem({ variations: [ { key: "k1", variant: "", variantName: "", components: [{ skuVariationCode: "BA-010-S", label: "", qty: "1.5", qtyFoc: "0", qyTouched: false }], }, ], }), tEn, ); expect(errors["salesItem.variations[0].components[0]"]).toBe("Quantities must be whole numbers"); }); it("flags the second of two active prices with the same key and no dates", () => { expect(findPriceOverlaps([price(), price({ price: "64.90" })])).toEqual([1]); }); it("allows a scheduled price that does not overlap, another country, or an inactive row", () => { expect(findPriceOverlaps([price({ effectiveTo: "2026-11-30T23:59:59" }), price({ effectiveFrom: "2026-12-01T00:00:00" })])).toEqual([]); expect(findPriceOverlaps([price({ country: "1" }), price()])).toEqual([]); expect(findPriceOverlaps([price({ status: "N" }), price()])).toEqual([]); }); it("reports the duplicate and missing fields at the backend's paths", () => { const errors = validatePrices([price(), price({ priceType: "" }), price()], salesItem(), tEn, TODAY); expect(errors["pricing.rows[1].priceType"]).toBeDefined(); expect(errors["pricing.rows[2]"]).toMatch(/duplicate/i); }); }); describe("BABYBOO-32 setup feedback", () => { it("a new Sales Item is an Inactive draft (redesign-product-setup-stepper replaced \"starts Active\")", () => { expect(emptySalesItem().status).toBe("N"); }); it("the Sales Item has no effective date and never sends one", () => { expect(emptySalesItem()).not.toHaveProperty("effectiveDate"); expect(salesItemPart(salesItem())).not.toHaveProperty("effectiveDate"); }); it("a new price row needs a From date", () => { expect(validatePrices([price({ country: "1" })], salesItem(), tEn, TODAY)).toEqual({ "pricing.rows[0].effectiveFrom": "Effective from is required", }); expect(validatePrices([price({ country: "1", effectiveFrom: "2026-12-01" })], salesItem(), tEn, TODAY)).toEqual({}); }); it("a saved price row without a From date stays saveable", () => { expect(validatePrices([price({ id: 51 })], salesItem(), tEn, TODAY)).toEqual({}); }); }); describe("drafts (redesign-product-setup-stepper)", () => { it("the pricing part leaves out rows of variations this save removes", () => { const item = { ...salesItem(), id: 26, variations: [{ id: 31, key: "v31", variant: "", variantName: "", components: [] }] }; const part = pricingPart([price({ id: 1, variationKey: "v31" }), price({ id: 2, variationKey: "v32" })], item); expect(part.rows.map((r) => r.id)).toEqual([1]); }); it("an Inactive draft needs no variations, an Active item does", () => { const base = { ...salesItem(), code: "P-1", name: "P", brand: "B", category: "C", images: [{ url: "u", sortOrder: 0 }], variations: [] }; expect(validateSalesItem({ ...base, status: "N" }, tEn)["salesItem.variations"]).toBeUndefined(); expect(validateSalesItem({ ...base, status: "Y" }, tEn)["salesItem.variations"]).toBe("Add at least one variation"); }); it("an Inactive Group draft needs no components, an Active Group does", () => { const base = { ...salesItem(), code: "G-1", name: "G", brand: "B", category: "C", images: [{ url: "u", sortOrder: 0 }], isGroup: true, groupComponents: [] }; expect(validateSalesItem({ ...base, status: "N" }, tEn)).toEqual({}); expect(validateSalesItem({ ...base, status: "Y" }, tEn)["salesItem.groupComponentProductIds"]).toBe("Add at least one component"); }); }); describe("server errors", () => { it("splits the field path from the message", () => { expect(parseServerError("sku.variations[2].code: variant code already exists: BA-1")).toEqual({ path: "sku.variations[2].code", message: "variant code already exists: BA-1", }); expect(parseServerError("Request failed (500)")).toEqual({ path: null, message: "Request failed (500)" }); }); it("picks the errors under a prefix", () => { expect(errorsUnder({ "sku.code": "a", "salesItem.name": "b", "sku.variations[0].weight": "c" }, "sku")).toEqual({ "sku.code": "a", "sku.variations[0].weight": "c", }); }); }); describe("prefillFromQianyi", () => { const filledKeys = (draft: SkuDraft, i: number) => Object.keys(draft.variants[i].qyFilled ?? {}); const option = { sku: "0626400016351", title: "HEY STRIPE", weightKg: 0.245, color: "#BLU010", size: "35", linkedTo: null }; const twoVariants = (second: Partial = {}): SkuDraft => ({ ...emptySku(), code: "HEY", optionNames: ["Colour", "Size"], optionValues: ["Red", "S"], variants: [ { ...emptySkuVariant(), id: 1, code: "HEY-RED", variant: "Colour / Size", variantName: "Red / S", options: ["Red", "S"], weight: "0.3" }, { ...emptySkuVariant(), options: ["", ""], ...second }, ], }); it("fills an unsaved variant's empty code, weight and option values, links it and notes what it filled", () => { const next = prefillFromQianyi(twoVariants(), 1, option); expect(next.variants[1]).toMatchObject({ code: "0626400016351", weight: "0.245", options: ["#BLU010", "35"], variant: "Colour / Size", variantName: "#BLU010 / 35", qySku: "0626400016351", qyTitle: "HEY STRIPE", qyStatus: "OK", qyTouched: true, }); expect(filledKeys(next, 1)).toEqual(["code", "weight", "variant"]); expect(next.variants[0]).toEqual(twoVariants().variants[0]); }); it("never overwrites what the admin typed", () => { const next = prefillFromQianyi(twoVariants({ code: "MY-CODE", weight: "0.3", options: ["Blue", "M"] }), 1, option); expect(next.variants[1]).toMatchObject({ code: "MY-CODE", weight: "0.3", options: ["Blue", "M"], qySku: "0626400016351" }); expect(filledKeys(next, 1)).toEqual([]); }); it("replaces a code that is still the suggestion", () => { const suggested = suggestVariantCode("HEY", "Colour / Size", "Blue / M"); const next = prefillFromQianyi(twoVariants({ code: suggested, variant: "Colour / Size", variantName: "Blue / M", options: ["Blue", "M"] }), 1, option); expect(next.variants[1].code).toBe("0626400016351"); }); it("keeps a saved variant's code", () => { const next = prefillFromQianyi(twoVariants(), 0, option); expect(next.variants[0]).toMatchObject({ code: "HEY-RED", weight: "0.3", options: ["Red", "S"] }); expect(filledKeys(next, 0)).toEqual([]); }); it("leaves a SKU without options as the default variant, with the SKU code", () => { const single: SkuDraft = { ...emptySku(), code: "HEY", variants: [emptySkuVariant()] }; const next = prefillFromQianyi(single, 0, option); expect(next.variants[0]).toMatchObject({ code: "", weight: "0.245", options: [], variant: "", variantName: "" }); expect(filledKeys(next, 0)).toEqual(["weight"]); }); it("re-picking replaces what the previous pick filled, and clears what the new pick lacks", () => { const first = prefillFromQianyi(twoVariants(), 1, option); const other = { sku: "QY-OTHER", title: "Other", weightKg: null, color: "#RED000", size: null, linkedTo: null }; const next = prefillFromQianyi(first, 1, other); expect(next.variants[1]).toMatchObject({ code: "QY-OTHER", weight: "", options: ["#RED000", ""], qySku: "QY-OTHER" }); expect(filledKeys(next, 1)).toEqual(["code", "variant"]); }); it("re-picking keeps a field the admin edited after the first pick", () => { const first = prefillFromQianyi(twoVariants(), 1, option); const edited: SkuDraft = { ...first, variants: [first.variants[0], { ...first.variants[1], weight: "0.4" }] }; const next = prefillFromQianyi(edited, 1, { ...option, sku: "QY-2", weightKg: 1 }); expect(next.variants[1]).toMatchObject({ code: "QY-2", weight: "0.4" }); expect(filledKeys(next, 1)).toEqual(["code", "variant"]); }); it("leaves values empty when QianYi has none, and uses colour or size alone", () => { const bare = { sku: "QY-1", title: null, weightKg: null, color: null, size: null, linkedTo: null }; const next = prefillFromQianyi(twoVariants(), 1, bare); expect(next.variants[1]).toMatchObject({ weight: "", options: ["", ""] }); expect(filledKeys(next, 1)).toEqual(["code"]); expect(prefillFromQianyi(twoVariants(), 1, { ...bare, color: "MULTI" }).variants[1].options).toEqual(["MULTI", ""]); expect(prefillFromQianyi(twoVariants(), 1, { ...bare, size: "M" }).variants[1].options).toEqual(["", "M"]); }); }); describe("validateSalesItem long description size", () => { it("accepts a description at the byte limit", () => { expect(validateSalesItem(salesItem({ longDesc: "a".repeat(65535) }), tEn)["salesItem.longDesc"]).toBeUndefined(); }); it("rejects a description over the byte limit, counting UTF-8 bytes", () => { // 16,384 emoji × 4 bytes = 65,536 bytes > 65,535 although .length is 32,768 expect(validateSalesItem(salesItem({ longDesc: "🍼".repeat(16384) }), tEn)["salesItem.longDesc"]).toBe( "Description is too long (max 64 KB)", ); }); it("checks GROUP products too", () => { const group = salesItem({ isGroup: true, groupComponents: [{ componentProductId: "1" }], groupPickCount: 1, longDesc: "a".repeat(65536), }); expect(validateSalesItem(group, tEn)["salesItem.longDesc"]).toBe("Description is too long (max 64 KB)"); }); }); describe("tags (add-storefront-collections)", () => { it("a new Sales Item starts with no tags", () => { expect(emptySalesItem().tags).toEqual([]); }); it("the Sales Item part sends the tags", () => { expect(salesItemPart(salesItem({ tags: ["newborn", "gift idea"] })).tags).toEqual(["newborn", "gift idea"]); }); it("an empty tag list is still sent, so removing every tag clears them", () => { expect(salesItemPart(salesItem({ tags: [] })).tags).toEqual([]); }); it("loading a Sales Item reads its tags, and a view without tags loads none", () => { const base: SalesItemView = { id: 26, code: "TEST-001", name: "Test", brand: "B", category: "C", status: "Y", productType: "STANDARD", groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false, variations: [], }; expect(salesItemFromView({ ...base, tags: ["gift idea", "newborn"] }).salesItem.tags).toEqual(["gift idea", "newborn"]); expect(salesItemFromView(base).salesItem.tags).toEqual([]); }); }); describe("variant dimensions (cm)", () => { it("round-trips lengthCm 20 from view to draft to skuPart", () => { const draft = skuFromView({ id: 14, code: "BA-001", name: "Bodysuit", status: "Y", variations: [{ id: 14, code: "BA-001", variant: "DEFAULT", variantName: "DEFAULT", weight: 0.3, lengthCm: 20, widthCm: null, heightCm: null, usedBy: [], onHand: 0 }], }); expect(draft.variants[0].lengthCm).toBe("20"); expect(skuPart(draft).variations[0]).toMatchObject({ lengthCm: 20 }); }); it("skuPart always sends all three dimensions, null when empty", () => { const v = skuPart(sku()).variations[0]; expect(v).toHaveProperty("lengthCm", null); expect(v).toHaveProperty("widthCm", null); expect(v).toHaveProperty("heightCm", null); }); it("refuses a zero or empty dimension", () => { const errors = validateSku(sku({ variants: [{ ...emptySkuVariant(), variant: "Size", variantName: "S", code: "BA-010-S", weight: "0.2", lengthCm: " ", widthCm: "0" }] }), tEn); expect(errors["sku.variations[0].lengthCm"]).toBe("Length must be greater than 0"); expect(errors["sku.variations[0].widthCm"]).toBe("Width must be greater than 0"); expect(errors["sku.variations[0].heightCm"]).toBe("Height must be greater than 0"); }); it("accepts a variant with weight and every dimension", () => { const errors = validateSku( sku({ variants: [{ ...emptySkuVariant(), variant: "Size", variantName: "S", options: ["S"], code: "BA-010-S", weight: "0.2", lengthCm: "20", widthCm: "10", heightCm: "5" }] }), tEn, ); expect(errors).toEqual({}); }); describe("QianYi prefill", () => { const option = { sku: "QY-D", title: "Dim", weightKg: 0.2, lengthCm: 30, widthCm: 20, heightCm: 10, color: null, size: null, linkedTo: null }; const draftWith = (second: Partial = {}): SkuDraft => ({ ...emptySku(), code: "HEY", variants: [ { ...emptySkuVariant(), id: 1, code: "HEY-RED", variant: "Colour", variantName: "Red" }, { ...emptySkuVariant(), ...second }, ], }); it("fills empty dimension fields", () => { const next = prefillFromQianyi(draftWith(), 1, option); expect(next.variants[1]).toMatchObject({ lengthCm: "30", widthCm: "20", heightCm: "10" }); }); it("does not overwrite a value the admin typed", () => { const next = prefillFromQianyi(draftWith({ lengthCm: "99" }), 1, option); expect(next.variants[1]).toMatchObject({ lengthCm: "99", widthCm: "20", heightCm: "10" }); }); }); }); describe("later price rows", () => { const rows = (...p: PriceDraft[]) => validatePrices(p, salesItem(), tEn, TODAY); const later = (over: Partial = {}) => price({ later: true, country: "1", ...over }); it("a later row must start after today", () => { expect(rows(later({ effectiveFrom: TODAY }))["pricing.rows[0].effectiveFrom"]).toBe("Pick a date after today"); expect(rows(later({ effectiveFrom: "2026-10-01" }))["pricing.rows[0].effectiveFrom"]).toBe("Pick a date after today"); expect(rows(later({ effectiveFrom: "" }))["pricing.rows[0].effectiveFrom"]).toBe("Effective from is required"); expect(rows(later({ effectiveFrom: "2026-10-22" }))).toEqual({}); }); it("a row that is not a later row may start today", () => { expect(rows(price({ country: "1", effectiveFrom: TODAY }))).toEqual({}); }); it("a later row clashing with another row of the variant and type reads 'duplicate'", () => { const errors = rows(price({ country: "1", effectiveFrom: "2026-10-01" }), later({ effectiveFrom: "2026-10-22" })); expect(errors["pricing.rows[1]"]).toMatch(/^Duplicate active price for this variation/); }); it("an empty country counts as the default country in the overlap check", () => { const open = price({ id: 5, country: "", effectiveFrom: "2026-01-01" }); const errors = validatePrices([open, later({ country: "2", effectiveFrom: "2026-11-01" })], salesItem(), tEn, TODAY, "2"); expect(errors["pricing.rows[1]"]).toMatch(/^Duplicate active price for this variation/); expect(validatePrices([{ ...open, effectiveTo: "2026-10-31" }, later({ country: "2", effectiveFrom: "2026-11-01" })], salesItem(), tEn, TODAY, "2")).toEqual({}); }); it("a row with a country-less stored value no longer asks for a country", () => { expect(rows(price({ id: 5, country: "", effectiveFrom: "2026-10-01" }))).toEqual({}); }); it("the pricing part never sends the client-only later flag", () => { const part = pricingPart([later({ effectiveFrom: "2026-10-22" })], salesItem()); expect(part.rows[0]).not.toHaveProperty("later"); }); }); describe("emptyPrice market (country price setup)", () => { it("defaults to MYR with no country", () => { expect(emptyPrice("k1")).toMatchObject({ currency: "MYR", country: "" }); }); it("takes the country id and currency from the market", () => { expect(emptyPrice("k1", { id: "9", code: "SG", currency: "SGD", isDefault: false })).toMatchObject({ variationKey: "k1", currency: "SGD", country: "9", region: "" }); expect(emptyPrice("k1", { id: "2", code: "MY", currency: "MYR", isDefault: true })).toMatchObject({ currency: "MYR", country: "2" }); }); });