143 lines
3.4 KiB
TypeScript
143 lines
3.4 KiB
TypeScript
import { beforeAll, describe, test } from "bun:test";
|
|
import { type AppEnv, LegacyVersion, type Organization } from "@autumn/shared";
|
|
import chalk from "chalk";
|
|
import { addWeeks } from "date-fns";
|
|
import type Stripe from "stripe";
|
|
import { TestFeature } from "tests/setup/v2Features.js";
|
|
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
|
|
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
|
import ctx from "tests/utils/testInitUtils/createTestContext.js";
|
|
import type { DrizzleCli } from "@/db/initDrizzle.js";
|
|
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
|
import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";
|
|
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
|
import { initCustomerV3 } from "@/utils/scriptUtils/testUtils/initCustomerV3.js";
|
|
import { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
|
|
|
|
// Shared products for attach tests
|
|
const testCase = "upgrade2";
|
|
export const pro = constructProduct({
|
|
id: "pro",
|
|
items: [constructArrearItem({ featureId: TestFeature.Words })],
|
|
type: "pro",
|
|
});
|
|
|
|
export const proAnnual = constructProduct({
|
|
id: "pro_annual",
|
|
items: [constructArrearItem({ featureId: TestFeature.Words })],
|
|
type: "pro",
|
|
isAnnual: true,
|
|
});
|
|
|
|
export const premiumAnnual = constructProduct({
|
|
id: "premium_annual",
|
|
items: [constructArrearItem({ featureId: TestFeature.Words })],
|
|
type: "premium",
|
|
isAnnual: true,
|
|
});
|
|
|
|
/**
|
|
* upgrade2:
|
|
* 1. Start with pro monthly plan (usage-based)
|
|
* 2. Upgrade to pro annual plan (usage-based)
|
|
* 3. Upgrade to premium annual plan (usage-based)
|
|
*
|
|
* Verifies subscription items and anchors are correct after each upgrade
|
|
*/
|
|
|
|
describe(`${chalk.yellowBright("upgrade2: Testing usage upgrades with monthly -> annual")}`, () => {
|
|
const customerId = "upgrade2";
|
|
const autumn: AutumnInt = new AutumnInt({ version: LegacyVersion.v1_4 });
|
|
let stripeCli: Stripe;
|
|
let testClockId: string;
|
|
let db: DrizzleCli, org: Organization, env: AppEnv;
|
|
|
|
let curUnix = new Date().getTime();
|
|
|
|
beforeAll(async () => {
|
|
db = ctx.db;
|
|
org = ctx.org;
|
|
env = ctx.env;
|
|
stripeCli = ctx.stripeCli;
|
|
|
|
const { testClockId: testClockId1 } = await initCustomerV3({
|
|
ctx,
|
|
customerId,
|
|
customerData: {},
|
|
attachPm: "success",
|
|
withTestClock: true,
|
|
});
|
|
|
|
await initProductsV0({
|
|
ctx,
|
|
products: [pro, proAnnual, premiumAnnual],
|
|
prefix: testCase,
|
|
});
|
|
|
|
testClockId = testClockId1!;
|
|
});
|
|
|
|
test("should attach pro product", async () => {
|
|
await attachAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: pro,
|
|
stripeCli,
|
|
db,
|
|
org,
|
|
env,
|
|
});
|
|
});
|
|
|
|
test("should attach pro annual product", async () => {
|
|
await autumn.track({
|
|
customer_id: customerId,
|
|
feature_id: TestFeature.Words,
|
|
value: 100000,
|
|
});
|
|
|
|
curUnix = await advanceTestClock({
|
|
stripeCli,
|
|
testClockId,
|
|
advanceTo: addWeeks(curUnix, 2).getTime(),
|
|
});
|
|
return;
|
|
|
|
await attachAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: proAnnual,
|
|
stripeCli,
|
|
db,
|
|
org,
|
|
env,
|
|
});
|
|
});
|
|
return;
|
|
|
|
test("should attach premium annual product", async () => {
|
|
await autumn.track({
|
|
customer_id: customerId,
|
|
feature_id: TestFeature.Words,
|
|
value: 5000000,
|
|
});
|
|
|
|
await advanceTestClock({
|
|
stripeCli,
|
|
testClockId,
|
|
advanceTo: addWeeks(curUnix, 1).getTime(),
|
|
waitForSeconds: 10,
|
|
});
|
|
|
|
await attachAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: premiumAnnual,
|
|
stripeCli,
|
|
db,
|
|
org,
|
|
env,
|
|
});
|
|
});
|
|
});
|