Files
cfw-autumn/server/tests/attach/prepaid/prepaid2.backup.ts
2025-10-27 14:13:40 +00:00

150 lines
3.3 KiB
TypeScript

import {
type AppEnv,
LegacyVersion,
OnDecrease,
OnIncrease,
type Organization,
} from "@autumn/shared";
import chalk from "chalk";
import { addWeeks } from "date-fns";
import type Stripe from "stripe";
import { setupBefore } from "tests/before.js";
import { TestFeature } from "tests/setup/v2Features.js";
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
import { expectProductAttached } from "tests/utils/expectUtils/expectProductAttached.js";
import { createProducts } from "tests/utils/productUtils.js";
import type { DrizzleCli } from "@/db/initDrizzle.js";
import { AutumnInt } from "@/external/autumn/autumnCli.js";
import { constructPrepaidItem } from "@/utils/scriptUtils/constructItem.js";
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
import { advanceTestClock } from "@/utils/scriptUtils/testClockUtils.js";
import { addPrefixToProducts } from "../utils.js";
const testCase = "prepaid2";
export const pro = constructProduct({
items: [
constructPrepaidItem({
featureId: TestFeature.Messages,
billingUnits: 100,
price: 12.5,
config: {
on_increase: OnIncrease.ProrateImmediately,
on_decrease: OnDecrease.None,
},
}),
],
excludeBase: true,
type: "pro",
});
describe(`${chalk.yellowBright(`attach/${testCase}: upgrade quantity, prorate immediately, single use`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: LegacyVersion.v1_4 });
let testClockId: string;
let db: DrizzleCli, org: Organization, env: AppEnv;
let stripeCli: Stripe;
const curUnix = new Date().getTime();
before(async function () {
await setupBefore(this);
const { autumnJs } = this;
db = this.db;
org = this.org;
env = this.env;
stripeCli = this.stripeCli;
const res = await initCustomer({
autumn: autumnJs,
customerId,
db,
org,
env,
attachPm: "success",
});
addPrefixToProducts({
products: [pro],
prefix: testCase,
});
await createProducts({
autumn,
products: [pro],
db,
orgId: org.id,
env,
});
testClockId = res.testClockId!;
});
const options = [
{
feature_id: TestFeature.Messages,
quantity: 300,
},
];
it("should attach pro product to customer", async () => {
await attachAndExpectCorrect({
autumn,
customerId,
product: pro,
stripeCli,
db,
org,
env,
options,
});
const customer = await autumn.customers.get(customerId);
expectProductAttached({
customer,
product: pro,
});
});
it("should increase advance test clock, increase quantity to 400 and have correct sub item quantity + invoice..", async () => {
const usage = Math.floor(Math.random() * 220);
await autumn.track({
customer_id: customerId,
feature_id: TestFeature.Messages,
value: usage,
});
await advanceTestClock({
stripeCli,
testClockId,
advanceTo: addWeeks(new Date(), 2).getTime(),
waitForSeconds: 30,
});
await attachAndExpectCorrect({
autumn,
customerId,
product: pro,
stripeCli,
db,
org,
env,
options: [
{
feature_id: TestFeature.Messages,
quantity: 400,
},
],
usage: [
{
featureId: TestFeature.Messages,
value: usage,
},
],
waitForInvoice: 5000,
});
});
});