119 lines
3.0 KiB
TypeScript
119 lines
3.0 KiB
TypeScript
import { type AppEnv, LegacyVersion, type Organization } from "@autumn/shared";
|
|
import { expect } from "chai";
|
|
import chalk from "chalk";
|
|
import { addHours, addMonths } from "date-fns";
|
|
import type Stripe from "stripe";
|
|
import { addPrefixToProducts } from "tests/attach/utils.js";
|
|
import { setupBefore } from "tests/before.js";
|
|
import { TestFeature } from "tests/setup/v2Features.js";
|
|
import { hoursToFinalizeInvoice } from "tests/utils/constants.js";
|
|
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
|
|
import { getExpectedInvoiceTotal } from "tests/utils/expectUtils/expectInvoiceUtils.js";
|
|
import { createProducts } from "tests/utils/productUtils.js";
|
|
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
|
import type { DrizzleCli } from "@/db/initDrizzle.js";
|
|
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
|
import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";
|
|
import { constructRawProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
|
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
|
|
|
const testCase = "customInterval2";
|
|
|
|
export const pro = constructRawProduct({
|
|
id: "pro",
|
|
items: [
|
|
constructArrearItem({
|
|
includedUsage: 0,
|
|
featureId: TestFeature.Words,
|
|
intervalCount: 2,
|
|
}),
|
|
],
|
|
});
|
|
|
|
describe(`${chalk.yellowBright(`${testCase}: Testing custom interval on arrear prorated price`)}`, () => {
|
|
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;
|
|
|
|
before(async function () {
|
|
await setupBefore(this);
|
|
const { autumnJs } = this;
|
|
db = this.db;
|
|
org = this.org;
|
|
env = this.env;
|
|
|
|
stripeCli = this.stripeCli;
|
|
|
|
const { testClockId: testClockId1 } = 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 = testClockId1!;
|
|
});
|
|
|
|
it("should attach pro product", async () => {
|
|
await attachAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: pro,
|
|
stripeCli,
|
|
db,
|
|
org,
|
|
env,
|
|
});
|
|
});
|
|
|
|
const usage = 100012;
|
|
it("should upgrade to premium product and have correct invoice next cycle", async () => {
|
|
await autumn.track({
|
|
customer_id: customerId,
|
|
feature_id: TestFeature.Words,
|
|
value: usage,
|
|
});
|
|
|
|
const curUnix = await advanceTestClock({
|
|
stripeCli,
|
|
testClockId,
|
|
advanceTo: addHours(
|
|
addMonths(new Date(), 2),
|
|
hoursToFinalizeInvoice,
|
|
).getTime(),
|
|
waitForSeconds: 30,
|
|
});
|
|
|
|
const invoiceAmount = await getExpectedInvoiceTotal({
|
|
customerId,
|
|
productId: pro.id,
|
|
usage: [{ featureId: TestFeature.Words, value: usage }],
|
|
stripeCli,
|
|
db,
|
|
org,
|
|
env,
|
|
});
|
|
|
|
const customer = await autumn.customers.get(customerId);
|
|
expect(customer.invoices.length).to.equal(2);
|
|
expect(invoiceAmount).to.equal(customer.invoices[0].total);
|
|
});
|
|
});
|