114 lines
3.0 KiB
TypeScript
114 lines
3.0 KiB
TypeScript
import { beforeAll, describe, test } from "bun:test";
|
|
import { LegacyVersion, OnDecrease, OnIncrease } from "@autumn/shared";
|
|
import { replaceItems } from "@tests/attach/utils.js";
|
|
import { TestFeature } from "@tests/setup/v2Features.js";
|
|
import { attachAndExpectCorrect } from "@tests/utils/expectUtils/expectAttach.js";
|
|
import { attachNewContUseAndExpectCorrect } from "@tests/utils/expectUtils/expectContUse/expectUpdateContUse.js";
|
|
import { expectSubQuantityCorrect } from "@tests/utils/expectUtils/expectContUseUtils.js";
|
|
import ctx from "@tests/utils/testInitUtils/createTestContext.js";
|
|
import chalk from "chalk";
|
|
import { AutumnInt } from "@/external/autumn/autumnCli.js";
|
|
import { constructArrearProratedItem } from "@/utils/scriptUtils/constructItem.js";
|
|
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
|
|
import { initCustomerV3 } from "@/utils/scriptUtils/testUtils/initCustomerV3.js";
|
|
import { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
|
|
|
|
const userItem = constructArrearProratedItem({
|
|
featureId: TestFeature.Users,
|
|
pricePerUnit: 50,
|
|
includedUsage: 1,
|
|
config: {
|
|
on_increase: OnIncrease.BillImmediately,
|
|
on_decrease: OnDecrease.None,
|
|
},
|
|
});
|
|
|
|
export const pro = constructProduct({
|
|
items: [userItem],
|
|
type: "pro",
|
|
});
|
|
|
|
const testCase = "updateContUse3";
|
|
|
|
describe(`${chalk.yellowBright(`contUse/${testCase}: Testing update contUse included usage when no entities created`)}`, () => {
|
|
const customerId = testCase;
|
|
const autumn: AutumnInt = new AutumnInt({ version: LegacyVersion.v1_4 });
|
|
let testClockId: string;
|
|
const curUnix = new Date().getTime();
|
|
|
|
beforeAll(async () => {
|
|
await initProductsV0({
|
|
ctx,
|
|
products: [pro],
|
|
prefix: testCase,
|
|
customerId,
|
|
});
|
|
|
|
const { testClockId: testClockId1 } = await initCustomerV3({
|
|
ctx,
|
|
customerId,
|
|
customerData: {},
|
|
attachPm: "success",
|
|
withTestClock: true,
|
|
});
|
|
|
|
testClockId = testClockId1!;
|
|
});
|
|
|
|
test("should attach pro", async () => {
|
|
await attachAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: pro,
|
|
stripeCli: ctx.stripeCli,
|
|
db: ctx.db,
|
|
org: ctx.org,
|
|
env: ctx.env,
|
|
});
|
|
});
|
|
|
|
const extraUsage = 2;
|
|
const newItem = constructArrearProratedItem({
|
|
featureId: TestFeature.Users,
|
|
pricePerUnit: 50,
|
|
includedUsage: (userItem.included_usage as number) + extraUsage,
|
|
config: {
|
|
on_increase: OnIncrease.BillImmediately,
|
|
on_decrease: OnDecrease.None,
|
|
},
|
|
});
|
|
|
|
test("should update product with extra included usage", async () => {
|
|
await autumn.track({
|
|
customer_id: customerId,
|
|
feature_id: TestFeature.Users,
|
|
value: 1,
|
|
});
|
|
|
|
const customItems = replaceItems({
|
|
featureId: TestFeature.Users,
|
|
items: pro.items,
|
|
newItem,
|
|
});
|
|
|
|
await attachNewContUseAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: pro,
|
|
customItems,
|
|
numInvoices: 2,
|
|
});
|
|
|
|
await expectSubQuantityCorrect({
|
|
stripeCli: ctx.stripeCli,
|
|
productId: pro.id,
|
|
db: ctx.db,
|
|
org: ctx.org,
|
|
env: ctx.env,
|
|
customerId,
|
|
usage: 1,
|
|
numReplaceables: 0,
|
|
});
|
|
});
|
|
});
|