165 lines
4.1 KiB
TypeScript
165 lines
4.1 KiB
TypeScript
import {
|
|
LegacyVersion,
|
|
OnDecrease,
|
|
OnIncrease,
|
|
} from "@autumn/shared";
|
|
import { beforeAll, describe, expect, test } from "bun:test";
|
|
import chalk from "chalk";
|
|
import { addHours, addMonths, addWeeks } from "date-fns";
|
|
import { TestFeature } from "tests/setup/v2Features.js";
|
|
import { hoursToFinalizeInvoice } from "tests/utils/constants.js";
|
|
import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js";
|
|
import { expectSubQuantityCorrect } from "tests/utils/expectUtils/expectContUseUtils.js";
|
|
import { advanceTestClock } from "tests/utils/stripeUtils.js";
|
|
import { getBasePrice } from "tests/utils/testProductUtils/testProductUtils.js";
|
|
import ctx from "tests/utils/testInitUtils/createTestContext.js";
|
|
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 = "entity3";
|
|
|
|
describe(`${chalk.yellowBright(`contUse/${testCase}: Testing replaceables deleted at end of cycle`)}`, () => {
|
|
const customerId = testCase;
|
|
const autumn: AutumnInt = new AutumnInt({ version: LegacyVersion.v1_4 });
|
|
let testClockId: string;
|
|
let 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!;
|
|
});
|
|
|
|
let usage = 0;
|
|
const firstEntities = [
|
|
{
|
|
id: "1",
|
|
name: "test",
|
|
feature_id: TestFeature.Users,
|
|
},
|
|
{
|
|
id: "2",
|
|
name: "test",
|
|
feature_id: TestFeature.Users,
|
|
},
|
|
{
|
|
id: "3",
|
|
name: "test",
|
|
feature_id: TestFeature.Users,
|
|
},
|
|
];
|
|
|
|
test("should create three entities, then attach pro", async () => {
|
|
await autumn.entities.create(customerId, firstEntities);
|
|
usage += firstEntities.length;
|
|
|
|
await attachAndExpectCorrect({
|
|
autumn,
|
|
customerId,
|
|
product: pro,
|
|
stripeCli: ctx.stripeCli,
|
|
db: ctx.db,
|
|
org: ctx.org,
|
|
env: ctx.env,
|
|
usage: [
|
|
{
|
|
featureId: TestFeature.Users,
|
|
value: usage,
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test("should delete 2 entities and have no new invoice", async () => {
|
|
curUnix = await advanceTestClock({
|
|
stripeCli: ctx.stripeCli,
|
|
testClockId,
|
|
advanceTo: addWeeks(new Date(), 2).getTime(),
|
|
waitForSeconds: 30,
|
|
});
|
|
|
|
await autumn.entities.delete(customerId, firstEntities[0].id);
|
|
await autumn.entities.delete(customerId, firstEntities[1].id);
|
|
|
|
const numReplaceables = 2;
|
|
await expectSubQuantityCorrect({
|
|
stripeCli: ctx.stripeCli,
|
|
productId: pro.id,
|
|
db: ctx.db,
|
|
org: ctx.org,
|
|
env: ctx.env,
|
|
customerId,
|
|
usage,
|
|
numReplaceables,
|
|
itemQuantity: usage - numReplaceables,
|
|
});
|
|
|
|
const customer = await autumn.customers.get(customerId);
|
|
const invoices = customer.invoices!;
|
|
expect(invoices.length).toBe(1);
|
|
});
|
|
|
|
test("should advance clock to next cycle and have correct invoice", async () => {
|
|
await advanceTestClock({
|
|
stripeCli: ctx.stripeCli,
|
|
testClockId,
|
|
advanceTo: addHours(
|
|
addMonths(new Date(), 1),
|
|
hoursToFinalizeInvoice,
|
|
).getTime(),
|
|
});
|
|
|
|
usage -= 2; // 2 entities deleted
|
|
|
|
const customer = await autumn.customers.get(customerId);
|
|
const invoices = customer.invoices;
|
|
|
|
const basePrice = getBasePrice({ product: pro });
|
|
expect(invoices.length).toBe(2);
|
|
expect(invoices[0].total).toBe(basePrice); // 0 entities
|
|
|
|
await expectSubQuantityCorrect({
|
|
stripeCli: ctx.stripeCli,
|
|
productId: pro.id,
|
|
db: ctx.db,
|
|
org: ctx.org,
|
|
env: ctx.env,
|
|
customerId,
|
|
usage,
|
|
itemQuantity: usage,
|
|
numReplaceables: 0,
|
|
});
|
|
});
|
|
});
|