100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import type { AttachPreviewResponse } from "@autumn/shared";
|
|
import { hoursToFinalizeInvoice } from "@tests/utils/constants.js";
|
|
import { items } from "@tests/utils/fixtures/items.js";
|
|
import { products } from "@tests/utils/fixtures/products.js";
|
|
import { advanceTestClock } from "@tests/utils/stripeUtils.js";
|
|
import ctx from "@tests/utils/testInitUtils/createTestContext.js";
|
|
import { initScenario, s } from "@tests/utils/testInitUtils/initScenario.js";
|
|
import chalk from "chalk";
|
|
import { addHours, addMonths } from "date-fns";
|
|
import { createStripeCli } from "@/external/connect/createStripeCli.js";
|
|
import { createPercentCoupon } from "../utils/discounts/discountTestUtils.js";
|
|
|
|
test.concurrent(
|
|
`${chalk.yellowBright("invoice-matched-credits create-schedule: pro + addon credits reflect stored discounted charges")}`,
|
|
async () => {
|
|
const customerId = "imc-sched-disc-addon";
|
|
|
|
const pro = products.pro({
|
|
id: "pro",
|
|
items: [items.monthlyMessages({ includedUsage: 500 })],
|
|
});
|
|
|
|
const addon = products.recurringAddOn({
|
|
id: "addon",
|
|
items: [items.monthlyWords({ includedUsage: 200 })],
|
|
});
|
|
|
|
const premium = products.premium({
|
|
id: "premium",
|
|
items: [items.monthlyMessages({ includedUsage: 1000 })],
|
|
});
|
|
|
|
const { autumnV1, autumnV2_2, testClockId, advancedTo } =
|
|
await initScenario({
|
|
customerId,
|
|
setup: [
|
|
s.customer({ testClock: true, paymentMethod: "success" }),
|
|
s.products({ list: [pro, addon, premium] }),
|
|
],
|
|
actions: [],
|
|
});
|
|
|
|
const stripeCli = createStripeCli({ org: ctx.org, env: ctx.env });
|
|
const coupon = await createPercentCoupon({ stripeCli, percentOff: 20 });
|
|
|
|
await autumnV1.billing.attach({
|
|
customer_id: customerId,
|
|
product_id: `pro_${customerId}`,
|
|
discounts: [{ reward_id: coupon.id }],
|
|
});
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
|
|
await autumnV1.billing.attach({
|
|
customer_id: customerId,
|
|
product_id: `addon_${customerId}`,
|
|
});
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
|
|
const renewalTime = await advanceTestClock({
|
|
stripeCli: ctx.stripeCli,
|
|
testClockId: testClockId!,
|
|
advanceTo: addHours(
|
|
addMonths(new Date(advancedTo), 1),
|
|
hoursToFinalizeInvoice,
|
|
).getTime(),
|
|
waitForSeconds: 30,
|
|
});
|
|
|
|
await advanceTestClock({
|
|
stripeCli: ctx.stripeCli,
|
|
testClockId: testClockId!,
|
|
startingFrom: new Date(renewalTime),
|
|
numberOfDays: 15,
|
|
});
|
|
|
|
const preview = (await autumnV2_2.billing.previewAttach({
|
|
customer_id: customerId,
|
|
plan_id: `premium_${customerId}`,
|
|
})) as AttachPreviewResponse;
|
|
|
|
const creditLines = preview.line_items.filter((li) => li.total < 0);
|
|
expect(creditLines.length).toBeGreaterThan(0);
|
|
|
|
const creditTotal = creditLines.reduce((sum, li) => sum + li.total, 0);
|
|
expect(creditTotal).toBeLessThan(0);
|
|
expect(Math.abs(creditTotal)).toBeLessThan(20 + 10);
|
|
|
|
const result = await autumnV2_2.billing.attach({
|
|
customer_id: customerId,
|
|
plan_id: `premium_${customerId}`,
|
|
});
|
|
|
|
expect(result.invoice?.total).toBeCloseTo(preview.total, 0);
|
|
},
|
|
300_000,
|
|
);
|