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
172 lines
6.6 KiB
TypeScript
172 lines
6.6 KiB
TypeScript
/** Pure-function unit tests for transferWorkbook.ts (real in-memory xlsx, no DOM). */
|
|
import { describe, it, expect } from "vitest";
|
|
import * as XLSX from "xlsx";
|
|
import { tEn } from "@/test/i18nCatalog";
|
|
import {
|
|
TRANSFER_HEADERS,
|
|
buildTransferWorkbook,
|
|
parseTransferWorkbook,
|
|
transferShortfalls,
|
|
} from "./transferWorkbook";
|
|
import { readSheetRows } from "./workbookRows";
|
|
import type { StockOnHandRow } from "@/app/lib/types";
|
|
|
|
const row = (over: Partial<StockOnHandRow>): StockOnHandRow => ({
|
|
skuCode: "BB-001",
|
|
skuVariationId: "100001",
|
|
variation: "100g",
|
|
skuName: "Baby Milk",
|
|
stockOnHand: 10,
|
|
committedQty: 0,
|
|
...over,
|
|
} as StockOnHandRow);
|
|
|
|
const STOCK = [
|
|
row({}),
|
|
row({ skuVariationId: "100002", variation: null, stockOnHand: 3 }),
|
|
row({ skuVariationId: "100003", stockOnHand: 0 }),
|
|
];
|
|
|
|
describe("buildTransferWorkbook", () => {
|
|
const wb = XLSX.read(buildTransferWorkbook(STOCK), { type: "array" });
|
|
const sheet = wb.Sheets["Stock Transfer"];
|
|
|
|
it("has Stock Transfer and Instructions sheets", () => {
|
|
expect(wb.SheetNames).toEqual(["Stock Transfer", "Instructions"]);
|
|
});
|
|
|
|
it("writes the exact header row", () => {
|
|
const rows = XLSX.utils.sheet_to_json<string[]>(sheet, { header: 1 });
|
|
expect(rows[0]).toEqual([...TRANSFER_HEADERS]);
|
|
expect(rows[0]).toEqual([
|
|
"SKU Code", "SKU Name", "Variation", "On Hand", "Quantity",
|
|
"Batch Number", "Expiry (YYYY-MM-DD)", "skuVariationId",
|
|
]);
|
|
});
|
|
|
|
it("includes only rows with stock, ids as strings, null variation blank", () => {
|
|
const rows = XLSX.utils.sheet_to_json<Record<string, unknown>>(sheet);
|
|
expect(rows).toHaveLength(2);
|
|
expect(rows[0]).toMatchObject({ skuVariationId: "100001", "On Hand": 10, Variation: "100g" });
|
|
expect(rows[1]).toMatchObject({ skuVariationId: "100002", "On Hand": 3 });
|
|
expect(rows[1].Variation).toBeUndefined();
|
|
expect(rows[0].Quantity).toBeUndefined();
|
|
});
|
|
|
|
it("formats the Expiry column as text so typed dates stay text", () => {
|
|
const styled = XLSX.read(buildTransferWorkbook(STOCK), { type: "array", cellNF: true });
|
|
expect(styled.Sheets["Stock Transfer"]["G2"].z).toBe("@");
|
|
});
|
|
|
|
it("hides the id column and gives every visible column a width", () => {
|
|
// cellStyles: true is required to round-trip !cols through XLSX.read.
|
|
const styled = XLSX.read(buildTransferWorkbook(STOCK), { type: "array", cellStyles: true });
|
|
const cols = styled.Sheets["Stock Transfer"]["!cols"] ?? [];
|
|
expect(cols[7]?.hidden).toBe(true);
|
|
for (let i = 0; i < 7; i++) {
|
|
expect(cols[i]?.hidden, `column ${i}`).toBeFalsy();
|
|
expect(cols[i]?.wch ?? 0, `column ${i}`).toBeGreaterThanOrEqual(14);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("parseTransferWorkbook", () => {
|
|
const base = { "SKU Code": "BB-001", "SKU Name": "Baby Milk", Variation: "100g", skuVariationId: "100001" };
|
|
|
|
it("takes positive rows with display, batch and expiry; skips blank/0/negative", () => {
|
|
const { lines, errors } = parseTransferWorkbook(
|
|
[
|
|
{ ...base, Quantity: 3, "Batch Number": "B1", "Expiry (YYYY-MM-DD)": "2027-12-31" },
|
|
{ ...base, Quantity: 0 },
|
|
{ ...base },
|
|
{ ...base, Quantity: -1 },
|
|
],
|
|
tEn,
|
|
);
|
|
expect(errors).toEqual([]);
|
|
expect(lines).toEqual([
|
|
{
|
|
skuVariationId: "100001", quantity: 3, batchNumber: "B1", expiryDate: "2027-12-31",
|
|
skuCode: "BB-001", skuName: "Baby Milk", variation: "100g",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("reports bad rows with the xlsx row number and drops none silently", () => {
|
|
const { lines, errors } = parseTransferWorkbook(
|
|
[
|
|
{ ...base, Quantity: "five" },
|
|
{ ...base, Quantity: 2.5 },
|
|
{ ...base, Quantity: 1, skuVariationId: "" },
|
|
{ ...base, Quantity: 1, "Expiry (YYYY-MM-DD)": "31/12/2027" },
|
|
],
|
|
tEn,
|
|
);
|
|
expect(lines).toEqual([]);
|
|
expect(errors).toHaveLength(4);
|
|
expect(errors[0]).toBe("Row 2: quantity must be a number");
|
|
expect(errors[1]).toBe("Row 3: quantity must be a whole number");
|
|
expect(errors[2]).toBe("Row 4: missing SKU id");
|
|
expect(errors[3]).toContain("Row 5: expiry must be YYYY-MM-DD");
|
|
expect(errors[3]).toContain("31/12/2027");
|
|
});
|
|
|
|
// Round trip like the form: write cells, read the file WITHOUT cellDates, then readSheetRows + parse.
|
|
function importExpiry(cell: XLSX.CellObject) {
|
|
const wb = XLSX.read(buildTransferWorkbook(STOCK), { type: "array" });
|
|
const ws = wb.Sheets["Stock Transfer"];
|
|
ws["E2"] = { t: "n", v: 3 };
|
|
ws["G2"] = cell;
|
|
const back = XLSX.read(new Uint8Array(XLSX.write(wb, { type: "array", bookType: "xlsx" })), { type: "array", cellNF: true });
|
|
return parseTransferWorkbook(readSheetRows(back.Sheets["Stock Transfer"], "Expiry (YYYY-MM-DD)"), tEn);
|
|
}
|
|
|
|
it("keeps a date-formatted Batch Number as its raw cell value, not a Date string", () => {
|
|
const wb = XLSX.read(buildTransferWorkbook(STOCK), { type: "array" });
|
|
const ws = wb.Sheets["Stock Transfer"];
|
|
ws["E2"] = { t: "n", v: 3 };
|
|
ws["F2"] = { t: "n", v: 46752, z: "yyyy-mm-dd" };
|
|
const back = XLSX.read(new Uint8Array(XLSX.write(wb, { type: "array", bookType: "xlsx" })), { type: "array", cellNF: true });
|
|
const { lines, errors } = parseTransferWorkbook(readSheetRows(back.Sheets["Stock Transfer"], "Expiry (YYYY-MM-DD)"), tEn);
|
|
expect(errors).toEqual([]);
|
|
expect(lines[0].batchNumber).toBe("46752");
|
|
});
|
|
|
|
it("accepts a real Excel date cell as YYYY-MM-DD in any time zone", () => {
|
|
const { lines, errors } = importExpiry({ t: "n", v: 46752, z: "yyyy-mm-dd" }); // 2027-12-31
|
|
expect(errors).toEqual([]);
|
|
expect(lines[0].expiryDate).toBe("2027-12-31");
|
|
});
|
|
|
|
it.each([5, 2027, 0])("rejects a General-format number %s as a bad expiry", n => {
|
|
const { lines, errors } = importExpiry({ t: "n", v: n });
|
|
expect(lines).toEqual([]);
|
|
expect(errors).toHaveLength(1);
|
|
expect(errors[0]).toContain("Row 2: expiry must be YYYY-MM-DD");
|
|
});
|
|
|
|
it("still accepts a text 2027-12-31", () => {
|
|
const { lines, errors } = importExpiry({ t: "s", v: "2027-12-31" });
|
|
expect(errors).toEqual([]);
|
|
expect(lines[0].expiryDate).toBe("2027-12-31");
|
|
});
|
|
});
|
|
|
|
describe("transferShortfalls", () => {
|
|
it("sums per id and reports the excess over on hand", () => {
|
|
const onHand = new Map([["50", 5], ["60", 10]]);
|
|
const out = transferShortfalls(
|
|
[
|
|
{ skuVariationId: "50", quantity: 3 },
|
|
{ skuVariationId: "50", quantity: 4 },
|
|
{ skuVariationId: "60", quantity: 10 },
|
|
{ skuVariationId: "70", quantity: 2 },
|
|
],
|
|
onHand,
|
|
);
|
|
expect(out.get("50")).toBe(2);
|
|
expect(out.has("60")).toBe(false);
|
|
expect(out.get("70")).toBe(2);
|
|
});
|
|
});
|