tests for multi attach
This commit is contained in:
@@ -28,7 +28,7 @@ import {
|
||||
advanceTestClock,
|
||||
completeCheckoutForm,
|
||||
} from "tests/utils/stripeUtils.js";
|
||||
import { addWeeks } from "date-fns";
|
||||
import { addDays, addWeeks } from "date-fns";
|
||||
import { getExpectedInvoiceTotal } from "tests/utils/expectUtils/expectInvoiceUtils.js";
|
||||
import { expectMultiAttachCorrect } from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
|
||||
@@ -75,7 +75,7 @@ const ops = [
|
||||
];
|
||||
|
||||
const testCase = "multiAttach1";
|
||||
describe(`${chalk.yellowBright("multiAttach1: Testing multi attach for trial products")}`, () => {
|
||||
describe(`${chalk.yellowBright("multiAttach1: Testing multi attach for trial products and update product quantities mid trial")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
@@ -115,49 +115,88 @@ describe(`${chalk.yellowBright("multiAttach1: Testing multi attach for trial pro
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
// attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach and have correct sub", async function () {
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: pro.id,
|
||||
quantity: 5,
|
||||
product: pro,
|
||||
status: CusProductStatus.Active,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 3,
|
||||
product: premium,
|
||||
status: CusProductStatus.Active,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product_id: growth.id,
|
||||
quantity: 2,
|
||||
product: growth,
|
||||
status: CusProductStatus.Active,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
// const { checkout_url } = await autumn.attach({
|
||||
// customer_id: customerId,
|
||||
// // @ts-ignore
|
||||
// products: productsList,
|
||||
// force_checkout: true,
|
||||
// });
|
||||
it("should advance clock and update premium & growth while trialing", async function () {
|
||||
const newProducts = [
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
product_id: growth.id,
|
||||
quantity: 5,
|
||||
},
|
||||
];
|
||||
|
||||
// await completeCheckoutForm(checkout_url);
|
||||
const results = [
|
||||
{
|
||||
product: pro,
|
||||
quantity: 5,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product: premium,
|
||||
quantity: 1,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
|
||||
{
|
||||
product: growth,
|
||||
quantity: 5,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(new Date(), 3).getTime(),
|
||||
});
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: newProducts,
|
||||
results,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
189
server/tests/core/multiAttach/multiAttach2.test.ts
Normal file
189
server/tests/core/multiAttach/multiAttach2.test.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { addPrefixToProducts } from "tests/utils/testProductUtils/testProductUtils.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
import { expectMultiAttachCorrect } from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
|
||||
let growth = constructProduct({
|
||||
id: "growth",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 100 }),
|
||||
],
|
||||
type: "growth",
|
||||
});
|
||||
|
||||
let premium = constructProduct({
|
||||
id: "premium",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 200 }),
|
||||
],
|
||||
type: "premium",
|
||||
trial: true,
|
||||
});
|
||||
|
||||
let pro = constructProduct({
|
||||
id: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 300,
|
||||
}),
|
||||
],
|
||||
type: "pro",
|
||||
trial: true,
|
||||
});
|
||||
|
||||
const ops = [
|
||||
{
|
||||
entityId: "1",
|
||||
product: pro,
|
||||
results: [{ product: pro, status: CusProductStatus.Active }],
|
||||
},
|
||||
{
|
||||
entityId: "2",
|
||||
product: pro,
|
||||
results: [{ product: pro, status: CusProductStatus.Active }],
|
||||
},
|
||||
];
|
||||
|
||||
const testCase = "multiAttach2";
|
||||
describe(`${chalk.yellowBright("multiAttach2: Testing multi attach for trial products and update product quantities mid trial")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium, growth],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn: autumnJs,
|
||||
products: [pro, premium, growth],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: pro.id,
|
||||
quantity: 5,
|
||||
product: pro,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 3,
|
||||
product: premium,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product_id: growth.id,
|
||||
quantity: 2,
|
||||
product: growth,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should advance clock and update premium & growth while trialing", async function () {
|
||||
const newProducts = [
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
product_id: growth.id,
|
||||
quantity: 5,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: pro,
|
||||
quantity: 5,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product: premium,
|
||||
quantity: 1,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
|
||||
{
|
||||
product: growth,
|
||||
quantity: 5,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(new Date(), 3).getTime(),
|
||||
});
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: newProducts,
|
||||
results,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
});
|
||||
252
server/tests/core/multiAttach/multiAttach3.test.ts
Normal file
252
server/tests/core/multiAttach/multiAttach3.test.ts
Normal file
@@ -0,0 +1,252 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import {
|
||||
addPrefixToProducts,
|
||||
getBasePrice,
|
||||
} from "tests/utils/testProductUtils/testProductUtils.js";
|
||||
import {
|
||||
expectMultiAttachCorrect,
|
||||
expectResultsCorrect,
|
||||
} from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
import { expectSubToBeCorrect } from "tests/merged/mergeUtils/expectSubCorrect.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
import { expect } from "chai";
|
||||
|
||||
let premium = constructProduct({
|
||||
id: "premium",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 200 }),
|
||||
],
|
||||
type: "premium",
|
||||
trial: true,
|
||||
});
|
||||
|
||||
let pro = constructProduct({
|
||||
id: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 300,
|
||||
}),
|
||||
],
|
||||
type: "pro",
|
||||
trial: true,
|
||||
});
|
||||
|
||||
const testCase = "multiAttach3";
|
||||
describe(`${chalk.yellowBright("multiAttach3: Testing multi attach for trial products transfer to entity, then cancel products on entities...")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn: autumnJs,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: pro.id,
|
||||
quantity: 5,
|
||||
product: pro,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 3,
|
||||
product: premium,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
const entities = [
|
||||
{
|
||||
id: "1",
|
||||
name: "Entity 1",
|
||||
feature_id: TestFeature.Users,
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
name: "Entity 2",
|
||||
feature_id: TestFeature.Users,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: pro,
|
||||
quantity: 4,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product: premium,
|
||||
quantity: 2,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product: pro,
|
||||
quantity: 1,
|
||||
entityId: "2",
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product: pro,
|
||||
quantity: 1,
|
||||
entityId: "2",
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
it("should transfer to entity and have correct sub", async function () {
|
||||
await autumn.entities.create(customerId, entities);
|
||||
|
||||
await autumn.transfer(customerId, {
|
||||
to_entity_id: "2",
|
||||
product_id: pro.id,
|
||||
});
|
||||
await autumn.transfer(customerId, {
|
||||
to_entity_id: "1",
|
||||
product_id: premium.id,
|
||||
});
|
||||
|
||||
await expectResultsCorrect({
|
||||
customerId,
|
||||
results,
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should cancel one entity's sub at end of cycle and have correct schedule...", async function () {
|
||||
await autumn.cancel({
|
||||
customer_id: customerId,
|
||||
product_id: pro.id,
|
||||
entity_id: "2",
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should cancel one entity's sub immediately", async function () {
|
||||
await autumn.cancel({
|
||||
customer_id: customerId,
|
||||
product_id: premium.id,
|
||||
entity_id: "1",
|
||||
cancel_immediately: true,
|
||||
// @ts-ignore
|
||||
prorate: false,
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should advance test clock to end of trial and have correct sub", async function () {
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(new Date(), 8).getTime(),
|
||||
waitForSeconds: 30,
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const latestInvoice = customer.invoices[0];
|
||||
|
||||
// Should only have paid for 4 pro and 2 premium...
|
||||
const invoiceTotal =
|
||||
getBasePrice({ product: pro }) * 4 +
|
||||
getBasePrice({ product: premium }) * 2;
|
||||
|
||||
expect(invoiceTotal).to.equal(latestInvoice.total);
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
});
|
||||
151
server/tests/core/multiAttach/multiReward/multiReward1.test.ts
Normal file
151
server/tests/core/multiAttach/multiReward/multiReward1.test.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { expectMultiAttachCorrect } from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
import {
|
||||
multiRewardPremium,
|
||||
multiRewardPro,
|
||||
premiumReward,
|
||||
proReward,
|
||||
setupMultiRewardBefore,
|
||||
} from "./multiRewardUtils.test.js";
|
||||
|
||||
const testCase = "multiReward1";
|
||||
describe(`${chalk.yellowBright("multiReward1: Testing multi attach with rewards")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
await setupMultiRewardBefore({
|
||||
orgId: org.id,
|
||||
db,
|
||||
env,
|
||||
});
|
||||
|
||||
// addPrefixToProducts({
|
||||
// products: [pro, premium, growth],
|
||||
// prefix: testCase,
|
||||
// });
|
||||
|
||||
// await createProducts({
|
||||
// autumn: autumnJs,
|
||||
// products: [pro, premium, growth],
|
||||
// db,
|
||||
// orgId: org.id,
|
||||
// env,
|
||||
// customerId,
|
||||
// });
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: multiRewardPro.id,
|
||||
quantity: 3,
|
||||
product: multiRewardPro,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product_id: multiRewardPremium.id,
|
||||
quantity: 3,
|
||||
product: multiRewardPremium,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
rewards: [proReward.id, premiumReward.id],
|
||||
expectedRewards: [proReward.id, premiumReward.id],
|
||||
});
|
||||
});
|
||||
return;
|
||||
|
||||
// it("should advance clock and update premium & growth while trialing", async function () {
|
||||
// const newProducts = [
|
||||
// {
|
||||
// product_id: premium.id,
|
||||
// quantity: 1,
|
||||
// },
|
||||
// {
|
||||
// product_id: growth.id,
|
||||
// quantity: 5,
|
||||
// },
|
||||
// ];
|
||||
|
||||
// const results = [
|
||||
// {
|
||||
// product: pro,
|
||||
// quantity: 5,
|
||||
// status: CusProductStatus.Trialing,
|
||||
// },
|
||||
// {
|
||||
// product: premium,
|
||||
// quantity: 1,
|
||||
// status: CusProductStatus.Trialing,
|
||||
// },
|
||||
|
||||
// {
|
||||
// product: growth,
|
||||
// quantity: 5,
|
||||
// status: CusProductStatus.Trialing,
|
||||
// },
|
||||
// ];
|
||||
|
||||
// await advanceTestClock({
|
||||
// stripeCli,
|
||||
// testClockId,
|
||||
// advanceTo: addDays(new Date(), 3).getTime(),
|
||||
// });
|
||||
|
||||
// await expectMultiAttachCorrect({
|
||||
// customerId,
|
||||
// products: newProducts,
|
||||
// results,
|
||||
// db,
|
||||
// org,
|
||||
// env,
|
||||
// });
|
||||
// });
|
||||
});
|
||||
189
server/tests/core/multiAttach/multiReward/multiReward2.test.ts
Normal file
189
server/tests/core/multiAttach/multiReward/multiReward2.test.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { expectMultiAttachCorrect } from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
import {
|
||||
multiRewardPremium,
|
||||
multiRewardPro,
|
||||
premiumReward,
|
||||
proReward,
|
||||
setupMultiRewardBefore,
|
||||
} from "./multiRewardUtils.test.js";
|
||||
import { CusService } from "@/internal/customers/CusService.js";
|
||||
import { cusProductToSub } from "@/internal/customers/cusProducts/cusProductUtils/convertCusProduct.js";
|
||||
import { createStripeCli } from "@/external/stripe/utils.js";
|
||||
|
||||
const testCase = "multiReward2";
|
||||
describe(`${chalk.yellowBright("multiReward2: Testing multi attach with rewards -- delete reward and prorate")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
await setupMultiRewardBefore({
|
||||
orgId: org.id,
|
||||
db,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: multiRewardPro.id,
|
||||
quantity: 3,
|
||||
product: multiRewardPro,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product_id: multiRewardPremium.id,
|
||||
quantity: 3,
|
||||
product: multiRewardPremium,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
rewards: [proReward.id, premiumReward.id],
|
||||
expectedRewards: [proReward.id, premiumReward.id],
|
||||
});
|
||||
});
|
||||
|
||||
it("should delete discounts from subscription and prorate correctly", async function () {
|
||||
const fullCus = await CusService.getFull({
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
idOrInternalId: customerId,
|
||||
});
|
||||
|
||||
const cusProduct = fullCus.customer_products.find(
|
||||
(cp) => cp.product.id === multiRewardPro.id
|
||||
);
|
||||
const sub = await cusProductToSub({ cusProduct, stripeCli });
|
||||
|
||||
await stripeCli.subscriptions.update(sub!.id, {
|
||||
discounts: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("should update pro quantity and have correct checkout amount", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: multiRewardPro.id,
|
||||
quantity: 5,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: multiRewardPro,
|
||||
quantity: 5,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: multiRewardPremium,
|
||||
quantity: 3,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
expectedRewards: [],
|
||||
});
|
||||
});
|
||||
return;
|
||||
|
||||
// it("should advance clock and update premium & growth while trialing", async function () {
|
||||
// const newProducts = [
|
||||
// {
|
||||
// product_id: premium.id,
|
||||
// quantity: 1,
|
||||
// },
|
||||
// {
|
||||
// product_id: growth.id,
|
||||
// quantity: 5,
|
||||
// },
|
||||
// ];
|
||||
|
||||
// const results = [
|
||||
// {
|
||||
// product: pro,
|
||||
// quantity: 5,
|
||||
// status: CusProductStatus.Trialing,
|
||||
// },
|
||||
// {
|
||||
// product: premium,
|
||||
// quantity: 1,
|
||||
// status: CusProductStatus.Trialing,
|
||||
// },
|
||||
|
||||
// {
|
||||
// product: growth,
|
||||
// quantity: 5,
|
||||
// status: CusProductStatus.Trialing,
|
||||
// },
|
||||
// ];
|
||||
|
||||
// await advanceTestClock({
|
||||
// stripeCli,
|
||||
// testClockId,
|
||||
// advanceTo: addDays(new Date(), 3).getTime(),
|
||||
// });
|
||||
|
||||
// await expectMultiAttachCorrect({
|
||||
// customerId,
|
||||
// products: newProducts,
|
||||
// results,
|
||||
// db,
|
||||
// org,
|
||||
// env,
|
||||
// });
|
||||
// });
|
||||
});
|
||||
171
server/tests/core/multiAttach/multiReward/multiReward3.test.ts
Normal file
171
server/tests/core/multiAttach/multiReward/multiReward3.test.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { expectMultiAttachCorrect } from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
import {
|
||||
multiRewardPremium,
|
||||
multiRewardPro,
|
||||
premiumReward,
|
||||
premiumTrial,
|
||||
proReward,
|
||||
proTrial,
|
||||
setupMultiRewardBefore,
|
||||
} from "./multiRewardUtils.test.js";
|
||||
import { CusService } from "@/internal/customers/CusService.js";
|
||||
import { cusProductToSub } from "@/internal/customers/cusProducts/cusProductUtils/convertCusProduct.js";
|
||||
import { createStripeCli } from "@/external/stripe/utils.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
import { expectSubToBeCorrect } from "tests/merged/mergeUtils/expectSubCorrect.js";
|
||||
import { expect } from "chai";
|
||||
import { getBasePrice } from "tests/utils/testProductUtils/testProductUtils.js";
|
||||
import { Decimal } from "decimal.js";
|
||||
|
||||
const testCase = "multiReward3";
|
||||
describe(`${chalk.yellowBright("multiReward3: Testing multi attach with rewards -- advance clock and update pro quantity")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
await setupMultiRewardBefore({
|
||||
orgId: org.id,
|
||||
db,
|
||||
env,
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: proTrial.id,
|
||||
quantity: 3,
|
||||
product: proTrial,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product_id: premiumTrial.id,
|
||||
quantity: 3,
|
||||
product: premiumTrial,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
rewards: [proReward.id, premiumReward.id],
|
||||
expectedRewards: [proReward.id, premiumReward.id],
|
||||
});
|
||||
});
|
||||
|
||||
let checkoutRes: any;
|
||||
|
||||
it("should advance clock and update pro quantity", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: proTrial.id,
|
||||
quantity: 5,
|
||||
product: proTrial,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: proTrial,
|
||||
quantity: 5,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
{
|
||||
product: premiumTrial,
|
||||
quantity: 3,
|
||||
status: CusProductStatus.Trialing,
|
||||
},
|
||||
];
|
||||
const res = await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
rewards: [proReward.id, premiumReward.id],
|
||||
expectedRewards: [proReward.id, premiumReward.id],
|
||||
});
|
||||
|
||||
checkoutRes = res.checkoutRes;
|
||||
});
|
||||
|
||||
it("should advance to trial end and have correct quantity", async function () {
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(new Date(), 12).getTime(),
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
// sub: curSub,
|
||||
// cusProduct: curMainProduct,
|
||||
// results: productsList,
|
||||
});
|
||||
|
||||
const customer = await autumn.customers.get(customerId);
|
||||
const latestInvoice = customer.invoices[0];
|
||||
|
||||
const checkoutNextCycleTotal = checkoutRes.next_cycle?.total;
|
||||
const premiumPrice = new Decimal(getBasePrice({ product: premiumTrial }))
|
||||
.mul(3)
|
||||
.mul(0.2)
|
||||
.toNumber();
|
||||
|
||||
console.log("Premium price: ", premiumPrice);
|
||||
console.log("Checkout next cycle total: ", checkoutNextCycleTotal);
|
||||
expect(latestInvoice.total).to.equal(
|
||||
checkoutRes.next_cycle?.total + premiumPrice
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,144 @@
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { ProductService } from "@/internal/products/ProductService.js";
|
||||
import { RewardService } from "@/internal/rewards/RewardService.js";
|
||||
import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CouponDurationType,
|
||||
CreateReward,
|
||||
ProductItemInterval,
|
||||
RewardType,
|
||||
} from "@autumn/shared";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
|
||||
export let premiumTrial = constructProduct({
|
||||
id: "multiReward_premiumTrial",
|
||||
group: "multiReward",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 200 }),
|
||||
],
|
||||
type: "premium",
|
||||
trial: true,
|
||||
});
|
||||
|
||||
export let proTrial = constructProduct({
|
||||
id: "multiReward_proTrial",
|
||||
group: "multiReward",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 300 }),
|
||||
],
|
||||
type: "pro",
|
||||
trial: true,
|
||||
});
|
||||
export let multiRewardPremium = constructProduct({
|
||||
id: "multiReward_premium",
|
||||
group: "multiReward",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 200 }),
|
||||
],
|
||||
type: "premium",
|
||||
});
|
||||
|
||||
export let multiRewardPro = constructProduct({
|
||||
id: "multiReward_pro",
|
||||
group: "multiReward",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 300 }),
|
||||
],
|
||||
type: "pro",
|
||||
});
|
||||
|
||||
export const proReward: CreateReward = {
|
||||
id: "pro_reward",
|
||||
name: "pro_reward",
|
||||
promo_codes: [{ code: "pro_reward" }],
|
||||
type: RewardType.PercentageDiscount,
|
||||
discount_config: {
|
||||
discount_value: 50,
|
||||
duration_type: CouponDurationType.Months,
|
||||
duration_value: 3,
|
||||
should_rollover: true,
|
||||
apply_to_all: false,
|
||||
price_ids: [proTrial.id],
|
||||
},
|
||||
};
|
||||
|
||||
export const premiumReward: CreateReward = {
|
||||
id: "premium_reward",
|
||||
name: "premium_reward",
|
||||
promo_codes: [{ code: "premium_reward" }],
|
||||
type: RewardType.PercentageDiscount,
|
||||
discount_config: {
|
||||
discount_value: 80,
|
||||
duration_type: CouponDurationType.Months,
|
||||
duration_value: 3,
|
||||
should_rollover: true,
|
||||
apply_to_all: false,
|
||||
},
|
||||
};
|
||||
|
||||
export const setupMultiRewardBefore = async ({
|
||||
orgId,
|
||||
db,
|
||||
env,
|
||||
}: {
|
||||
orgId: string;
|
||||
db: DrizzleCli;
|
||||
env: AppEnv;
|
||||
}) => {
|
||||
const autumn = new AutumnInt({ version: APIVersion.v1_2 });
|
||||
for (const product of [
|
||||
proTrial,
|
||||
premiumTrial,
|
||||
multiRewardPro,
|
||||
multiRewardPremium,
|
||||
]) {
|
||||
// let res = await autumn.products.get(product.id);
|
||||
|
||||
try {
|
||||
await autumn.products.delete(product.id);
|
||||
} catch (error) {
|
||||
// console.log("Error deleting product:", error);
|
||||
}
|
||||
|
||||
try {
|
||||
await autumn.products.create(product);
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
const products = await ProductService.listFull({
|
||||
db,
|
||||
orgId,
|
||||
env,
|
||||
});
|
||||
|
||||
const proTrialPrice = products.find((p) => p.id === proTrial.id)?.prices[0];
|
||||
const premiumTrialPrice = products.find((p) => p.id === premiumTrial.id)
|
||||
?.prices[0];
|
||||
const proProduct = products.find((p) => p.id === multiRewardPro.id);
|
||||
const premiumProduct = products.find((p) => p.id === multiRewardPremium.id);
|
||||
|
||||
const proPriceIds = [proTrialPrice!.id, proProduct!.prices[0]!.id];
|
||||
const premiumPriceIds = [
|
||||
premiumTrialPrice!.id,
|
||||
premiumProduct!.prices[0]!.id,
|
||||
];
|
||||
|
||||
for (const reward of [proReward, premiumReward]) {
|
||||
try {
|
||||
await autumn.rewards.delete(reward.id);
|
||||
} catch (error) {}
|
||||
try {
|
||||
await autumn.rewards.create({
|
||||
...reward,
|
||||
discount_config: {
|
||||
...reward.discount_config,
|
||||
price_ids: reward.id == proReward.id ? proPriceIds : premiumPriceIds,
|
||||
},
|
||||
});
|
||||
} catch (error) {}
|
||||
}
|
||||
};
|
||||
201
server/tests/core/multiAttach/multiUpgrade/multiUpgrade1.test.ts
Normal file
201
server/tests/core/multiAttach/multiUpgrade/multiUpgrade1.test.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import {
|
||||
addPrefixToProducts,
|
||||
getBasePrice,
|
||||
} from "tests/utils/testProductUtils/testProductUtils.js";
|
||||
import {
|
||||
expectMultiAttachCorrect,
|
||||
expectResultsCorrect,
|
||||
} from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
import { expectSubToBeCorrect } from "tests/merged/mergeUtils/expectSubCorrect.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
import { expect } from "chai";
|
||||
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
|
||||
|
||||
let premium = constructProduct({
|
||||
id: "premium",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 200 }),
|
||||
],
|
||||
type: "premium",
|
||||
});
|
||||
|
||||
let pro = constructProduct({
|
||||
id: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 300,
|
||||
}),
|
||||
],
|
||||
type: "pro",
|
||||
});
|
||||
|
||||
const testCase = "multiUpgrade1";
|
||||
describe(`${chalk.yellowBright("multiUpgrade1: Testing multi attach and upgrade")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn: autumnJs,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: pro.id,
|
||||
quantity: 5,
|
||||
product: pro,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 3,
|
||||
product: premium,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
const entities = [
|
||||
{
|
||||
id: "1",
|
||||
name: "Entity 1",
|
||||
feature_id: TestFeature.Users,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: pro,
|
||||
quantity: 4,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: premium,
|
||||
quantity: 3,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: pro,
|
||||
quantity: 1,
|
||||
entityId: "1",
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
|
||||
it("should transfer to entity and have correct sub", async function () {
|
||||
await autumn.entities.create(customerId, entities);
|
||||
|
||||
await autumn.transfer(customerId, {
|
||||
to_entity_id: "1",
|
||||
product_id: pro.id,
|
||||
});
|
||||
|
||||
await expectResultsCorrect({
|
||||
customerId,
|
||||
results,
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should upgrade entity's sub to premium", async function () {
|
||||
const checkoutRes = await autumn.checkout({
|
||||
customer_id: customerId,
|
||||
product_id: premium.id,
|
||||
entity_id: "1",
|
||||
});
|
||||
|
||||
await autumn.attach({
|
||||
customer_id: customerId,
|
||||
product_id: premium.id,
|
||||
entity_id: "1",
|
||||
});
|
||||
|
||||
const entity = await autumn.entities.get(customerId, "1");
|
||||
const invoices = entity.invoices[0];
|
||||
|
||||
expect(invoices.total).to.equal(checkoutRes.total);
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
await expectResultsCorrect({
|
||||
customerId,
|
||||
results,
|
||||
});
|
||||
});
|
||||
});
|
||||
211
server/tests/core/multiAttach/multiUpgrade/multiUpgrade2.test.ts
Normal file
211
server/tests/core/multiAttach/multiUpgrade/multiUpgrade2.test.ts
Normal file
@@ -0,0 +1,211 @@
|
||||
import chalk from "chalk";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { Stripe } from "stripe";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
APIVersion,
|
||||
AppEnv,
|
||||
CusProductStatus,
|
||||
Organization,
|
||||
} from "@autumn/shared";
|
||||
import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import {
|
||||
addPrefixToProducts,
|
||||
getBasePrice,
|
||||
} from "tests/utils/testProductUtils/testProductUtils.js";
|
||||
import {
|
||||
expectMultiAttachCorrect,
|
||||
expectResultsCorrect,
|
||||
} from "tests/utils/expectUtils/expectMultiAttach.js";
|
||||
import { expectSubToBeCorrect } from "tests/merged/mergeUtils/expectSubCorrect.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
import { expect } from "chai";
|
||||
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
|
||||
|
||||
let premium = constructProduct({
|
||||
id: "premium",
|
||||
items: [
|
||||
constructFeatureItem({ featureId: TestFeature.Words, includedUsage: 200 }),
|
||||
],
|
||||
type: "premium",
|
||||
});
|
||||
|
||||
let pro = constructProduct({
|
||||
id: "pro",
|
||||
items: [
|
||||
constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 300,
|
||||
}),
|
||||
],
|
||||
type: "pro",
|
||||
});
|
||||
|
||||
const testCase = "multiUpgrade2";
|
||||
describe(`${chalk.yellowBright("multiUpgrade2: Testing multi attach and update quantities downward")}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: APIVersion.v1_4 });
|
||||
|
||||
let stripeCli: Stripe;
|
||||
let testClockId: string;
|
||||
let curUnix: number;
|
||||
let db: DrizzleCli;
|
||||
let org: Organization;
|
||||
let env: AppEnv;
|
||||
|
||||
before(async function () {
|
||||
await setupBefore(this);
|
||||
const { autumnJs } = this;
|
||||
db = this.db;
|
||||
org = this.org;
|
||||
env = this.env;
|
||||
|
||||
stripeCli = this.stripeCli;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro, premium],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
autumn: autumnJs,
|
||||
products: [pro, premium],
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should run multi attach through checkout and have correct sub", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: pro.id,
|
||||
quantity: 5,
|
||||
product: pro,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 3,
|
||||
product: premium,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results: productsList,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
const entities = [
|
||||
{
|
||||
id: "1",
|
||||
name: "Entity 1",
|
||||
feature_id: TestFeature.Users,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: pro,
|
||||
quantity: 4,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: premium,
|
||||
quantity: 3,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: pro,
|
||||
quantity: 1,
|
||||
entityId: "1",
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
|
||||
it("should transfer to entity and have correct sub", async function () {
|
||||
await autumn.entities.create(customerId, entities);
|
||||
|
||||
await autumn.transfer(customerId, {
|
||||
to_entity_id: "1",
|
||||
product_id: pro.id,
|
||||
});
|
||||
|
||||
await expectResultsCorrect({
|
||||
customerId,
|
||||
results,
|
||||
});
|
||||
|
||||
await expectSubToBeCorrect({
|
||||
db,
|
||||
customerId,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should update premium and pro quantities downward", async function () {
|
||||
const productsList = [
|
||||
{
|
||||
product_id: pro.id,
|
||||
quantity: 3,
|
||||
},
|
||||
{
|
||||
product_id: premium.id,
|
||||
quantity: 2,
|
||||
},
|
||||
];
|
||||
|
||||
const results = [
|
||||
{
|
||||
product: pro,
|
||||
quantity: 3,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: premium,
|
||||
quantity: 2,
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
{
|
||||
product: pro,
|
||||
quantity: 1,
|
||||
entityId: "1",
|
||||
status: CusProductStatus.Active,
|
||||
},
|
||||
];
|
||||
|
||||
await expectMultiAttachCorrect({
|
||||
customerId,
|
||||
products: productsList,
|
||||
results,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user