import { describe, expect, it } from "vitest"; import { cartDeliveryOptionsTransformRun as run, formatDate, formatTime } from "../src/cart_delivery_options_transform_run.js"; const sel = (o = {}) => JSON.stringify({ t: "tok", m: "delivery", l: "kit", d: "2026-11-07", s: "10:00-13:00", f: 0, ...o }); const options = [ { handle: "local", title: "Local Delivery", deliveryMethodType: "LOCAL" }, { handle: "std", title: "Standard", deliveryMethodType: "SHIPPING" }, { handle: "exp", title: "Express", deliveryMethodType: "SHIPPING" }, { handle: "pick", title: "Kitchener — Guelph St", deliveryMethodType: "PICK_UP" }, ]; const input = (raw, lang = "EN") => ({ cart: { selection: raw == null ? null : { value: raw }, deliveryGroups: [{ deliveryOptions: options }] }, localization: { language: { isoCode: lang } }, }); const renames = (r) => r.operations.map((o) => [o.deliveryOptionRename.deliveryOptionHandle, o.deliveryOptionRename.title]); describe("delivery label", () => { it("formats dates without Intl, including weekday", () => { expect(formatDate("2026-11-07", "en")).toBe("Sat, Nov 7"); expect(formatDate("2024-02-29", "en")).toBe("Thu, Feb 29"); expect(formatDate("2026-01-01", "fr")).toBe("jeu. 1 janv."); }); it("formats times in 12 h English and 24 h French", () => { expect(formatTime("00:00", "en")).toBe("12 AM"); expect(formatTime("12:30", "en")).toBe("12:30 PM"); expect(formatTime("13:00", "en")).toBe("1 PM"); expect(formatTime("09:05", "fr")).toBe("9 h05"); }); it("does nothing without a selection or with a malformed one", () => { expect(run(input(null)).operations).toEqual([]); expect(run(input("{not json")).operations).toEqual([]); expect(run(input(sel({ m: "teleport" }))).operations).toEqual([]); expect(run(input(sel({ d: "07/11/2026" }))).operations).toEqual([]); }); it("renames only local-delivery options for a delivery choice", () => { expect(renames(run(input(sel())))).toEqual([["local", "Local Delivery — Sat, Nov 7, 10 AM–1 PM"]]); }); it("renames only pickup options for a pickup choice", () => { expect(renames(run(input(sel({ m: "pickup", s: "14:00-16:00" }))))).toEqual([["pick", "Kitchener — Guelph St — Sat, Nov 7, 2 PM–4 PM"]]); }); it("keeps carrier names distinct and adds the ship date for shipping", () => { expect(renames(run(input(sel({ m: "shipping" }))))).toEqual([ ["std", "Standard (ships Sat, Nov 7)"], ["exp", "Express (ships Sat, Nov 7)"], ]); }); it("uses the buyer's language", () => { expect(renames(run(input(sel(), "FR")))[0][1]).toBe("Local Delivery — sam. 7 nov., 10 h–13 h"); }); it("omits the time when the slot isn't a time range", () => { expect(renames(run(input(sel({ s: "*" }))))[0][1]).toBe("Local Delivery — Sat, Nov 7"); }); });