Files
co-lite/solvway-admin-portal/app/lib/fulfilment/trackingWorkbook.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

123 lines
5.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import * as XLSX from "xlsx";
import { toXlsx } from "@/app/lib/xlsx";
import { MAX_TRACKING_ROWS, TRACKING_COLUMNS, parseTrackingSheet, templateRows } from "./trackingWorkbook";
// Tracking import workbook (add-manual-fulfilment-console, task 11): DO no | Courier code | Tracking no.
const HEADER = ["DO no", "Courier code", "Tracking no"];
const sheetOf = (aoa: unknown[][]) => XLSX.utils.aoa_to_sheet(aoa);
describe("trackingWorkbook", () => {
it("has the three spec columns", () => {
expect(TRACKING_COLUMNS).toEqual([
{ key: "doNumber", title: "DO no" },
{ key: "courierCode", title: "Courier code" },
{ key: "trackingNumber", title: "Tracking no" },
]);
expect(MAX_TRACKING_ROWS).toBe(10_000);
});
it("round-trips the template through the parser", async () => {
const bytes = await toXlsx(templateRows(), TRACKING_COLUMNS, "Tracking");
const wb = XLSX.read(bytes, { type: "array" });
const { rows, errors } = parseTrackingSheet(wb.Sheets[wb.SheetNames[0]]);
expect(errors).toEqual([]);
expect(rows).toEqual([{ row: 2, doNumber: "MYB-ON-DO261000001", courierCode: "", trackingNumber: "JT0000000001" }]);
});
it("parses rows with their sheet row numbers, trimming cells and keeping a blank courier as ''", () => {
const { rows, errors } = parseTrackingSheet(sheetOf([
HEADER,
[" D1 ", "JNT", " T1"],
["D2", null, 12345],
]));
expect(errors).toEqual([]);
expect(rows).toEqual([
{ row: 2, doNumber: "D1", courierCode: "JNT", trackingNumber: "T1" },
{ row: 3, doNumber: "D2", courierCode: "", trackingNumber: "12345" },
]);
});
it("matches headers ignoring case and surrounding spaces", () => {
const { errors } = parseTrackingSheet(sheetOf([[" do NO", "courier CODE ", "Tracking No"], ["D1", "", "T1"]]));
expect(errors).toEqual([]);
});
it("refuses unknown headers", () => {
const { rows, errors } = parseTrackingSheet(sheetOf([["DO", "Courier", "Tracking"], ["D1", "", "T1"]]));
expect(rows).toEqual([]);
expect(errors).toEqual(["Row 1: the columns must be DO no, Courier code, Tracking no"]);
});
it("lists an empty DO or tracking number by row and skips fully empty rows", () => {
const { errors } = parseTrackingSheet(sheetOf([
HEADER,
["", "JNT", "T1"],
[null, null, null],
["D3", "JNT", ""],
]));
expect(errors).toEqual(["Row 2: DO no is empty", "Row 4: Tracking no is empty"]);
});
it("refuses a formula cell and formula-like text, naming the row", () => {
const sheet = sheetOf([HEADER, ["D1", "", "T1"], ["D2", "", "T2"], ["D3", "", '=HYPERLINK("http://x")'], ["D4", "+cmd", "T4"]]);
sheet["C3"] = { t: "s", v: "T2", f: "CONCAT(\"T\",2)" };
const { rows, errors } = parseTrackingSheet(sheet);
expect(errors).toEqual([
"Row 3: formulas aren't allowed",
"Row 4: formulas aren't allowed",
"Row 5: formulas aren't allowed",
]);
expect(rows).toEqual([]);
});
it("refuses a tracking number repeated in the file, naming both rows", () => {
const { errors } = parseTrackingSheet(sheetOf([HEADER, ["D1", "", "T1"], ["D2", "", "T2"], ["D3", "", "T1"]]));
expect(errors).toEqual(["Row 4: tracking number T1 is repeated (first on row 2)"]);
});
it("refuses over-long values like the server does", () => {
const { errors } = parseTrackingSheet(sheetOf([HEADER, ["D1", "C".repeat(31), "T".repeat(101)]]));
expect(errors).toEqual([
"Row 2: Courier code is longer than 30 characters",
"Row 2: Tracking no is longer than 100 characters",
]);
});
it("refuses an empty file and more than 10,000 rows", () => {
expect(parseTrackingSheet(sheetOf([HEADER])).errors).toEqual(["The file has no rows"]);
const many: unknown[][] = [HEADER];
for (let i = 0; i <= MAX_TRACKING_ROWS; i++) many.push([`D${i}`, "", `T${i}`]);
const { rows, errors } = parseTrackingSheet(sheetOf(many));
expect(rows).toEqual([]);
expect(errors).toEqual(["A tracking file can hold at most 10,000 rows"]);
});
it("returns no rows when any row fails, so nothing is sent", () => {
const { rows } = parseTrackingSheet(sheetOf([HEADER, ["D1", "", "T1"], ["", "", "T2"]]));
expect(rows).toEqual([]);
});
it("keeps a whole number tracking cell but refuses one Excel may have changed", () => {
const sheet = sheetOf([HEADER, ["D1", "", 123456789012], ["D2", "", 1.5], ["D3", "", 12345678901234567890]]);
const { errors } = parseTrackingSheet(sheet);
expect(errors).toEqual([
"Row 3: Tracking no is a number Excel may have changed — format the column as Text and re-enter it",
"Row 4: Tracking no is a number Excel may have changed — format the column as Text and re-enter it",
]);
const ok = parseTrackingSheet(sheetOf([HEADER, ["D1", "", 123456789012]]));
expect(ok.rows[0].trackingNumber).toBe("123456789012");
});
it("refuses a DO no repeated in the file", () => {
const { errors } = parseTrackingSheet(sheetOf([HEADER, ["D1", "", "T1"], ["d1", "", "T2"]]));
expect(errors).toEqual(["Row 3: DO no d1 is repeated (first on row 2)"]);
});
it("refuses extra columns", () => {
const { errors } = parseTrackingSheet(sheetOf([[...HEADER, "Notes"], ["D1", "", "T1", "x"]]));
expect(errors).toEqual(["Row 1: the columns must be DO no, Courier code, Tracking no"]);
});
});