import { describe, it, expect } from "vitest"; import { tEn } from "@/test/i18nCatalog"; import * as XLSX from "xlsx"; import { toXlsx } from "@/app/lib/xlsx"; import { RATE_COLUMNS, diffRates, parseRateSheet, templateRows } from "./rateCardWorkbook"; import type { RateRow } from "@/app/lib/services/adminShipping"; const ZONES = ["WM", "EM"]; const BOX = { maxLengthCm: 20, maxWidthCm: 15, maxHeightCm: 10 }; const NOBOX = { maxLengthCm: null, maxWidthCm: null, maxHeightCm: null }; const ROWS: RateRow[] = [ { zoneCode: "WM", type: "BRACKET", fromKg: 0, toKg: 1, stepKg: null, fee: 8, ...BOX }, { zoneCode: "WM", type: "BRACKET", fromKg: 1, toKg: 3, stepKg: null, fee: 12.5, ...BOX }, { zoneCode: "WM", type: "OVERFLOW", fromKg: null, toKg: null, stepKg: 1, fee: 3, ...NOBOX }, ]; function sheetOf(aoa: unknown[][]): XLSX.WorkSheet { return XLSX.utils.aoa_to_sheet(aoa); } const HEADER = RATE_COLUMNS.map((c) => c.title); describe("rateCardWorkbook (add-shipping-rates)", () => { it("round-trips an exported card through the import parser", async () => { const bytes = await toXlsx(ROWS as unknown as Record[], RATE_COLUMNS, "Rates"); const wb = XLSX.read(bytes, { type: "array" }); const { rows, errors } = parseRateSheet(wb.Sheets[wb.SheetNames[0]], ZONES, tEn); expect(errors).toEqual([]); expect(rows).toEqual(ROWS); }); it("has the eight columns in order", () => { expect(HEADER).toEqual(["Zone code", "Type", "Up to kg", "Max L cm", "Max W cm", "Max H cm", "Step kg", "Fee RM"]); }); it("builds a template with a bracket (with a box) and an overflow row per zone", () => { expect(templateRows(["WM"])).toEqual([ { zoneCode: "WM", type: "BRACKET", toKg: 1, maxLengthCm: 20, maxWidthCm: 15, maxHeightCm: 10, stepKg: null, fee: 0 }, { zoneCode: "WM", type: "OVERFLOW", toKg: null, maxLengthCm: null, maxWidthCm: null, maxHeightCm: null, stepKg: 1, fee: 0 }, ]); }); it("refuses a formula cell and formula-like text, naming the row", () => { const sheet = sheetOf([HEADER, ["WM", "BRACKET", 1, 20, 15, 10, null, 8], ["=cmd|' /C calc'!A0", "BRACKET", 2, 20, 15, 10, null, 9], ["WM", "OVERFLOW", null, null, null, null, 1, 3]]); sheet["H2"] = { t: "n", v: 8, f: "4*2" }; const { errors } = parseRateSheet(sheet, ZONES, tEn); expect(errors).toContain("Row 2: formulas aren't allowed"); expect(errors).toContain("Row 3: formulas aren't allowed"); }); it("names non-numbers, unsorted rows, unknown zones and missing boxes by row", () => { const { errors } = parseRateSheet(sheetOf([ HEADER, ["WM", "BRACKET", 2, 20, 15, 10, null, 8], ["WM", "BRACKET", 1.5, 20, 15, 10, null, 12], ["EM", "BRACKET", 2, 20, 15, null, null, 8], ["XX", "BRACKET", 1, 20, 15, 10, null, 8], ["WM", "BRACKET", "abc", 20, 15, 10, null, 1], ["WM", "OVERFLOW", null, null, null, null, 1, 3], ["EM", "OVERFLOW", null, null, null, null, 1, 3], ]), ZONES, tEn); expect(errors).toEqual(expect.arrayContaining([ "Row 3: Up to kg must be greater than 2 kg", "Row 4: Max L, W and H must each be greater than 0", "Row 5: unknown zone XX", "Row 6: Up to kg must be a number", ])); }); it("refuses an old six-column file with the new titles", () => { expect(parseRateSheet(sheetOf([["Zone code", "Type", "From kg", "To kg", "Step kg", "Fee RM"], ["WM", "BRACKET", 0, 1, null, 8]]), ZONES, tEn).errors) .toEqual(["Row 1: the columns must be Zone code, Type, Up to kg, Max L cm, Max W cm, Max H cm, Step kg, Fee RM"]); }); it("refuses a sheet with the wrong columns", () => { expect(parseRateSheet(sheetOf([["Zone", "Fee"], ["WM", 8]]), ZONES, tEn).errors) .toEqual(["Row 1: the columns must be Zone code, Type, Up to kg, Max L cm, Max W cm, Max H cm, Step kg, Fee RM"]); }); it("ignores blank rows", () => { const { rows, errors } = parseRateSheet(sheetOf([HEADER, ["WM", "BRACKET", 1, 20, 15, 10, null, 8], [], ["WM", "OVERFLOW", null, null, null, null, 1, 3]]), ZONES, tEn); expect(errors).toEqual([]); expect(rows).toHaveLength(2); }); it("derives From from the previous row of the zone", () => { const { rows } = parseRateSheet(sheetOf([HEADER, ["WM", "BRACKET", 1, 20, 15, 10, null, 8], ["WM", "BRACKET", 3, 20, 15, 10, null, 9], ["WM", "OVERFLOW", null, null, null, null, 1, 3]]), ZONES, tEn); expect(rows.map((r) => r.fromKg)).toEqual([0, 1, null]); }); it("counts what an import adds, removes and changes per zone", () => { const next: RateRow[] = [ { ...ROWS[0], fee: 9 }, ROWS[2], { zoneCode: "EM", type: "BRACKET", fromKg: 0, toKg: 1, stepKg: null, fee: 10, ...BOX }, ]; expect(diffRates(ROWS, next)).toEqual({ added: 1, removed: 1, changed: 1, byZone: { WM: { added: 0, removed: 1, changed: 1 }, EM: { added: 1, removed: 0, changed: 0 } }, }); }); it("counts a box-only change as changed", () => { const next = [{ ...ROWS[0], maxHeightCm: 12 }, ROWS[1], ROWS[2]]; expect(diffRates(ROWS, next)).toMatchObject({ added: 0, removed: 0, changed: 1 }); }); });