chore: added tests for v0 / v1 of attach responses

This commit is contained in:
John Yeo
2025-11-27 10:50:52 +00:00
parent f47bbf9320
commit 32f4192213
9 changed files with 411 additions and 0 deletions

View File

@@ -225,6 +225,7 @@ export const handleScheduleFunction2 = async ({
} else {
res.status(200).json({
success: true,
message: `Successfully downgraded from ${curCusProduct.product.name} to ${product.name}`,
});
}
}

View File

@@ -34,6 +34,7 @@ export const handleUpdateQuantityFunction = async ({
});
const invoices: Stripe.Invoice[] = [];
for (const options of optionsToUpdate) {
const result = await handleUpdateFeatureQuantity({
req,

View File

@@ -92,6 +92,8 @@ const getOptionsToUpdate = ({
}) => {
const optionsToUpdate: { new: FeatureOptions; old: FeatureOptions }[] = [];
const prices = cusProductToPrices({ cusProduct: curSameProduct });
console.log("Old options list: ", oldOptionsList);
console.log("New options list: ", newOptionsList);
for (const newOptions of newOptionsList) {
const internalFeatureId = newOptions.internal_feature_id;

View File

@@ -31,6 +31,11 @@ export const handleAttach = async (req: any, res: any) =>
attachBody,
});
// console.log("Options list: ", attachParams.optionsList);
// throw new Error(
// "Options list: " + JSON.stringify(attachParams.optionsList),
// );
// Handle existing product
const branch = await getAttachBranch({
ctx,

View File

@@ -0,0 +1,62 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { ApiVersion } 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 { constructArrearItem } from "@/utils/scriptUtils/constructItem.js";
import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js";
import { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
const testCase = "attach-response1";
const pro = constructProduct({
items: [
constructArrearItem({
featureId: TestFeature.Words,
includedUsage: 1000,
}),
],
type: "pro",
});
describe(`${chalk.yellowBright(`${testCase}: Testing v0.2 response for attach, scenario: new`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: ApiVersion.V0_2 });
beforeAll(async () => {
await initProductsV0({
ctx,
products: [pro],
prefix: testCase,
customerId,
});
});
test("should return v0.2 responses for attach", async () => {
const attachResponse = await autumn.attach({
customer_id: customerId,
product_id: pro.id,
});
expect(attachResponse.checkout_url).toBeDefined();
expect(Object.keys(attachResponse)).toEqual(["checkout_url"]);
});
test("should return correct v1.2 responses for attach", async () => {
const autumnV1 = new AutumnInt({ version: ApiVersion.V1_2 });
const attachResponse = await autumnV1.attach({
customer_id: customerId,
product_id: pro.id,
});
expect(attachResponse).toMatchObject({
customer_id: customerId,
product_ids: [pro.id],
checkout_url: expect.any(String),
});
expect(attachResponse.code).toBeDefined();
expect(attachResponse.message).toBeDefined();
expect(attachResponse.message).toBeDefined();
});
});

View File

@@ -0,0 +1,99 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { ApiVersion } 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 { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
import { initCustomerV3 } from "../../../src/utils/scriptUtils/testUtils/initCustomerV3";
const testCase = "attach-response2";
const pro = constructProduct({
type: "pro",
isDefault: false,
items: [
constructFeatureItem({
featureId: TestFeature.Words,
includedUsage: 1000,
}),
],
});
const premium = constructProduct({
type: "premium",
items: [
constructFeatureItem({
featureId: TestFeature.Words,
includedUsage: 1000,
}),
],
});
describe(`${chalk.yellowBright(`${testCase}: Testing v0.2 / v1.2 response for attach, scenario: upgrade`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: ApiVersion.V0_2 });
beforeAll(async () => {
await initCustomerV3({
ctx,
customerId,
customerData: {},
attachPm: "success",
withTestClock: true,
});
await initProductsV0({
ctx,
products: [pro, premium],
prefix: testCase,
});
await autumn.attach({
customer_id: customerId,
product_id: pro.id,
});
});
test("should return v0.2 responses for attach", async () => {
const attachResponse = await autumn.attach({
customer_id: customerId,
product_id: premium.id,
});
// expect(attachResponse.checkout_url).toBeDefined();
expect(Object.keys(attachResponse)).toEqual(["success", "message"]);
});
test("should return correct v1.2 responses for attach", async () => {
const autumnV1 = new AutumnInt({ version: ApiVersion.V1_2 });
await autumnV1.cancel({
customer_id: customerId,
product_id: premium.id,
cancel_immediately: true,
});
await autumnV1.attach({
customer_id: customerId,
product_id: pro.id,
});
const attachResponse = await autumnV1.attach({
customer_id: customerId,
product_id: premium.id,
});
expect(attachResponse).toMatchObject({
customer_id: customerId,
product_ids: [premium.id],
code: expect.any(String),
message: expect.any(String),
});
// expect(attachResponse.code).toBeDefined();
// expect(attachResponse.message).toBeDefined();
// expect(attachResponse.message).toBeDefined();
});
});

View File

@@ -0,0 +1,91 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { ApiVersion } 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 { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
import { initCustomerV3 } from "../../../src/utils/scriptUtils/testUtils/initCustomerV3";
const testCase = "attach-response3";
const pro = constructProduct({
type: "pro",
isDefault: false,
items: [
constructFeatureItem({
featureId: TestFeature.Words,
includedUsage: 1000,
}),
],
});
const premium = constructProduct({
type: "premium",
items: [
constructFeatureItem({
featureId: TestFeature.Words,
includedUsage: 1000,
}),
],
});
describe(`${chalk.yellowBright(`${testCase}: Testing v0.2 / v1.2 response for attach, scenario: downgrade`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: ApiVersion.V0_2 });
beforeAll(async () => {
await initCustomerV3({
ctx,
customerId,
customerData: {},
attachPm: "success",
withTestClock: true,
});
await initProductsV0({
ctx,
products: [pro, premium],
prefix: testCase,
});
await autumn.attach({
customer_id: customerId,
product_id: premium.id,
});
});
test("should return v0.2 responses for attach", async () => {
const attachResponse = await autumn.attach({
customer_id: customerId,
product_id: pro.id,
});
console.log(attachResponse);
expect(Object.keys(attachResponse)).toEqual(["success", "message"]);
});
test("should return correct v1.2 responses for attach", async () => {
const autumnV1 = new AutumnInt({ version: ApiVersion.V1_2 });
await autumnV1.cancel({
customer_id: customerId,
product_id: pro.id,
cancel_immediately: true,
});
const attachResponse = await autumnV1.attach({
customer_id: customerId,
product_id: pro.id,
});
expect(attachResponse).toMatchObject({
customer_id: customerId,
product_ids: [pro.id],
code: expect.any(String),
message: expect.any(String),
});
});
});

View File

@@ -0,0 +1,75 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { ApiVersion } 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 { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
import { initCustomerV3 } from "../../../src/utils/scriptUtils/testUtils/initCustomerV3";
const testCase = "attach-response4";
const pro = constructProduct({
type: "pro",
isDefault: false,
items: [
constructFeatureItem({
featureId: TestFeature.Words,
includedUsage: 1000,
}),
],
});
describe(`${chalk.yellowBright(`${testCase}: Testing v0.2 / v1.2 response for attach, scenario: new (card on file)`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: ApiVersion.V0_2 });
beforeAll(async () => {
await initCustomerV3({
ctx,
customerId,
customerData: {},
attachPm: "success",
withTestClock: true,
});
await initProductsV0({
ctx,
products: [pro],
prefix: testCase,
});
});
test("should return v0.2 responses for attach", async () => {
const attachResponse = await autumn.attach({
customer_id: customerId,
product_id: pro.id,
});
expect(Object.keys(attachResponse)).toEqual(["success", "message"]);
});
test("should return correct v1.2 responses for attach", async () => {
const autumnV1 = new AutumnInt({ version: ApiVersion.V1_2 });
await autumnV1.cancel({
customer_id: customerId,
product_id: pro.id,
cancel_immediately: true,
});
const attachResponse = await autumnV1.attach({
customer_id: customerId,
product_id: pro.id,
});
expect(attachResponse).toMatchObject({
customer_id: customerId,
product_ids: [pro.id],
code: expect.any(String),
message: expect.any(String),
});
});
});

View File

@@ -0,0 +1,75 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { ApiVersion } 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 { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js";
import { initCustomerV3 } from "../../../src/utils/scriptUtils/testUtils/initCustomerV3";
const testCase = "attach-response5";
const oneOff = constructProduct({
type: "one_off",
isDefault: false,
items: [
constructFeatureItem({
featureId: TestFeature.Words,
includedUsage: 1000,
interval: null,
}),
],
});
describe(`${chalk.yellowBright(`${testCase}: Testing v0.2 / v1.2 response for attach, scenario: one off (card on file)`)}`, () => {
const customerId = testCase;
const autumn: AutumnInt = new AutumnInt({ version: ApiVersion.V0_2 });
beforeAll(async () => {
await initCustomerV3({
ctx,
customerId,
customerData: {},
attachPm: "success",
withTestClock: true,
});
await initProductsV0({
ctx,
products: [oneOff],
prefix: testCase,
});
});
test("should return v0.2 responses for attach", async () => {
const attachResponse = await autumn.attach({
customer_id: customerId,
product_id: oneOff.id,
});
expect(attachResponse).toMatchObject({
customer_id: customerId,
product_ids: [oneOff.id],
code: expect.any(String),
message: expect.any(String),
});
});
test("should return correct v1.2 responses for attach", async () => {
const autumnV1 = new AutumnInt({ version: ApiVersion.V1_2 });
const attachResponse = await autumnV1.attach({
customer_id: customerId,
product_id: oneOff.id,
});
expect(attachResponse).toMatchObject({
customer_id: customerId,
product_ids: [oneOff.id],
code: expect.any(String),
message: expect.any(String),
});
});
});