Files
cfw-autumn/server/tests/unit/billing/existing-rollovers/apply-existing-rollovers/apply-existing-rollovers3.test.ts
2026-02-17 12:48:11 +00:00

64 lines
1.8 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { type ExistingRollover, RolloverExpiryDurationType } from "@autumn/shared";
import { customerEntitlements } from "@tests/utils/fixtures/db/customerEntitlements";
import { customerProducts } from "@tests/utils/fixtures/db/customerProducts";
import chalk from "chalk";
import { applyExistingRollovers } from "@/internal/billing/v2/utils/handleExistingRollovers/applyExistingRollovers";
describe(
chalk.yellowBright(
"applyExistingRollovers (multiple rollovers same feature)",
),
() => {
test("applies multiple rollovers to the same feature", () => {
const internalFeatureId = "internal_words";
const cusEnt = customerEntitlements.create({
internalFeatureId,
featureId: "words",
featureName: "Words",
allowance: 5000,
balance: 5000,
rollover: { max: null, duration: RolloverExpiryDurationType.Month, length: 1 },
});
const newCusProduct = customerProducts.create({
customerEntitlements: [cusEnt],
});
const existingRollovers: ExistingRollover[] = [
{
id: "rollover_1",
cus_ent_id: "old_cus_ent_id",
balance: 1000,
usage: 0,
expires_at: null,
entities: {},
internal_feature_id: internalFeatureId,
},
{
id: "rollover_2",
cus_ent_id: "old_cus_ent_id",
balance: 500,
usage: 0,
expires_at: null,
entities: {},
internal_feature_id: internalFeatureId,
},
];
// Act
applyExistingRollovers({
customerProduct: newCusProduct,
existingRollovers,
});
// Assert: both rollovers should be added to the same cusEnt
const updatedCusEnt = newCusProduct.customer_entitlements[0];
expect(updatedCusEnt.rollovers.length).toBe(2);
expect(updatedCusEnt.rollovers[0].balance).toBe(1000);
expect(updatedCusEnt.rollovers[1].balance).toBe(500);
});
},
);