restructured tests

This commit is contained in:
John Yeo
2025-06-10 19:54:05 +01:00
parent 519752ff44
commit ff7f46d273
127 changed files with 4980 additions and 3443 deletions

View File

@@ -0,0 +1,68 @@
import { AutumnCli } from "tests/cli/AutumnCli.js";
import { attachProducts } from "tests/global.js";
import { compareMainProduct } from "tests/utils/compare.js";
import chalk from "chalk";
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
import { setupBefore } from "tests/before.js";
import { Customer } from "@autumn/shared";
/*
FLOW:
1. Attach pro group 1 & pro group 2 at once -> should have both products as main
2. Upgrade pro group 1 -> premium group 1
3. Upgrade pro group 2 -> premium group 2
*/
const testCase = "multiProduct1";
describe(
chalk.yellowBright(`${testCase}: Testing multi product attach, and upgrade`),
() => {
let customerId = testCase;
let customer: Customer;
before(async function () {
await setupBefore(this);
const res = await initCustomer({
customerId,
db: this.db,
org: this.org,
env: this.env,
autumn: this.autumnJs,
attachPm: "success",
});
customer = res.customer;
});
it("should attach pro group 1 and pro group 2", async function () {
await AutumnCli.attach({
customerId: customerId,
productIds: [attachProducts.proGroup1.id, attachProducts.proGroup2.id],
});
let cusRes = await AutumnCli.getCustomer(customerId);
compareMainProduct({ sent: attachProducts.proGroup1, cusRes });
compareMainProduct({ sent: attachProducts.proGroup2, cusRes });
});
it("should upgrade to premium group 1", async function () {
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.premiumGroup1.id,
});
// 1. Compare main product
const cusRes = await AutumnCli.getCustomer(customerId);
compareMainProduct({ sent: attachProducts.premiumGroup1, cusRes });
});
it("should upgrade to premium group 2", async function () {
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.premiumGroup2.id,
});
// 1. Compare main product
const cusRes = await AutumnCli.getCustomer(customerId);
compareMainProduct({ sent: attachProducts.premiumGroup2, cusRes });
});
},
);

View File

@@ -0,0 +1,158 @@
import chalk from "chalk";
import { Stripe } from "stripe";
import { CusProductService } from "@/internal/customers/cusProducts/CusProductService.js";
import { CusProductStatus, Customer } from "@autumn/shared";
import { expect } from "chai";
import { AutumnCli } from "tests/cli/AutumnCli.js";
import { attachProducts } from "tests/global.js";
import {
checkProductIsScheduled,
compareMainProduct,
} from "tests/utils/compare.js";
import { searchCusProducts } from "tests/utils/genUtils.js";
import { checkScheduleContainsProducts } from "tests/utils/scheduleCheckUtils.js";
import { setupBefore } from "tests/before.js";
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
/*
FLOW:
1. Attach pro group 1 & premium group 2
2. Downgrade to starter group 1
3. Downgrade to starter group 2
4. Change downgrade to pro group 2
*/
const testCase = "multiProduct2";
describe(`${chalk.yellowBright(
"multiProduct2: premium1->starter1, premium2->starter2, then premium2->pro2, then premium2->free",
)}`, () => {
let customerId = testCase;
let customer: Customer;
let stripeCli: Stripe;
before(async function () {
await setupBefore(this);
stripeCli = this.stripeCli;
const res = await initCustomer({
db: this.db,
org: this.org,
customerId,
env: this.env,
autumn: this.autumnJs,
attachPm: "success",
});
customer = res.customer;
});
it("should attach premium group 1 and premium group 2", async function () {
await AutumnCli.attach({
customerId: customerId,
productIds: [
attachProducts.premiumGroup1.id,
attachProducts.premiumGroup2.id,
],
});
let cusRes = await AutumnCli.getCustomer(customerId);
compareMainProduct({ sent: attachProducts.premiumGroup1, cusRes });
compareMainProduct({ sent: attachProducts.premiumGroup2, cusRes });
});
it("should downgrade to starter group 1 and starter group 2", async function () {
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.starterGroup1.id,
});
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.starterGroup2.id,
});
// Check starter group 1 scheduled and starter group 2 scheduled
let cusRes = await AutumnCli.getCustomer(customerId);
checkProductIsScheduled({
product: attachProducts.starterGroup1,
cusRes,
});
checkProductIsScheduled({
product: attachProducts.starterGroup2,
cusRes,
});
// Check if scheduled id is the same
const cusProducts = await CusProductService.list({
db: this.db,
internalCustomerId: customer.internal_id,
inStatuses: [
CusProductStatus.Active,
CusProductStatus.PastDue,
CusProductStatus.Scheduled,
],
});
// 1. Pro group 1:
const starter1 = searchCusProducts({
cusProducts,
productId: attachProducts.starterGroup1.id,
});
const starter2 = searchCusProducts({
cusProducts,
productId: attachProducts.starterGroup2.id,
});
expect(starter1).to.exist;
expect(starter2).to.exist;
expect(starter1?.scheduled_ids![0]).to.equal(starter2?.scheduled_ids![0]);
const stripeSchedule = await stripeCli.subscriptionSchedules.retrieve(
starter1?.scheduled_ids![0]!,
);
// console.log(stripeSchedule);
checkScheduleContainsProducts({
db: this.db,
schedule: stripeSchedule,
productIds: [
attachProducts.starterGroup1.id,
attachProducts.starterGroup2.id,
],
org: this.org,
env: this.env,
});
});
it("should downgrade to free", async function () {
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.freeGroup2.id,
});
let cusRes = await AutumnCli.getCustomer(customerId);
checkProductIsScheduled({
product: attachProducts.freeGroup2,
cusRes,
});
const cusProducts = await CusProductService.list({
db: this.db,
internalCustomerId: customer.internal_id,
});
const starterGroup2 = searchCusProducts({
cusProducts,
productId: attachProducts.starterGroup2.id,
});
checkScheduleContainsProducts({
db: this.db,
scheduleId: starterGroup2?.scheduled_ids![0],
productIds: [attachProducts.starterGroup2.id],
org: this.org,
env: this.env,
});
});
});

