Add a Vitest unit test suite (cart totals, invoice tax grouping, bundle contents)
No test infrastructure existed in this repo yet. Covers the pure logic most likely to silently produce wrong numbers on a live order: discount/ shipping math, per-rate invoice grouping, and bundle-contents string building. Extracted describeBundleContents() out of the checkout route into its own module so it's importable from a test (route.ts files only allow HTTP-method exports).
This commit is contained in:
@@ -4,25 +4,11 @@ import { getShippingMethods, getPaymentMethods, getCompanySettings } from "../..
|
||||
import { validateDiscountCode, redeemDiscountCode } from "../../lib/discountServer";
|
||||
import { createOrder } from "../../lib/orderServer";
|
||||
import { getSessionCustomer, registerCustomer, setSessionCookie, type CustomerSummary } from "../../lib/customerAuth";
|
||||
import { fetchProductsBySlug, type RawProduct } from "../../lib/productsServer";
|
||||
import { fetchProductsBySlug } from "../../lib/productsServer";
|
||||
import { describeBundleContents } from "../../lib/bundleContents";
|
||||
import { sendCriticalAlert } from "../../lib/alertAdmin";
|
||||
import { sendOrderConfirmationEmail } from "../../lib/orderEmail";
|
||||
|
||||
// A bundle's `bundleItems` is only ever resolved once, right here, into a
|
||||
// plain readable string — the order line stores this snapshot
|
||||
// (`items[].bundleContents`), not a structured sub-list, so a later change
|
||||
// to the bundle's own composition can never rewrite what a past order
|
||||
// actually contained. `product.bundleItems[].product` is a relationship
|
||||
// resolved by fetchProductsBySlug's depth:2 fetch — {id, name} objects
|
||||
// when populated, a bare id if depth somehow didn't reach it (skipped).
|
||||
function describeBundleContents(product: RawProduct): string | null {
|
||||
if (!product.bundleItems || product.bundleItems.length === 0) return null;
|
||||
return product.bundleItems
|
||||
.map((line) => (typeof line.product === "object" ? `${line.quantity}× ${line.product.name}` : null))
|
||||
.filter((s): s is string => Boolean(s))
|
||||
.join(", ");
|
||||
}
|
||||
|
||||
type CheckoutBody = {
|
||||
cart: CartItem[];
|
||||
shippingMethodId: number;
|
||||
|
||||
Reference in New Issue
Block a user