writing tests for migrations
This commit is contained in:
183
server/tests/attach/migrations/migration1.ts
Normal file
183
server/tests/attach/migrations/migration1.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import { expect } from "chai";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import { AppEnv, Organization, ProductV2 } from "@autumn/shared";
|
||||
import chalk from "chalk";
|
||||
import Stripe from "stripe";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { addPrefixToProducts, runAttachTest } from "../utils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { replaceItems } from "../utils.js";
|
||||
import { timeout } from "@/utils/genUtils.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addWeeks } from "date-fns";
|
||||
import { defaultApiVersion } from "tests/constants.js";
|
||||
import { runMigrationTest } from "./runMigrationTest.js";
|
||||
|
||||
let messagesItem = constructFeatureItem({
|
||||
featureId: TestFeature.Messages,
|
||||
includedUsage: 500,
|
||||
});
|
||||
|
||||
let wordsItem = constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 100,
|
||||
});
|
||||
|
||||
export let free = constructProduct({
|
||||
items: [messagesItem, wordsItem],
|
||||
type: "free",
|
||||
isDefault: false,
|
||||
});
|
||||
|
||||
const testCase = "migrations1";
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing migration for free product`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: defaultApiVersion });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
|
||||
let 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;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [free],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
autumn,
|
||||
products: [free],
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach free product", async function () {
|
||||
await runAttachTest({
|
||||
autumn,
|
||||
customerId,
|
||||
product: free,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
let newFree: ProductV2;
|
||||
let increaseMessagesBy = 100;
|
||||
let reduceWordsBy = 50;
|
||||
it("should update product to new version", async function () {
|
||||
newFree = structuredClone(free);
|
||||
|
||||
let newItems = replaceItems({
|
||||
items: free.items,
|
||||
featureId: TestFeature.Messages,
|
||||
newItem: constructFeatureItem({
|
||||
featureId: TestFeature.Messages,
|
||||
includedUsage:
|
||||
(messagesItem.included_usage as number) + increaseMessagesBy,
|
||||
}),
|
||||
});
|
||||
|
||||
newItems = replaceItems({
|
||||
items: newItems,
|
||||
featureId: TestFeature.Words,
|
||||
newItem: constructFeatureItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: (wordsItem.included_usage as number) - reduceWordsBy,
|
||||
}),
|
||||
});
|
||||
|
||||
newFree.items = newItems;
|
||||
|
||||
await autumn.products.update(free.id, {
|
||||
items: newItems,
|
||||
});
|
||||
});
|
||||
|
||||
it("should attach track usage and get correct balance", async function () {
|
||||
let wordsUsage = 25;
|
||||
let messagesUsage = 20;
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
value: wordsUsage,
|
||||
feature_id: TestFeature.Words,
|
||||
});
|
||||
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
value: messagesUsage,
|
||||
feature_id: TestFeature.Messages,
|
||||
});
|
||||
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addWeeks(Date.now(), 1).getTime(),
|
||||
});
|
||||
|
||||
let customer = await autumn.customers.get(customerId);
|
||||
|
||||
await autumn.migrate({
|
||||
from_product_id: free.id,
|
||||
to_product_id: newFree.id,
|
||||
from_version: 1,
|
||||
to_version: 2,
|
||||
});
|
||||
|
||||
await timeout(4000);
|
||||
|
||||
// 1. Get features
|
||||
customer = await autumn.customers.get(customerId);
|
||||
|
||||
await runMigrationTest({
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
fromProduct: free,
|
||||
toProduct: newFree,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
usage: [
|
||||
{
|
||||
featureId: TestFeature.Words,
|
||||
value: wordsUsage,
|
||||
},
|
||||
{
|
||||
featureId: TestFeature.Messages,
|
||||
value: messagesUsage,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
163
server/tests/attach/migrations/migration2.ts
Normal file
163
server/tests/attach/migrations/migration2.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
AppEnv,
|
||||
BillingInterval,
|
||||
Organization,
|
||||
ProductItemInterval,
|
||||
ProductV2,
|
||||
} from "@autumn/shared";
|
||||
import chalk from "chalk";
|
||||
import Stripe from "stripe";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { addPrefixToProducts, runAttachTest } from "../utils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { replaceItems } from "../utils.js";
|
||||
import { timeout } from "@/utils/genUtils.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addWeeks } from "date-fns";
|
||||
import { defaultApiVersion } from "tests/constants.js";
|
||||
import { runMigrationTest } from "./runMigrationTest.js";
|
||||
|
||||
let wordsItem = constructArrearItem({
|
||||
featureId: TestFeature.Words,
|
||||
});
|
||||
|
||||
export let pro = constructProduct({
|
||||
items: [wordsItem],
|
||||
type: "pro",
|
||||
isDefault: false,
|
||||
});
|
||||
|
||||
const testCase = "migrations2";
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing migration for pro usage product`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: defaultApiVersion });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
|
||||
let 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;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
autumn,
|
||||
products: [pro],
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach free product", async function () {
|
||||
await runAttachTest({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
let newPro: ProductV2;
|
||||
let increaseWordsBy = 1500;
|
||||
it("should update product to new version", async function () {
|
||||
newPro = structuredClone(pro);
|
||||
|
||||
let newItems = replaceItems({
|
||||
items: pro.items,
|
||||
featureId: TestFeature.Words,
|
||||
newItem: constructArrearItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: (wordsItem.included_usage as number) + increaseWordsBy,
|
||||
}),
|
||||
});
|
||||
|
||||
newItems = replaceItems({
|
||||
items: newItems,
|
||||
interval: BillingInterval.Month,
|
||||
newItem: {
|
||||
price: 50,
|
||||
interval: ProductItemInterval.Month,
|
||||
},
|
||||
});
|
||||
|
||||
newPro.items = newItems;
|
||||
await autumn.products.update(pro.id, {
|
||||
items: newItems,
|
||||
});
|
||||
});
|
||||
|
||||
it("should attach track usage and get correct balance", async function () {
|
||||
let wordsUsage = 120000;
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
value: wordsUsage,
|
||||
feature_id: TestFeature.Words,
|
||||
});
|
||||
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addWeeks(Date.now(), 1).getTime(),
|
||||
});
|
||||
|
||||
await autumn.migrate({
|
||||
from_product_id: pro.id,
|
||||
to_product_id: newPro.id,
|
||||
from_version: 1,
|
||||
to_version: 2,
|
||||
});
|
||||
|
||||
await timeout(4000);
|
||||
|
||||
await runMigrationTest({
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
fromProduct: pro,
|
||||
toProduct: newPro,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
usage: [
|
||||
{
|
||||
featureId: TestFeature.Words,
|
||||
value: wordsUsage,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
159
server/tests/attach/migrations/migration3.ts
Normal file
159
server/tests/attach/migrations/migration3.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
AppEnv,
|
||||
BillingInterval,
|
||||
Organization,
|
||||
ProductItemInterval,
|
||||
ProductV2,
|
||||
} from "@autumn/shared";
|
||||
import chalk from "chalk";
|
||||
import Stripe from "stripe";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { addPrefixToProducts, runAttachTest } from "../utils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { replaceItems } from "../utils.js";
|
||||
|
||||
import { defaultApiVersion } from "tests/constants.js";
|
||||
import { runMigrationTest } from "./runMigrationTest.js";
|
||||
import { timeout } from "@/utils/genUtils.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
|
||||
let wordsItem = constructArrearItem({
|
||||
featureId: TestFeature.Words,
|
||||
});
|
||||
|
||||
export let pro = constructProduct({
|
||||
items: [wordsItem],
|
||||
type: "pro",
|
||||
isDefault: false,
|
||||
trial: true,
|
||||
});
|
||||
|
||||
const testCase = "migrations3";
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing migration for pro with trial`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: defaultApiVersion });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
|
||||
let 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;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
autumn,
|
||||
products: [pro],
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach free product", async function () {
|
||||
await runAttachTest({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
let newPro: ProductV2;
|
||||
let increaseWordsBy = 1500;
|
||||
it("should update product to new version", async function () {
|
||||
newPro = structuredClone(pro);
|
||||
|
||||
let newItems = replaceItems({
|
||||
items: pro.items,
|
||||
featureId: TestFeature.Words,
|
||||
newItem: constructArrearItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: (wordsItem.included_usage as number) + increaseWordsBy,
|
||||
}),
|
||||
});
|
||||
|
||||
newItems = replaceItems({
|
||||
items: newItems,
|
||||
interval: BillingInterval.Month,
|
||||
newItem: {
|
||||
price: 50,
|
||||
interval: ProductItemInterval.Month,
|
||||
},
|
||||
});
|
||||
|
||||
newPro.items = newItems;
|
||||
newPro.version = 2;
|
||||
await autumn.products.update(pro.id, {
|
||||
items: newItems,
|
||||
});
|
||||
});
|
||||
|
||||
it("should attach track usage and get correct balance", async function () {
|
||||
let wordsUsage = 120000;
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
value: wordsUsage,
|
||||
feature_id: TestFeature.Words,
|
||||
});
|
||||
|
||||
await advanceTestClock({
|
||||
stripeCli,
|
||||
testClockId,
|
||||
advanceTo: addDays(Date.now(), 4).getTime(),
|
||||
});
|
||||
|
||||
await timeout(5000);
|
||||
|
||||
await runMigrationTest({
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
fromProduct: pro,
|
||||
toProduct: newPro,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
usage: [
|
||||
{
|
||||
featureId: TestFeature.Words,
|
||||
value: wordsUsage,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
146
server/tests/attach/migrations/migration4.ts
Normal file
146
server/tests/attach/migrations/migration4.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
|
||||
import {
|
||||
AppEnv,
|
||||
BillingInterval,
|
||||
Organization,
|
||||
ProductItemInterval,
|
||||
ProductV2,
|
||||
} from "@autumn/shared";
|
||||
import chalk from "chalk";
|
||||
import Stripe from "stripe";
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { setupBefore } from "tests/before.js";
|
||||
import { createProducts } from "tests/utils/productUtils.js";
|
||||
import { addPrefixToProducts, runAttachTest } from "../utils.js";
|
||||
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
||||
import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";
|
||||
import { TestFeature } from "tests/setup/v2Features.js";
|
||||
import { replaceItems } from "../utils.js";
|
||||
|
||||
import { defaultApiVersion } from "tests/constants.js";
|
||||
import { runMigrationTest } from "./runMigrationTest.js";
|
||||
import { timeout } from "@/utils/genUtils.js";
|
||||
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
||||
import { addDays } from "date-fns";
|
||||
|
||||
let wordsItem = constructArrearItem({
|
||||
featureId: TestFeature.Words,
|
||||
});
|
||||
|
||||
export let pro = constructProduct({
|
||||
items: [wordsItem],
|
||||
type: "pro",
|
||||
isDefault: false,
|
||||
});
|
||||
|
||||
let newWordsItem = constructArrearItem({
|
||||
featureId: TestFeature.Words,
|
||||
includedUsage: 120100,
|
||||
});
|
||||
|
||||
let proWithTrial = constructProduct({
|
||||
items: [newWordsItem],
|
||||
type: "pro",
|
||||
isDefault: false,
|
||||
trial: true,
|
||||
});
|
||||
|
||||
const testCase = "migrations4";
|
||||
|
||||
describe(`${chalk.yellowBright(`${testCase}: Testing migration for pro with trial`)}`, () => {
|
||||
let customerId = testCase;
|
||||
let autumn: AutumnInt = new AutumnInt({ version: defaultApiVersion });
|
||||
let testClockId: string;
|
||||
let db: DrizzleCli, org: Organization, env: AppEnv;
|
||||
let stripeCli: Stripe;
|
||||
|
||||
let 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;
|
||||
|
||||
addPrefixToProducts({
|
||||
products: [pro],
|
||||
prefix: testCase,
|
||||
});
|
||||
|
||||
await createProducts({
|
||||
db,
|
||||
orgId: org.id,
|
||||
env,
|
||||
autumn,
|
||||
products: [pro],
|
||||
customerId,
|
||||
});
|
||||
|
||||
const { testClockId: testClockId1 } = await initCustomer({
|
||||
autumn: autumnJs,
|
||||
customerId,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
attachPm: "success",
|
||||
});
|
||||
|
||||
testClockId = testClockId1!;
|
||||
});
|
||||
|
||||
it("should attach free product", async function () {
|
||||
await runAttachTest({
|
||||
autumn,
|
||||
customerId,
|
||||
product: pro,
|
||||
stripeCli,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
});
|
||||
|
||||
it("should update product to new version", async function () {
|
||||
await autumn.products.update(pro.id, {
|
||||
items: proWithTrial.items,
|
||||
});
|
||||
});
|
||||
|
||||
it("should attach track usage and get correct balance", async function () {
|
||||
let wordsUsage = 120000;
|
||||
await autumn.track({
|
||||
customer_id: customerId,
|
||||
value: wordsUsage,
|
||||
feature_id: TestFeature.Words,
|
||||
});
|
||||
|
||||
await timeout(4000);
|
||||
|
||||
// await advanceTestClock({
|
||||
// stripeCli,
|
||||
// testClockId,
|
||||
// advanceTo: addDays(Date.now(), 4).getTime(),
|
||||
// });
|
||||
|
||||
await runMigrationTest({
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
fromProduct: pro,
|
||||
toProduct: proWithTrial,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
usage: [
|
||||
{
|
||||
featureId: TestFeature.Words,
|
||||
value: wordsUsage,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
115
server/tests/attach/migrations/runMigrationTest.ts
Normal file
115
server/tests/attach/migrations/runMigrationTest.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
||||
import { ProductV2, Organization } from "@autumn/shared";
|
||||
import { AppEnv } from "autumn-js";
|
||||
import {
|
||||
expectSubItemsCorrect,
|
||||
getSubsFromCusId,
|
||||
} from "tests/utils/expectUtils/expectSubUtils.js";
|
||||
import Stripe from "stripe";
|
||||
import { expect } from "chai";
|
||||
import { expectFeaturesCorrect } from "tests/utils/expectUtils/expectFeaturesCorrect.js";
|
||||
import { expectResetAtCorrect } from "tests/utils/expectUtils/expectAttach/expectResetAtCorrect.js";
|
||||
import { isFreeProductV2 } from "@/internal/products/productUtils/classifyProduct.js";
|
||||
import { expectTrialEndsAtCorrect } from "tests/utils/expectUtils/expectAttach/expectTrialEndsAt.js";
|
||||
import { timeout } from "@/utils/genUtils.js";
|
||||
|
||||
export const expectSubsSame = ({
|
||||
subsBefore,
|
||||
subsAfter,
|
||||
}: {
|
||||
subsBefore: Stripe.Subscription[];
|
||||
subsAfter: Stripe.Subscription[];
|
||||
}) => {
|
||||
let invoicesBefore = subsBefore.map((sub) => sub.latest_invoice);
|
||||
let invoicesAfter = subsAfter.map((sub) => sub.latest_invoice);
|
||||
let subIdsBefore = subsBefore.map((sub) => sub.id);
|
||||
let subIdsAfter = subsAfter.map((sub) => sub.id);
|
||||
let periodEndsBefore = subsBefore.map((sub) => sub.current_period_end);
|
||||
let periodEndsAfter = subsAfter.map((sub) => sub.current_period_end);
|
||||
|
||||
expect(invoicesAfter).to.deep.equal(invoicesBefore);
|
||||
expect(subIdsAfter).to.deep.equal(subIdsBefore);
|
||||
expect(periodEndsAfter).to.deep.equal(periodEndsBefore);
|
||||
};
|
||||
|
||||
export const runMigrationTest = async ({
|
||||
autumn,
|
||||
stripeCli,
|
||||
customerId,
|
||||
fromProduct,
|
||||
toProduct,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
usage,
|
||||
numInvoices = 1,
|
||||
}: {
|
||||
autumn: AutumnInt;
|
||||
stripeCli: Stripe;
|
||||
customerId: string;
|
||||
fromProduct: ProductV2;
|
||||
toProduct: ProductV2;
|
||||
db: DrizzleCli;
|
||||
org: Organization;
|
||||
env: AppEnv;
|
||||
usage?: {
|
||||
featureId: string;
|
||||
value: number;
|
||||
}[];
|
||||
numInvoices?: number;
|
||||
}) => {
|
||||
const { subs: subsBefore } = await getSubsFromCusId({
|
||||
stripeCli,
|
||||
customerId,
|
||||
productId: fromProduct.id,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
const cusBefore = await autumn.customers.get(customerId);
|
||||
|
||||
await autumn.migrate({
|
||||
from_product_id: fromProduct.id,
|
||||
to_product_id: toProduct.id,
|
||||
from_version: fromProduct.version,
|
||||
to_version: toProduct.version,
|
||||
});
|
||||
|
||||
await timeout(5000);
|
||||
|
||||
const { subs: subsAfter } = await getSubsFromCusId({
|
||||
stripeCli,
|
||||
customerId,
|
||||
productId: toProduct.id,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
expectSubsSame({ subsBefore, subsAfter });
|
||||
|
||||
const cusAfter = await autumn.customers.get(customerId);
|
||||
expectFeaturesCorrect({
|
||||
customer: cusAfter,
|
||||
product: toProduct,
|
||||
usage,
|
||||
});
|
||||
|
||||
expectResetAtCorrect({ cusBefore, cusAfter });
|
||||
expectTrialEndsAtCorrect({ cusBefore, cusAfter });
|
||||
|
||||
await expectSubItemsCorrect({
|
||||
stripeCli,
|
||||
customerId,
|
||||
product: toProduct,
|
||||
db,
|
||||
org,
|
||||
env,
|
||||
});
|
||||
|
||||
if (!isFreeProductV2({ product: toProduct })) {
|
||||
expect(cusAfter.invoices.length).to.equal(numInvoices);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user