chore: ran biome format
This commit is contained in:
@@ -22,126 +22,126 @@ import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js"
|
||||
const testCase = "customInterval1";
|
||||
|
||||
export let pro = constructProduct({
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
includedUsage: 500,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
includedUsage: 500,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
});
|
||||
|
||||
export let premium = constructProduct({
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
}),
|
||||
// constructArrearItem({ featureId: TestFeature.Words }),
|
||||
// constructArrearProratedItem({
|
||||
// featureId: TestFeature.Users,
|
||||
// pricePerUnit: 30,
|
||||
// }),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "premium",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
}),
|
||||
// constructArrearItem({ featureId: TestFeature.Words }),
|
||||
// constructArrearProratedItem({
|
||||
// featureId: TestFeature.Users,
|
||||
// pricePerUnit: 30,
|
||||
// }),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "premium",
|
||||
});
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing custom interval and interval count`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.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;
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
let usage = 100012;
|
||||
it("should upgrade to premium product and have correct invoice next cycle", async function () {
|
||||
const curUnix = await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addMonths(new Date(), 1).getTime(),
|
||||
waitForSeconds: 15,
|
||||
});
|
||||
let usage = 100012;
|
||||
it("should upgrade to premium product and have correct invoice next cycle", async function () {
|
||||
const curUnix = await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addMonths(new Date(), 1).getTime(),
|
||||
waitForSeconds: 15,
|
||||
});
|
||||
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: premium,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: premium,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
expect(customer.invoices.length).to.equal(2);
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
expect(customer.invoices.length).to.equal(2);
|
||||
|
||||
const nextUnix = await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addHours(
|
||||
addMonths(new Date(curUnix), 1),
|
||||
hoursToFinalizeInvoice
|
||||
).getTime(),
|
||||
waitForSeconds: 30,
|
||||
});
|
||||
const nextUnix = await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addHours(
|
||||
addMonths(new Date(curUnix), 1),
|
||||
hoursToFinalizeInvoice,
|
||||
).getTime(),
|
||||
waitForSeconds: 30,
|
||||
});
|
||||
|
||||
const customer2 = await autumn.customers.get(customerId);
|
||||
const invoices = customer2.invoices;
|
||||
expect(invoices.length).to.equal(3);
|
||||
expect(invoices[0].product_ids).to.include(premium.id);
|
||||
expect(invoices[0].total).to.equal(getBasePrice({ product: premium }));
|
||||
const customer2 = await autumn.customers.get(customerId);
|
||||
const invoices = customer2.invoices;
|
||||
expect(invoices.length).to.equal(3);
|
||||
expect(invoices[0].product_ids).to.include(premium.id);
|
||||
expect(invoices[0].total).to.equal(getBasePrice({ product: premium }));
|
||||
|
||||
const wordsFeature = customer2.features[TestFeature.Words];
|
||||
// @ts-ignore
|
||||
expect(wordsFeature.interval_count).to.equal(2);
|
||||
});
|
||||
const wordsFeature = customer2.features[TestFeature.Words];
|
||||
// @ts-ignore
|
||||
expect(wordsFeature.interval_count).to.equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,99 +23,99 @@ import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js"
|
||||
const testCase = "customInterval2";
|
||||
|
||||
export let pro = constructRawProduct({
|
||||
id: "pro",
|
||||
items: [
|
||||
constructArrearItem({
|
||||
includedUsage: 0,
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
}),
|
||||
],
|
||||
id: "pro",
|
||||
items: [
|
||||
constructArrearItem({
|
||||
includedUsage: 0,
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing custom interval on arrear prorated price`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.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;
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
let usage = 100012;
|
||||
it("should upgrade to premium product and have correct invoice next cycle", async function () {
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
feature_id: TestFeature.Words,
|
||||
value: usage,
|
||||
});
|
||||
let usage = 100012;
|
||||
it("should upgrade to premium product and have correct invoice next cycle", async function () {
|
||||
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 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 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);
|
||||
});
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
expect(customer.invoices.length).to.equal(2);
|
||||
expect(invoiceAmount).to.equal(customer.invoices[0].total);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
BillingInterval,
|
||||
LimitedItem,
|
||||
Organization,
|
||||
Product,
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
BillingInterval,
|
||||
LimitedItem,
|
||||
Organization,
|
||||
Product,
|
||||
} from "@autumn/shared";
|
||||
import chalk from "chalk";
|
||||
import Stripe from "stripe";
|
||||
@@ -15,14 +15,14 @@ import { setupBefore } from "tests/before.js";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
|
||||
import {
|
||||
constructArrearItem,
|
||||
constructFeatureItem,
|
||||
constructPrepaidItem,
|
||||
constructArrearItem,
|
||||
constructFeatureItem,
|
||||
constructPrepaidItem,
|
||||
} from "@/utils/scriptUtils/constructItem.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import {
|
||||
constructProduct,
|
||||
constructRawProduct,
|
||||
constructProduct,
|
||||
constructRawProduct,
|
||||
} from "@/utils/scriptUtils/createTestProducts.js";
|
||||
|
||||
import { addPrefixToProducts } from "tests/attach/utils.js";
|
||||
@@ -40,135 +40,135 @@ import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js"
|
||||
const testCase = "customInterval3";
|
||||
|
||||
export let pro = constructProduct({
|
||||
type: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
});
|
||||
|
||||
const prepaidWordsItem = constructPrepaidItem({
|
||||
featureId: TestFeature.Words,
|
||||
price: 10,
|
||||
billingUnits: 1,
|
||||
includedUsage: 0,
|
||||
intervalCount: 2,
|
||||
featureId: TestFeature.Words,
|
||||
price: 10,
|
||||
billingUnits: 1,
|
||||
includedUsage: 0,
|
||||
intervalCount: 2,
|
||||
});
|
||||
|
||||
export const addOn = constructRawProduct({
|
||||
id: "addOn",
|
||||
items: [prepaidWordsItem],
|
||||
isAddOn: true,
|
||||
id: "addOn",
|
||||
items: [prepaidWordsItem],
|
||||
isAddOn: true,
|
||||
});
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing custom interval on add on merged product`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.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;
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, addOn],
|
||||
prefix: testCase,
|
||||
});
|
||||
addPrefixToProducts({
|
||||
products: [pro, addOn],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro, addOn],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro, addOn],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should upgrade to attached add on and have correct invoice next cycle", async function () {
|
||||
const curUnix = await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(new Date(), 20).getTime(),
|
||||
waitForSeconds: 15,
|
||||
});
|
||||
it("should upgrade to attached add on and have correct invoice next cycle", async function () {
|
||||
const curUnix = await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(new Date(), 20).getTime(),
|
||||
waitForSeconds: 15,
|
||||
});
|
||||
|
||||
const wordBillingSets = 2;
|
||||
const wordsBillingUnits = prepaidWordsItem.billing_units! * wordBillingSets;
|
||||
await autumn.attach({
|
||||
customer_id: customerId,
|
||||
product_id: addOn.id,
|
||||
options: [
|
||||
{
|
||||
feature_id: TestFeature.Words,
|
||||
quantity: wordsBillingUnits,
|
||||
},
|
||||
],
|
||||
});
|
||||
const wordBillingSets = 2;
|
||||
const wordsBillingUnits = prepaidWordsItem.billing_units! * wordBillingSets;
|
||||
await autumn.attach({
|
||||
customer_id: customerId,
|
||||
product_id: addOn.id,
|
||||
options: [
|
||||
{
|
||||
feature_id: TestFeature.Words,
|
||||
quantity: wordsBillingUnits,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const proProduct = customer.products.find((p) => p.id === pro.id);
|
||||
const invoices = customer.invoices;
|
||||
expectProductAttached({
|
||||
customer,
|
||||
product: pro,
|
||||
});
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const proProduct = customer.products.find((p) => p.id === pro.id);
|
||||
const invoices = customer.invoices;
|
||||
expectProductAttached({
|
||||
customer,
|
||||
product: pro,
|
||||
});
|
||||
|
||||
expectProductAttached({
|
||||
customer,
|
||||
product: addOn,
|
||||
});
|
||||
expectProductAttached({
|
||||
customer,
|
||||
product: addOn,
|
||||
});
|
||||
|
||||
let expectedPrice = wordsBillingUnits * prepaidWordsItem.price!;
|
||||
const proratedPrice = calculateProrationAmount({
|
||||
amount: expectedPrice,
|
||||
periodStart: new Date().getTime(),
|
||||
periodEnd: addMonths(new Date(), 2).getTime(),
|
||||
now: curUnix!,
|
||||
});
|
||||
let expectedPrice = wordsBillingUnits * prepaidWordsItem.price!;
|
||||
const proratedPrice = calculateProrationAmount({
|
||||
amount: expectedPrice,
|
||||
periodStart: new Date().getTime(),
|
||||
periodEnd: addMonths(new Date(), 2).getTime(),
|
||||
now: curUnix!,
|
||||
});
|
||||
|
||||
expect(invoices[0].product_ids).to.include(addOn.id);
|
||||
expect(invoices[0].total).to.approximately(proratedPrice, 0.1);
|
||||
expect(invoices[0].product_ids).to.include(addOn.id);
|
||||
expect(invoices[0].total).to.approximately(proratedPrice, 0.1);
|
||||
|
||||
const expectedAddonEnd = addMonths(new Date(), 2);
|
||||
const approximate = 1000 * 60 * 60 * 24; // +- 1 day
|
||||
const addOnProduct = customer.products.find((p) => p.id === addOn.id);
|
||||
const expectedAddonEnd = addMonths(new Date(), 2);
|
||||
const approximate = 1000 * 60 * 60 * 24; // +- 1 day
|
||||
const addOnProduct = customer.products.find((p) => p.id === addOn.id);
|
||||
|
||||
expect(addOnProduct?.current_period_end).to.be.approximately(
|
||||
expectedAddonEnd.getTime(),
|
||||
approximate
|
||||
);
|
||||
});
|
||||
expect(addOnProduct?.current_period_end).to.be.approximately(
|
||||
expectedAddonEnd.getTime(),
|
||||
approximate,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,8 +16,8 @@ import { addPrefixToProducts } from "tests/attach/utils.js";
|
||||
import { addMonths } from "date-fns";
|
||||
import { expect } from "chai";
|
||||
import {
|
||||
expectDowngradeCorrect,
|
||||
expectNextCycleCorrect,
|
||||
expectDowngradeCorrect,
|
||||
expectNextCycleCorrect,
|
||||
} from "tests/utils/expectUtils/expectScheduleUtils.js";
|
||||
import { getBasePrice } from "tests/utils/testProductUtils/testProductUtils.js";
|
||||
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
|
||||
@@ -25,128 +25,128 @@ import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js"
|
||||
const testCase = "customInterval4";
|
||||
|
||||
export let pro = constructProduct({
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 500,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 500,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
});
|
||||
|
||||
export let premium = constructProduct({
|
||||
id: "premium",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 500,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "premium",
|
||||
id: "premium",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 500,
|
||||
}),
|
||||
],
|
||||
intervalCount: 2,
|
||||
type: "premium",
|
||||
});
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing downgrades for custom intervals`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.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;
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach premium product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: premium,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
it("should attach premium product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: premium,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should have correct next cycle at on checkout", async function () {
|
||||
const checkout = await autumn.checkout({
|
||||
customer_id: customerId,
|
||||
product_id: pro.id,
|
||||
});
|
||||
it("should have correct next cycle at on checkout", async function () {
|
||||
const checkout = await autumn.checkout({
|
||||
customer_id: customerId,
|
||||
product_id: pro.id,
|
||||
});
|
||||
|
||||
let expectedNextCycle = addMonths(new Date(), 2);
|
||||
expect(checkout.next_cycle?.starts_at).to.be.approximately(
|
||||
expectedNextCycle.getTime(),
|
||||
1000 * 60 * 60 * 24
|
||||
);
|
||||
let expectedNextCycle = addMonths(new Date(), 2);
|
||||
expect(checkout.next_cycle?.starts_at).to.be.approximately(
|
||||
expectedNextCycle.getTime(),
|
||||
1000 * 60 * 60 * 24,
|
||||
);
|
||||
|
||||
expect(checkout.total).to.equal(0);
|
||||
});
|
||||
expect(checkout.total).to.equal(0);
|
||||
});
|
||||
|
||||
let preview: any;
|
||||
it("should downgrade to pro", async function () {
|
||||
const { preview: preview_ } = await expectDowngradeCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
curProduct: premium,
|
||||
newProduct: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
let preview: any;
|
||||
it("should downgrade to pro", async function () {
|
||||
const { preview: preview_ } = await expectDowngradeCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
curProduct: premium,
|
||||
newProduct: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
preview = preview_;
|
||||
});
|
||||
preview = preview_;
|
||||
});
|
||||
|
||||
it("should have pro attached on next cycle", async function () {
|
||||
await expectNextCycleCorrect({
|
||||
preview: preview!,
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
testClockId,
|
||||
product: pro,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
it("should have pro attached on next cycle", async function () {
|
||||
await expectNextCycleCorrect({
|
||||
preview: preview!,
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
testClockId,
|
||||
product: pro,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const invoices = customer.invoices;
|
||||
expect(invoices.length).to.equal(2);
|
||||
expect(invoices[0].total).to.equal(getBasePrice({ product: pro }));
|
||||
});
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const invoices = customer.invoices;
|
||||
expect(invoices.length).to.equal(2);
|
||||
expect(invoices[0].total).to.equal(getBasePrice({ product: pro }));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,144 +21,144 @@ const testCase = "customInterval5";
|
||||
|
||||
const includedUsage = 500;
|
||||
const monthlyWords = constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage,
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage,
|
||||
});
|
||||
|
||||
const biMonthlyWords = constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
includedUsage,
|
||||
featureId: TestFeature.Words,
|
||||
intervalCount: 2,
|
||||
includedUsage,
|
||||
});
|
||||
|
||||
export let pro = constructProduct({
|
||||
items: [monthlyWords, biMonthlyWords],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
items: [monthlyWords, biMonthlyWords],
|
||||
intervalCount: 2,
|
||||
type: "pro",
|
||||
});
|
||||
|
||||
const getBreakdown = ({
|
||||
customer,
|
||||
intervalCount,
|
||||
customer,
|
||||
intervalCount,
|
||||
}: {
|
||||
customer: Customer;
|
||||
intervalCount: number;
|
||||
customer: Customer;
|
||||
intervalCount: number;
|
||||
}) => {
|
||||
const wordsFeature = customer.features[TestFeature.Words];
|
||||
// @ts-ignore
|
||||
return wordsFeature.breakdown?.find(
|
||||
(b: any) => b.interval_count == intervalCount
|
||||
);
|
||||
const wordsFeature = customer.features[TestFeature.Words];
|
||||
// @ts-ignore
|
||||
return wordsFeature.breakdown?.find(
|
||||
(b: any) => b.interval_count == intervalCount,
|
||||
);
|
||||
};
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing multi interval features with custom intervals`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.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;
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
await createProducts({
|
||||
autumn,
|
||||
products: [pro],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
it("should attach pro product", async function () {
|
||||
await attachAndExpectCorrect({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const wordsFeature = customer.features[TestFeature.Words];
|
||||
// @ts-ignore
|
||||
expect(wordsFeature.interval_count).to.equal(null);
|
||||
expect(wordsFeature.breakdown?.length).to.equal(2);
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const wordsFeature = customer.features[TestFeature.Words];
|
||||
// @ts-ignore
|
||||
expect(wordsFeature.interval_count).to.equal(null);
|
||||
expect(wordsFeature.breakdown?.length).to.equal(2);
|
||||
|
||||
expect(
|
||||
wordsFeature.breakdown?.some(
|
||||
(b: any) => b.interval_count == 1 && b.interval == "month"
|
||||
)
|
||||
).to.equal(true);
|
||||
expect(
|
||||
wordsFeature.breakdown?.some(
|
||||
(b: any) => b.interval_count == 2 && b.interval == "month"
|
||||
)
|
||||
).to.equal(true);
|
||||
});
|
||||
expect(
|
||||
wordsFeature.breakdown?.some(
|
||||
(b: any) => b.interval_count == 1 && b.interval == "month",
|
||||
),
|
||||
).to.equal(true);
|
||||
expect(
|
||||
wordsFeature.breakdown?.some(
|
||||
(b: any) => b.interval_count == 2 && b.interval == "month",
|
||||
),
|
||||
).to.equal(true);
|
||||
});
|
||||
|
||||
const trackVal = 300;
|
||||
it("should have correct breakdown after usage", async function () {
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
feature_id: TestFeature.Words,
|
||||
value: trackVal,
|
||||
});
|
||||
const trackVal = 300;
|
||||
it("should have correct breakdown after usage", async function () {
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
feature_id: TestFeature.Words,
|
||||
value: trackVal,
|
||||
});
|
||||
|
||||
await timeout(3000);
|
||||
await timeout(3000);
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
|
||||
// Should deduct
|
||||
const monthlyBreakdown = getBreakdown({ customer, intervalCount: 1 });
|
||||
const biMonthlyBreakdown = getBreakdown({ customer, intervalCount: 2 });
|
||||
// Should deduct
|
||||
const monthlyBreakdown = getBreakdown({ customer, intervalCount: 1 });
|
||||
const biMonthlyBreakdown = getBreakdown({ customer, intervalCount: 2 });
|
||||
|
||||
expect(monthlyBreakdown?.balance).to.equal(includedUsage - trackVal);
|
||||
expect(biMonthlyBreakdown?.balance).to.equal(includedUsage);
|
||||
expect(monthlyBreakdown?.balance).to.equal(includedUsage - trackVal);
|
||||
expect(biMonthlyBreakdown?.balance).to.equal(includedUsage);
|
||||
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
feature_id: TestFeature.Words,
|
||||
value: trackVal,
|
||||
});
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
feature_id: TestFeature.Words,
|
||||
value: trackVal,
|
||||
});
|
||||
|
||||
await timeout(3000);
|
||||
await timeout(3000);
|
||||
|
||||
const customer2 = await autumn.customers.get(customerId);
|
||||
const monthlyBreakdown2 = getBreakdown({
|
||||
customer: customer2,
|
||||
intervalCount: 1,
|
||||
});
|
||||
const biMonthlyBreakdown2 = getBreakdown({
|
||||
customer: customer2,
|
||||
intervalCount: 2,
|
||||
});
|
||||
const customer2 = await autumn.customers.get(customerId);
|
||||
const monthlyBreakdown2 = getBreakdown({
|
||||
customer: customer2,
|
||||
intervalCount: 1,
|
||||
});
|
||||
const biMonthlyBreakdown2 = getBreakdown({
|
||||
customer: customer2,
|
||||
intervalCount: 2,
|
||||
});
|
||||
|
||||
expect(monthlyBreakdown2?.balance).to.equal(0);
|
||||
expect(biMonthlyBreakdown2?.balance).to.equal(includedUsage - 100);
|
||||
});
|
||||
expect(monthlyBreakdown2?.balance).to.equal(0);
|
||||
expect(biMonthlyBreakdown2?.balance).to.equal(includedUsage - 100);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user