View File

@@ -0,0 +1,155 @@
import chalk from "chalk";
import { expect } from "chai";
import { AutumnCli } from "tests/cli/AutumnCli.js";
import { attachProducts } from "tests/global.js";
import { compareMainProduct } from "tests/utils/compare.js";
import { searchCusProducts, timeout } from "tests/utils/genUtils.js";
import { createStripeCli } from "@/external/stripe/utils.js";
import { getStripeSubs } from "@/external/stripe/stripeSubUtils.js";
import { CusProductService } from "@/internal/customers/cusProducts/CusProductService.js";
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
import { setupBefore } from "tests/before.js";
import Stripe from "stripe";
// TESTING DOWNGRADE DOWNGRADE THEN
// 1. UPGRADE FIRST PRODUCT BACK -- SHOULD REPLACE SCHEDULE WITH OLD FIRST PRODUCT
// 2. UPGRADE SECOND PRODUCT BACK -- SHOULD CANCEL SCHEDULE
const testCase = "multiProduct3";
describe(
chalk.yellowBright(`${testCase}: double downgrade, double upgrade (back)`),
() => {
let customerId = testCase;
let customer;
let stripeCli: Stripe;
before(async function () {
await setupBefore(this);
stripeCli = this.stripeCli;
const res = await initCustomer({
db: this.db,
org: this.org,
env: this.env,
customerId,
autumn: this.autumnJs,
attachPm: "success",
});
customer = res.customer;
});
it("should attach premium group 1 and premium group 2", async function () {
await AutumnCli.attach({
customerId: customerId,
productIds: [
attachProducts.premiumGroup1.id,
attachProducts.premiumGroup2.id,
],
});
let cusRes = await AutumnCli.getCustomer(customerId);
compareMainProduct({ sent: attachProducts.premiumGroup1, cusRes });
compareMainProduct({ sent: attachProducts.premiumGroup2, cusRes });
});
it("should attach starter group 1, then starter group 2", async function () {
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.starterGroup1.id,
});
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.starterGroup2.id,
});
});
it("should reattach premium group 1", async function () {
await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.premiumGroup1.id,
});
await timeout(10000);
const cusProducts = await CusProductService.list({
db: this.db,
internalCustomerId: customer!.internal_id,
});
let premiumGroup1 = searchCusProducts({
cusProducts,
productId: attachProducts.premiumGroup1.id,
});
let starterGroup2 = searchCusProducts({
cusProducts,
productId: attachProducts.starterGroup2.id,
});
expect(premiumGroup1!.scheduled_ids!.length).to.equal(1);
expect(starterGroup2!.scheduled_ids!.length).to.equal(1);
expect(premiumGroup1!.scheduled_ids![0]).to.equal(
starterGroup2!.scheduled_ids![0],
);
// 2. Check that there's no starter group 1
let starterGroup1 = searchCusProducts({
cusProducts,
productId: attachProducts.starterGroup1.id,
});
expect(starterGroup1).to.not.exist;
// 3. TODO: check that in Stripe schedule, premium group 1 and starter group 2 are scheduled
});
it("should reattach premium group 2 (scheduled should be cancelled)", async function () {
await timeout(3000);
let res = await AutumnCli.attach({
customerId: customerId,
productId: attachProducts.premiumGroup2.id,
});
await timeout(10000);
const cusProducts = await CusProductService.list({
db: this.db,
internalCustomerId: customer!.internal_id,
});
let premiumGroup2 = searchCusProducts({
cusProducts,
productId: attachProducts.premiumGroup2.id,
});
let premiumGroup1 = searchCusProducts({
cusProducts,
productId: attachProducts.premiumGroup1.id,
});
expect(premiumGroup2).to.exist.and.have.property("scheduled_ids");
expect(premiumGroup1).to.exist.and.have.property("scheduled_ids");
expect(premiumGroup2!.scheduled_ids!.length).to.equal(0);
expect(premiumGroup1!.scheduled_ids!.length).to.equal(0);
// Check that subscription is activated
let stripeCli = createStripeCli({
org: this.org,
env: this.env,
});
let subs = await getStripeSubs({
stripeCli,
subIds: premiumGroup1!.subscription_ids!,
});
let sub = subs[0];
expect(sub.canceled_at).to.equal(null);
expect(sub.cancel_at).to.equal(null);
expect(sub.status).to.equal("active");
});
},
);