added a ton of tests for new plan, immediate and scheduled switch

This commit is contained in:
John Yeo
2026-02-02 14:48:30 +00:00
parent fa893306bf
commit 78f1d131e5
157 changed files with 18692 additions and 1634 deletions

View File

@@ -0,0 +1,109 @@
import { expect, test } from "bun:test";
import { TestFeature } from "@tests/setup/v2Features.js";
import { expectAutumnError } from "@tests/utils/expectUtils/expectErrUtils.js";
import { products } from "@tests/utils/fixtures/products.js";
import { initScenario, s } from "@tests/utils/testInitUtils/initScenario.js";
import chalk from "chalk";
import { constructPrepaidItem } from "@/utils/scriptUtils/constructItem.js";
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 1: includedUsage must be a multiple of billingUnits (or 0)
// ═══════════════════════════════════════════════════════════════════════════════
/**
* Scenario:
* - Create a prepaid product where includedUsage is NOT a multiple of billingUnits
* - Attempt to attach the product
*
* Why this fails:
* - When creating Stripe tiered pricing, the first tier's `up_to` is calculated as:
* `includedUsage / billingUnits`
* - If this results in a non-integer (e.g., 50 / 100 = 0.5), Stripe rejects it
* - Stripe requires `up_to` to be a positive integer or "inf"
*
* Expected Result:
* - Error thrown during attach (when Stripe price creation fails)
*/
test.concurrent(`${chalk.yellowBright("error: prepaid includedUsage must be multiple of billingUnits")}`, async () => {
const customerId = "prepaid-error-invalid-included-usage";
const billingUnits = 100;
// Invalid: 50 is NOT a multiple of 100
const invalidPrepaidItem = constructPrepaidItem({
featureId: TestFeature.Messages,
includedUsage: 50, // 50 / 100 = 0.5 → invalid for Stripe tiers
billingUnits,
price: 10,
});
const pro = products.pro({
id: "pro-invalid-prepaid",
items: [invalidPrepaidItem],
});
const { autumnV1 } = await initScenario({
customerId,
setup: [
s.customer({ paymentMethod: "success" }),
s.products({ list: [pro] }),
],
actions: [],
});
// Attempt to attach should fail due to invalid Stripe tier configuration
await expectAutumnError({
func: async () => {
await autumnV1.billing.attach({
customer_id: customerId,
product_id: pro.id,
options: [{ feature_id: TestFeature.Messages, quantity: 100 }],
});
},
});
});
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 2: Valid prepaid configurations (sanity check)
// ═══════════════════════════════════════════════════════════════════════════════
/**
* These should all work:
* - includedUsage: 0 (no free tier)
* - includedUsage: 100 (exactly 1 billing unit)
* - includedUsage: 200 (exactly 2 billing units)
*/
test.concurrent(`${chalk.yellowBright("prepaid: valid includedUsage multiples work correctly")}`, async () => {
const customerId = "prepaid-valid-included-usage";
const billingUnits = 100;
// Valid: 0 is a valid multiple (no free tier)
const validPrepaidZero = constructPrepaidItem({
featureId: TestFeature.Messages,
includedUsage: 0,
billingUnits,
price: 10,
});
const pro = products.pro({
id: "pro-valid-prepaid",
items: [validPrepaidZero],
});
const { autumnV1 } = await initScenario({
customerId,
setup: [
s.customer({ paymentMethod: "success" }),
s.products({ list: [pro] }),
],
actions: [],
});
// This should succeed
const result = await autumnV1.billing.attach({
customer_id: customerId,
product_id: pro.id,
options: [{ feature_id: TestFeature.Messages, quantity: 100 }],
});
expect(result.code).toBe("success");
});

View File

