import { describe, expect, it } from "vitest"; import { cartValidationsGenerateRun as run } from "../src/cart_validations_generate_run.js"; const line = (amount, qty = 1, fee = false) => ({ quantity: qty, cost: { amountPerQuantity: { amount: String(amount) } }, merchandise: { __typename: "ProductVariant", requiresShipping: true, product: { isFee: fee } }, }); const input = (o = {}) => ({ buyerJourney: { step: "CHECKOUT_COMPLETION" }, cart: { selection: o.sel === undefined ? null : { value: o.sel }, lines: o.lines ?? [line("19.99", 2)] }, shop: { localTime: { date: o.today ?? "2026-11-05" } }, validation: { metafield: o.config === undefined ? { jsonValue: { requireScheduling: true } } : o.config }, }); const sel = (o = {}) => JSON.stringify({ t: "tok_aaaaaaaaaaaaaaaa", m: "delivery", l: "kit", d: "2026-11-07", s: "10:00-13:00", f: 0, ...o }); const msgs = (r) => r.operations.flatMap((op) => op.validationAdd.errors.map((e) => e.message)); describe("checkout guard", () => { it("never blocks while adding to cart", () => { expect(run({ ...input(), buyerJourney: { step: "CART_INTERACTION" } }).operations).toEqual([]); }); it("blocks a missing time when required", () => { expect(msgs(run(input()))[0]).toMatch(/choose a delivery or pickup time/); }); it("allows a missing time when not required", () => { expect(run(input({ config: { jsonValue: { requireScheduling: false } } })).operations).toEqual([]); }); it("defaults to required when the metafield is missing", () => { expect(msgs(run(input({ config: null }))).length).toBe(1); }); it("passes a valid selection with no fee", () => { expect(run(input({ sel: sel() })).operations).toEqual([]); }); it("blocks a past date", () => { expect(msgs(run(input({ sel: sel({ d: "2026-11-04" }) })))[0]).toMatch(/passed/); }); it("checks the fee line against the quote", () => { const ok = input({ sel: sel({ f: 1299 }), lines: [line("19.99"), line("12.99", 1, true)] }); expect(run(ok).operations).toEqual([]); const tampered = input({ sel: sel({ f: 0 }), lines: [line("19.99"), line("12.99", 1, true)] }); expect(msgs(run(tampered))[0]).toMatch(/fee doesn't match/); const removed = input({ sel: sel({ f: 1299 }), lines: [line("19.99")] }); expect(msgs(run(removed))[0]).toMatch(/fee doesn't match/); }); it("blocks a fee without a selection", () => { const r = run(input({ config: { jsonValue: { requireScheduling: false } }, lines: [line("5"), line("7.99", 1, true)] })); expect(msgs(r)[0]).toMatch(/no delivery time/); }); it("treats malformed JSON as missing", () => { expect(msgs(run(input({ sel: "{oops" })))[0]).toMatch(/choose a delivery/); }); });