metatrondelivery/tests/unit/product-rules.test.ts
metatroncubeswdev a2c78d703f
Some checks failed
CI / Lint, Unit & Integration Tests (push) Has been cancelled
feat: close DS study coverage gaps (inventory exclusion, live checkout re-validation, per-day cap, payment fn, checkout ext)
Audited the implementation against DS_Delivery_Date_Time_App_Study.docx and
closed the actionable gaps (see IMPLEMENTATION_REVIEW_2026-09-04.md).

Core (code + unit tests, 156 green):
- Wire excludeLocationsWithoutStock into resolveAvailabilityRequest; widget
  now sends variantIds so inventory-based location exclusion actually runs.
- Live slot re-validation at checkout: new checkout-snapshot.server.ts writes
  a shop-metafield capacity snapshot; validation-slot's evaluateCheckout
  rejects a complete selection that has since filled / blacked out / closed /
  hit the daily cap / left the schedule. Refreshed on order webhooks and
  slot/blackout/location/enforcement edits.
- Scopable checkout enforcement: Shop.enforcementMode (all|tagged|off) +
  enforcementTag, new app.settings.tsx admin page, honoured via the snapshot.
- Per-day order cap: Location.dailyOrderCap threaded through getAvailability
  (dailyCap + consumedPerDate); admin field on the location screen.
- Product-rule slot blocking: ProductRule.blockedStartMins, unioned in
  resolveProductRuleConstraints, enforced in the engine and resolveHoldRequest;
  admin field on the product rules screen.
- Product-page placement: product-availability.liquid block + widget
  data-mode="preview" (read-only earliest-date line).
- Second locale: datetime-widget fr.json / fr.schema.json.
- Migration 20260904120000_review_gaps (apply with prisma migrate deploy).

New Functions (source + unit tests; need `shopify app deploy` to ship):
- extensions/payment-customization: cart.payment-methods.transform.run — hides
  cash-on-delivery / pay-in-store gateways on SHIPPING orders.
- extensions/checkout-datetime/src: restored from a gitignored dist-only state
  — Plus native picker + Thank you / Order status confirmation blocks, all
  calling the existing checkout.scheduling.* routes (one capacity pool).
  tsconfig ships checkJs:false pending reconciliation with live checkout types.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-04 01:31:02 -04:00

143 lines
5.9 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, blockedStartMins: [] });
});
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, blockedStartMins: [] });
});
it("unions blockedStartMins across matching rules, sorted and de-duped", () => {
const rules = [
rule({ scopeType: "vendor", scopeValue: "Acme", blockedStartMins: [540, 600] }),
rule({ scopeType: "tag", scopeValue: "fragile", blockedStartMins: [600, 480] }),
];
const result = resolveProductRuleConstraints(rules, [product()]);
expect(result.blockedStartMins).toEqual([480, 540, 600]);
});
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);
});
});