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
138 lines
7.1 KiB
TypeScript
138 lines
7.1 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { tEn } from "@/test/i18nCatalog";
|
|
import type { RateRow } from "@/app/lib/services/adminShipping";
|
|
import { fitsBox, priceParcel, rowErrors, withDerivedFrom, type ParcelItem } from "./rateRows";
|
|
|
|
const bracket = (toKg: number, fee: number, box: [number, number, number] | null = [20, 15, 10], zoneCode = "WM", fromKg: number | null = null): RateRow => ({
|
|
zoneCode, type: "BRACKET", fromKg, toKg, stepKg: null, fee,
|
|
maxLengthCm: box?.[0] ?? null, maxWidthCm: box?.[1] ?? null, maxHeightCm: box?.[2] ?? null,
|
|
});
|
|
const overflow = (fee: number, stepKg = 1, zoneCode = "WM"): RateRow => ({
|
|
zoneCode, type: "OVERFLOW", fromKg: null, toKg: null, stepKg, fee, maxLengthCm: null, maxWidthCm: null, maxHeightCm: null,
|
|
});
|
|
const item = (weightKg: number, l: number, w: number, h: number, qty = 1): ParcelItem => ({ weightKg, lengthCm: l, widthCm: w, heightCm: h, qty });
|
|
|
|
const CARD = [bracket(1, 8), bracket(3, 12, [30, 20, 15]), overflow(3)];
|
|
|
|
describe("fitsBox", () => {
|
|
it("fits when sorted sides and total volume fit", () => {
|
|
expect(fitsBox([item(1, 10, 15, 20)], bracket(1, 8))).toBe(true); // exact fit, rotated
|
|
});
|
|
it("rejects a side that is too long", () => {
|
|
expect(fitsBox([item(1, 21, 5, 5)], bracket(1, 8))).toBe(false);
|
|
});
|
|
it("rejects when the combined volume is too large", () => {
|
|
expect(fitsBox([item(1, 20, 15, 10, 2)], bracket(1, 8))).toBe(false);
|
|
});
|
|
it("a row with any null side fits anything", () => {
|
|
expect(fitsBox([item(1, 500, 500, 500)], bracket(1, 8, null))).toBe(true);
|
|
expect(fitsBox([item(1, 500, 500, 500)], { ...bracket(1, 8), maxHeightCm: null })).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("priceParcel", () => {
|
|
it("has no rate when the matched row's fee is blank", () => {
|
|
expect(priceParcel([{ ...bracket(1, 8), fee: null }, overflow(3)], 1, [item(1, 10, 10, 10)])).toEqual({ noRate: true });
|
|
});
|
|
it("has no rate when the overflow fee or step is blank", () => {
|
|
expect(priceParcel([bracket(1, 8), { ...overflow(3), fee: null }], 5, [item(5, 10, 10, 10)])).toEqual({ noRate: true });
|
|
expect(priceParcel([bracket(1, 8), { ...overflow(3), stepKg: null }], 5, [item(5, 10, 10, 10)])).toEqual({ noRate: true });
|
|
});
|
|
it("has no rate when the last row's fee is blank and overflow is used", () => {
|
|
expect(priceParcel([{ ...bracket(1, 8), fee: null }, overflow(3)], 5, [item(5, 10, 10, 10)])).toEqual({ noRate: true });
|
|
});
|
|
it("prices a parcel exactly on the bound by the first row (inclusive)", () => {
|
|
expect(priceParcel(CARD, 1, [item(1, 20, 15, 10)])).toEqual({ fee: 8, row: 1 });
|
|
});
|
|
it("moves a light but bulky parcel to the next row", () => {
|
|
expect(priceParcel(CARD, 0.5, [item(0.5, 25, 15, 10)])).toEqual({ fee: 12, row: 2 });
|
|
});
|
|
it("sums the volume of three items", () => {
|
|
expect(priceParcel(CARD, 0.6, [item(0.2, 20, 15, 10, 3)])).toEqual({ fee: 12, row: 2 });
|
|
});
|
|
it("prices an overweight parcel by the overflow on the last row", () => {
|
|
expect(priceParcel(CARD, 5, [item(5, 30, 20, 15)])).toEqual({ fee: 18, row: 2 });
|
|
});
|
|
it("is too large when it fits no box", () => {
|
|
expect(priceParcel(CARD, 0.5, [item(0.5, 50, 10, 10)])).toEqual({ tooLarge: true });
|
|
expect(priceParcel(CARD, 5, [item(5, 50, 10, 10)])).toEqual({ tooLarge: true });
|
|
});
|
|
it("a row without a box takes anything", () => {
|
|
expect(priceParcel([bracket(1, 8, null), overflow(3)], 0.5, [item(0.5, 500, 500, 500)])).toEqual({ fee: 8, row: 1 });
|
|
});
|
|
it("does not round 0.1 x 3 kg above the 300 g edge", () => {
|
|
expect(priceParcel([bracket(0.3, 5), bracket(1, 9), overflow(3)], 0.3, [item(0.1, 5, 5, 5, 3)])).toEqual({ fee: 5, row: 1 });
|
|
});
|
|
it("sorts rows by up to before pricing", () => {
|
|
expect(priceParcel([bracket(3, 12), bracket(1, 8), overflow(3)], 0.5, [item(0.5, 5, 5, 5)])).toEqual({ fee: 8, row: 1 });
|
|
});
|
|
it("has no rate without brackets, or without an overflow when one is needed", () => {
|
|
expect(priceParcel([overflow(3)], 1, [item(1, 1, 1, 1)])).toEqual({ noRate: true });
|
|
expect(priceParcel([bracket(1, 8)], 2, [item(2, 1, 1, 1)])).toEqual({ noRate: true });
|
|
});
|
|
});
|
|
|
|
describe("withDerivedFrom", () => {
|
|
it("derives from per zone, 0 for the first", () => {
|
|
const rows = [bracket(1, 8), bracket(3, 12), bracket(2, 5, [1, 1, 1], "EM"), overflow(3)];
|
|
expect(withDerivedFrom(rows).map((r) => r.fromKg)).toEqual([0, 1, 0, null]);
|
|
});
|
|
it("re-derives after the middle row is deleted", () => {
|
|
const rows = [bracket(1, 8), bracket(3, 12), bracket(5, 15)];
|
|
const after = withDerivedFrom(rows.filter((r) => r.toKg !== 3));
|
|
expect(after[1].fromKg).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe("rowErrors", () => {
|
|
it("reports each rule against the right row index", () => {
|
|
const rows = [
|
|
bracket(2, 8),
|
|
bracket(2, 9), // not above 2
|
|
bracket(0, 9, [20, 15, 10], "EM"),
|
|
bracket(1, 9, [0, 15, 10], "EM"),
|
|
bracket(2, 9, [200000, 15, 10], "EM"),
|
|
overflow(3),
|
|
];
|
|
const errs = rowErrors(rows, [2, 3, 4, 5, 6, 7], tEn);
|
|
expect(errs.get(1)).toEqual(["Row 3: Up to kg must be greater than 2 kg"]);
|
|
expect(errs.get(2)).toEqual(["Row 4: Up to kg must be greater than 0"]);
|
|
expect(errs.get(3)).toEqual(["Row 5: Max L, W and H must each be greater than 0"]);
|
|
expect(errs.get(4)).toEqual(["Row 6: Max L, W and H must be at most 100000 cm"]);
|
|
expect(errs.has(0)).toBe(false);
|
|
expect(errs.has(5)).toBe(false);
|
|
});
|
|
it("reports a blank, negative or too large bracket fee on that row", () => {
|
|
const rows = [
|
|
{ ...bracket(1, 8), fee: null },
|
|
{ ...bracket(2, 8), fee: -1 },
|
|
{ ...bracket(3, 8), fee: 100000000 },
|
|
bracket(4, 8),
|
|
];
|
|
const errs = rowErrors(rows, [1, 2, 3, 4], tEn);
|
|
expect(errs.get(0)).toEqual(["Row 1: Fee must be 0 or more"]);
|
|
expect(errs.get(1)).toEqual(["Row 2: Fee must be 0 or more"]);
|
|
expect(errs.get(2)).toEqual(["Row 3: Fee must be at most 99999999.99"]);
|
|
expect(errs.has(3)).toBe(false);
|
|
});
|
|
it("reports an overflow line with a missing or non-positive step or a missing fee on the overflow row", () => {
|
|
const rows = [bracket(1, 8), { ...overflow(3), stepKg: null }, { ...overflow(3, 1, "EM"), fee: null }, { ...overflow(3, 0, "XM") }];
|
|
const errs = rowErrors(rows, [1, 2, 1, 1], tEn);
|
|
expect(errs.has(0)).toBe(false);
|
|
expect(errs.get(1)).toEqual(["Row 2: Step kg must be greater than 0"]);
|
|
expect(errs.get(2)).toEqual(["Row 1: Fee must be 0 or more"]);
|
|
expect(errs.get(3)).toEqual(["Row 1: Step kg must be greater than 0"]);
|
|
});
|
|
it("accepts an overflow line with a fee of 0", () => {
|
|
expect(rowErrors([bracket(1, 8), overflow(0)], [1, 2], tEn).size).toBe(0);
|
|
});
|
|
it("treats a box side that rounds to 0.00 cm as missing", () => {
|
|
expect(rowErrors([bracket(1, 8, [0.004, 15, 10])], [1], tEn).get(0)).toEqual(["Row 1: Max L, W and H must each be greater than 0"]);
|
|
expect(rowErrors([bracket(1, 8, [0.01, 15, 10])], [1], tEn).has(0)).toBe(false);
|
|
});
|
|
it("treats a blank side as a missing box", () => {
|
|
const errs = rowErrors([{ ...bracket(1, 8), maxHeightCm: null }], [2], tEn);
|
|
expect(errs.get(0)).toEqual(["Row 2: Max L, W and H must each be greater than 0"]);
|
|
});
|
|
});
|