Files
cfw-autumn/server/tests/advanced/rollovers/rollover6.test.ts
amianthus 4b9ce6af70 test: 💍 bun
2025-10-31 11:08:00 +00:00

133 lines
3.5 KiB
TypeScript

import {
type Customer,
LegacyVersion,
type LimitedItem,
RolloverDuration,
} from "@autumn/shared";
import { beforeAll, describe, expect, test } from "bun:test";
import chalk from "chalk";
import { addHours, addMonths } from "date-fns";
import type Stripe from "stripe";
import ctx from "tests/utils/testInitUtils/createTestContext.js";
import { TestFeature } from "tests/setup/v2Features.js";
import { hoursToFinalizeInvoice } from "tests/utils/constants.js";
import { AutumnInt } from "@/external/autumn/autumnCli.js";
import { constructFeatureItem } 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";
import { advanceTestClock } from "@/utils/scriptUtils/testClockUtils.js";
import { resetAndGetCusEnt } from "./rolloverTestUtils.js";
const freeRollover = { max: 600, length: 1, duration: RolloverDuration.Month };
const proRollover = { max: 1000, length: 1, duration: RolloverDuration.Month };
const freeMsges = constructFeatureItem({
featureId: TestFeature.Messages,
includedUsage: 500,
rolloverConfig: freeRollover,
}) as LimitedItem;
const proMsges = constructFeatureItem({
featureId: TestFeature.Messages,
includedUsage: 500,
rolloverConfig: proRollover,
}) as LimitedItem;
const free = constructProduct({
items: [freeMsges],
type: "free",
isDefault: false,
});
const pro = constructProduct({
items: [proMsges],
type: "pro",
});
const testCase = "rollover6";
describe(`${chalk.yellowBright(`${testCase}: Testing rollovers for upgrade`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: LegacyVersion.v1_4 });
let testClockId: string;
let customer: Customer;
let stripeCli: Stripe;
const curUnix = new Date().getTime();
beforeAll(async () => {
stripeCli = ctx.stripeCli;
await initProductsV0({
ctx,
products: [free, pro],
prefix: testCase,
customerId,
});
const res = await initCustomerV3({
ctx,
customerId,
customerData: {},
attachPm: "success",
withTestClock: true,
});
testClockId = res.testClockId!;
customer = res.customer;
});
test("should attach free product", async () => {
await autumn.attach({
customer_id: customerId,
product_id: pro.id,
});
});
test("should create rollovers", async () => {
await resetAndGetCusEnt({
customer,
db: ctx.db,
productGroup: testCase,
featureId: TestFeature.Messages,
});
await resetAndGetCusEnt({
customer,
db: ctx.db,
productGroup: testCase,
featureId: TestFeature.Messages,
});
// Attach pro
await autumn.attach({
customer_id: customerId,
product_id: free.id,
});
await advanceTestClock({
stripeCli,
testClockId,
advanceTo: addHours(
addMonths(curUnix, 1),
hoursToFinalizeInvoice,
).getTime(),
waitForSeconds: 20,
});
const cus = await autumn.customers.get(customerId);
const msgesFeature = cus.features[TestFeature.Messages];
const proRolloverBalance = proMsges.included_usage * 2;
const freeRolloverBalance = Math.min(freeRollover.max, proRolloverBalance);
expect(msgesFeature).toBeDefined();
expect(msgesFeature?.balance).toBe(
freeMsges.included_usage + freeRolloverBalance,
);
// @ts-expect-error
const rollovers = msgesFeature?.rollovers;
expect(rollovers[0].balance).toBe(100);
expect(rollovers[1].balance).toBe(500);
});
});