import { describe, expect, it } from "vitest"; import { matchesRule, resolveProductRuleConstraints, type ProductRuleLike, type ProductRef } from "../../app/services/product-rules.server"; function rule(overrides: Partial = {}): ProductRuleLike { return { scopeType: "vendor", scopeValue: "Acme", allowedMethods: [], leadTimeMin: null, allowedLocationIds: [], active: true, ...overrides, }; } function product(overrides: Partial = {}): ProductRef { return { id: "gid://shopify/Product/1", vendor: "Acme", productType: "Furniture", tags: ["fragile"], collectionIds: ["gid://shopify/Collection/1"], ...overrides, }; } describe("matchesRule", () => { it("does not match an inactive rule", () => { expect(matchesRule(rule({ active: false }), product())).toBe(false); }); it("matches vendor case-insensitively", () => { expect(matchesRule(rule({ scopeType: "vendor", scopeValue: "acme" }), product({ vendor: "Acme" }))).toBe(true); }); it("does not match a different vendor", () => { expect(matchesRule(rule({ scopeType: "vendor", scopeValue: "Acme" }), product({ vendor: "Other" }))).toBe(false); }); it("matches product type case-insensitively", () => { expect(matchesRule(rule({ scopeType: "type", scopeValue: "FURNITURE" }), product({ productType: "Furniture" }))).toBe(true); }); it("matches a tag case-insensitively", () => { expect(matchesRule(rule({ scopeType: "tag", scopeValue: "Fragile" }), product({ tags: ["fragile", "new"] }))).toBe(true); }); it("does not match a missing tag", () => { expect(matchesRule(rule({ scopeType: "tag", scopeValue: "clearance" }), product({ tags: ["fragile"] }))).toBe(false); }); it("matches product id exactly", () => { expect(matchesRule(rule({ scopeType: "product", scopeValue: "gid://shopify/Product/1" }), product())).toBe(true); }); it("matches collection id membership", () => { expect( matchesRule(rule({ scopeType: "collection", scopeValue: "gid://shopify/Collection/1" }), product()), ).toBe(true); }); it("an unrecognized scope type never matches", () => { expect(matchesRule(rule({ scopeType: "bogus" }), product())).toBe(false); }); }); describe("resolveProductRuleConstraints", () => { it("returns unrestricted defaults when nothing matches", () => { const result = resolveProductRuleConstraints([rule({ scopeValue: "Other" })], [product()]); expect(result).toEqual({ minLeadTimeMin: 0, allowedMethods: null, allowedLocationIds: null }); }); it("takes the max lead time across multiple matching rules", () => { const rules = [ rule({ scopeType: "vendor", scopeValue: "Acme", leadTimeMin: 60 }), rule({ scopeType: "tag", scopeValue: "fragile", leadTimeMin: 1440 }), ]; const result = resolveProductRuleConstraints(rules, [product()]); expect(result.minLeadTimeMin).toBe(1440); }); it("intersects allowedMethods across matching rules", () => { const rules = [ rule({ scopeType: "vendor", scopeValue: "Acme", allowedMethods: ["PICKUP", "LOCAL_DELIVERY"] }), rule({ scopeType: "tag", scopeValue: "fragile", allowedMethods: ["PICKUP", "SHIPPING"] }), ]; const result = resolveProductRuleConstraints(rules, [product()]); expect(result.allowedMethods).toEqual(["PICKUP"]); }); it("intersecting to nothing blocks every method (empty array, not null)", () => { const rules = [ rule({ scopeType: "vendor", scopeValue: "Acme", allowedMethods: ["PICKUP"] }), rule({ scopeType: "tag", scopeValue: "fragile", allowedMethods: ["SHIPPING"] }), ]; const result = resolveProductRuleConstraints(rules, [product()]); expect(result.allowedMethods).toEqual([]); }); it("a rule with no allowedMethods restriction doesn't narrow an already-restricted set", () => { const rules = [ rule({ scopeType: "vendor", scopeValue: "Acme", allowedMethods: ["PICKUP"] }), rule({ scopeType: "tag", scopeValue: "fragile", allowedMethods: [] }), ]; const result = resolveProductRuleConstraints(rules, [product()]); expect(result.allowedMethods).toEqual(["PICKUP"]); }); it("intersects allowedLocationIds the same way as allowedMethods", () => { const rules = [ rule({ scopeType: "vendor", scopeValue: "Acme", allowedLocationIds: ["loc_1", "loc_2"] }), rule({ scopeType: "tag", scopeValue: "fragile", allowedLocationIds: ["loc_2"] }), ]; const result = resolveProductRuleConstraints(rules, [product()]); expect(result.allowedLocationIds).toEqual(["loc_2"]); }); it("only rules matching something in the cart are combined — a non-matching rule is ignored entirely", () => { const rules = [ rule({ scopeType: "vendor", scopeValue: "Other", leadTimeMin: 999, allowedMethods: ["PICKUP"] }), rule({ scopeType: "vendor", scopeValue: "Acme", leadTimeMin: 30 }), ]; const result = resolveProductRuleConstraints(rules, [product()]); expect(result).toEqual({ minLeadTimeMin: 30, allowedMethods: null, allowedLocationIds: null }); }); it("multiple cart products each contribute their own matching rules", () => { const rules = [rule({ scopeType: "vendor", scopeValue: "Acme", leadTimeMin: 30 }), rule({ scopeType: "vendor", scopeValue: "Other", leadTimeMin: 90 })]; const products = [product({ vendor: "Acme" }), product({ id: "gid://shopify/Product/2", vendor: "Other" })]; const result = resolveProductRuleConstraints(rules, products); expect(result.minLeadTimeMin).toBe(90); }); });