205 lines
6.8 KiB
TypeScript
205 lines
6.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
reserveRecurringProductCheckoutLedgerWithClient,
|
|
type RecurringCheckoutPolicyClient,
|
|
} from "./recurring-checkout-policy.server";
|
|
|
|
const NOW = new Date("2026-08-03T05:00:00.000Z");
|
|
|
|
function activeSubscription() {
|
|
return {
|
|
id: "subscription_1",
|
|
tenantId: "tenant_1",
|
|
status: "active",
|
|
frozenAt: null,
|
|
cancelledAt: null,
|
|
planCode: "pro",
|
|
officeSeatQuantity: 5,
|
|
fieldSeatQuantity: 15,
|
|
billingCadence: "monthly",
|
|
storageAddOnCodes: [],
|
|
products: [
|
|
{
|
|
id: "base_1",
|
|
subscriptionProductId: "product_team",
|
|
planCode: "pro",
|
|
officeSeatQuantity: 5,
|
|
fieldSeatQuantity: 15,
|
|
billingCadence: "monthly",
|
|
storageAddOnCodes: [],
|
|
cancelledAt: null,
|
|
currentPeriodStart: new Date("2026-08-01T00:00:00.000Z"),
|
|
endsAt: null,
|
|
subscriptionProduct: { kind: "base_plan", code: "pro" },
|
|
prices: [],
|
|
},
|
|
{
|
|
id: "existing_addon_1",
|
|
subscriptionProductId: "product_existing",
|
|
planCode: "existing_addon",
|
|
officeSeatQuantity: null,
|
|
fieldSeatQuantity: null,
|
|
billingCadence: "monthly",
|
|
storageAddOnCodes: [],
|
|
cancelledAt: null,
|
|
currentPeriodStart: new Date("2026-08-01T00:00:00.000Z"),
|
|
endsAt: null,
|
|
subscriptionProduct: { kind: "recurring_add_on", code: "existing_addon" },
|
|
prices: [
|
|
{
|
|
isMetered: false,
|
|
quantity: 1,
|
|
subscriptionPrice: { price: "173.27", currency: "EUR" },
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
function acquisitionInput(productId: string, priceId: string) {
|
|
return {
|
|
tenantId: "tenant_1",
|
|
tenantName: "ACME",
|
|
userId: "user_1",
|
|
userEmail: "admin@example.test",
|
|
userName: "Admin",
|
|
productId,
|
|
priceId,
|
|
scopeKey: "tenant",
|
|
redirectUrl: "https://app.example.test/return",
|
|
webhookUrl: "https://api.example.test/webhook",
|
|
paymentMethod: "ideal",
|
|
};
|
|
}
|
|
|
|
describe("tenant-wide recurring checkout policy", () => {
|
|
it("makes a parallel second add-on include the first durable reservation before enforcing the EUR 1,000 card limit", async () => {
|
|
const openAcquisitions: Array<{
|
|
id: string;
|
|
status: "pending";
|
|
subscriptionProductId: string;
|
|
scopeKey: string;
|
|
amountCents: number;
|
|
currency: string;
|
|
reservationExpiresAt: Date;
|
|
productCode: string;
|
|
}> = [];
|
|
const selectedNetCents = new Map([
|
|
["product_plus_100", 10_000],
|
|
["product_plus_150", 15_000],
|
|
]);
|
|
const client = {
|
|
$queryRaw: vi.fn(async (query: TemplateStringsArray, ...values: unknown[]) => {
|
|
const sql = query.join("");
|
|
if (sql.includes("pg_advisory_xact_lock")) return [{ lock: "" }];
|
|
if (sql.includes('FROM "CheckoutSessionStatus"')) return [];
|
|
if (sql.includes('FROM "TenantProductAcquisition"')) return [...openAcquisitions];
|
|
if (sql.includes('FROM "SubscriptionProduct" product')) {
|
|
const productId = String(values[0]);
|
|
const netAmountCents = selectedNetCents.get(productId);
|
|
return netAmountCents
|
|
? [{
|
|
productId,
|
|
priceId: String(values[1]),
|
|
productCode: productId,
|
|
kind: "recurring_add_on",
|
|
productActive: true,
|
|
productPublic: true,
|
|
hasQuantity: false,
|
|
priceActive: true,
|
|
priceType: 1,
|
|
price: (netAmountCents / 100).toFixed(2),
|
|
currency: "EUR",
|
|
}]
|
|
: [];
|
|
}
|
|
return [];
|
|
}),
|
|
tenantSubscription: {
|
|
findUnique: vi.fn(async () => activeSubscription()),
|
|
},
|
|
} as unknown as RecurringCheckoutPolicyClient;
|
|
const observedPolicyAmounts: number[] = [];
|
|
const reserveLedger = vi.fn(async (input) => {
|
|
observedPolicyAmounts.push(input.paymentPolicyAmountCents!);
|
|
const amountCents = input.productId === "product_plus_100" ? 11_900 : 17_850;
|
|
openAcquisitions.push({
|
|
id: `reservation_${openAcquisitions.length + 1}`,
|
|
status: "pending",
|
|
subscriptionProductId: input.productId,
|
|
scopeKey: input.scopeKey,
|
|
amountCents,
|
|
currency: "EUR",
|
|
reservationExpiresAt: new Date(NOW.getTime() + 15 * 60 * 1000),
|
|
productCode: input.productId,
|
|
});
|
|
return {
|
|
id: openAcquisitions.at(-1)!.id,
|
|
status: "pending",
|
|
productKind: "recurring_add_on",
|
|
scopeKey: input.scopeKey,
|
|
checkoutUrl: null,
|
|
providerPaymentId: null,
|
|
replayed: false,
|
|
} as const;
|
|
});
|
|
|
|
const first = await reserveRecurringProductCheckoutLedgerWithClient(
|
|
client,
|
|
acquisitionInput("product_plus_100", "price_plus_100"),
|
|
{},
|
|
{ reserveLedger, now: () => NOW },
|
|
);
|
|
const second = await reserveRecurringProductCheckoutLedgerWithClient(
|
|
client,
|
|
acquisitionInput("product_plus_150", "price_plus_150"),
|
|
{},
|
|
{ reserveLedger, now: () => NOW },
|
|
);
|
|
|
|
expect(first.paymentPolicyAmountCents).toBe(91_900);
|
|
expect(second.paymentPolicyAmountCents).toBe(109_750);
|
|
expect(observedPolicyAmounts).toEqual([91_900, 109_750]);
|
|
expect(String(client.$queryRaw.mock.calls[0]?.[0])).toContain("pg_advisory_xact_lock");
|
|
expect(String(client.$queryRaw.mock.calls[4]?.[0])).toContain("pg_advisory_xact_lock");
|
|
});
|
|
|
|
it("fails closed while a base-plan checkout owns the tenant policy", async () => {
|
|
const client = {
|
|
$queryRaw: vi.fn(async (query: TemplateStringsArray) => {
|
|
const sql = query.join("");
|
|
if (sql.includes("pg_advisory_xact_lock")) return [{ lock: "" }];
|
|
if (sql.includes('FROM "SubscriptionProduct" product')) {
|
|
return [{
|
|
productId: "product_plus_100",
|
|
priceId: "price_plus_100",
|
|
productCode: "product_plus_100",
|
|
kind: "recurring_add_on",
|
|
productActive: true,
|
|
productPublic: true,
|
|
hasQuantity: false,
|
|
priceActive: true,
|
|
priceType: 1,
|
|
price: "100.00",
|
|
currency: "EUR",
|
|
}];
|
|
}
|
|
if (sql.includes('FROM "CheckoutSessionStatus"')) return [{ id: "base_checkout_open" }];
|
|
return [];
|
|
}),
|
|
tenantSubscription: { findUnique: vi.fn(async () => activeSubscription()) },
|
|
} as unknown as RecurringCheckoutPolicyClient;
|
|
const reserveLedger = vi.fn();
|
|
|
|
await expect(reserveRecurringProductCheckoutLedgerWithClient(
|
|
client,
|
|
acquisitionInput("product_plus_100", "price_plus_100"),
|
|
{},
|
|
{ reserveLedger, now: () => NOW },
|
|
)).rejects.toMatchObject({ code: "IDEMPOTENCY_CONFLICT" });
|
|
expect(reserveLedger).not.toHaveBeenCalled();
|
|
});
|
|
});
|