Files
cfw-autumn/server/tests/utils/expectUtils/expectSingleUse/expectUsageInvoice.ts
2026-01-15 15:04:37 +00:00

82 lines
1.7 KiB
TypeScript

// curUnix = await advanceTestClock({
// stripeCli,
// testClockId,
// advanceTo: addHours(
// addMonths(curUnix, 1),
// hoursToFinalizeInvoice,
// ).getTime(),
import type { AppEnv, Organization, ProductV2 } from "@autumn/shared";
import { expect } from "chai";
import type Stripe from "stripe";
import type { DrizzleCli } from "@/db/initDrizzle.js";
import type { AutumnInt } from "@/external/autumn/autumnCli.js";
import { expectFeaturesCorrect } from "../expectFeaturesCorrect.js";
import { getExpectedInvoiceTotal } from "../expectInvoiceUtils.js";
// });
export const expectInvoiceAfterUsage = async ({
autumn,
customerId,
entityId,
featureId,
product,
usage,
stripeCli,
db,
org,
env,
numInvoices = 2,
expectExpired = false,
}: {
autumn: AutumnInt;
customerId: string;
entityId: string;
featureId: string;
product: ProductV2;
usage: number;
stripeCli: Stripe;
db: DrizzleCli;
org: Organization;
env: AppEnv;
numInvoices?: number;
expectExpired?: boolean;
}) => {
const entity = await autumn.entities.get(customerId, entityId);
if (expectExpired) {
const matchingProduct = entity.products.find(
(p: any) => p.id === product.id,
);
expect(matchingProduct).to.not.exist;
} else {
expectFeaturesCorrect({
customer: entity,
product,
});
}
const invoiceTotal = await getExpectedInvoiceTotal({
org,
env,
customerId,
productId: product.id,
stripeCli,
db,
usage: [
{
featureId,
value: usage,
},
],
onlyIncludeMonthly: true,
onlyIncludeUsage: true,
expectExpired,
});
const invoices = entity.invoices;
expect(invoices?.length).to.equal(numInvoices);
expect(invoices?.[0].total).to.equal(invoiceTotal);
};