@@ -0,0 +1,307 @@
/**
* Stripe Checkout Error Tests (Attach V2)
*
* Tests for error handling in Stripe Checkout flows.
*
* Key error scenarios:
* - Multi-interval products cannot use Stripe checkout (monthly + annual in same product)
*/
import { expect, test } from "bun:test";
import { ErrCode } from "@autumn/shared";
import { TestFeature } from "@tests/setup/v2Features";
import { expectAutumnError } from "@tests/utils/expectUtils/expectErrUtils";
import { items } from "@tests/utils/fixtures/items";
import { products } from "@tests/utils/fixtures/products";
import { initScenario, s } from "@tests/utils/testInitUtils/initScenario";
import chalk from "chalk";
import { constructPrepaidItem } from "@/utils/scriptUtils/constructItem";
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 1: Multi-interval checkout error (monthly + annual prepaid)
// ═══════════════════════════════════════════════════════════════════════════════
/**
* Scenario:
* - Customer with NO payment method (triggers checkout flow)
* - Product has prepaid items with different intervals (monthly + annual)
*
* Why this fails:
* - Stripe checkout sessions can only handle one recurring interval
* - Having monthly and annual prepaid in same checkout is not supported
*
* Expected Result:
* - Error thrown: "Cannot create Stripe checkout when there are multiple intervals"
*/
test.concurrent(`${chalk.yellowBright("error: multi-interval checkout not supported")}`, async () => {
const customerId = "stripe-checkout-error-multi-interval";
// Monthly prepaid messages: $10/pack (100 units)
const monthlyPrepaidItem = items.prepaidMessages({
includedUsage: 0,
billingUnits: 100,
price: 10,
});
// Annual prepaid words: $50/pack (100 units)
const annualPrepaidItem = constructPrepaidItem({
featureId: TestFeature.Words,
includedUsage: 0,
billingUnits: 100,
price: 50,
intervalCount: 12, // Annual (12 months)
});
// Product with both monthly and annual prepaid
const mixedIntervalProduct = products.base({
id: "mixed-interval",
items: [monthlyPrepaidItem, annualPrepaidItem],
});
const { autumnV1 } = await initScenario({
customerId,
setup: [
s.customer({ testClock: true }), // No payment method → checkout flow
s.products({ list: [mixedIntervalProduct] }),
],
actions: [],
});
// Attempt to attach should fail due to multi-interval checkout
await expectAutumnError({
errCode: ErrCode.InvalidRequest,
func: async () => {
await autumnV1.billing.attach({
customer_id: customerId,
product_id: mixedIntervalProduct.id,
options: [
{ feature_id: TestFeature.Messages, quantity: 100 },
{ feature_id: TestFeature.Words, quantity: 100 },
],
});
},
});
});
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 2: Multi-interval checkout error with allocated users + entities
// ═══════════════════════════════════════════════════════════════════════════════
/**
* Scenario:
* - Customer with NO payment method (triggers checkout flow)
* - Create 5 user entities BEFORE attach
* - Product has monthly allocated users + annual prepaid words
*
* Why this fails:
* - Stripe checkout sessions can only handle one recurring interval
* - Having monthly allocated users and annual prepaid in same checkout is not supported
*
* Expected Result:
* - Error thrown: "Cannot create Stripe checkout when there are multiple intervals"
*/
test.concurrent(`${chalk.yellowBright("error: multi-interval checkout with allocated users")}`, async () => {
const customerId = "stripe-checkout-error-multi-allocated";
const userCount = 5;
// Monthly allocated users: $10/user
const allocatedUsersItem = items.allocatedUsers({
includedUsage: 0,
});
// Annual prepaid words: $50/pack (100 units)
const annualPrepaidItem = constructPrepaidItem({
featureId: TestFeature.Words,
includedUsage: 0,
billingUnits: 100,
price: 50,
intervalCount: 12, // Annual (12 months)
});
// Product with both monthly allocated users and annual prepaid
const mixedIntervalProduct = products.base({
id: "mixed-interval-allocated",
items: [allocatedUsersItem, annualPrepaidItem],
});
const { autumnV1, entities } = await initScenario({
customerId,
setup: [
s.customer({ testClock: true }), // No payment method → checkout flow
s.products({ list: [mixedIntervalProduct] }),
// Create 5 user entities BEFORE attach
s.entities({ count: userCount, featureId: TestFeature.Users }),
],
actions: [],
});
// Verify we have 5 entities
expect(entities.length).toBe(userCount);
// Attempt to attach should fail due to multi-interval checkout
await expectAutumnError({
errCode: ErrCode.InvalidRequest,
func: async () => {
await autumnV1.billing.attach({
customer_id: customerId,
product_id: mixedIntervalProduct.id,
options: [{ feature_id: TestFeature.Words, quantity: 100 }],
});
},
});
});
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 3: Zero price checkout error (allocated messages, no usage)
// ═══════════════════════════════════════════════════════════════════════════════
/**
* Scenario:
* - Customer with NO payment method (triggers checkout flow)
* - Base product with allocated messages (no base price, no usage)
*
* Why this fails:
* - Stripe checkout doesn't allow $0 total
* - Allocated with no usage = $0
*
* Expected Result:
* - Error thrown
*/
test.concurrent(`${chalk.yellowBright("error: zero price checkout (allocated, no usage)")}`, async () => {
const customerId = "stripe-checkout-error-zero-allocated";
// Allocated messages: $10/unit, no included usage
const allocatedMessagesItem = items.allocatedMessages({ includedUsage: 0 });
// Base product (no base price) with allocated messages
const base = products.base({
id: "zero-allocated",
items: [allocatedMessagesItem],
});
const { autumnV1 } = await initScenario({
customerId,
setup: [
s.customer({ testClock: true }), // No payment method → checkout flow
s.products({ list: [base] }),
],
actions: [],
});
// Attempt to attach should fail - $0 total not allowed in checkout
await expectAutumnError({
func: async () => {
await autumnV1.billing.attach({
customer_id: customerId,
product_id: base.id,
});
},
});
});
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 4: Zero price checkout error (prepaid messages, quantity 0)
// ═══════════════════════════════════════════════════════════════════════════════
/**
* Scenario:
* - Customer with NO payment method (triggers checkout flow)
* - Base product with prepaid messages, attach with quantity: 0
*
* Why this fails:
* - Stripe checkout doesn't allow $0 total
* - Prepaid with quantity 0 + no base price = $0
*
* Expected Result:
* - Error thrown
*/
test.concurrent(`${chalk.yellowBright("error: zero price checkout (prepaid, quantity 0)")}`, async () => {
const customerId = "stripe-checkout-error-zero-prepaid";
// Prepaid messages: $10/pack (100 units), no included usage
const prepaidMessagesItem = items.prepaidMessages({
includedUsage: 0,
billingUnits: 100,
price: 10,
});
// Base product (no base price) with prepaid messages
const base = products.base({
id: "zero-prepaid",
items: [prepaidMessagesItem],
});
const { autumnV1 } = await initScenario({
customerId,
setup: [
s.customer({ testClock: true }), // No payment method → checkout flow
s.products({ list: [base] }),
],
actions: [],
});
// Attempt to attach with quantity 0 should fail - $0 total not allowed in checkout
await expectAutumnError({
func: async () => {
await autumnV1.billing.attach({
customer_id: customerId,
product_id: base.id,
options: [{ feature_id: TestFeature.Messages, quantity: 0 }],
});
},
});
});
// ═══════════════════════════════════════════════════════════════════════════════
// TEST 5: One-off + recurring is allowed in checkout
// ═══════════════════════════════════════════════════════════════════════════════
/**
* Scenario:
* - Customer with NO payment method
* - Product has one-off item + recurring item
*
* Expected Result:
* - Should work because one-off is excluded from interval check
*/
test.concurrent(`${chalk.yellowBright("checkout: one-off + recurring works")}`, async () => {
const customerId = "stripe-checkout-oneoff-recurring";
// Monthly prepaid messages
const monthlyPrepaidItem = items.prepaidMessages({
includedUsage: 0,
billingUnits: 100,
price: 10,
});
// One-off messages
const oneOffItem = items.oneOffMessages({
includedUsage: 0,
billingUnits: 100,
price: 25,
});
const mixedProduct = products.pro({
id: "oneoff-recurring",
items: [monthlyPrepaidItem, oneOffItem],
});
const { autumnV1 } = await initScenario({
customerId,
setup: [
s.customer({ testClock: true }), // No payment method → checkout
s.products({ list: [mixedProduct] }),
],
actions: [],
});
// Should succeed - one-off doesn't count toward interval check
const result = await autumnV1.billing.attach({
customer_id: customerId,
product_id: mixedProduct.id,
options: [{ feature_id: TestFeature.Messages, quantity: 100 }],
});
expect(result.payment_url).toBeDefined();
expect(result.payment_url).toContain("checkout.stripe.com");
});