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:
Marco
2026-07-22 11:40:31 +00:00
parent 91f6fef6ea
commit 51fee198f4
9 changed files with 1101 additions and 26 deletions
+2 -16
View File
@@ -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;