Files
cfw-autumn/server/tests/balances/check/basic/check1.test.ts
John Yeo 3d47e1effa wip
2025-11-12 11:01:19 +00:00

96 lines
2.6 KiB
TypeScript

import { beforeAll, describe, expect, test } from "bun:test";
import {
ApiVersion,
type CheckResponseV0,
type CheckResponseV1,
type CheckResponseV2,
SuccessCode,
} from "@autumn/shared";
import { TestFeature } from "@tests/setup/v2Features.js";
import ctx from "@tests/utils/testInitUtils/createTestContext.js";
import chalk from "chalk";
import { AutumnInt } from "@/external/autumn/autumnCli.js";
import { constructFeatureItem } 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 dashboardFeature = constructFeatureItem({
featureId: TestFeature.Dashboard,
isBoolean: true,
});
const messagesFeature = constructFeatureItem({
featureId: TestFeature.Messages,
includedUsage: 1000,
});
const freeProd = constructProduct({
type: "free",
isDefault: false,
items: [dashboardFeature, messagesFeature],
});
const testCase = "check1";
describe(`${chalk.yellowBright("check1: test /check when no feature attached")}`, () => {
const customerId = "check1";
const autumnV0: AutumnInt = new AutumnInt({ version: ApiVersion.V0_2 });
const autumnV1: AutumnInt = new AutumnInt({ version: ApiVersion.V1_2 });
const autumnV2: AutumnInt = new AutumnInt({ version: ApiVersion.V2_0 });
beforeAll(async () => {
await initCustomerV3({
ctx,
customerId,
withTestClock: false,
});
await initProductsV0({
ctx,
products: [freeProd],
prefix: testCase,
});
});
test("should have correct v2 response", async () => {
const res = (await autumnV2.check({
customer_id: customerId,
feature_id: TestFeature.Messages,
})) as unknown as CheckResponseV2;
expect(res).toEqual({
allowed: false,
customer_id: testCase,
required_balance: 1,
balance: null,
});
});
test("should have correct v1 response", async () => {
const res = (await autumnV1.check({
customer_id: customerId,
feature_id: TestFeature.Messages,
})) as unknown as CheckResponseV1;
expect(res).toStrictEqual({
allowed: false,
customer_id: customerId,
feature_id: TestFeature.Messages,
required_balance: 1,
code: SuccessCode.FeatureFound,
});
});
test("should have correct v0 response", async () => {
const res = (await autumnV0.check({
customer_id: customerId,
feature_id: TestFeature.Messages,
})) as unknown as CheckResponseV0;
expect(res.allowed).toBe(false);
expect(res.balances).toBeDefined();
expect(res.balances).toHaveLength(0);
});
});