Closes remaining DS-parity gaps from the feature audit: - ProductRule model (product/collection/vendor/type/tag scoping) with real server-side enforcement in hold-request.server.ts, plus shaped availability in availability-request.server.ts. Covers per-product prep time, cart-content-based slot blocking, and product-restricted locations in one mechanism. New /app/rules admin page (Growth+). - Driving-distance delivery zones via Google's Distance Matrix API, cached like existing geocoding results. - SHIPPING-only estimated arrival range (transitMinDays/transitMaxDays on SlotTemplate) — widget shows "Arrives Thu-Sat" instead of a meaningless ship-out time slot; carried through to the order metafield write-back. Storefront widget and POS extension now send cart contents (vendor/ type from cart.js, product ids for Admin-API-resolved collection/tag rules) to both availability and hold endpoints. checkout-datetime remains excluded from this deploy pending Shopify's Network Access approval (unrelated to this work) — re-add from ../checkout-datetime-disabled and redeploy once granted. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
134 lines
5.5 KiB
TypeScript
134 lines
5.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { matchesRule, resolveProductRuleConstraints, type ProductRuleLike, type ProductRef } from "../../app/services/product-rules.server";
|
|
|
|
function rule(overrides: Partial<ProductRuleLike> = {}): ProductRuleLike {
|
|
return {
|
|
scopeType: "vendor",
|
|
scopeValue: "Acme",
|
|
allowedMethods: [],
|
|
leadTimeMin: null,
|
|
allowedLocationIds: [],
|
|
active: true,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function product(overrides: Partial<ProductRef> = {}): 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);
|
|
});
|
|
});
|