Files
cfw-autumn/server/tests/attach/basic/basic4.ts
2025-10-08 09:16:42 +01:00

123 lines
3.1 KiB
TypeScript

import { expect } from "chai";
import chalk from "chalk";
import { setupBefore } from "tests/before.js";
import { AutumnCli } from "tests/cli/AutumnCli.js";
import { features, products } from "tests/global.js";
import { compareMainProduct } from "tests/utils/compare.js";
import { createProducts } from "tests/utils/productUtils.js";
import { AutumnInt } from "@/external/autumn/autumnCli.js";
import { constructPrepaidItem } from "@/utils/scriptUtils/constructItem.js";
import { constructRawProduct } from "@/utils/scriptUtils/createTestProducts.js";
import { initCustomer } from "@/utils/scriptUtils/initCustomer.js";
const monthlyItem = constructPrepaidItem({
featureId: features.metered1.id,
price: 9,
billingUnits: 250,
});
const monthly = constructRawProduct({
id: "basic4_monthly",
items: [monthlyItem],
});
const testCase = "basic4";
describe(`${chalk.yellowBright("basic4: Testing attach monthly add on")}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt();
let db, org, env;
before(async function () {
await setupBefore(this);
db = this.db;
org = this.org;
env = this.env;
await initCustomer({
autumn: this.autumnJs,
customerId,
db,
org,
env,
attachPm: "success",
});
await createProducts({
autumn: this.autumnJs,
db,
orgId: org.id,
env,
products: [monthly],
});
});
it("should attach pro", async () => {
await autumn.attach({
customer_id: customerId,
product_id: products.pro.id,
});
const res = await AutumnCli.getCustomer(customerId);
compareMainProduct({
sent: products.pro,
cusRes: res,
});
});
const monthlyQuantity = 500;
it("should attach monthly add on", async () => {
await AutumnCli.attach({
customerId: customerId,
productId: products.monthlyAddOnMetered1.id,
forceCheckout: false,
options: [
{
feature_id: features.metered1.id,
quantity: monthlyQuantity,
},
],
});
});
it("should have correct product & entitlements", async () => {
const cusRes = await AutumnCli.getCustomer(customerId);
const proMetered1 = products.pro.entitlements.metered1.allowance;
const monthlyMetered1Balance = cusRes.entitlements.find(
(e: any) =>
e.feature_id === features.metered1.id &&
e.interval ===
products.monthlyAddOnMetered1.entitlements.metered1.interval,
);
expect(monthlyMetered1Balance!.balance).to.equal(
proMetered1! + monthlyQuantity,
);
expect(cusRes.add_ons).to.have.lengthOf(1);
const monthlyAddOnId = cusRes.add_ons.find(
(a: any) => a.id === products.monthlyAddOnMetered1.id,
);
expect(monthlyAddOnId).to.exist;
expect(cusRes.invoices.length).to.equal(2);
});
it("should have correct /check result for metered1", async () => {
const res: any = await AutumnCli.entitled(customerId, features.metered1.id);
const metered1Balance = res!.balances.find(
(b: any) => b.feature_id === features.metered1.id,
);
const proMetered1Amt = products.pro.entitlements.metered1.allowance;
const monthlyAddOnMetered1Amt = monthlyQuantity;
expect(metered1Balance!.balance).to.equal(
proMetered1Amt! + monthlyAddOnMetered1Amt,
);
});
});