Files
co-lite/solvway-admin-portal/app/lib/catalogSetup.barcodes.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

156 lines
6.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { tEn } from "@/test/i18nCatalog";
import { emptySku, emptySkuVariant, skuFromView, skuPart, validateSku, type SkuDraft, type SkuView } from "./catalogSetup";
// add-sku-barcodes: each SKU variant carries barcodes. They load into the draft, go out only once the admin
// touched them (null on the wire leaves them as stored), and are checked at the backend's field paths.
function sku(overrides: Partial<SkuDraft> = {}): SkuDraft {
return {
...emptySku(),
code: "BA-002",
name: "Romper",
variants: [{ ...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2" }],
...overrides,
};
}
describe("barcodes in the SKU draft", () => {
it("a new variant starts with no barcodes, untouched", () => {
expect(emptySkuVariant().barcodes).toEqual([]);
expect(emptySkuVariant().barcodesTouched).toBe(false);
});
it("loads each variant's barcodes from the view", () => {
const view: SkuView = {
id: 16,
code: "BA-002",
name: "Romper",
status: "Y",
variations: [
{
id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: 0.2, usedBy: [], onHand: 0,
barcodes: [
{ id: 1, barcode: "9555123400011", batchNumber: "B2501", expiryDate: "2027-06-30", status: "Y", source: "MANUAL" },
{ id: 2, barcode: "6971234567890", batchNumber: null, expiryDate: null, status: "N", source: "QIANYI" },
],
},
{ id: 71, code: "BA-002-BLU", variant: "Colour", variantName: "Blue", weight: 0.2, usedBy: [], onHand: 0 },
],
};
const draft = skuFromView(view);
expect(draft.variants[0].barcodes).toEqual([
{ id: 1, barcode: "9555123400011", batchNumber: "B2501", expiryDate: "2027-06-30", status: "Y", source: "MANUAL" },
{ id: 2, barcode: "6971234567890", batchNumber: "", expiryDate: "", status: "N", source: "QIANYI" },
]);
expect(draft.variants[0].barcodesTouched).toBe(false);
expect(draft.variants[1].barcodes).toEqual([]);
});
it("leaves barcodes off the request until the admin touches them", () => {
const part = skuPart(sku());
expect(part.variations[0]).not.toHaveProperty("barcodes");
});
it("sends the whole touched list, trimmed, with blanks as null", () => {
const part = skuPart(
sku({
variants: [
{
...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2",
barcodesTouched: true,
barcodes: [
{ id: 1, barcode: "9555123400011", batchNumber: " B2501 ", expiryDate: "2027-06-30", status: "N", source: "MANUAL" },
{ barcode: " 9555123400028 ", batchNumber: "", expiryDate: "", status: "Y", source: "MANUAL" },
],
},
],
}),
);
expect(part.variations[0].barcodes).toEqual([
{ id: 1, barcode: "9555123400011", batchNumber: "B2501", expiryDate: "2027-06-30", status: "N" },
{ barcode: "9555123400028", batchNumber: null, expiryDate: null, status: "Y" },
]);
});
it("sends an emptied touched list as [] so the barcodes are removed", () => {
const part = skuPart(
sku({ variants: [{ ...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2", barcodesTouched: true, barcodes: [] }] }),
);
expect(part.variations[0].barcodes).toEqual([]);
});
});
describe("barcode checks", () => {
const row = (barcode: string, extra: Record<string, string> = {}) => ({
barcode, batchNumber: "", expiryDate: "", status: "Y" as const, source: "MANUAL" as const, ...extra,
});
it("refuses malformed barcodes at the backend's path", () => {
const errors = validateSku(
sku({
variants: [
{
...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2",
barcodes: [row("12 34"), row("123"), row("9555123400011"), row("x".repeat(65))],
},
],
}),
tEn,
);
expect(errors["sku.variations[0].barcodes[0].barcode"]).toBe("Use 4–64 letters, digits or symbols, no spaces");
expect(errors["sku.variations[0].barcodes[1].barcode"]).toBe("Use 4–64 letters, digits or symbols, no spaces");
expect(errors["sku.variations[0].barcodes[2].barcode"]).toBeUndefined();
expect(errors["sku.variations[0].barcodes[3].barcode"]).toBe("Use 4–64 letters, digits or symbols, no spaces");
});
it("refuses a barcode repeated anywhere in the SKU, on the later one", () => {
const errors = validateSku(
sku({
variants: [
{ ...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2", barcodes: [row("9555123400011")] },
{ ...emptySkuVariant(), id: 71, code: "BA-002-BLU", variant: "Colour", variantName: "Blue", weight: "0.2", barcodes: [row(" 9555123400011 ")] },
],
}),
tEn,
);
expect(errors["sku.variations[0].barcodes[0].barcode"]).toBeUndefined();
expect(errors["sku.variations[1].barcodes[0].barcode"]).toBe("Barcode 9555123400011 is repeated");
});
it("treats barcodes differing only in case as repeated (the database does)", () => {
const errors = validateSku(
sku({
variants: [
{ ...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2", barcodes: [row("abc1234"), row("ABC1234")] },
],
}),
tEn,
);
expect(errors["sku.variations[0].barcodes[1].barcode"]).toBe("Barcode ABC1234 is repeated");
});
it("refuses a batch number over 64 characters", () => {
const errors = validateSku(
sku({
variants: [
{ ...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2", barcodes: [row("9555123400011", { batchNumber: "B".repeat(65) })] },
],
}),
tEn,
);
expect(errors["sku.variations[0].barcodes[0].batchNumber"]).toBe("Batch number is at most 64 characters");
});
it("a SKU without barcodes has no barcode errors", () => {
expect(Object.keys(validateSku(sku(), tEn)).filter((k) => k.includes("barcodes"))).toEqual([]);
});
});